From 8650fae4e89daabb2082a6d2da918da75992ef0e Mon Sep 17 00:00:00 2001 From: Moon Date: Wed, 17 Nov 2021 14:08:52 -0800 Subject: [PATCH] Add a new tool to call db update callbacks --- api/api.php | 1 + api/tools/trigger-update-callbacks.php | 48 +++++++++++++++++ src/tools/commands/index.js | 12 +++++ .../commands/trigger-update-callbacks.js | 51 +++++++++++++++++++ src/tools/data/action-types.js | 1 + src/tools/data/actions.js | 17 +++++++ src/tools/data/reducer.js | 6 +++ src/tools/data/resolvers.js | 20 +++++++- src/tools/data/selectors.js | 4 ++ 9 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 api/tools/trigger-update-callbacks.php create mode 100644 src/tools/commands/trigger-update-callbacks.js diff --git a/api/api.php b/api/api.php index 8d2701b156c..2d752a1783a 100644 --- a/api/api.php +++ b/api/api.php @@ -34,3 +34,4 @@ require( 'tools/run-wc-admin-daily.php' ); require( 'options/rest-api.php' ); require( 'tools/delete-all-products.php'); require( 'tools/disable-wc-email.php' ); +require( 'tools/trigger-update-callbacks.php' ); diff --git a/api/tools/trigger-update-callbacks.php b/api/tools/trigger-update-callbacks.php new file mode 100644 index 00000000000..61aab3292a9 --- /dev/null +++ b/api/tools/trigger-update-callbacks.php @@ -0,0 +1,48 @@ + 'GET', + ) +); +register_woocommerce_admin_test_helper_rest_route( + '/tools/trigger-selected-update-callbacks/v1', + 'trigger_selected_update_callbacks', + array( + 'methods' => 'POST', + 'args' => array( + 'version' => array( + 'description' => 'Name of the update version', + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + ) + ), + ) +); + +function tools_get_wc_admin_versions() { + $db_updates = Install::get_db_update_callbacks(); + + return new WP_REST_Response( array_keys( $db_updates ), 200 ); +} + +function trigger_selected_update_callbacks( $request ) { + $version = $request->get_param( 'version' ); + if ( ! isset( $version ) ) { + return; + } + + $db_updates = Install::get_db_update_callbacks(); + $update_callbacks = $db_updates[ $version ]; + + foreach ( $update_callbacks as $update_callback ) { + call_user_func( $update_callback ); + } + + return false; +} diff --git a/src/tools/commands/index.js b/src/tools/commands/index.js index d43b7db2a92..d2cef0661cf 100644 --- a/src/tools/commands/index.js +++ b/src/tools/commands/index.js @@ -1,5 +1,12 @@ +/** + * Internal dependencies + */ import { TriggerCronJob, TRIGGER_CRON_ACTION_NAME } from './trigger-cron'; import { DisableEmail } from './disable-email'; +import { + TriggerUpdateCallbacks, + TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, +} from './trigger-update-callbacks'; export default [ { @@ -52,4 +59,9 @@ export default [ description: , action: 'runDisableEmail', }, + { + command: 'Run version update callbacks', + description: , + action: TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, + }, ]; diff --git a/src/tools/commands/trigger-update-callbacks.js b/src/tools/commands/trigger-update-callbacks.js new file mode 100644 index 00000000000..e830416ddc1 --- /dev/null +++ b/src/tools/commands/trigger-update-callbacks.js @@ -0,0 +1,51 @@ +/** + * External dependencies + */ +import { SelectControl } from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { STORE_KEY } from '../data/constants'; + +export const TRIGGER_UPDATE_CALLBACKS_ACTION_NAME = + 'runSelectedUpdateCallbacks'; + +export const TriggerUpdateCallbacks = () => { + const { dbUpdateVersions } = useSelect((select) => { + const { getDBUpdateVersions } = select(STORE_KEY); + return { + dbUpdateVersions: getDBUpdateVersions(), + }; + }); + + const { updateCommandParams } = useDispatch(STORE_KEY); + + function onCronChange(version) { + updateCommandParams(TRIGGER_UPDATE_CALLBACKS_ACTION_NAME, { + version, + }); + } + + function getOptions() { + return dbUpdateVersions.map((version) => { + return { label: version, value: version }; + }); + } + + return ( +
+ {!dbUpdateVersions ? ( +

Loading ...

+ ) : ( + + )} +
+ ); +}; diff --git a/src/tools/data/action-types.js b/src/tools/data/action-types.js index fc9265f96e6..3cb725cd38a 100644 --- a/src/tools/data/action-types.js +++ b/src/tools/data/action-types.js @@ -7,6 +7,7 @@ const TYPES = { ADD_COMMAND_PARAMS: 'ADD_COMMAND_PARAMS', SET_CRON_JOBS: 'SET_CRON_JOBS', IS_EMAIL_DISABLED: 'IS_EMAIL_DISABLED', + SET_DB_UPDATE_VERSIONS: 'SET_DB_UPDATE_VERSIONS', }; export default TYPES; diff --git a/src/tools/data/actions.js b/src/tools/data/actions.js index c1b1bfb227b..c149889ab57 100644 --- a/src/tools/data/actions.js +++ b/src/tools/data/actions.js @@ -62,6 +62,13 @@ export function setCronJobs(cronJobs) { }; } +export function setDBUpdateVersions(versions) { + return { + type: TYPES.SET_DB_UPDATE_VERSIONS, + versions, + }; +} + export function setIsEmailDisabled(isEmailDisabled) { return { type: TYPES.IS_EMAIL_DISABLED, @@ -183,6 +190,16 @@ export function* runSelectedCronJob(params) { }); } +export function* runSelectedUpdateCallbacks(params) { + yield runCommand('Run version update callbacks', function* () { + yield apiFetch({ + path: API_NAMESPACE + '/tools/trigger-selected-update-callbacks/v1', + method: 'POST', + data: params, + }); + }); +} + export function* runDisableEmail() { yield runCommand('Disable/Enable WooCommerce emails', function* () { const response = yield apiFetch({ diff --git a/src/tools/data/reducer.js b/src/tools/data/reducer.js index 3a2303ad38b..7756dfcec6c 100644 --- a/src/tools/data/reducer.js +++ b/src/tools/data/reducer.js @@ -11,6 +11,7 @@ const DEFAULT_STATE = { messages: {}, params: [], status: '', + dbUpdateVersions: [], }; const reducer = ( state = DEFAULT_STATE, action ) => { @@ -74,6 +75,11 @@ const reducer = ( state = DEFAULT_STATE, action ) => { [ action.source ]: action.params, }, }; + case TYPES.SET_DB_UPDATE_VERSIONS: + return { + ...state, + dbUpdateVersions: action.versions, + }; default: return state; } diff --git a/src/tools/data/resolvers.js b/src/tools/data/resolvers.js index 54b53ef4eb8..bca43fb594a 100644 --- a/src/tools/data/resolvers.js +++ b/src/tools/data/resolvers.js @@ -7,7 +7,11 @@ import { apiFetch } from '@wordpress/data-controls'; * Internal dependencies */ import { API_NAMESPACE } from './constants'; -import { setCronJobs, setIsEmailDisabled } from './actions'; +import { + setCronJobs, + setDBUpdateVersions, + setIsEmailDisabled, +} from './actions'; export function* getCronJobs() { const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`; @@ -23,6 +27,20 @@ export function* getCronJobs() { } } +export function* getDBUpdateVersions() { + const path = `${API_NAMESPACE}/tools/get-update-versions/v1`; + + try { + const response = yield apiFetch({ + path, + method: 'GET', + }); + yield setDBUpdateVersions(response); + } catch (error) { + throw new Error(error); + } +} + export function* getIsEmailDisabled() { const path = `${API_NAMESPACE}/tools/get-email-status/v1`; diff --git a/src/tools/data/selectors.js b/src/tools/data/selectors.js index d34ee766934..9fdeabe0760 100644 --- a/src/tools/data/selectors.js +++ b/src/tools/data/selectors.js @@ -21,3 +21,7 @@ export function getCronJobs( state ) { export function getIsEmailDisabled( state ) { return state.isEmailDisabled; } + +export function getDBUpdateVersions(state) { + return state.dbUpdateVersions; +}