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
This commit is contained in:
Chi-Hsuan Huang 2024-07-16 19:55:38 +08:00 committed by GitHub
parent b8fcc56e43
commit 8d23ac11a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 283 additions and 48 deletions

View File

@ -53,12 +53,13 @@ require 'tools/delete-all-products.php';
require 'tools/disable-wc-email.php'; require 'tools/disable-wc-email.php';
require 'tools/trigger-update-callbacks.php'; require 'tools/trigger-update-callbacks.php';
require 'tools/reset-cys.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 'tracks/class-tracks-debug-log.php';
require 'features/features.php'; require 'features/features.php';
require 'rest-api-filters/class-wca-test-helper-rest-api-filters.php'; require 'rest-api-filters/class-wca-test-helper-rest-api-filters.php';
require 'rest-api-filters/hook.php'; require 'rest-api-filters/hook.php';
require 'live-branches/manifest.php'; require 'live-branches/manifest.php';
require 'live-branches/install.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-spec-validator/class-wca-test-helper-remote-spec-validator.php';
require 'remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php'; require 'remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php';

View File

@ -0,0 +1,52 @@
<?php
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\ComingSoon\ComingSoonCacheInvalidator;
register_woocommerce_admin_test_helper_rest_route(
'/tools/update-coming-soon-mode/v1',
'tools_set_coming_soon_mode',
array(
'methods' => '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 );
}

View File

@ -48,7 +48,7 @@ function trigger_selected_update_callbacks( $request ) {
$update_callbacks = $db_updates[ $version ]; $update_callbacks = $db_updates[ $version ];
foreach ( $update_callbacks as $update_callback ) { foreach ( $update_callbacks as $update_callback ) {
call_user_func( $update_callback ); \WC_Install::run_update_callback( $update_callback );
} }
return false; return false;

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add tool to force the coming soon landing pages to display on the front-end

View File

@ -0,0 +1,77 @@
<?php
/**
* Override the coming soon options.
* @package WC_Beta_Tester
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Beta_Tester Override Coming Soon Options Class.
*/
class WC_Beta_Tester_Override_Coming_Soon_Options {
/**
* Constructor.
*/
public function __construct() {
$this->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();

View File

@ -26,3 +26,4 @@ add_filter( 'woocommerce_admin_get_feature_config', function( $feature_config )
} }
return $feature_config; return $feature_config;
} ); } );

View File

@ -11,6 +11,10 @@ import {
TriggerUpdateCallbacks, TriggerUpdateCallbacks,
TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, TRIGGER_UPDATE_CALLBACKS_ACTION_NAME,
} from './trigger-update-callbacks'; } from './trigger-update-callbacks';
import {
SetComingSoonMode,
UPDATE_COMING_SOON_MODE_ACTION_NAME,
} from './set-coming-soon-mode';
export default [ export default [
{ {
@ -78,4 +82,9 @@ export default [
description: <UpdateBlockTemplateLoggingThreshold />, description: <UpdateBlockTemplateLoggingThreshold />,
action: UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME, action: UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME,
}, },
{
command: 'Force coming soon page to show',
description: <SetComingSoonMode />,
action: UPDATE_COMING_SOON_MODE_ACTION_NAME,
},
]; ];

View File

@ -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 (
<div className="select-description">
{ ! comingSoonMode ? (
<p>Loading ...</p>
) : (
<SelectControl
label="Mode"
labelPosition="side"
value={ comingSoonMode }
onChange={ onChange }
options={ OPTIONS }
/>
) }
</div>
);
};

View File

@ -3,6 +3,7 @@
*/ */
import { SelectControl } from '@wordpress/components'; import { SelectControl } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
/** /**
* Internal dependencies * Internal dependencies
@ -13,26 +14,33 @@ export const TRIGGER_UPDATE_CALLBACKS_ACTION_NAME =
'runSelectedUpdateCallbacks'; 'runSelectedUpdateCallbacks';
export const TriggerUpdateCallbacks = () => { export const TriggerUpdateCallbacks = () => {
const { dbUpdateVersions } = useSelect( ( select ) => { const dbUpdateVersions = useSelect(
const { getDBUpdateVersions } = select( STORE_KEY ); ( select ) => select( STORE_KEY ).getDBUpdateVersions(),
return { []
dbUpdateVersions: getDBUpdateVersions(), );
}; const selectedVersion = useSelect(
} ); ( select ) =>
select( STORE_KEY ).getCommandParams(
TRIGGER_UPDATE_CALLBACKS_ACTION_NAME
).runSelectedUpdateCallbacks.version,
[]
);
const { updateCommandParams } = useDispatch( STORE_KEY ); const { updateCommandParams } = useDispatch( STORE_KEY );
function onCronChange( version ) { function onChange( version ) {
updateCommandParams( TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, { updateCommandParams( TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, {
version, version,
} ); } );
} }
function getOptions() { const options = useMemo(
return dbUpdateVersions.map( ( version ) => { () =>
return { label: version, value: version }; dbUpdateVersions.map( ( version ) => ( {
} ); label: version,
} value: version,
} ) ),
[ dbUpdateVersions ]
);
return ( return (
<div className="select-description"> <div className="select-description">
@ -41,9 +49,10 @@ export const TriggerUpdateCallbacks = () => {
) : ( ) : (
<SelectControl <SelectControl
label="Select a version to run" label="Select a version to run"
onChange={ onCronChange } onChange={ onChange }
labelPosition="side" labelPosition="side"
options={ getOptions().reverse() } options={ options }
value={ selectedVersion }
/> />
) } ) }
</div> </div>

View File

@ -3,7 +3,6 @@
*/ */
import { SelectControl } from '@wordpress/components'; import { SelectControl } from '@wordpress/components';
import { useEffect, useState } from '@wordpress/element';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore no types // @ts-ignore no types
// eslint-disable-next-line @woocommerce/dependency-group // eslint-disable-next-line @woocommerce/dependency-group
@ -35,7 +34,6 @@ export const UpdateBlockTemplateLoggingThreshold = () => {
const retrievedLoggingLevels = getLoggingLevels(); const retrievedLoggingLevels = getLoggingLevels();
const retrievedThreshold = getBlockTemplateLoggingThreshold(); const retrievedThreshold = getBlockTemplateLoggingThreshold();
return { return {
loggingLevels: retrievedLoggingLevels, loggingLevels: retrievedLoggingLevels,
threshold: retrievedThreshold, threshold: retrievedThreshold,
@ -44,12 +42,9 @@ export const UpdateBlockTemplateLoggingThreshold = () => {
} }
); );
const [ newThreshold, setNewThreshold ] = useState( threshold );
const { updateCommandParams } = useDispatch( STORE_KEY ); const { updateCommandParams } = useDispatch( STORE_KEY );
function onThresholdChange( selectedThreshold: string ) { function onThresholdChange( selectedThreshold: string ) {
setNewThreshold( selectedThreshold );
updateCommandParams( updateCommandParams(
UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME, UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME,
{ {
@ -67,10 +62,6 @@ export const UpdateBlockTemplateLoggingThreshold = () => {
} ); } );
} }
useEffect( () => {
setNewThreshold( threshold );
}, [ threshold ] );
return ( return (
<div className="select-description"> <div className="select-description">
{ isLoading ? ( { isLoading ? (
@ -83,7 +74,7 @@ export const UpdateBlockTemplateLoggingThreshold = () => {
// @ts-ignore labelPosition prop exists // @ts-ignore labelPosition prop exists
labelPosition="side" labelPosition="side"
options={ getOptions() } options={ getOptions() }
value={ newThreshold } value={ threshold }
/> />
) } ) }
</div> </div>

View File

@ -9,10 +9,9 @@ const TYPES = {
IS_EMAIL_DISABLED: 'IS_EMAIL_DISABLED', IS_EMAIL_DISABLED: 'IS_EMAIL_DISABLED',
SET_DB_UPDATE_VERSIONS: 'SET_DB_UPDATE_VERSIONS', SET_DB_UPDATE_VERSIONS: 'SET_DB_UPDATE_VERSIONS',
SET_LOGGING_LEVELS: 'SET_LOGGING_LEVELS', 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_BLOCK_TEMPLATE_LOGGING_THRESHOLD', 'UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD',
UPDATE_COMING_SOON_MODE: 'UPDATE_COMING_SOON_MODE',
}; };
export default TYPES; export default TYPES;

View File

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

View File

@ -9,11 +9,14 @@ const DEFAULT_STATE = {
cronJobs: false, cronJobs: false,
isEmailDisabled: '', isEmailDisabled: '',
messages: {}, messages: {},
params: [], params: {
updateComingSoonMode: {},
updateBlockTemplateLoggingThreshold: {},
runSelectedUpdateCallbacks: {},
},
status: '', status: '',
dbUpdateVersions: [], dbUpdateVersions: [],
loggingLevels: null, loggingLevels: null,
blockTemplateLoggingThreshold: null,
}; };
const reducer = ( state = DEFAULT_STATE, action ) => { const reducer = ( state = DEFAULT_STATE, action ) => {
@ -48,7 +51,7 @@ const reducer = ( state = DEFAULT_STATE, action ) => {
return { return {
...state, ...state,
currentlyRunning: { currentlyRunning: {
...state, ...state.currentlyRunning,
[ action.command ]: true, [ action.command ]: true,
}, },
}; };
@ -56,7 +59,7 @@ const reducer = ( state = DEFAULT_STATE, action ) => {
return { return {
...state, ...state,
currentlyRunning: { currentlyRunning: {
...state, ...state.currentlyRunning,
[ action.command ]: false, [ action.command ]: false,
}, },
}; };
@ -74,6 +77,7 @@ const reducer = ( state = DEFAULT_STATE, action ) => {
return { return {
...state, ...state,
params: { params: {
...state.params,
[ action.source ]: action.params, [ action.source ]: action.params,
}, },
}; };
@ -87,12 +91,6 @@ const reducer = ( state = DEFAULT_STATE, action ) => {
...state, ...state,
loggingLevels: action.loggingLevels, loggingLevels: action.loggingLevels,
}; };
case TYPES.SET_BLOCK_TEMPLATE_LOGGING_THRESHOLD:
return {
...state,
blockTemplateLoggingThreshold:
action.blockTemplateLoggingThreshold,
};
default: default:
return state; return state;
} }

View File

@ -8,12 +8,15 @@ import { apiFetch } from '@wordpress/data-controls';
*/ */
import { API_NAMESPACE } from './constants'; import { API_NAMESPACE } from './constants';
import { import {
setBlockTemplateLoggingThreshold,
setCronJobs, setCronJobs,
setDBUpdateVersions, setDBUpdateVersions,
setIsEmailDisabled, setIsEmailDisabled,
setLoggingLevels, setLoggingLevels,
updateCommandParams,
} from './actions'; } 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() { export function* getCronJobs() {
const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`; 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`; const path = `${ API_NAMESPACE }/tools/get-update-versions/v1`;
try { try {
const response = yield apiFetch( { const dbUpdateVersions = yield apiFetch( {
path, path,
method: 'GET', 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 ) { } catch ( error ) {
throw new Error( error ); throw new Error( error );
} }
@ -76,11 +87,32 @@ export function* getBlockTemplateLoggingThreshold() {
const path = `${ API_NAMESPACE }/tools/get-block-template-logging-threshold/v1`; const path = `${ API_NAMESPACE }/tools/get-block-template-logging-threshold/v1`;
try { try {
const response = yield apiFetch( { const threshold = yield apiFetch( {
path, path,
method: 'GET', 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 ) { } catch ( error ) {
throw new Error( error ); throw new Error( error );
} }

View File

@ -31,5 +31,9 @@ export function getLoggingLevels( state ) {
} }
export function getBlockTemplateLoggingThreshold( state ) { export function getBlockTemplateLoggingThreshold( state ) {
return state.blockTemplateLoggingThreshold; return state.params.updateBlockTemplateLoggingThreshold.threshold;
}
export function getComingSoonMode( state ) {
return state.params.updateComingSoonMode.mode;
} }

View File

@ -77,9 +77,8 @@ function Tools( {
export default compose( export default compose(
withSelect( ( select ) => { withSelect( ( select ) => {
const { getCurrentlyRunning, getMessages, getCommandParams } = select( const { getCurrentlyRunning, getMessages, getCommandParams } =
STORE_KEY select( STORE_KEY );
);
return { return {
currentlyRunningCommands: getCurrentlyRunning(), currentlyRunningCommands: getCurrentlyRunning(),
messages: getMessages(), messages: getMessages(),

View File

@ -63,6 +63,7 @@ function _wc_beta_tester_bootstrap() {
new WC_Beta_Tester_Import_Export(); new WC_Beta_Tester_Import_Export();
// Tools. // Tools.
include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-version-picker.php'; 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' ) ); register_activation_hook( __FILE__, array( 'WC_Beta_Tester', 'activate' ) );