Runtime feature config override (https://github.com/woocommerce/woocommerce-admin/pull/4523)
This commit is contained in:
parent
8854a71d9c
commit
d7aec170f1
|
@ -5,10 +5,6 @@ IS_CUSTOM_BUILD=false;
|
|||
SLUG='';
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
if [[ $1 == '-f' || $1 == '--features' ]]; then
|
||||
export WC_ADMIN_ADDITIONAL_FEATURES="$2"
|
||||
IS_CUSTOM_BUILD=true
|
||||
fi
|
||||
if [[ $1 == '-s' || $1 == '--slug' ]]; then
|
||||
IS_CUSTOM_BUILD=true
|
||||
SLUG=$2
|
||||
|
@ -51,7 +47,6 @@ read -r VERSION
|
|||
if [ $IS_CUSTOM_BUILD = true ]; then
|
||||
PLUGIN_TAG="${VERSION}-${SLUG}"
|
||||
|
||||
warning "You are building a custom build of wc-admin with these features ${GREEN_BOLD}$WC_ADMIN_ADDITIONAL_FEATURES${YELLOW_BOLD} applied."
|
||||
warning "A release on Github will be made with the tag ${GREEN_BOLD}$PLUGIN_TAG${COLOR_RESET}"
|
||||
warning "The resulting zip will be called ${GREEN_BOLD}$ZIP_FILE${COLOR_RESET}"
|
||||
else
|
||||
|
|
|
@ -20,15 +20,6 @@ if ( ! in_array( $phase, array( 'development', 'plugin', 'core' ), true ) ) {
|
|||
$config_json = file_get_contents( 'config/' . $phase . '.json' );
|
||||
$config = json_decode( $config_json );
|
||||
|
||||
if ( ! empty( getenv( 'WC_ADMIN_ADDITIONAL_FEATURES' ) ) ) {
|
||||
$additional_features = json_decode( getenv( 'WC_ADMIN_ADDITIONAL_FEATURES' ), true );
|
||||
if ( is_array( $additional_features ) ) {
|
||||
foreach ( $additional_features as $feature => $enabled ) {
|
||||
$config->features->$feature = $enabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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";
|
||||
|
|
|
@ -32,11 +32,11 @@ const ProfileWizard = lazy( () =>
|
|||
|
||||
class Dashboard extends Component {
|
||||
render() {
|
||||
const { path, profileItems, query, homepageEnabled } = this.props;
|
||||
const { path, profileItems, query } = this.props;
|
||||
if (
|
||||
isOnboardingEnabled() &&
|
||||
! profileItems.completed &&
|
||||
! homepageEnabled
|
||||
! window.wcAdminFeatures.homepage
|
||||
) {
|
||||
return (
|
||||
<Suspense fallback={ <Spinner /> }>
|
||||
|
|
|
@ -20,7 +20,6 @@ import { Spinner } from '@woocommerce/components';
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { getSetting } from '@woocommerce/wc-admin-settings';
|
||||
import { getUrlParams } from 'utils';
|
||||
|
||||
const AnalyticsReport = lazy( () =>
|
||||
|
@ -47,7 +46,7 @@ const TIME_EXCLUDED_SCREENS_FILTER = 'woocommerce_admin_time_excluded_screens';
|
|||
|
||||
export const PAGES_FILTER = 'woocommerce_admin_pages_list';
|
||||
|
||||
export const getPages = ( homepageEnabled ) => {
|
||||
export const getPages = () => {
|
||||
const pages = [];
|
||||
const initialBreadcrumbs = [ [ '', wcSettings.woocommerceTranslation ] ];
|
||||
|
||||
|
@ -74,7 +73,7 @@ export const getPages = ( homepageEnabled ) => {
|
|||
|
||||
if (
|
||||
window.wcAdminFeatures[ 'analytics-dashboard' ] &&
|
||||
! homepageEnabled
|
||||
! window.wcAdminFeatures.homepage
|
||||
) {
|
||||
pages.push( {
|
||||
container: Dashboard,
|
||||
|
@ -87,7 +86,7 @@ export const getPages = ( homepageEnabled ) => {
|
|||
} );
|
||||
}
|
||||
|
||||
if ( homepageEnabled ) {
|
||||
if ( window.wcAdminFeatures.homepage ) {
|
||||
pages.push( {
|
||||
container: Homepage,
|
||||
path: '/',
|
||||
|
@ -100,7 +99,7 @@ export const getPages = ( homepageEnabled ) => {
|
|||
}
|
||||
|
||||
if ( window.wcAdminFeatures.analytics ) {
|
||||
if ( homepageEnabled ) {
|
||||
if ( window.wcAdminFeatures.homepage ) {
|
||||
pages.push( {
|
||||
container: Dashboard,
|
||||
path: '/analytics/overview',
|
||||
|
@ -116,7 +115,7 @@ export const getPages = ( homepageEnabled ) => {
|
|||
} );
|
||||
}
|
||||
const ReportWpOpenMenu = `toplevel_page_wc-admin-path--analytics-${
|
||||
homepageEnabled ? 'overview' : 'revenue'
|
||||
window.wcAdminFeatures.homepage ? 'overview' : 'revenue'
|
||||
}`;
|
||||
|
||||
pages.push( {
|
||||
|
@ -218,7 +217,7 @@ export class Controller extends Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { page, match, location, homepageEnabled } = this.props;
|
||||
const { page, match, location } = this.props;
|
||||
const { url, params } = match;
|
||||
const query = this.getQuery( location.search );
|
||||
|
||||
|
@ -231,7 +230,6 @@ export class Controller extends Component {
|
|||
path={ url }
|
||||
pathMatch={ page.path }
|
||||
query={ query }
|
||||
homepageEnabled={ homepageEnabled }
|
||||
/>
|
||||
</Suspense>
|
||||
);
|
||||
|
@ -252,12 +250,8 @@ export function updateLinkHref( item, nextQuery, excludedScreens ) {
|
|||
if ( isWCAdmin ) {
|
||||
const search = last( item.href.split( '?' ) );
|
||||
const query = parse( search );
|
||||
const { woocommerce_homescreen_enabled: homepageOption } = getSetting(
|
||||
'preloadOptions',
|
||||
{}
|
||||
);
|
||||
const defaultPath =
|
||||
window.wcAdminFeatures.homepage && homepageOption === 'yes'
|
||||
window.wcAdminFeatures.homepage
|
||||
? 'homepage'
|
||||
: 'dashboard';
|
||||
const path = query.path || defaultPath;
|
||||
|
|
|
@ -15,7 +15,6 @@ import { useFilters, Spinner } from '@woocommerce/components';
|
|||
import { getHistory } from '@woocommerce/navigation';
|
||||
import { getSetting } from '@woocommerce/wc-admin-settings';
|
||||
import {
|
||||
OPTIONS_STORE_NAME,
|
||||
PLUGINS_STORE_NAME,
|
||||
withPluginsHydration,
|
||||
withOptionsHydration,
|
||||
|
@ -79,7 +78,6 @@ class _Layout extends Component {
|
|||
installedPlugins,
|
||||
isEmbedded,
|
||||
isJetpackConnected,
|
||||
homepageEnabled,
|
||||
} = this.props;
|
||||
|
||||
if ( isEmbedded ) {
|
||||
|
@ -98,7 +96,9 @@ class _Layout extends Component {
|
|||
|
||||
// When pathname is `/` we are on the dashboard
|
||||
if ( path.length === 0 ) {
|
||||
path = homepageEnabled ? 'home_screen' : 'dashboard';
|
||||
path = window.wcAdminFeatures.homepage
|
||||
? 'home_screen'
|
||||
: 'dashboard';
|
||||
}
|
||||
|
||||
recordPageView( path, {
|
||||
|
@ -186,22 +186,17 @@ const Layout = compose(
|
|||
|
||||
class _PageLayout extends Component {
|
||||
render() {
|
||||
const { homepageEnabled } = this.props;
|
||||
return (
|
||||
<Router history={ getHistory() }>
|
||||
<Switch>
|
||||
{ getPages( homepageEnabled ).map( ( page ) => {
|
||||
{ getPages().map( ( page ) => {
|
||||
return (
|
||||
<Route
|
||||
key={ page.path }
|
||||
path={ page.path }
|
||||
exact
|
||||
render={ ( props ) => (
|
||||
<Layout
|
||||
page={ page }
|
||||
homepageEnabled={ homepageEnabled }
|
||||
{ ...props }
|
||||
/>
|
||||
<Layout page={ page } { ...props } />
|
||||
) }
|
||||
/>
|
||||
);
|
||||
|
@ -219,14 +214,7 @@ export const PageLayout = compose(
|
|||
? withOptionsHydration( {
|
||||
...window.wcSettings.preloadOptions,
|
||||
} )
|
||||
: identity,
|
||||
withSelect( ( select ) => {
|
||||
const { getOption } = select( OPTIONS_STORE_NAME );
|
||||
const homepageEnabled =
|
||||
window.wcAdminFeatures.homepage &&
|
||||
getOption( 'woocommerce_homescreen_enabled' ) === 'yes';
|
||||
return { homepageEnabled };
|
||||
} )
|
||||
: identity
|
||||
)( _PageLayout );
|
||||
|
||||
export class EmbedLayout extends Component {
|
||||
|
|
|
@ -199,7 +199,7 @@ class FeaturePlugin {
|
|||
* Set up our admin hooks and plugin loader.
|
||||
*/
|
||||
protected function hooks() {
|
||||
add_filter( 'woocommerce_admin_features', array( $this, 'replace_supported_features' ) );
|
||||
add_filter( 'woocommerce_admin_features', array( $this, 'replace_supported_features' ), 0 );
|
||||
add_action( 'admin_menu', array( $this, 'register_devdocs_page' ) );
|
||||
|
||||
new Loader();
|
||||
|
|
|
@ -77,8 +77,7 @@ class Analytics {
|
|||
* Registers report pages.
|
||||
*/
|
||||
public function register_pages() {
|
||||
$features = wc_admin_get_feature_config();
|
||||
$homepage_enabled = Loader::is_homepage_enabled( $features );
|
||||
$homepage_enabled = Loader::is_feature_enabled( 'homepage' );
|
||||
$report_pages = array(
|
||||
array(
|
||||
'id' => 'woocommerce-analytics',
|
||||
|
|
|
@ -82,10 +82,8 @@ class AnalyticsDashboard {
|
|||
* Registers dashboard page.
|
||||
*/
|
||||
public function register_page() {
|
||||
$features = wc_admin_get_feature_config();
|
||||
$homepage_enabled = Loader::is_homepage_enabled( $features );
|
||||
$id = $homepage_enabled ? 'woocommerce-home' : 'woocommerce-dashboard';
|
||||
$title = $homepage_enabled ? __( 'Home', 'woocommerce-admin' ) : __( 'Dashboard', 'woocommerce-admin' );
|
||||
$id = Loader::is_feature_enabled( 'homepage' ) ? 'woocommerce-home' : 'woocommerce-dashboard';
|
||||
$title = Loader::is_feature_enabled( 'homepage' ) ? __( 'Home', 'woocommerce-admin' ) : __( 'Dashboard', 'woocommerce-admin' );
|
||||
|
||||
wc_admin_register_page(
|
||||
array(
|
||||
|
|
|
@ -91,8 +91,6 @@ class Loader {
|
|||
* Gutenberg has also disabled emojis. More on that here -> https://github.com/WordPress/gutenberg/pull/6151
|
||||
*/
|
||||
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
|
||||
|
||||
add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,9 +121,9 @@ class Loader {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets an array of enabled WooCommerce Admin features/sections.
|
||||
* Gets a build configured array of enabled WooCommerce Admin features/sections.
|
||||
*
|
||||
* @return bool Enabled Woocommerce Admin features/sections.
|
||||
* @return array Enabled Woocommerce Admin features/sections.
|
||||
*/
|
||||
public static function get_features() {
|
||||
return apply_filters( 'woocommerce_admin_features', array() );
|
||||
|
@ -164,6 +162,10 @@ class Loader {
|
|||
* @return bool Returns true if the feature is enabled.
|
||||
*/
|
||||
public static function is_feature_enabled( $feature ) {
|
||||
if ( 'homepage' === $feature && 'yes' !== get_option( 'woocommerce_homescreen_enabled', 'no' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$features = self::get_features();
|
||||
return in_array( $feature, $features, true );
|
||||
}
|
||||
|
@ -171,7 +173,7 @@ class Loader {
|
|||
/**
|
||||
* Returns if the onboarding feature of WooCommerce Admin should be enabled.
|
||||
*
|
||||
* While we preform an a/b test of onboarding, the feature will be enabled within the plugin build, but only if the user recieved the test/opted in.
|
||||
* While we preform an a/b test of onboarding, the feature will be enabled within the plugin build, but only if the user received the test/opted in.
|
||||
*
|
||||
* @return bool Returns true if the onboarding is enabled.
|
||||
*/
|
||||
|
@ -190,29 +192,6 @@ class Loader {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preload options to prime state of the application.
|
||||
*
|
||||
* @param array $options Array of options to preload.
|
||||
* @return array
|
||||
*/
|
||||
public function preload_options( $options ) {
|
||||
$options[] = 'woocommerce_homescreen_enabled';
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if homescreen is enabled.
|
||||
*
|
||||
* @param array $features Array of features returned from wc_admin_get_feature_config.
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_homepage_enabled( $features ) {
|
||||
$option = get_option( 'woocommerce_homescreen_enabled', 'no' );
|
||||
return $features['homepage'] && 'yes' === $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the URL to an asset file.
|
||||
*
|
||||
|
@ -275,8 +254,7 @@ class Loader {
|
|||
* @todo The entry point for the embed needs moved to this class as well.
|
||||
*/
|
||||
public static function register_page_handler() {
|
||||
$features = wc_admin_get_feature_config();
|
||||
$id = self::is_homepage_enabled( $features ) ? 'woocommerce-home' : 'woocommerce-dashboard';
|
||||
$id = self::is_feature_enabled( 'homepage' ) ? 'woocommerce-home' : 'woocommerce-dashboard';
|
||||
|
||||
wc_admin_register_page(
|
||||
array(
|
||||
|
@ -481,6 +459,13 @@ class Loader {
|
|||
return;
|
||||
}
|
||||
|
||||
$features = self::get_features();
|
||||
$enabled_features = array();
|
||||
foreach ( $features as $key ) {
|
||||
$enabled_features[ $key ] = self::is_feature_enabled( $key );
|
||||
}
|
||||
wp_add_inline_script( WC_ADMIN_APP, 'window.wcAdminFeatures = ' . wp_json_encode( $enabled_features ), 'before' );
|
||||
|
||||
wp_enqueue_script( WC_ADMIN_APP );
|
||||
wp_enqueue_style( WC_ADMIN_APP );
|
||||
wp_enqueue_style( 'wc-material-icons' );
|
||||
|
@ -633,10 +618,8 @@ class Loader {
|
|||
* The initial contents here are meant as a place loader for when the PHP page initialy loads.
|
||||
*/
|
||||
public static function embed_page_header() {
|
||||
|
||||
$features = wc_admin_get_feature_config();
|
||||
if (
|
||||
$features['navigation'] &&
|
||||
self::is_feature_enabled( 'navigation' ) &&
|
||||
\Automattic\WooCommerce\Admin\Features\Navigation::instance()->is_woocommerce_page()
|
||||
) {
|
||||
self::embed_navigation_menu();
|
||||
|
|
|
@ -5,7 +5,6 @@ const MiniCssExtractPlugin = require( '@automattic/mini-css-extract-plugin-with-
|
|||
const { get } = require( 'lodash' );
|
||||
const path = require( 'path' );
|
||||
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
|
||||
const { DefinePlugin } = require( 'webpack' );
|
||||
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
|
||||
const FixStyleOnlyEntriesPlugin = require( 'webpack-fix-style-only-entries' );
|
||||
const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' )
|
||||
|
@ -21,21 +20,6 @@ const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-web
|
|||
|
||||
const NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
||||
// generate-feature-config.php defaults to 'plugin', so lets match that here.
|
||||
let WC_ADMIN_PHASE = process.env.WC_ADMIN_PHASE || 'plugin';
|
||||
if ( [ 'development', 'plugin', 'core' ].indexOf( WC_ADMIN_PHASE ) === -1 ) {
|
||||
WC_ADMIN_PHASE = 'plugin';
|
||||
}
|
||||
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' ] },
|
||||
'@wordpress/blocks': { this: [ 'wp', 'blocks' ] },
|
||||
|
@ -200,13 +184,6 @@ const webpackConfig = {
|
|||
},
|
||||
plugins: [
|
||||
new FixStyleOnlyEntriesPlugin(),
|
||||
// Inject the current feature flags.
|
||||
new DefinePlugin( {
|
||||
'window.wcAdminFeatures': {
|
||||
...WC_ADMIN_CONFIG.features,
|
||||
...WC_ADMIN_ADDITIONAL_FEATURES,
|
||||
},
|
||||
} ),
|
||||
new CustomTemplatedPathPlugin( {
|
||||
modulename( outputPath, data ) {
|
||||
const entryName = get( data, [ 'chunk', 'name' ] );
|
||||
|
|
Loading…
Reference in New Issue