From c61f493e4f30e895c42f93d2f3abd17e0b1b5010 Mon Sep 17 00:00:00 2001 From: Tareq Hasan Date: Tue, 6 Jun 2023 16:09:33 +0300 Subject: [PATCH] Initial commit --- .gitignore | 2 + .php-cs-fixer.dist.php | 27 +++ README.md | 140 ++++++++++++++ composer.json | 38 ++++ composer.lock | 419 +++++++++++++++++++++++++++++++++++++++++ src/ContainerTrait.php | 28 +++ src/HookTrait.php | 60 ++++++ src/LogTrait.php | 61 ++++++ src/SingletonTrait.php | 26 +++ 9 files changed, 801 insertions(+) create mode 100644 .gitignore create mode 100644 .php-cs-fixer.dist.php create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/ContainerTrait.php create mode 100644 src/HookTrait.php create mode 100644 src/LogTrait.php create mode 100644 src/SingletonTrait.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2a69e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +vendor diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..6fa5226 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,27 @@ +exclude( 'node_modules' ) + ->exclude( 'vendors' ) + ->exclude( 'assets' ) + ->exclude( 'languages' ) + ->exclude( 'src' ) + ->exclude( 'bin' ) + ->in( __DIR__ ) +; + +$config = new PhpCsFixer\Config(); +$config + ->registerCustomFixers( [ + new WeDevs\Fixer\SpaceInsideParenthesisFixer(), + new WeDevs\Fixer\BlankLineAfterClassOpeningFixer(), + ] ) + ->setRiskyAllowed( true ) + ->setUsingCache( false ) + ->setRules( WeDevs\Fixer\Fixer::rules() ) + ->setFinder( $finder ) +; + +return $config; \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..34c2206 --- /dev/null +++ b/README.md @@ -0,0 +1,140 @@ +# WeDevs WP Utils + +[![License: LGPL v3](https://img.shields.io/badge/License-LGPL_v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) +[![Packagist](https://img.shields.io/packagist/v/wedevs/wp-utils.svg)](https://packagist.org/packages/wedevs/wp-utils) + +A collection of useful codes and utilities for WordPress plugin development. These simplifies common tasks and promote code reusability in WordPress projects. + +## Installation + +Install the package via Composer: + +```bash +composer require wedevs/wp-utils +``` + +## Usage + +### ContainerTrait + +With `ContainerTrait` you can easily store and retrieve dynamic properties within your classes, providing a flexible and convenient way to manage and access data without the need for explicitly defined class properties. + +Usage example: + +```php +use WeDevs\WpUtils\ContainerTrait; + +class MyPlugin { + + use ContainerTrait; + + public function __construct() { + // Register an instance + $this->register('my_service', new MyService()); + + // Retrieve the instance + $myService = $this->get('my_service'); + + // Use the instance + $myService->doSomething(); + } + + // Rest of your plugin code... +} + +``` + +### HookTrait + +The `HookTrait` provides reusable methods for managing WordPress action and filter hooks in your plugin. It simplifies the process of adding, removing, or executing hooks. + +```php + +use WeDevs\WpUtils\HookTrait; + +class MyPlugin { + + use HookTrait; + + public function __construct() { + // Add an action hook + $this->add_action('init', 'my_init_function'); + + // Add a filter hook + $this->add_filter('the_title', 'my_title_filter'); + + // Execute an action hook + $this->do_action('custom_action', $arg1, $arg2); + } + + public function my_init_function() { + // Actions to be performed during 'init' + } + + public function my_title_filter($title) { + // Modify the post title + return $title . ' - Customized'; + } + + // Rest of your plugin code... +} + +``` + +### LogTrait + +The `LogTrait` provides a simple logging mechanism for your WordPress plugin. It allows you to log messages to the PHP error log. + +Usage example: + +```php +use WeDevs\WpUtils\LogTrait; + +class MyPlugin { + + use LogTrait; + + public function some_method() { + // Log an informational message + $this->log_info('Some informational message.'); + + // Log an error message + $this->log_error('An error occurred.'); + + // Log a debug message + $this->log_debug('A debug message'); + } + + // Rest of your plugin code... +} + +``` + +### SingletonTrait + +The `SingletonTrait` implements the singleton pattern for your WordPress plugin classes. It ensures that only one instance of the class is created and provides a global access point to that instance. + +Usage example: + +```php +use WeDevs\WpUtils\SingletonTrait; + +class MySingletonClass { + + use SingletonTrait; + + // Rest of your singleton class implementation... +} + +// Get the instance of the singleton class +$instance = MySingletonClass::instance(); + +// Use the instance +$instance->doSomething(); + +``` + + +## License + +This project is licensed under the GPL 2.0 or Later License. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..02f58b6 --- /dev/null +++ b/composer.json @@ -0,0 +1,38 @@ +{ + "name": "wedevs/wp-utils", + "description": "Various WordPress classes and traits for WordPress", + "homepage": "https://wedevs.com/", + "keywords": [ + "wordpress", + "utils", + "traits", + "classes" + ], + "type": "library", + "license": "GPL-3.0-or-later", + "autoload": { + "psr-4": { + "WeDevs\\WpUtils\\": "src/" + } + }, + "authors": [ + { + "name": "Tareq Hasan", + "email": "tareq@wedevs.com" + } + ], + "minimum-stability": "dev", + "require": {}, + "require-dev": { + "tareq1988/wp-php-cs-fixer": "dev-master", + "wp-coding-standards/wpcs": "dev-master", + "phpcompatibility/php-compatibility": "9.*", + "phpcompatibility/phpcompatibility-wp": "*", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + } +} \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..e86e0b1 --- /dev/null +++ b/composer.lock @@ -0,0 +1,419 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "e6e2c64f54fbd2dcca936e1669d3f0ab", + "packages": [], + "packages-dev": [ + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v0.7.2", + "source": { + "type": "git", + "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0", + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" + }, + "require-dev": { + "composer/composer": "*", + "php-parallel-lint/php-parallel-lint": "^1.3.1", + "phpcompatibility/php-compatibility": "^9.0" + }, + "type": "composer-plugin", + "extra": { + "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcbf", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "support": { + "issues": "https://github.com/dealerdirect/phpcodesniffer-composer-installer/issues", + "source": "https://github.com/dealerdirect/phpcodesniffer-composer-installer" + }, + "time": "2022-02-04T12:51:07+00:00" + }, + { + "name": "phpcompatibility/php-compatibility", + "version": "9.3.5", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" + }, + "conflict": { + "squizlabs/php_codesniffer": "2.6.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "homepage": "https://github.com/wimg", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", + "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" + } + ], + "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", + "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", + "keywords": [ + "compatibility", + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibility/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibility" + }, + "time": "2019-12-27T09:44:58+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-paragonie", + "version": "1.3.2", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", + "reference": "bba5a9dfec7fcfbd679cfaf611d86b4d3759da26" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/bba5a9dfec7fcfbd679cfaf611d86b4d3759da26", + "reference": "bba5a9dfec7fcfbd679cfaf611d86b4d3759da26", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7", + "paragonie/random_compat": "dev-master", + "paragonie/sodium_compat": "dev-master" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" + } + ], + "description": "A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by the Paragonie polyfill libraries.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "paragonie", + "phpcs", + "polyfill", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie" + }, + "time": "2022-10-25T01:46:02+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-wp", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityWP.git", + "reference": "262f9d81273932315d15d704f69b9d678b939cb3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityWP/zipball/262f9d81273932315d15d704f69b9d678b939cb3", + "reference": "262f9d81273932315d15d704f69b9d678b939cb3", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0", + "phpcompatibility/phpcompatibility-paragonie": "^1.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "default-branch": true, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" + } + ], + "description": "A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by WordPress.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "phpcs", + "standards", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityWP/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityWP" + }, + "time": "2023-01-05T13:34:27+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "d148febc2a2eb82972121d7f962883f7a5697b55" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/d148febc2a2eb82972121d7f962883f7a5697b55", + "reference": "d148febc2a2eb82972121d7f962883f7a5697b55", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "default-branch": true, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2023-05-26T22:32:02+00:00" + }, + { + "name": "tareq1988/wp-php-cs-fixer", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/tareq1988/wp-php-cs-fixer.git", + "reference": "eeef65598ae7bac55a09073e666ec0eabf303a12" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tareq1988/wp-php-cs-fixer/zipball/eeef65598ae7bac55a09073e666ec0eabf303a12", + "reference": "eeef65598ae7bac55a09073e666ec0eabf303a12", + "shasum": "" + }, + "default-branch": true, + "type": "library", + "autoload": { + "psr-4": { + "WeDevs\\Fixer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Tareq Hasan", + "email": "tareq@wedevs.com" + } + ], + "description": "WordPress rules for php-cs-fixer", + "support": { + "issues": "https://github.com/tareq1988/wp-php-cs-fixer/issues", + "source": "https://github.com/tareq1988/wp-php-cs-fixer/tree/master" + }, + "time": "2022-08-26T09:36:52+00:00" + }, + { + "name": "wp-coding-standards/wpcs", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", + "reference": "7da1894633f168fe244afc6de00d141f27517b62" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/7da1894633f168fe244afc6de00d141f27517b62", + "reference": "7da1894633f168fe244afc6de00d141f27517b62", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "squizlabs/php_codesniffer": "^3.3.1" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6", + "phpcompatibility/php-compatibility": "^9.0", + "phpcsstandards/phpcsdevtools": "^1.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Contributors", + "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", + "keywords": [ + "phpcs", + "standards", + "wordpress" + ], + "support": { + "issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues", + "source": "https://github.com/WordPress/WordPress-Coding-Standards", + "wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki" + }, + "time": "2020-05-13T23:57:56+00:00" + } + ], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": { + "tareq1988/wp-php-cs-fixer": 20, + "wp-coding-standards/wpcs": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/src/ContainerTrait.php b/src/ContainerTrait.php new file mode 100644 index 0000000..a342311 --- /dev/null +++ b/src/ContainerTrait.php @@ -0,0 +1,28 @@ +container[ $name ] ) ) { + return $this->container[ $name ]; + } + + return null; + } +} diff --git a/src/HookTrait.php b/src/HookTrait.php new file mode 100644 index 0000000..b18dc2e --- /dev/null +++ b/src/HookTrait.php @@ -0,0 +1,60 @@ +log( $message, 'info' ); + } + + /** + * Log a error message. + * + * @param string $message + * + * @return void + */ + public function log_error( $message ) { + $this->log( $message, 'error' ); + } + + /** + * Log a debug message. + * + * @param string $message + * + * @return void + */ + public function log_debug( $message ) { + $this->log( $message, 'debug' ); + } +} diff --git a/src/SingletonTrait.php b/src/SingletonTrait.php new file mode 100644 index 0000000..591671e --- /dev/null +++ b/src/SingletonTrait.php @@ -0,0 +1,26 @@ +