From 8fe0f93c03702237486e5463f00f230ede9207b8 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Thu, 17 Oct 2019 14:00:20 -0400 Subject: [PATCH] Add the ability to create custom plugin builds (https://github.com/woocommerce/woocommerce-admin/pull/3044) * Add the ability to create custom plugin builds * Use $_ENV instead of $_SERVER inside the generate-feature-config CLI script. --- plugins/woocommerce-admin/bin/build-plugin-zip.sh | 14 +++++++++++++- .../bin/generate-feature-config.php | 11 ++++++++++- plugins/woocommerce-admin/docs/feature-flags.md | 10 ++++++++++ .../src/API/OnboardingPlugins.php | 11 +++-------- .../woocommerce-admin/src/Features/Onboarding.php | 3 ++- plugins/woocommerce-admin/webpack.config.js | 4 +++- 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/plugins/woocommerce-admin/bin/build-plugin-zip.sh b/plugins/woocommerce-admin/bin/build-plugin-zip.sh index 52c21e3c41a..3a8698182dc 100755 --- a/plugins/woocommerce-admin/bin/build-plugin-zip.sh +++ b/plugins/woocommerce-admin/bin/build-plugin-zip.sh @@ -1,5 +1,17 @@ #!/bin/bash +ZIP_FILE='woocommerce-admin.zip'; + +while [ $# -gt 0 ]; do + if [[ $1 == '-f' || $1 == '--features' ]]; then + export WC_ADMIN_ADDITIONAL_FEATURES="$2" + fi + if [[ $1 == '-s' || $1 == '--slug' ]]; then + ZIP_FILE="woocommerce-admin-$2.zip"; + fi + shift +done + # Exit if any command fails. set -e @@ -74,7 +86,7 @@ build_files=$(ls dist/*/*.{js,css}) # Generate the plugin zip file. status "Creating archive... 🎁" -zip -r woocommerce-admin.zip \ +zip -r ${ZIP_FILE} \ woocommerce-admin.php \ uninstall.php \ includes/ \ diff --git a/plugins/woocommerce-admin/bin/generate-feature-config.php b/plugins/woocommerce-admin/bin/generate-feature-config.php index 4dd23a67965..ac3eb2f6845 100644 --- a/plugins/woocommerce-admin/bin/generate-feature-config.php +++ b/plugins/woocommerce-admin/bin/generate-feature-config.php @@ -11,13 +11,22 @@ * - plugin: For the standalone feature plugin, for GitHub and WordPress.org. * - core: Stable features for WooCommerce core merge. */ -$phase = isset( $_SERVER['WC_ADMIN_PHASE'] ) ? $_SERVER['WC_ADMIN_PHASE'] : ''; // WPCS: sanitization ok. +$phase = isset( $_ENV['WC_ADMIN_PHASE'] ) ? $_ENV['WC_ADMIN_PHASE'] : ''; // WPCS: sanitization ok. + if ( ! in_array( $phase, array( 'development', 'plugin', 'core' ), true ) ) { $phase = 'plugin'; // Default to plugin when running `npm run build`. } $config_json = file_get_contents( 'config/' . $phase . '.json' ); $config = json_decode( $config_json ); +if ( ! empty( $_ENV['WC_ADMIN_ADDITIONAL_FEATURES'] ) ) { + $additional_features = json_decode( $_ENV['WC_ADMIN_ADDITIONAL_FEATURES'], true ); + if ( is_array( $additional_features ) ) { + foreach ( $additional_features as $feature => $enabled ) { + $config->features->$feature = $enabled; + } + } +} if ( 'core' !== $phase ) { $write = "build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' ); - if ( defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ) { - $connect_url = add_query_arg( - array( - 'calypso_env' => WOOCOMMERCE_CALYPSO_ENVIRONMENT, - ), - $connect_url - ); - } + // @todo When implementing user-facing split testing, this should be abled to a default of 'production'. + $calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'wpcalypso'; + $connect_url = add_query_arg( array( 'calypso_env' => $calypso_env ), $connect_url ); return( array( 'slug' => 'jetpack', diff --git a/plugins/woocommerce-admin/src/Features/Onboarding.php b/plugins/woocommerce-admin/src/Features/Onboarding.php index 06bed033a49..10b0ba734ad 100644 --- a/plugins/woocommerce-admin/src/Features/Onboarding.php +++ b/plugins/woocommerce-admin/src/Features/Onboarding.php @@ -566,7 +566,8 @@ class Onboarding { * Allows quick access to testing the calypso parts of onboarding. */ public static function calypso_tests() { - $calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production'; + // @todo When implementing user-facing split testing, this should be abled to a default of 'production'. + $calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'wpcalypso'; if ( Loader::is_admin_page() && class_exists( 'Jetpack' ) && isset( $_GET['test_wc_jetpack_connect'] ) && 1 === absint( $_GET['test_wc_jetpack_connect'] ) ) { // WPCS: CSRF ok. $redirect_url = esc_url_raw( diff --git a/plugins/woocommerce-admin/webpack.config.js b/plugins/woocommerce-admin/webpack.config.js index 2405738981a..21793731bb4 100644 --- a/plugins/woocommerce-admin/webpack.config.js +++ b/plugins/woocommerce-admin/webpack.config.js @@ -23,6 +23,8 @@ if ( [ 'development', 'plugin', 'core' ].indexOf( WC_ADMIN_PHASE ) === -1 ) { WC_ADMIN_PHASE = 'core'; } const WC_ADMIN_CONFIG = require( path.join( __dirname, 'config', WC_ADMIN_PHASE + '.json' ) ); +const WC_ADMIN_ADDITIONAL_FEATURES = process.env.WC_ADMIN_ADDITIONAL_FEATURES && + JSON.parse( process.env.WC_ADMIN_ADDITIONAL_FEATURES ) || {}; const externals = { '@wordpress/api-fetch': { this: [ 'wp', 'apiFetch' ] }, @@ -161,7 +163,7 @@ const webpackConfig = { new FixStyleOnlyEntriesPlugin(), // Inject the current feature flags. new DefinePlugin( { - 'window.wcAdminFeatures': { ...WC_ADMIN_CONFIG.features }, + 'window.wcAdminFeatures': { ...WC_ADMIN_CONFIG.features, ...WC_ADMIN_ADDITIONAL_FEATURES }, } ), new CustomTemplatedPathPlugin( { modulename( outputPath, data ) {