Tool to trigger WCA install

This commit is contained in:
Rebecca Scott 2021-03-04 10:56:39 +10:00
parent b59d3ce80e
commit e2f20bd546
6 changed files with 96 additions and 0 deletions

View File

@ -23,3 +23,4 @@ function register_woocommerce_admin_test_helper_rest_route( $route, $callback )
require( 'admin-notes/delete-all-notes.php' );
require( 'admin-notes/add-note.php' );
require( 'tools/trigger-wca-install.php' );

View File

@ -0,0 +1,11 @@
<?php
register_woocommerce_admin_test_helper_rest_route(
'/tools/trigger-wca-install/v1',
'tools_trigger_wca_install'
);
function tools_trigger_wca_install() {
Automattic\WooCommerce\Admin\Install::install();
return true;
}

View File

@ -11,6 +11,7 @@ import { applyFilters, addFilter } from '@wordpress/hooks';
// import { Options } from '../Options';
const Options = () => <h2>Options</h2>;
import { AdminNotes } from '../admin-notes';
import { Tools } from '../tools';
const tabs = applyFilters(
'woocommerce_admin_test_helper_tabs',
@ -25,6 +26,11 @@ const tabs = applyFilters(
title: 'Admin notes',
content: <AdminNotes/>,
},
{
name: 'tools',
title: 'Tools',
content: <Tools/>,
},
]
);

1
src/tools/index.js Normal file
View File

@ -0,0 +1 @@
export { Tools } from './tools';

14
src/tools/tools.js Normal file
View File

@ -0,0 +1,14 @@
/**
* Internal dependencies.
*/
import { TriggerWooCommerceAdminInstall } from './trigger-woocommerce-admin-install';
export const Tools = () => {
return (
<>
<h2>Tools</h2>
<p>This section contains miscellaneous tools.</p>
<TriggerWooCommerceAdminInstall/>
</>
);
};

View File

@ -0,0 +1,63 @@
/**
* External dependencies.
*/
import { useState } from '@wordpress/element';
import { Button } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
export const TriggerWooCommerceAdminInstall = () => {
const [ isInstalling, setIsInstalling ] = useState( false );
const [ hasInstalled, setHasInstalled ] = useState( false );
const [ errorMessage, setErrorMessage ] = useState( false );
async function triggerInstall() {
if ( ! confirm( 'Are you sure you want to trigger a WCA install?' ) ) {
return;
}
setIsInstalling( true );
setHasInstalled( false );
setErrorMessage( false );
try {
await apiFetch( {
path: '/wc-admin-test-helper/tools/trigger-wca-install/v1',
method: 'POST',
} );
setHasInstalled( true );
} catch ( ex ) {
setErrorMessage( ex.message );
}
setIsInstalling( false );
}
return (
<>
<p><strong>Trigger WooCommerce Admin install</strong></p>
<p>
This will trigger a WooCommerce Admin install, which usually
happens when a new version (or new install) of WooCommerce
Admin is installed. Triggering the install manually can
run tasks such as removing obsolete admin notes.
<br/>
<Button
onClick={ triggerInstall }
disabled={ isInstalling }
isPrimary
>
Trigger WCA install
</Button>
<div className="woocommerce-admin-test-helper__action-status">
{ isInstalling && 'Running install, please wait' }
{ hasInstalled && 'Install completed' }
{ errorMessage && (
<>
<strong>Error:</strong> { errorMessage }
</>
) }
</div>
</p>
</>
);
};