diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index fbedf61..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml index f0d0b5f..8415c0c 100644 --- a/.github/workflows/js.yml +++ b/.github/workflows/js.yml @@ -24,4 +24,4 @@ jobs: - name: Test the app uses: borales/actions-yarn@v4 with: - cmd: test # will run `yarn test` command + cmd: test:js # will run `yarn test` command diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 49e09e7..d75409f 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -31,10 +31,10 @@ jobs: restore-keys: ${{ runner.os }}-composer- - name: Install Composer dependencies - run: composer install + run: composer update - name: PHP Lint - run: vendor/bin/phpcs + run: composer run lint:php - name: PHP test - run: vendor/bin/phpunit --testdox + run: composer run test:php diff --git a/.gitignore b/.gitignore index 3064e04..61c9e30 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build vendor yarn-error.log .phpunit.result.cache +.DS_Store diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..bb3e497 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,6 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +yarn lint:js && yarn test:js +composer run lint:php +composer run test:php diff --git a/composer.lock b/composer.lock index ec17e42..bda98fd 100644 --- a/composer.lock +++ b/composer.lock @@ -991,16 +991,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.5", + "version": "9.6.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "86e761949019ae83f49240b2f2123fb5ab3b2fc5" + "reference": "b65d59a059d3004a040c16a82e07bbdf6cfdd115" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/86e761949019ae83f49240b2f2123fb5ab3b2fc5", - "reference": "86e761949019ae83f49240b2f2123fb5ab3b2fc5", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b65d59a059d3004a040c16a82e07bbdf6cfdd115", + "reference": "b65d59a059d3004a040c16a82e07bbdf6cfdd115", "shasum": "" }, "require": { @@ -1073,7 +1073,8 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.5" + "security": "https://github.com/sebastianbergmann/phpunit/security/policy", + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.6" }, "funding": [ { @@ -1089,7 +1090,7 @@ "type": "tidelift" } ], - "time": "2023-03-09T06:34:10+00:00" + "time": "2023-03-27T11:43:46+00:00" }, { "name": "sebastian/cli-parser", diff --git a/includes/Utils.php b/includes/Flag.php similarity index 89% rename from includes/Utils.php rename to includes/Flag.php index a1bbd91..09155a9 100644 --- a/includes/Utils.php +++ b/includes/Flag.php @@ -10,13 +10,15 @@ namespace MR\FeatureFlags; +use MR\FeatureFlags\Helper\Helper; + /** - * Class Utils + * Utils class for feature flags * * @package mr-feature-flags * @since 1.0.0 */ -class Utils { +class Flag { /** * Name in options table. diff --git a/helper/Helper.php b/includes/helper/Helper.php similarity index 84% rename from helper/Helper.php rename to includes/helper/Helper.php index 7e8415d..1edf586 100644 --- a/helper/Helper.php +++ b/includes/helper/Helper.php @@ -8,7 +8,7 @@ declare(strict_types=1); -namespace MR\FeatureFlags; +namespace MR\FeatureFlags\Helper; /** * Class FeatureFlags @@ -19,12 +19,12 @@ class Helper { /** - * Flag search helper. + * Flag search helper, returns true if flag is found and enabled. * * @param array $flags flags array. * @param string $field field to search. * @param string $flag name of the flag. - * @return mixed + * @return boolean * @since 1.0.0 */ public static function search_flag( $flags, $field, $flag ) { diff --git a/package.json b/package.json index 64c9264..4374aff 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "start": "wp-scripts start", "build": "wp-scripts build", "lint:js": "wp-scripts lint-js", - "test": "wp-scripts test-unit-js", - "test:watch": "wp-scripts test-unit-js --watch" + "test:js": "wp-scripts test-unit-js", + "test:watch": "wp-scripts test-unit-js --watch", + "prepare": "husky install" }, "devDependencies": { "@testing-library/jest-dom": "^5.16.5", @@ -21,7 +22,8 @@ "eslint-import-resolver-alias": "^1.1.2", "eslint-plugin-cypress": "^2.12.1", "eslint-plugin-import": "^2.27.5", - "prettier": "^2.4.1" + "prettier": "^2.4.1", + "husky": "^8.0.0" }, "keywords": [], "author": "Mohan Raj ", diff --git a/plugin.php b/plugin.php index 4ae63b8..0a7462a 100644 --- a/plugin.php +++ b/plugin.php @@ -32,48 +32,16 @@ */ define( 'MR_FEATURE_FLAGS_PLUGIN_PATH', __FILE__ ); -if ( ! file_exists( Utils::class ) ) { +if ( ! file_exists( Flag::class ) ) { include_once __DIR__ . '/vendor/autoload.php'; } -add_action( - 'wp_enqueue_scripts', - function(): void { - $plugin_url = plugin_dir_url( MR_FEATURE_FLAGS_PLUGIN_PATH ); - $script_asset_file = include_once plugin_dir_path( MR_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/index.asset.php'; - - wp_enqueue_script( - 'mr-feature-flags-script', - $plugin_url . 'build/index.js', - $script_asset_file['dependencies'], - $script_asset_file['version'], - true - ); - - - $feature_flag_meta = get_option( Utils::$option_name ); - $flags_list = []; - if ( is_array( $feature_flag_meta ) ) { - $flags_list = $feature_flag_meta; - } - - - wp_localize_script( - 'mr-feature-flags-script', - 'mrFeatureFlags', - [ - 'flags' => $flags_list, - ] - ); - - } -); - +// Enqueure scripts, styles in settings page. add_action( 'admin_enqueue_scripts', function( string $page ): void { if ( 'toplevel_page_mr-feature-flags' === $page ) { - load_settings_scripts(); + mr_feature_flags_load_settings_scripts(); } } ); @@ -83,7 +51,8 @@ function( string $page ): void { * * @return void */ -function load_settings_scripts(): void { +function mr_feature_flags_load_settings_scripts(): void { + $plugin_url = plugin_dir_url( MR_FEATURE_FLAGS_PLUGIN_PATH ); $settings_asset_file = require_once plugin_dir_path( MR_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/settings.asset.php'; // @phpcs:ignore @@ -106,46 +75,61 @@ function load_settings_scripts(): void { } +// Enqueue scripts and styles for front end. +add_action( + 'wp_enqueue_scripts', + __NAMESPACE__ . '\mr_feature_flags_scripts_enqueue' +); + +// Enqueue scripts and styles for wp-admin. add_action( 'admin_enqueue_scripts', - function( string $page ): void { - $plugin_url = plugin_dir_url( MR_FEATURE_FLAGS_PLUGIN_PATH ); - $script_asset_file = include_once plugin_dir_path( MR_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/index.asset.php'; - - wp_enqueue_script( - 'mr-feature-flags-script', - $plugin_url . 'build/index.js', - $script_asset_file['dependencies'], - $script_asset_file['version'], - true - ); + __NAMESPACE__ . '\mr_feature_flags_scripts_enqueue' +); - $feature_flag_meta = get_option( Utils::$option_name ); - $flags_list = []; - if ( is_array( $feature_flag_meta ) ) { - $flags_list = $feature_flag_meta; - } +/** + * Enqueue scripts and assets for admin and front end + */ +function mr_feature_flags_scripts_enqueue(): void { + $plugin_url = plugin_dir_url( MR_FEATURE_FLAGS_PLUGIN_PATH ); + $script_asset_file = include_once plugin_dir_path( MR_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/index.asset.php'; + wp_enqueue_script( + 'mr-feature-flags-script', + $plugin_url . 'build/index.js', + $script_asset_file['dependencies'], + $script_asset_file['version'], + true + ); - wp_localize_script( - 'mr-feature-flags-script', - 'mrFeatureFlags', - [ - 'flags' => $flags_list, - ] - ); + $feature_flag_meta = get_option( Flag::$option_name ); + $flags_list = []; + + if ( is_array( $feature_flag_meta ) ) { + $flags_list = $feature_flag_meta; } -); + wp_localize_script( + 'mr-feature-flags-script', + 'mrFeatureFlags', + [ + 'flags' => $flags_list, + ] + ); + +} + +// Registers feature flags admin setting page. $mr_feature_flags_admin_settings = new Settings(); $mr_feature_flags_admin_settings->register_feature_settings(); +// Registers feature flags API's. $mr_feature_flags_register_api = new Flags(); $mr_feature_flags_register_api->register_flags_endpoints(); - +// Displays setting page link in plugin page. add_filter( 'plugin_action_links_mr-feature-flags/plugin.php', function ( $links ) { @@ -173,7 +157,7 @@ function ( $links ) { * Uninstall method for the plugin. */ function mr_feature_flags_uninstall() { - delete_option( Utils::$option_name ); + delete_option( Flag::$option_name ); } diff --git a/src/components/snippets/JsSnippet.tsx b/src/components/snippets/JsSnippet.tsx index 396cba8..704eb1a 100644 --- a/src/components/snippets/JsSnippet.tsx +++ b/src/components/snippets/JsSnippet.tsx @@ -25,7 +25,6 @@ domReady(function () { float: 'right', position: 'relative', right: 40, - top: 24, }} /> diff --git a/src/components/snippets/PhpSnippet.tsx b/src/components/snippets/PhpSnippet.tsx index cd593c6..fcaeb2b 100644 --- a/src/components/snippets/PhpSnippet.tsx +++ b/src/components/snippets/PhpSnippet.tsx @@ -5,7 +5,7 @@ import Clipboard from '../common/Clipboard'; export default function ({ flag }: { flag: string }): JSX.Element { const phpSnippet = useMemo(() => { - return `if ( class_exists( '\\MR\\FeatureFlags\\Utils' ) && \\MR\\FeatureFlags\\Utils::is_enabled( '${flag}' ) ) { + return `if ( class_exists( '\\MR\\FeatureFlags\\Flag' ) && \\MR\\FeatureFlags\\Flag::is_enabled( '${flag}' ) ) { // php code goes here... }`; }, [flag]); diff --git a/tests/Unit/UtilsTest.php b/tests/Unit/FlagTest.php similarity index 87% rename from tests/Unit/UtilsTest.php rename to tests/Unit/FlagTest.php index 5a193f9..b21f70b 100644 --- a/tests/Unit/UtilsTest.php +++ b/tests/Unit/FlagTest.php @@ -9,7 +9,7 @@ -class UtilsTest extends \PHPUnit\Framework\TestCase +class FlagTest extends \PHPUnit\Framework\TestCase { public function setUp() : void { parent::setUp(); @@ -27,7 +27,7 @@ public function test_is_enabled_method_should_return_true_if_flag_name_present_a \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); - $result = Utils::is_enabled('Test'); + $result = Flag::is_enabled('Test'); $this->assertTrue($result); } @@ -36,7 +36,7 @@ public function test_is_enabled_method_should_return_false_if_no_flags_exist() { \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); - $result = Utils::is_enabled('Test'); + $result = Flag::is_enabled('Test'); $this->assertFalse($result); } @@ -45,7 +45,7 @@ public function test_is_enabled_method_should_return_false_if_flag_name_present_ \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); - $result = Utils::is_enabled('Test'); + $result = Flag::is_enabled('Test'); $this->assertFalse($result); } @@ -54,7 +54,7 @@ public function test_is_enabled_method_should_return_false_if_flag_name_nor_pres \Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value); - $result = Utils::is_enabled('Test1'); + $result = Flag::is_enabled('Test1'); $this->assertFalse($result); } diff --git a/tests/Unit/HelperTest.php b/tests/Unit/HelperTest.php index e72f74a..9290931 100644 --- a/tests/Unit/HelperTest.php +++ b/tests/Unit/HelperTest.php @@ -1,6 +1,6 @@