From aebf22dc2f722d519823f893516023f62f144450 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Mon, 20 Feb 2023 15:01:17 -0800 Subject: [PATCH] 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 --- .../changelog/add-site-health-cache | 4 + .../woocommerce/src/Internal/Admin/Loader.php | 1 + .../src/Internal/Admin/SiteHealth.php | 103 ++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 plugins/woocommerce/changelog/add-site-health-cache create mode 100644 plugins/woocommerce/src/Internal/Admin/SiteHealth.php diff --git a/plugins/woocommerce/changelog/add-site-health-cache b/plugins/woocommerce/changelog/add-site-health-cache new file mode 100644 index 00000000000..6cea8f36be5 --- /dev/null +++ b/plugins/woocommerce/changelog/add-site-health-cache @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add WC-specific criteria to the Site Health test for persistent object caches diff --git a/plugins/woocommerce/src/Internal/Admin/Loader.php b/plugins/woocommerce/src/Internal/Admin/Loader.php index 659290fb325..27758986e18 100644 --- a/plugins/woocommerce/src/Internal/Admin/Loader.php +++ b/plugins/woocommerce/src/Internal/Admin/Loader.php @@ -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 ); diff --git a/plugins/woocommerce/src/Internal/Admin/SiteHealth.php b/plugins/woocommerce/src/Internal/Admin/SiteHealth.php new file mode 100644 index 00000000000..ac3b593d417 --- /dev/null +++ b/plugins/woocommerce/src/Internal/Admin/SiteHealth.php @@ -0,0 +1,103 @@ + 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; + } +}