Merge pull request #18505 from liquidweb/feature/post-system-status-tool-hook

Add a hook after executing a REST system status tool
This commit is contained in:
Claudio Sanches 2018-02-02 14:19:03 -02:00 committed by GitHub
commit 0755038e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 165 additions and 123 deletions

View File

@ -227,6 +227,7 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
/** /**
* Update (execute) a tool. * Update (execute) a tool.
*
* @param WP_REST_Request $request * @param WP_REST_Request $request
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
@ -247,6 +248,14 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
$execute_return = $this->execute_tool( $request['id'] ); $execute_return = $this->execute_tool( $request['id'] );
$tool = array_merge( $tool, $execute_return ); $tool = array_merge( $tool, $execute_return );
/**
* Fires after a WooCommerce REST system status tool has been executed.
*
* @param array $tool Details about the tool that has been executed.
* @param WP_REST_Request $request The current WP_REST_Request object.
*/
do_action( 'woocommerce_rest_insert_system_status_tool', $tool, $request );
$request->set_param( 'context', 'edit' ); $request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $tool, $request ); $response = $this->prepare_item_for_response( $tool, $request );
return rest_ensure_response( $response ); return rest_ensure_response( $response );
@ -378,9 +387,11 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
WC_Cache_Helper::get_transient_version( 'shipping', true ); WC_Cache_Helper::get_transient_version( 'shipping', true );
$message = __( 'Product transients cleared', 'woocommerce' ); $message = __( 'Product transients cleared', 'woocommerce' );
break; break;
case 'clear_expired_transients': case 'clear_expired_transients':
$message = sprintf( __( '%d transients rows cleared', 'woocommerce' ), wc_delete_expired_transients() ); $message = sprintf( __( '%d transients rows cleared', 'woocommerce' ), wc_delete_expired_transients() );
break; break;
case 'delete_orphaned_variations': case 'delete_orphaned_variations':
/** /**
* Delete orphans * Delete orphans
@ -391,6 +402,7 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
WHERE wp.ID IS NULL AND products.post_type = 'product_variation';" ) ); WHERE wp.ID IS NULL AND products.post_type = 'product_variation';" ) );
$message = sprintf( __( '%d orphaned variations deleted', 'woocommerce' ), $result ); $message = sprintf( __( '%d orphaned variations deleted', 'woocommerce' ), $result );
break; break;
case 'add_order_indexes': case 'add_order_indexes':
/** /**
* Add billing and shipping address indexes containing the customer name for orders * Add billing and shipping address indexes containing the customer name for orders
@ -409,41 +421,53 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
$message = sprintf( __( '%d indexes added', 'woocommerce' ), $rows ); $message = sprintf( __( '%d indexes added', 'woocommerce' ), $rows );
break; break;
case 'reset_roles': case 'reset_roles':
// Remove then re-add caps and roles // Remove then re-add caps and roles
WC_Install::remove_roles(); WC_Install::remove_roles();
WC_Install::create_roles(); WC_Install::create_roles();
$message = __( 'Roles successfully reset', 'woocommerce' ); $message = __( 'Roles successfully reset', 'woocommerce' );
break; break;
case 'recount_terms': case 'recount_terms':
$product_cats = get_terms( 'product_cat', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); $product_cats = get_terms( 'product_cat', array(
'hide_empty' => false,
'fields' => 'id=>parent',
) );
_wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false ); _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false );
$product_tags = get_terms( 'product_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) ); $product_tags = get_terms( 'product_tag', array(
'hide_empty' => false,
'fields' => 'id=>parent',
) );
_wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false ); _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false );
$message = __( 'Terms successfully recounted', 'woocommerce' ); $message = __( 'Terms successfully recounted', 'woocommerce' );
break; break;
case 'clear_sessions': case 'clear_sessions':
$wpdb->query( "TRUNCATE {$wpdb->prefix}woocommerce_sessions" ); $wpdb->query( "TRUNCATE {$wpdb->prefix}woocommerce_sessions" );
$result = absint( $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key='_woocommerce_persistent_cart_" . get_current_blog_id() . "';" ) ); $result = absint( $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key='_woocommerce_persistent_cart_" . get_current_blog_id() . "';" ) );
wp_cache_flush(); wp_cache_flush();
$message = sprintf( __( 'Deleted all active sessions, and %d saved carts.', 'woocommerce' ), absint( $result ) ); $message = sprintf( __( 'Deleted all active sessions, and %d saved carts.', 'woocommerce' ), absint( $result ) );
break; break;
case 'install_pages': case 'install_pages':
WC_Install::create_pages(); WC_Install::create_pages();
$message = __( 'All missing WooCommerce pages successfully installed', 'woocommerce' ); $message = __( 'All missing WooCommerce pages successfully installed', 'woocommerce' );
break; break;
case 'delete_taxes' :
case 'delete_taxes':
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rates;" ); $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rates;" );
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations;" ); $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations;" );
WC_Cache_Helper::incr_cache_prefix( 'taxes' ); WC_Cache_Helper::incr_cache_prefix( 'taxes' );
$message = __( 'Tax rates successfully deleted', 'woocommerce' ); $message = __( 'Tax rates successfully deleted', 'woocommerce' );
break; break;
case 'reset_tracking': case 'reset_tracking':
delete_option( 'woocommerce_allow_tracking' ); delete_option( 'woocommerce_allow_tracking' );
WC_Admin_Notices::add_notice( 'tracking' ); WC_Admin_Notices::add_notice( 'tracking' );
$message = __( 'Usage tracking settings successfully reset.', 'woocommerce' ); $message = __( 'Usage tracking settings successfully reset.', 'woocommerce' );
break; break;
default: default:
$tools = $this->get_tools(); $tools = $this->get_tools();
if ( isset( $tools[ $tool ]['callback'] ) ) { if ( isset( $tools[ $tool ]['callback'] ) ) {
@ -465,6 +489,9 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
break; break;
} }
return array( 'success' => $ran, 'message' => $message ); return array(
'success' => $ran,
'message' => $message,
);
} }
} }

View File

@ -52,7 +52,10 @@
</rule> </rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName"> <rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>includes/**/abstract-*.php</exclude-pattern> <exclude-pattern>includes/**/abstract-*.php</exclude-pattern>
<exclude-pattern>tests/</exclude-pattern> <exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.PHP.DevelopmentFunctions.error_log_print_r">
<exclude-pattern>tests/*</exclude-pattern>
</rule> </rule>
<rule ref="Generic.Commenting"> <rule ref="Generic.Commenting">
<exclude-pattern>tests/</exclude-pattern> <exclude-pattern>tests/</exclude-pattern>

View File

@ -1,6 +1,13 @@
<?php <?php
/** /**
* System Status REST Tests * Class WC_Tests_REST_System_Status file.
*
* @package WooCommerce/Tests
*/
/**
* System Status REST Tests.
*
* @package WooCommerce\Tests\API * @package WooCommerce\Tests\API
* @since 3.0 * @since 3.0
*/ */
@ -69,10 +76,10 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
$data = $response->get_data(); $data = $response->get_data();
$environment = (array) $data['environment']; $environment = (array) $data['environment'];
// Make sure all expected data is present // Make sure all expected data is present.
$this->assertEquals( 30, count( $environment ) ); $this->assertEquals( 30, count( $environment ) );
// Test some responses to make sure they match up // Test some responses to make sure they match up.
$this->assertEquals( get_option( 'home' ), $environment['home_url'] ); $this->assertEquals( get_option( 'home' ), $environment['home_url'] );
$this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] ); $this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] );
$this->assertEquals( WC()->version, $environment['version'] ); $this->assertEquals( WC()->version, $environment['version'] );
@ -131,7 +138,10 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
$theme = (array) $data['theme']; $theme = (array) $data['theme'];
$this->assertEquals( 13, count( $theme ) ); $this->assertEquals( 13, count( $theme ) );
// phpcs:disable WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
$this->assertEquals( $active_theme->Name, $theme['name'] ); $this->assertEquals( $active_theme->Name, $theme['name'] );
// phpcs:enable
} }
/** /**
@ -216,7 +226,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
public function test_get_system_tools() { public function test_get_system_tools() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller; $tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools(); $raw_tools = $tools_controller->get_tools();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
@ -259,7 +269,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
public function test_get_system_tool() { public function test_get_system_tool() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller; $tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools(); $raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms']; $raw_tool = $raw_tools['recount_terms'];
@ -293,7 +303,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
public function test_execute_system_tool() { public function test_execute_system_tool() {
wp_set_current_user( $this->user ); wp_set_current_user( $this->user );
$tools_controller = new WC_REST_System_Status_Tools_Controller; $tools_controller = new WC_REST_System_Status_Tools_Controller();
$raw_tools = $tools_controller->get_tools(); $raw_tools = $tools_controller->get_tools();
$raw_tool = $raw_tools['recount_terms']; $raw_tool = $raw_tools['recount_terms'];
@ -305,6 +315,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'Recount terms', $data['action'] ); $this->assertEquals( 'Recount terms', $data['action'] );
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] ); $this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
$this->assertTrue( $data['success'] ); $this->assertTrue( $data['success'] );
$this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) );
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) ); $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) );
$this->assertEquals( 404, $response->get_status() ); $this->assertEquals( 404, $response->get_status() );
@ -331,6 +342,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( $request ); $response = $this->server->dispatch( $request );
$data = $response->get_data(); $data = $response->get_data();
$properties = $data['schema']['properties']; $properties = $data['schema']['properties'];
$this->assertEquals( 6, count( $properties ) ); $this->assertEquals( 6, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties ); $this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'name', $properties );