Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
vendor
yarn-error.log
.phpunit.result.cache
.DS_Store
6 changes: 6 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions includes/Utils.php → includes/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions helper/Helper.php → includes/helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

declare(strict_types=1);

namespace MR\FeatureFlags;
namespace MR\FeatureFlags\Helper;

/**
* Class FeatureFlags
Expand All @@ -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 ) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 <https://mohanraj.dev>",
Expand Down
108 changes: 46 additions & 62 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
);
Expand All @@ -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

Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 );
}


1 change: 0 additions & 1 deletion src/components/snippets/JsSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ domReady(function () {
float: 'right',
position: 'relative',
right: 40,
top: 24,
}}
/>
<Snippet data={jsSnippet} language={'typescript'} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/snippets/PhpSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/UtilsTest.php → tests/Unit/FlagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@



class UtilsTest extends \PHPUnit\Framework\TestCase
class FlagTest extends \PHPUnit\Framework\TestCase
{
public function setUp() : void {
parent::setUp();
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/HelperTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MR\FeatureFlags;
namespace MR\FeatureFlags\Helper;

use \PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6128,6 +6128,11 @@ human-signals@^1.1.1:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
integrity sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==

husky@^8.0.3:
version "8.0.3"
resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184"
integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==

iconv-lite@0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down