Add a new tool to call db update callbacks

This commit is contained in:
Moon 2021-11-17 14:08:52 -08:00
parent ac702dc5c3
commit 8650fae4e8
9 changed files with 159 additions and 1 deletions

View File

@ -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' );

View File

@ -0,0 +1,48 @@
<?php
use Automattic\WooCommerce\Admin\Install;
use Automattic\WooCommerce\Admin\API\Reports\Cache;
register_woocommerce_admin_test_helper_rest_route(
'/tools/get-update-versions/v1',
'tools_get_wc_admin_versions',
array(
'methods' => '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;
}

View File

@ -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: <DisableEmail />,
action: 'runDisableEmail',
},
{
command: 'Run version update callbacks',
description: <TriggerUpdateCallbacks />,
action: TRIGGER_UPDATE_CALLBACKS_ACTION_NAME,
},
];

View File

@ -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 (
<div className="trigger-cron-job">
{!dbUpdateVersions ? (
<p>Loading ...</p>
) : (
<SelectControl
label="Select a version to run"
onChange={onCronChange}
labelPosition="side"
options={getOptions().reverse()}
/>
)}
</div>
);
};

View File

@ -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;

View File

@ -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({

View File

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

View File

@ -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`;

View File

@ -21,3 +21,7 @@ export function getCronJobs( state ) {
export function getIsEmailDisabled( state ) {
return state.isEmailDisabled;
}
export function getDBUpdateVersions(state) {
return state.dbUpdateVersions;
}