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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Caches / ProductCountCacheService.php
woocommerce / src / Caches Last commit date
OrderCache.php 1 year ago OrderCacheController.php 1 year ago OrderCountCache.php 3 days ago OrderCountCacheService.php 3 days ago ProductCountCache.php 3 days ago ProductCountCacheService.php 3 days ago
ProductCountCacheService.php
217 lines
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Automattic\WooCommerce\Caches;
6
7 use Automattic\WooCommerce\Internal\Utilities\ProductUtil;
8 use WC_Product;
9 use WP_Post;
10
11 /**
12 * A service class to help with updates to the aggregate product counts cache.
13 *
14 * @internal
15 */
16 class ProductCountCacheService {
17
18 public const BACKGROUND_EVENT_HOOK = 'woocommerce_refresh_product_count_cache';
19
20 /**
21 * ProductCountCache instance.
22 *
23 * @var ProductCountCache
24 */
25 private ProductCountCache $product_count_cache;
26
27 /**
28 * Array of product IDs with their last transitioned status as key value pairs.
29 * Guarantees idempotency for product status transitions when multiple hooks fire for the same product.
30 *
31 * @var array<int,string>
32 */
33 private array $product_statuses = array();
34
35 /**
36 * Array of product IDs with their initial status as key value pairs.
37 * Guarantees idempotency for product status transitions when multiple hooks fire for the same product.
38 *
39 * @var array<int,string>
40 */
41 private array $initial_product_statuses = array();
42
43 /**
44 * Set of product IDs currently being created in this request (keyed by ID; detected via old_status='new').
45 *
46 * @var array<int,true>
47 */
48 private array $products_in_creation = array();
49
50 /**
51 * Class initialization, invoked by the DI container.
52 *
53 * @internal
54 */
55 final public function init(): void {
56 $this->product_count_cache = new ProductCountCache();
57
58 add_action( 'action_scheduler_ensure_recurring_actions', array( $this, 'schedule_background_actions' ) );
59 add_action( self::BACKGROUND_EVENT_HOOK, array( $this, 'prime_cache_if_cold' ) );
60 if ( defined( 'WC_PLUGIN_BASENAME' ) ) {
61 add_action( 'deactivate_' . WC_PLUGIN_BASENAME, array( $this, 'unschedule_background_actions' ) );
62 }
63
64 // transition_post_status owns all mid-lifecycle status changes; woocommerce_new_product corrects for creation-time
65 // ephemeral transitions before the final status is committed; before_delete_post closes the lifecycle.
66 add_action( 'woocommerce_new_product', array( $this, 'update_on_new_product' ), 10, 2 );
67 add_action( 'transition_post_status', array( $this, 'update_on_product_status_changed' ), 10, 3 );
68 add_action( 'before_delete_post', array( $this, 'update_on_product_deleted' ), 10, 2 );
69 }
70
71 /**
72 * Primes the product count cache for a given post type when it is cold.
73 *
74 * @param string $product_type The product post type.
75 * @return void
76 */
77 public function prime_cache_if_cold( string $product_type = 'product' ): void {
78 // Cache warm-up is only effective when an object cache plugin is active, and the cache entry is missing.
79 if ( wp_using_ext_object_cache() && null === $this->product_count_cache->get( $product_type ) ) {
80 $this->product_count_cache->flush( $product_type );
81 wc_get_container()->get( ProductUtil::class )->get_counts_for_type( $product_type );
82 }
83 }
84
85 /**
86 * Register background caching for each product type.
87 *
88 * @return void
89 */
90 public function schedule_background_actions(): void {
91 $frequency = HOUR_IN_SECONDS * 12;
92 $timestamp = time() + $frequency;
93 as_schedule_recurring_action( $timestamp, $frequency, self::BACKGROUND_EVENT_HOOK, array( 'product' ), 'count', true );
94 }
95
96 /**
97 * Unschedules background actions.
98 *
99 * @return void
100 */
101 public function unschedule_background_actions(): void {
102 WC()->queue()->cancel_all( self::BACKGROUND_EVENT_HOOK );
103 }
104
105 /**
106 * Update the cache when a new product is created.
107 *
108 * @param int $product_id Product ID.
109 * @param WC_Product $product The product.
110 * @return void
111 */
112 public function update_on_new_product( int $product_id, WC_Product $product ): void {
113 // transition_post_status already counted this product — reverse any errant decrement from a cold step 1 and stop.
114 // In-memory status may diverge from DB after a mid-creation wp_update_post; do not increment here.
115 if ( isset( $this->product_statuses[ $product_id ] ) ) {
116 $this->maybe_restore_initial_status_count( $product_id );
117 unset( $this->products_in_creation[ $product_id ] );
118 return;
119 }
120
121 // Cache was cold throughout creation — transition_post_status never fired; use in-memory status as the sole count.
122 $product_status = $product->get_status();
123 if ( $this->product_count_cache->is_cached( 'product', $product_status ) ) {
124 $this->product_statuses[ $product_id ] = $product_status;
125 $this->product_count_cache->increment( 'product', $product_status );
126 }
127 unset( $this->products_in_creation[ $product_id ] );
128 }
129
130 /**
131 * Update the cache whenever a product status changes.
132 *
133 * @param string $new_status The new post status.
134 * @param string $old_status The previous post status.
135 * @param WP_Post $post The post object.
136 *
137 * @return void
138 */
139 public function update_on_product_status_changed( string $new_status, string $old_status, WP_Post $post ): void {
140 if ( 'product' !== $post->post_type ) {
141 return;
142 }
143
144 $product_id = $post->ID;
145
146 // WordPress uses 'new' as old_status exclusively on the first transition_post_status of a newly inserted post.
147 if ( 'new' === $old_status ) {
148 $this->products_in_creation[ $product_id ] = true;
149 }
150
151 $is_new_cached = $this->product_count_cache->is_cached( 'product', $new_status );
152 $is_old_cached = $this->product_count_cache->is_cached( 'product', $old_status );
153 if ( ! $is_new_cached && ! $is_old_cached ) {
154 return;
155 }
156
157 // If the status count has already been incremented for this product, skip.
158 if ( ( $this->product_statuses[ $product_id ] ?? null ) === $new_status ) {
159 return;
160 }
161
162 $previously_tracked = isset( $this->product_statuses[ $product_id ] );
163 $this->product_statuses[ $product_id ] = $new_status;
164 $was_decremented = $is_old_cached && false !== $this->product_count_cache->decrement( 'product', $old_status );
165 if ( $is_new_cached ) {
166 $this->product_count_cache->increment( 'product', $new_status );
167 }
168
169 // Record old status for creation-time correction only; existing-product decrements are correct and must not be reversed.
170 // If $previously_tracked, an earlier transition already counted the old status — decrement is legitimate.
171 if ( ! $previously_tracked && $was_decremented && ! isset( $this->initial_product_statuses[ $product_id ] ) && isset( $this->products_in_creation[ $product_id ] ) ) {
172 $this->initial_product_statuses[ $product_id ] = $old_status;
173 } elseif ( ( $this->initial_product_statuses[ $product_id ] ?? null ) === $new_status ) {
174 unset( $this->initial_product_statuses[ $product_id ] );
175 }
176 }
177
178 /**
179 * Update the cache when a product is permanently deleted.
180 *
181 * @param int $post_id Post ID.
182 * @param WP_Post $post The post object.
183 *
184 * @return void
185 */
186 public function update_on_product_deleted( int $post_id, WP_Post $post ): void {
187 if ( 'product' === $post->post_type ) {
188 // Reverse any errant decrement from a mid-creation status transition that update_on_new_product will never get to correct.
189 $this->maybe_restore_initial_status_count( $post_id );
190
191 $product_status = $post->post_status;
192 if ( $this->product_count_cache->is_cached( 'product', $product_status ) ) {
193 $this->product_count_cache->decrement( 'product', $product_status );
194 }
195
196 unset( $this->product_statuses[ $post_id ], $this->products_in_creation[ $post_id ] );
197 }
198 }
199
200 /**
201 * Reverses an errant decrement recorded in initial_product_statuses for a given product, if any.
202 *
203 * @param int $product_id Product ID.
204 *
205 * @return void
206 */
207 private function maybe_restore_initial_status_count( int $product_id ): void {
208 if ( isset( $this->initial_product_statuses[ $product_id ] ) ) {
209 $initial_status = $this->initial_product_statuses[ $product_id ];
210 unset( $this->initial_product_statuses[ $product_id ] );
211 if ( $this->product_count_cache->is_cached( 'product', $initial_status ) ) {
212 $this->product_count_cache->increment( 'product', $initial_status );
213 }
214 }
215 }
216 }
217