* Add "clear cache" button to WooCommerce Tools page.

* Hook up cache clear tool button.

* Add success message to clear cache tool.
This commit is contained in:
Jeff Stieler 2020-06-16 08:47:58 -05:00 committed by GitHub
parent d591d77a53
commit f2c8f34d22
1 changed files with 46 additions and 0 deletions

View File

@ -9,11 +9,17 @@
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\Loader;
use Automattic\WooCommerce\Admin\API\Reports\Cache;
/**
* Contains backend logic for the Analytics feature.
*/
class Analytics {
/**
* Clear cache tool identifier.
*/
const CACHE_TOOL_ID = 'clear_woocommerce_analytics_cache';
/**
* Class instance.
*
@ -38,6 +44,7 @@ class Analytics {
add_filter( 'woocommerce_component_settings_preload_endpoints', array( $this, 'add_preload_endpoints' ) );
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_menu', array( $this, 'register_pages' ) );
add_filter( 'woocommerce_debug_tools', array( $this, 'register_cache_clear_tool' ) );
}
/**
@ -73,6 +80,36 @@ class Analytics {
);
}
/**
* Register the cache clearing tool on the WooCommerce > Status > Tools page.
*
* @param array $debug_tools Available debug tool registrations.
* @return array Filtered debug tool registrations.
*/
public function register_cache_clear_tool( $debug_tools ) {
$settings_url = add_query_arg(
array(
'page' => 'wc-admin',
'path' => '/analytics/settings',
),
get_admin_url( null, 'admin.php' )
);
$debug_tools[ self::CACHE_TOOL_ID ] = array(
'name' => __( 'Clear analytics cache', 'woocommerce-admin' ),
'button' => __( 'Clear', 'woocommerce-admin' ),
'desc' => sprintf(
/* translators: 1: opening link tag, 2: closing tag */
'This tool will reset the cached values used in WooCommerce Analytics. If numbers still look off, try %1$sReimporting Historical Data%2$s.',
'<a href="' . esc_url( $settings_url ) . '">',
'</a>'
),
'callback' => array( $this, 'run_clear_cache_tool' ),
);
return $debug_tools;
}
/**
* Registers report pages.
*/
@ -163,4 +200,13 @@ class Analytics {
}
}
}
/**
* "Clear" analytics cache by invalidating it.
*/
public function run_clear_cache_tool() {
Cache::invalidate();
return __( 'Analytics cache cleared.', 'woocommerce-admin' );
}
}