From 8d23ac11a942c4339b554e0235d4cd05a5563cc3 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Tue, 16 Jul 2024 19:55:38 +0800 Subject: [PATCH] Add beta tester tool to force the coming soon landing pages to display on the front-end (#49341) * Add tool to force the coming soon landing pages to display on the front-end * Add changelog * Revert line change * Add doc comments and remove unnecessary check * Invalidate caches * Fix tools invalid json errors * Set default value * Call wc_beta_tester_override_coming_soon_options out of init --- plugins/woocommerce-beta-tester/api/api.php | 3 +- .../api/tools/set-coming-soon-mode.php | 52 +++++++++++++ .../api/tools/trigger-update-callbacks.php | 2 +- .../add-beta-tester-coming-soon-tool | 4 + ...ta-tester-override-coming-soon-options.php | 77 +++++++++++++++++++ plugins/woocommerce-beta-tester/plugin.php | 1 + .../src/tools/commands/index.js | 9 +++ .../tools/commands/set-coming-soon-mode.js | 48 ++++++++++++ .../commands/trigger-update-callbacks.js | 39 ++++++---- ...pdate-block-template-logging-threshold.tsx | 11 +-- .../src/tools/data/action-types.js | 3 +- .../src/tools/data/actions.js | 10 +++ .../src/tools/data/reducer.js | 18 ++--- .../src/tools/data/resolvers.js | 42 ++++++++-- .../src/tools/data/selectors.js | 6 +- .../src/tools/index.js | 5 +- .../woocommerce-beta-tester.php | 1 + 17 files changed, 283 insertions(+), 48 deletions(-) create mode 100644 plugins/woocommerce-beta-tester/api/tools/set-coming-soon-mode.php create mode 100644 plugins/woocommerce-beta-tester/changelog/add-beta-tester-coming-soon-tool create mode 100644 plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-override-coming-soon-options.php create mode 100644 plugins/woocommerce-beta-tester/src/tools/commands/set-coming-soon-mode.js diff --git a/plugins/woocommerce-beta-tester/api/api.php b/plugins/woocommerce-beta-tester/api/api.php index 4c0071e99a2..62a4b4df2af 100644 --- a/plugins/woocommerce-beta-tester/api/api.php +++ b/plugins/woocommerce-beta-tester/api/api.php @@ -53,12 +53,13 @@ require 'tools/delete-all-products.php'; require 'tools/disable-wc-email.php'; require 'tools/trigger-update-callbacks.php'; require 'tools/reset-cys.php'; +require 'tools/set-block-template-logging-threshold.php'; +require 'tools/set-coming-soon-mode.php'; require 'tracks/class-tracks-debug-log.php'; require 'features/features.php'; require 'rest-api-filters/class-wca-test-helper-rest-api-filters.php'; require 'rest-api-filters/hook.php'; require 'live-branches/manifest.php'; require 'live-branches/install.php'; -require 'tools/set-block-template-logging-threshold.php'; require 'remote-spec-validator/class-wca-test-helper-remote-spec-validator.php'; require 'remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php'; diff --git a/plugins/woocommerce-beta-tester/api/tools/set-coming-soon-mode.php b/plugins/woocommerce-beta-tester/api/tools/set-coming-soon-mode.php new file mode 100644 index 00000000000..00dd6e2884f --- /dev/null +++ b/plugins/woocommerce-beta-tester/api/tools/set-coming-soon-mode.php @@ -0,0 +1,52 @@ + 'POST', + 'args' => array( + 'mode' => array( + 'description' => 'Coming soon mode', + 'type' => 'enum', + 'enum' => array( 'site', 'store', 'disabled' ), + ), + ), + ) +); + +register_woocommerce_admin_test_helper_rest_route( + '/tools/get-force-coming-soon-mode/v1', + 'tools_get_coming_soon_mode', + array( + 'methods' => 'GET', + ) +); + +/** + * A tool to set the coming soon mode. + * + * @param WP_REST_Request $request Request object. + */ +function tools_set_coming_soon_mode( $request ) { + $mode = $request->get_param( 'mode' ); + + update_option( 'wc_admin_test_helper_force_coming_soon_mode', $mode ); + + wc_get_container()->get( ComingSoonCacheInvalidator::class )->invalidate_caches(); + + return new WP_REST_Response( $mode, 200 ); +} + +/** + * A tool to get the coming soon mode. + */ +function tools_get_coming_soon_mode() { + $mode = get_option( 'wc_admin_test_helper_force_coming_soon_mode', 'disabled' ); + + return new WP_REST_Response( $mode, 200 ); +} diff --git a/plugins/woocommerce-beta-tester/api/tools/trigger-update-callbacks.php b/plugins/woocommerce-beta-tester/api/tools/trigger-update-callbacks.php index 0a06bbb237c..b13243da012 100644 --- a/plugins/woocommerce-beta-tester/api/tools/trigger-update-callbacks.php +++ b/plugins/woocommerce-beta-tester/api/tools/trigger-update-callbacks.php @@ -48,7 +48,7 @@ function trigger_selected_update_callbacks( $request ) { $update_callbacks = $db_updates[ $version ]; foreach ( $update_callbacks as $update_callback ) { - call_user_func( $update_callback ); + \WC_Install::run_update_callback( $update_callback ); } return false; diff --git a/plugins/woocommerce-beta-tester/changelog/add-beta-tester-coming-soon-tool b/plugins/woocommerce-beta-tester/changelog/add-beta-tester-coming-soon-tool new file mode 100644 index 00000000000..c60a03d3bbf --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/add-beta-tester-coming-soon-tool @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add tool to force the coming soon landing pages to display on the front-end diff --git a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-override-coming-soon-options.php b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-override-coming-soon-options.php new file mode 100644 index 00000000000..ebb451da843 --- /dev/null +++ b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-override-coming-soon-options.php @@ -0,0 +1,77 @@ +wc_beta_tester_override_coming_soon_options(); + } + + /** + * Override the coming soon options. + */ + public function wc_beta_tester_override_coming_soon_options() { + $mode = get_option( 'wc_admin_test_helper_force_coming_soon_mode', 'disabled' ); + + if ( 'disabled' === $mode ) { + return; + } + + $is_request_frontend = ( ! is_admin() || defined( 'DOING_AJAX' ) ) + && ! defined( 'DOING_CRON' ) && ! WC()->is_rest_api_request(); + if ( ! $is_request_frontend ) { + return; + } + + $this->override_woocommerce_coming_soon_option( $mode ); + $this->override_woocommerce_store_pages_only_option( $mode ); + } + + /** + * Override the woocommerce_coming_soon option. + * + * @param string $mode The coming soon mode. + */ + private function override_woocommerce_coming_soon_option( $mode ) { + add_filter( + 'option_woocommerce_coming_soon', + function ( $value ) use ( $mode ) { + if ( 'site' === $mode || 'store' === $mode ) { + return 'yes'; + } + return $value; + } + ); + } + + /** + * Override the woocommerce_store_pages_only option. + * + * @param string $mode The coming soon mode. + */ + private function override_woocommerce_store_pages_only_option( $mode ) { + add_filter( + 'option_woocommerce_store_pages_only', + function ( $value ) use ( $mode ) { + if ( 'store' === $mode ) { + return 'yes'; + } + return $value; + } + ); + } +} + +new WC_Beta_Tester_Override_Coming_Soon_Options(); diff --git a/plugins/woocommerce-beta-tester/plugin.php b/plugins/woocommerce-beta-tester/plugin.php index 60ffe6283f3..ec60a8e0856 100644 --- a/plugins/woocommerce-beta-tester/plugin.php +++ b/plugins/woocommerce-beta-tester/plugin.php @@ -26,3 +26,4 @@ add_filter( 'woocommerce_admin_get_feature_config', function( $feature_config ) } return $feature_config; } ); + diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/index.js b/plugins/woocommerce-beta-tester/src/tools/commands/index.js index 7e131f0d8e4..e077ae389bf 100644 --- a/plugins/woocommerce-beta-tester/src/tools/commands/index.js +++ b/plugins/woocommerce-beta-tester/src/tools/commands/index.js @@ -11,6 +11,10 @@ import { TriggerUpdateCallbacks, TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, } from './trigger-update-callbacks'; +import { + SetComingSoonMode, + UPDATE_COMING_SOON_MODE_ACTION_NAME, +} from './set-coming-soon-mode'; export default [ { @@ -78,4 +82,9 @@ export default [ description: , action: UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME, }, + { + command: 'Force coming soon page to show', + description: , + action: UPDATE_COMING_SOON_MODE_ACTION_NAME, + }, ]; diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/set-coming-soon-mode.js b/plugins/woocommerce-beta-tester/src/tools/commands/set-coming-soon-mode.js new file mode 100644 index 00000000000..69cfab16828 --- /dev/null +++ b/plugins/woocommerce-beta-tester/src/tools/commands/set-coming-soon-mode.js @@ -0,0 +1,48 @@ +/** + * External dependencies + */ +import { SelectControl } from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { STORE_KEY } from '../data/constants'; + +export const UPDATE_COMING_SOON_MODE_ACTION_NAME = 'updateComingSoonMode'; + +const OPTIONS = [ + { label: 'Whole Site', value: 'site' }, + { label: 'Store Only', value: 'store' }, + { label: 'Disabled', value: 'disabled' }, +]; + +export const SetComingSoonMode = () => { + const comingSoonMode = useSelect( + ( select ) => select( STORE_KEY ).getComingSoonMode(), + [] + ); + const { updateCommandParams } = useDispatch( STORE_KEY ); + + function onChange( mode ) { + updateCommandParams( UPDATE_COMING_SOON_MODE_ACTION_NAME, { + mode, + } ); + } + + return ( +
+ { ! comingSoonMode ? ( +

Loading ...

+ ) : ( + + ) } +
+ ); +}; diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/trigger-update-callbacks.js b/plugins/woocommerce-beta-tester/src/tools/commands/trigger-update-callbacks.js index 1c6969fca3c..019a1441b32 100644 --- a/plugins/woocommerce-beta-tester/src/tools/commands/trigger-update-callbacks.js +++ b/plugins/woocommerce-beta-tester/src/tools/commands/trigger-update-callbacks.js @@ -3,6 +3,7 @@ */ import { SelectControl } from '@wordpress/components'; import { useDispatch, useSelect } from '@wordpress/data'; +import { useMemo } from '@wordpress/element'; /** * Internal dependencies @@ -13,26 +14,33 @@ export const TRIGGER_UPDATE_CALLBACKS_ACTION_NAME = 'runSelectedUpdateCallbacks'; export const TriggerUpdateCallbacks = () => { - const { dbUpdateVersions } = useSelect( ( select ) => { - const { getDBUpdateVersions } = select( STORE_KEY ); - return { - dbUpdateVersions: getDBUpdateVersions(), - }; - } ); - + const dbUpdateVersions = useSelect( + ( select ) => select( STORE_KEY ).getDBUpdateVersions(), + [] + ); + const selectedVersion = useSelect( + ( select ) => + select( STORE_KEY ).getCommandParams( + TRIGGER_UPDATE_CALLBACKS_ACTION_NAME + ).runSelectedUpdateCallbacks.version, + [] + ); const { updateCommandParams } = useDispatch( STORE_KEY ); - function onCronChange( version ) { + function onChange( version ) { updateCommandParams( TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, { version, } ); } - function getOptions() { - return dbUpdateVersions.map( ( version ) => { - return { label: version, value: version }; - } ); - } + const options = useMemo( + () => + dbUpdateVersions.map( ( version ) => ( { + label: version, + value: version, + } ) ), + [ dbUpdateVersions ] + ); return (
@@ -41,9 +49,10 @@ export const TriggerUpdateCallbacks = () => { ) : ( ) }
diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/update-block-template-logging-threshold.tsx b/plugins/woocommerce-beta-tester/src/tools/commands/update-block-template-logging-threshold.tsx index e17d601adb0..e266051f38c 100644 --- a/plugins/woocommerce-beta-tester/src/tools/commands/update-block-template-logging-threshold.tsx +++ b/plugins/woocommerce-beta-tester/src/tools/commands/update-block-template-logging-threshold.tsx @@ -3,7 +3,6 @@ */ import { SelectControl } from '@wordpress/components'; -import { useEffect, useState } from '@wordpress/element'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore no types // eslint-disable-next-line @woocommerce/dependency-group @@ -35,7 +34,6 @@ export const UpdateBlockTemplateLoggingThreshold = () => { const retrievedLoggingLevels = getLoggingLevels(); const retrievedThreshold = getBlockTemplateLoggingThreshold(); - return { loggingLevels: retrievedLoggingLevels, threshold: retrievedThreshold, @@ -44,12 +42,9 @@ export const UpdateBlockTemplateLoggingThreshold = () => { } ); - const [ newThreshold, setNewThreshold ] = useState( threshold ); - const { updateCommandParams } = useDispatch( STORE_KEY ); function onThresholdChange( selectedThreshold: string ) { - setNewThreshold( selectedThreshold ); updateCommandParams( UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME, { @@ -67,10 +62,6 @@ export const UpdateBlockTemplateLoggingThreshold = () => { } ); } - useEffect( () => { - setNewThreshold( threshold ); - }, [ threshold ] ); - return (
{ isLoading ? ( @@ -83,7 +74,7 @@ export const UpdateBlockTemplateLoggingThreshold = () => { // @ts-ignore labelPosition prop exists labelPosition="side" options={ getOptions() } - value={ newThreshold } + value={ threshold } /> ) }
diff --git a/plugins/woocommerce-beta-tester/src/tools/data/action-types.js b/plugins/woocommerce-beta-tester/src/tools/data/action-types.js index b481a90efa1..9cf871b08ba 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/action-types.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/action-types.js @@ -9,10 +9,9 @@ const TYPES = { IS_EMAIL_DISABLED: 'IS_EMAIL_DISABLED', SET_DB_UPDATE_VERSIONS: 'SET_DB_UPDATE_VERSIONS', SET_LOGGING_LEVELS: 'SET_LOGGING_LEVELS', - SET_BLOCK_TEMPLATE_LOGGING_THRESHOLD: - 'SET_BLOCK_TEMPLATE_LOGGING_THRESHOLD', UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD: 'UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD', + UPDATE_COMING_SOON_MODE: 'UPDATE_COMING_SOON_MODE', }; export default TYPES; diff --git a/plugins/woocommerce-beta-tester/src/tools/data/actions.js b/plugins/woocommerce-beta-tester/src/tools/data/actions.js index 00d630e3fd6..9e34802dd2c 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/actions.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/actions.js @@ -262,3 +262,13 @@ export function* updateBlockTemplateLoggingThreshold( params ) { } ); } ); } + +export function* updateComingSoonMode( params ) { + yield runCommand( 'Update coming soon mode', function* () { + yield apiFetch( { + path: API_NAMESPACE + '/tools/update-coming-soon-mode/v1', + method: 'POST', + data: params, + } ); + } ); +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js index 7688a308ead..9a15d468c83 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js @@ -9,11 +9,14 @@ const DEFAULT_STATE = { cronJobs: false, isEmailDisabled: '', messages: {}, - params: [], + params: { + updateComingSoonMode: {}, + updateBlockTemplateLoggingThreshold: {}, + runSelectedUpdateCallbacks: {}, + }, status: '', dbUpdateVersions: [], loggingLevels: null, - blockTemplateLoggingThreshold: null, }; const reducer = ( state = DEFAULT_STATE, action ) => { @@ -48,7 +51,7 @@ const reducer = ( state = DEFAULT_STATE, action ) => { return { ...state, currentlyRunning: { - ...state, + ...state.currentlyRunning, [ action.command ]: true, }, }; @@ -56,7 +59,7 @@ const reducer = ( state = DEFAULT_STATE, action ) => { return { ...state, currentlyRunning: { - ...state, + ...state.currentlyRunning, [ action.command ]: false, }, }; @@ -74,6 +77,7 @@ const reducer = ( state = DEFAULT_STATE, action ) => { return { ...state, params: { + ...state.params, [ action.source ]: action.params, }, }; @@ -87,12 +91,6 @@ const reducer = ( state = DEFAULT_STATE, action ) => { ...state, loggingLevels: action.loggingLevels, }; - case TYPES.SET_BLOCK_TEMPLATE_LOGGING_THRESHOLD: - return { - ...state, - blockTemplateLoggingThreshold: - action.blockTemplateLoggingThreshold, - }; default: return state; } diff --git a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js index 6409038783b..5f55565925d 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js @@ -8,12 +8,15 @@ import { apiFetch } from '@wordpress/data-controls'; */ import { API_NAMESPACE } from './constants'; import { - setBlockTemplateLoggingThreshold, setCronJobs, setDBUpdateVersions, setIsEmailDisabled, setLoggingLevels, + updateCommandParams, } from './actions'; +import { UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME } from '../commands/update-block-template-logging-threshold'; +import { UPDATE_COMING_SOON_MODE_ACTION_NAME } from '../commands/set-coming-soon-mode'; +import { TRIGGER_UPDATE_CALLBACKS_ACTION_NAME } from '../commands/trigger-update-callbacks'; export function* getCronJobs() { const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`; @@ -33,11 +36,19 @@ export function* getDBUpdateVersions() { const path = `${ API_NAMESPACE }/tools/get-update-versions/v1`; try { - const response = yield apiFetch( { + const dbUpdateVersions = yield apiFetch( { path, method: 'GET', } ); - yield setDBUpdateVersions( response ); + + dbUpdateVersions.reverse(); + yield setDBUpdateVersions( dbUpdateVersions ); + yield updateCommandParams( TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, { + version: + Array.isArray( dbUpdateVersions ) && dbUpdateVersions.length > 0 + ? dbUpdateVersions[ 0 ] + : null, + } ); } catch ( error ) { throw new Error( error ); } @@ -76,11 +87,32 @@ export function* getBlockTemplateLoggingThreshold() { const path = `${ API_NAMESPACE }/tools/get-block-template-logging-threshold/v1`; try { - const response = yield apiFetch( { + const threshold = yield apiFetch( { path, method: 'GET', } ); - yield setBlockTemplateLoggingThreshold( response ); + yield updateCommandParams( + UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME, + { + threshold, + } + ); + } catch ( error ) { + throw new Error( error ); + } +} + +export function* getComingSoonMode() { + const path = `${ API_NAMESPACE }/tools/get-force-coming-soon-mode/v1`; + + try { + const mode = yield apiFetch( { + path, + method: 'GET', + } ); + yield updateCommandParams( UPDATE_COMING_SOON_MODE_ACTION_NAME, { + mode: mode || 'disabled', + } ); } catch ( error ) { throw new Error( error ); } diff --git a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js index f28fdae371f..1ac46fc713e 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js @@ -31,5 +31,9 @@ export function getLoggingLevels( state ) { } export function getBlockTemplateLoggingThreshold( state ) { - return state.blockTemplateLoggingThreshold; + return state.params.updateBlockTemplateLoggingThreshold.threshold; +} + +export function getComingSoonMode( state ) { + return state.params.updateComingSoonMode.mode; } diff --git a/plugins/woocommerce-beta-tester/src/tools/index.js b/plugins/woocommerce-beta-tester/src/tools/index.js index be33ab55fff..822d03d85b5 100644 --- a/plugins/woocommerce-beta-tester/src/tools/index.js +++ b/plugins/woocommerce-beta-tester/src/tools/index.js @@ -77,9 +77,8 @@ function Tools( { export default compose( withSelect( ( select ) => { - const { getCurrentlyRunning, getMessages, getCommandParams } = select( - STORE_KEY - ); + const { getCurrentlyRunning, getMessages, getCommandParams } = + select( STORE_KEY ); return { currentlyRunningCommands: getCurrentlyRunning(), messages: getMessages(), diff --git a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php index fbcdcf84f8e..ca0e6b53604 100644 --- a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php +++ b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php @@ -63,6 +63,7 @@ function _wc_beta_tester_bootstrap() { new WC_Beta_Tester_Import_Export(); // Tools. include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-version-picker.php'; + include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-override-coming-soon-options.php'; register_activation_hook( __FILE__, array( 'WC_Beta_Tester', 'activate' ) );