Skip to content

Commit

Permalink
Reduce calls to wp_count_posts( 'product' ) from OnboardingTasks (#45125
Browse files Browse the repository at this point in the history
)

* 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 <github-actions@github.com>
  • Loading branch information
chihsuan and github-actions committed Feb 27, 2024
1 parent ef16260 commit 707c555
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Reduce calls to wp_count_posts( 'product' ) from OnboardingTasks
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -9,6 +9,7 @@
* Products Task
*/
class Products extends Task {
const PRODUCT_COUNT_TRANSIENT_NAME = 'woocommerce_product_task_product_count_transient';

/**
* Constructor
Expand All @@ -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' ) );
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 707c555

Please sign in to comment.