Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce calls to wp_count_posts( 'product' ) from OnboardingTasks #45125

Merged
merged 3 commits into from Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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