This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First pass at adding feature flags. Accessible over PHP and JS. * Hook up dev config to test enviornments, hook up features to their flags. * Cleanup some code and add documentation. Remove beta config. * Handle PR Feedback: Add devdocs flag, remove categories flag, fix auto-generated PHP warning. * Add todo for beta phase. * Handle PR feedback: Simplify is_array check, update build command, simplify test mock, remove empty webpack line.
- Loading branch information
1 parent
dccf61f
commit 0c4c17e
Showing
20 changed files
with
302 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ wp-cli.local.yml | |
yarn.lock | ||
tests | ||
vendor | ||
config | ||
node_modules | ||
*.sql | ||
*.tar.gz | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Generates an array of feature flags, based on the config used by the client application. | ||
* | ||
* @package WooCommerce Admin | ||
*/ | ||
|
||
$phase = isset( $_SERVER['WC_ADMIN_PHASE'] ) ? $_SERVER['WC_ADMIN_PHASE'] : ''; // WPCS: sanitization ok. | ||
if ( ! in_array( $phase, array( 'development', 'plugin', 'core' ) ) ) { | ||
$phase = 'core'; | ||
} | ||
$config_json = file_get_contents( 'config/' . $phase . '.json' ); | ||
$config = json_decode( $config_json ); | ||
|
||
$write = "<?php\n"; | ||
$write .= "// WARNING: Do not directly edit this file.\n"; | ||
$write .= "// This file is auto-generated as part of the build process and things may break.\n"; | ||
$write .= "function wc_admin_get_feature_config() {\n"; | ||
$write .= "\treturn array(\n"; | ||
foreach ( $config->features as $feature => $bool ) { | ||
$write .= "\t\t'{$feature}' => " . ( $bool ? 'true' : 'false' ) . ",\n"; | ||
} | ||
$write .= "\t);\n"; | ||
$write .= "}\n"; | ||
|
||
$config_file = fopen( 'includes/feature-config.php', 'w' ); | ||
fwrite( $config_file, $write ); | ||
fclose( $config_file ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"features": { | ||
"activity-panels": false, | ||
"analytics": false, | ||
"dashboard": false, | ||
"devdocs": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"features": { | ||
"activity-panels": true, | ||
"analytics": true, | ||
"dashboard": true, | ||
"devdocs": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"features": { | ||
"activity-panels": false, | ||
"analytics": true, | ||
"dashboard": true, | ||
"devdocs": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Feature Flags | ||
|
||
Features inside the `wc-admin` repository can be in various states of completeness. In addition to the development copy of `wc-admin`, feature plugin versions are bundled, and code is merged to WooCommerce core. To provide a way for improved control over how these features are released in these different environments, `wc-admin` has a system for feature flags. | ||
|
||
We currently support the following environments: | ||
|
||
| Environment | Description | | ||
|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| development | Development - All features should be enabled in development. These flags are also used in both JS and PHP tests. Ran using `npm start`. | | ||
| plugin | Plugin - A packaged release of the featured plugin, for GitHub WordPress.org. Ran using `npm run-script build:release`. | | | ||
| core | Core - assets/files ready and stable enough for core merge. @todo No build process exists yet. | ||
|
||
|
||
## Adding a new flag | ||
|
||
Flags can be added to the files located in the `config/` directory. Make sure to add a flag for each environment and explicitly set the flag to false. | ||
Please add new feature flags alphabetically so they are easy to find. | ||
|
||
## Basic Use - Client | ||
|
||
The `window.wcAdminFeatures` constant is a global variable containing the feature flags. | ||
|
||
Instances of `window.wcAdminFeatures` are replaced during the webpack build process by using webpack's [define plugin](https://webpack.js.org/plugins/define-plugin/). Using `webpack` for this allows us to eliminate dead code when making minified/production builds (`plugin`, or `core` environments). | ||
|
||
To check if a feature is enabled, you can simplify check the boolean value of a feature: | ||
|
||
``` | ||
{ window.wcAdminFeatures[ 'activity-panels' ] && <ActivityPanel /> } | ||
``` | ||
|
||
We also expose CSS classes on the `body` tag, so that you can target specific feature states: | ||
|
||
``` | ||
<body class="wp-admin woocommerce-page woocommerce-feature-disabled-analytics woocommerce-feature-enabled-activity-panels ...."> | ||
``` | ||
|
||
## Basic Use - Server | ||
|
||
Feature flags are also available via PHP. To ensure these are consistent with the built client assets, `includes/feature-flags.php` is generated by the plugin build process or `npm start`. Do not edit `includes/feature-flags.php` directly. | ||
|
||
To check if a feature is enabled, you can use the `wc_admin_is_feature_enabled()`: | ||
|
||
``` | ||
if ( wc_admin_is_feature_enabled( 'activity-panels' ) ) { | ||
add_action( 'admin_header', 'wc_admin_activity_panel' ); | ||
} | ||
``` |
Oops, something went wrong.