WC_Admin_List_Table_Products::prime_status_counts_cache │ public │ WC 11.0.0

Pre-warm the wp_count_posts cache before the list table renders.

Method of the class: WC_Admin_List_Table_Products{}

No Hooks.

Returns

void. Nothing (null).

Usage

$WC_Admin_List_Table_Products = new WC_Admin_List_Table_Products();
$WC_Admin_List_Table_Products->prime_status_counts_cache(): void;

Changelog

Since 11.0.0 Introduced.

WC_Admin_List_Table_Products::prime_status_counts_cache() code WC 11.1.2

public function prime_status_counts_cache(): void {
	$screen = get_current_screen();
	if ( ! $screen || 'edit-product' !== $screen->id ) {
		return;
	}

	// Performance note: the current listings architecture prevents us from isolating wp_count_posts calls.
	// In the context of the products page, we can still isolate the underlying SQL by warming up the wp_count_posts cache.
	$cache = (object) array_map(
		static fn ( $count ) => $count ? (string) $count : (int) $count,
		wc_get_container()->get( ProductUtil::class )->get_counts_for_type( 'product' )
	);
	// Trade-off: private-status tally may read slightly high for restricted roles (other users' privates included) — non-critical.
	wp_cache_set_multiple(
		array(
			'posts-product' => $cache,
			'posts-product_readable_' . get_current_user_id() => $cache,
		),
		'counts'
	);
}