From 707c555091503d30b49537b7074b3072b4a86a1c Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Tue, 27 Feb 2024 13:30:55 +0800 Subject: [PATCH] Reduce calls to wp_count_posts( 'product' ) from OnboardingTasks (#45125) * Reduce product count calls via transient caching * Check has_previously_completed first * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: github-actions --- .../45125-update-reduce-count-products | 4 ++++ .../Admin/Features/OnboardingTasks/Task.php | 5 ++-- .../OnboardingTasks/Tasks/Products.php | 24 ++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce/changelog/45125-update-reduce-count-products diff --git a/plugins/woocommerce/changelog/45125-update-reduce-count-products b/plugins/woocommerce/changelog/45125-update-reduce-count-products new file mode 100644 index 000000000000..24879d4b153c --- /dev/null +++ b/plugins/woocommerce/changelog/45125-update-reduce-count-products @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Reduce calls to wp_count_posts( 'product' ) from OnboardingTasks \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Task.php b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Task.php index cc26b2864c62..cfc54d4569cb 100644 --- a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Task.php +++ b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Task.php @@ -394,11 +394,12 @@ public function has_previously_completed() { * Track task completion if task is viewable. */ public function possibly_track_completion() { - if ( ! $this->is_complete() ) { + if ( $this->has_previously_completed() ) { return; } - if ( $this->has_previously_completed() ) { + // Expensive check. + if ( ! $this->is_complete() ) { return; } diff --git a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/Products.php b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/Products.php index 4895a90168da..ce1f93cad6af 100644 --- a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/Products.php +++ b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/Products.php @@ -9,6 +9,7 @@ * Products Task */ class Products extends Task { + const PRODUCT_COUNT_TRANSIENT_NAME = 'woocommerce_product_task_product_count_transient'; /** * Constructor @@ -20,6 +21,11 @@ public function __construct( $task_list ) { add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_manual_return_notice_script' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_import_return_notice_script' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_load_sample_return_notice_script' ) ); + + add_action( 'woocommerce_update_product', array( $this, 'delete_product_count_cache' ) ); + add_action( 'woocommerce_new_product', array( $this, 'delete_product_count_cache' ) ); + add_action( 'wp_trash_post', array( $this, 'delete_product_count_cache' ) ); + add_action( 'untrashed_post', array( $this, 'delete_product_count_cache' ) ); } /** @@ -155,13 +161,29 @@ public function possibly_add_load_sample_return_notice_script( $hook ) { WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-load-sample-products-notice', true ); } + /** + * Delete the product count transient used in has_products() method to refresh the cache. + * + * @return void + */ + public static function delete_product_count_cache() { + delete_transient( self::PRODUCT_COUNT_TRANSIENT_NAME ); + } + /** * Check if the store has any user created published products. * * @return bool */ public static function has_products() { - return self::count_user_products() > 0; + $product_counts = get_transient( self::PRODUCT_COUNT_TRANSIENT_NAME ); + if ( false !== $product_counts && is_numeric( $product_counts ) ) { + return (int) $product_counts > 0; + } + + $product_counts = self::count_user_products(); + set_transient( self::PRODUCT_COUNT_TRANSIENT_NAME, $product_counts ); + return $product_counts > 0; } /**