Site Health: Add WC-specific criteria for persistent object cache (#35202)

* Site Health: Add WC-specific criteria for persistent object cache

WP 6.1 introduces a new test in the Site Health module for whether the
site has an external object cache, and if not, whether it would benefit
from having one. However, the criteria it uses are based largely on
posts and comments. This introduces WooCommerce-specific criteria based
on orders and products. It uses much lower numbers for the
thresholds (100 instead of 1000). These thresholds are somewhat
arbitrary, but reflect that the data objects in a WooCommerce store are
larger and more complex than in a simple blog.

* Add changelog file
This commit is contained in:
Corey McKrill 2023-02-20 15:01:17 -08:00 committed by GitHub
parent 41eab0d669
commit aebf22dc2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add WC-specific criteria to the Site Health test for persistent object caches

View File

@ -67,6 +67,7 @@ class Loader {
Translations::get_instance();
WCAdminUser::get_instance();
Settings::get_instance();
SiteHealth::get_instance();
SystemStatusReport::get_instance();
wc_get_container()->get( Reviews::class );

View File

@ -0,0 +1,103 @@
<?php
/**
* Customize Site Health recommendations for WooCommerce.
*/
namespace Automattic\WooCommerce\Internal\Admin;
defined( 'ABSPATH' ) || exit;
/**
* SiteHealth class.
*/
class SiteHealth {
/**
* Class instance.
*
* @var SiteHealth instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
add_filter( 'site_status_should_suggest_persistent_object_cache', array( $this, 'should_suggest_persistent_object_cache' ) );
}
/**
* Counts specific types of WooCommerce entities to determine if a persistent object cache would be beneficial.
*
* Note that if all measured WooCommerce entities are below their thresholds, this will return null so that the
* other normal WordPress checks will still be run.
*
* @param true|null $check A non-null value will short-circuit WP's normal tests for this.
*
* @return true|null True if the store would benefit from a persistent object cache. Otherwise null.
*/
public function should_suggest_persistent_object_cache( $check ) {
// Skip this if some other filter has already determined yes.
if ( true === $check ) {
return $check;
}
$thresholds = array(
'orders' => 100,
'products' => 100,
);
foreach ( $thresholds as $key => $threshold ) {
try {
switch ( $key ) {
case 'orders':
$orders_query = new \WC_Order_Query(
array(
'status' => 'any',
'limit' => 1,
'paginate' => true,
'return' => 'ids',
)
);
$orders_results = $orders_query->get_orders();
if ( $orders_results->total >= $threshold ) {
$check = true;
}
break;
case 'products':
$products_query = new \WC_Product_Query(
array(
'status' => 'any',
'limit' => 1,
'paginate' => true,
'return' => 'ids',
)
);
$products_results = $products_query->get_products();
if ( $products_results->total >= $threshold ) {
$check = true;
}
break;
}
} catch ( \Exception $exception ) {
break;
}
if ( ! is_null( $check ) ) {
break;
}
}
return $check;
}
}