Merge pull request #3 from woocommerce/add/delete-all-admin-notes

Add/delete all admin notes
This commit is contained in:
Bec Scott 2021-03-01 16:38:03 +10:00 committed by GitHub
commit 27691cf78b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 2791 additions and 1949 deletions

View File

@ -0,0 +1,33 @@
<?php
add_action( 'rest_api_init', function() {
register_rest_route(
'wc-admin-test-helper/v1',
'/admin-notes/delete-all-notes',
array(
'methods' => 'POST',
'callback' => 'admin_notes_delete_all_notes',
'permission_callback' => function( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error(
'woocommerce_rest_cannot_edit',
__( 'Sorry, you cannot perform this action', 'woocommerce-admin-test-helper' )
);
}
return true;
},
)
);
} );
function admin_notes_delete_all_notes() {
global $wpdb;
$deleted_note_count = $wpdb->query( "DELETE FROM {$wpdb->prefix}wc_admin_notes" );
$deleted_action_count = $wpdb->query( "DELETE FROM {$wpdb->prefix}wc_admin_note_actions" );
return array(
'deleted_note_count' => $deleted_note_count,
'deleted_action_count' => $deleted_action_count,
);
}

2
api/api.php Normal file
View File

@ -0,0 +1,2 @@
<?php
require( 'admin-notes/delete-all-notes.php' );

4574
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -20,12 +20,15 @@
"test:unit": "wp-scripts test-unit-js" "test:unit": "wp-scripts test-unit-js"
}, },
"devDependencies": { "devDependencies": {
"@wordpress/scripts": "^12.2.1", "@woocommerce/dependency-extraction-webpack-plugin": "1.4.0",
"@woocommerce/eslint-plugin": "1.1.0", "@woocommerce/eslint-plugin": "1.1.0",
"@woocommerce/dependency-extraction-webpack-plugin": "1.4.0" "@wordpress/scripts": "^13.0.3"
}, },
"dependencies": { "dependencies": {
"@woocommerce/data": "^1.1.1",
"@wordpress/api-fetch": "^3.21.5",
"@wordpress/components": "^12.0.7", "@wordpress/components": "^12.0.7",
"@wordpress/data": "^4.26.7",
"@wordpress/element": "^2.19.1" "@wordpress/element": "^2.19.1"
} }
} }

View File

@ -10,3 +10,7 @@ add_action( 'admin_menu', function() {
} }
); );
} ); } );
add_action( 'wp_loaded', function() {
require( 'api/api.php' );
} );

View File

@ -9,7 +9,7 @@ import { TabPanel } from '@wordpress/components';
// TODO replace this with the actual controls // TODO replace this with the actual controls
// import { Options } from '../Options'; // import { Options } from '../Options';
const Options = () => <h2>Options</h2>; const Options = () => <h2>Options</h2>;
const AdminNotes = () => <h2>Admin notes</h2>; import { AdminNotes } from '../admin-notes';
const tabs = [ const tabs = [
{ {
@ -34,7 +34,7 @@ export function App() {
tabs={ tabs } tabs={ tabs }
initialTabName={ tabs[0].name } initialTabName={ tabs[0].name }
> >
{ ( tab ) => <p>{ tab.content }</p> } { ( tab ) => tab.content }
</TabPanel> </TabPanel>
</div> </div>
); );

View File

@ -0,0 +1,14 @@
/**
* Internal dependencies.
*/
import { DeleteAllNotes } from './delete-all-notes';
export const AdminNotes = () => {
return (
<>
<h2>Admin notes</h2>
<p>This section contains tools for managing admin notes.</p>
<DeleteAllNotes/>
</>
);
};

View File

@ -0,0 +1,69 @@
/**
* External dependencies.
*/
import { useState } from '@wordpress/element';
import { Button } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
export const DeleteAllNotes = () => {
const [ isDeleting, setIsDeleting ] = useState( false );
const [ deleteStatus, setDeleteStatus ] = useState( false );
const [ errorMessage, setErrorMessage ] = useState( false );
async function triggerDeleteAllNotes() {
setIsDeleting( true );
setErrorMessage( false );
setDeleteStatus( false );
try {
const response = await apiFetch( {
path: '/wc-admin-test-helper/v1/admin-notes/delete-all-notes',
method: 'POST',
} );
setDeleteStatus( response );
} catch ( ex ) {
setErrorMessage( ex.message );
}
setIsDeleting( false );
}
return (
<>
<p><strong>Delete all admin notes</strong></p>
<p>
This will delete all notes from the <code>wp_wc_admin_notes</code>
table, and actions from the <code>wp_wc_admin_note_actions</code>
table.
<br/>
<Button
onClick={ triggerDeleteAllNotes }
disabled={ isDeleting }
isPrimary
>
Delete all notes
</Button>
<br/>
<span className="woocommerce-admin-test-helper__action-status">
{ isDeleting && 'Deleting, please wait.' }
{ deleteStatus && (
<>
Deleted{ ' ' }
<strong>{ deleteStatus.deleted_note_count }</strong>{ ' ' }
admin notes and{ ' ' }
<strong>{ deleteStatus.deleted_action_count }</strong>{ ' ' }
actions.
</>
) }
{ errorMessage && (
<>
<strong>Error: </strong>
{ errorMessage }
</>
) }
</span>
</p>
</>
);
};

1
src/admin-notes/index.js Normal file
View File

@ -0,0 +1 @@
export { AdminNotes } from './admin-notes.js';

View File

@ -6,7 +6,7 @@ import { render } from '@wordpress/element';
/** /**
* Internal dependencies * Internal dependencies
*/ */
import { App } from './App'; import { App } from './app';
import './index.scss'; import './index.scss';
const appRoot = document.getElementById( 'woocommerce-admin-test-helper-app-root' ); const appRoot = document.getElementById( 'woocommerce-admin-test-helper-app-root' );

View File

@ -4,3 +4,9 @@
box-shadow: inset 0 1.5px var(--wp-admin-theme-color); box-shadow: inset 0 1.5px var(--wp-admin-theme-color);
} }
} }
.woocommerce-admin-test-helper__action-status {
color: #007cba;
color: var(--wp-admin-theme-color);
font-family: monospace;
}

View File

@ -10,6 +10,8 @@
* @package WooCommerce\Admin\TestHelper * @package WooCommerce\Admin\TestHelper
*/ */
define( 'WC_ADMIN_TEST_HELPER_PLUGIN_FILE', 'woocommerce-admin-test-helper/woocommerce-admin-test-helper.php' );
/** /**
* Register the JS. * Register the JS.
*/ */
@ -28,18 +30,26 @@ function add_extension_register_script() {
$script_asset['version'], $script_asset['version'],
true true
); );
wp_enqueue_script( 'woocommerce-admin-test-helper' );
$css_file_version = filemtime( dirname( __FILE__ ) . '/build/index.css' );
wp_register_style(
'wp-components',
plugins_url( 'dist/components/style.css', WC_ADMIN_TEST_HELPER_PLUGIN_FILE ),
array(),
$css_file_version
);
wp_register_style( wp_register_style(
'woocommerce-admin-test-helper', 'woocommerce-admin-test-helper',
plugins_url( '/build/index.css', __FILE__ ), plugins_url( '/build/index.css', __FILE__ ),
// Add any dependencies styles may have, such as wp-components. // Add any dependencies styles may have, such as wp-components.
array( array(
'wp-components', 'wp-components'
), ),
filemtime( dirname( __FILE__ ) . '/build/index.css' ) $css_file_version
); );
wp_enqueue_script( 'woocommerce-admin-test-helper' );
wp_enqueue_style( 'woocommerce-admin-test-helper' ); wp_enqueue_style( 'woocommerce-admin-test-helper' );
} }