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.2.0 10.0.1 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3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.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 / ProductCountCache.php
woocommerce / src / Caches Last commit date
OrderCache.php 1 year ago OrderCacheController.php 1 year ago OrderCountCache.php 1 month ago OrderCountCacheService.php 1 month ago ProductCountCache.php 2 weeks ago ProductCountCacheService.php 2 weeks ago
ProductCountCache.php
238 lines
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Automattic\WooCommerce\Caches;
6
7 /**
8 * A class to cache counts for various product statuses.
9 */
10 class ProductCountCache {
11
12 /**
13 * Cache prefix.
14 *
15 * @var string
16 */
17 private string $cache_prefix = 'product-count';
18
19 /**
20 * Default value for the duration of the objects in the cache, in seconds
21 * (may not be used depending on the cache engine used WordPress cache implementation).
22 *
23 * @var int
24 */
25 protected int $expiration = DAY_IN_SECONDS;
26
27 /**
28 * Retrieves the list of known statuses by product type. A cached array of statuses is saved per product type for
29 * improved backward compatibility with some of the extensions that don't register all statuses they use with
30 * WooCommerce.
31 *
32 * @param string $product_type The post type (e.g. 'product', 'product_variation').
33 *
34 * @return string[]
35 */
36 private function get_saved_statuses_for_type( string $product_type ): array {
37 $cache_key = $this->get_saved_statuses_cache_key( $product_type );
38 $statuses = wp_cache_get( $cache_key );
39 // Defensive perimeter: purge corrupted cache entries (external cache modification).
40 if ( false !== $statuses && ( ! is_array( $statuses ) || array_filter( $statuses, 'is_string' ) !== $statuses ) ) {
41 $statuses = false;
42 wp_cache_delete( $cache_key );
43 }
44
45 return is_array( $statuses ) ? $statuses : array();
46 }
47
48 /**
49 * Adds the given statuses to the cached statuses array for the product type if they are not already stored.
50 *
51 * @param string $product_type The post type (e.g. 'product', 'product_variation').
52 * @param string[] $product_statuses One or more statuses to add.
53 *
54 * @return void
55 */
56 private function ensure_statuses_for_type( string $product_type, array $product_statuses ): void {
57 if ( empty( $product_statuses ) ) {
58 return;
59 }
60
61 $existing = $this->get_saved_statuses_for_type( $product_type );
62 $new_statuses = array_diff( $product_statuses, $existing );
63 if ( empty( $new_statuses ) ) {
64 return;
65 }
66
67 $merged = array_unique( array_merge( $existing, $new_statuses ) );
68 wp_cache_set( $this->get_saved_statuses_cache_key( $product_type ), $merged, '', $this->expiration );
69 }
70
71 /**
72 * Get the cache key for a given product type and status.
73 *
74 * @param string $product_type The post type (e.g. 'product', 'product_variation').
75 * @param string $product_status The status of the product.
76 *
77 * @return string
78 */
79 private function get_cache_key( string $product_type, string $product_status ): string {
80 return $this->cache_prefix . '_' . $product_type . '_' . $product_status;
81 }
82
83 /**
84 * Get the cache key for saved statuses of the given product type.
85 *
86 * @param string $product_type The post type (e.g. 'product', 'product_variation').
87 *
88 * @return string
89 */
90 private function get_saved_statuses_cache_key( string $product_type ): string {
91 return $this->cache_prefix . '_' . $product_type . '_statuses';
92 }
93
94 /**
95 * Check if the cache has a value for a given product type and status.
96 *
97 * @param string $product_type The post type (e.g. 'product', 'product_variation').
98 * @param string $product_status The status of the product.
99 *
100 * @return bool
101 */
102 public function is_cached( string $product_type, string $product_status ): bool {
103 return false !== wp_cache_get( $this->get_cache_key( $product_type, $product_status ) );
104 }
105
106 /**
107 * Set the cache value for a given product type and status.
108 *
109 * @param string $product_type The post type (e.g. 'product', 'product_variation').
110 * @param string $product_status The status slug of the product.
111 * @param int $value The value to set.
112 *
113 * @return bool
114 */
115 public function set( string $product_type, string $product_status, int $value ): bool {
116 $this->ensure_statuses_for_type( $product_type, array( $product_status ) );
117 return wp_cache_set( $this->get_cache_key( $product_type, $product_status ), $value, '', $this->expiration );
118 }
119
120 /**
121 * Set the cache count value for multiple statuses at once.
122 *
123 * @deprecated 11.1.0
124 *
125 * @param string $product_type The post type (e.g. 'product', 'product_variation').
126 * @param array<string,int> $counts Counts keyed by status slug (e.g. [ 'publish' => 10, 'draft' => 5 ]).
127 *
128 * @return array<string,bool>
129 */
130 public function set_multiple( string $product_type, array $counts ) {
131 $results = array();
132 foreach ( $counts as $status => $count ) {
133 $results[ $this->get_cache_key( $product_type, (string) $status ) ] = $this->set( $product_type, (string) $status, (int) $count );
134 }
135
136 return $results;
137 }
138
139 /**
140 * Get the cache value for a given product type and set of statuses.
141 *
142 * @param string $product_type The post type (e.g. 'product', 'product_variation').
143 * @param string[] $product_statuses The statuses to retrieve.
144 *
145 * @return array<string,int>|null
146 */
147 public function get( string $product_type, array $product_statuses = array() ) {
148 if ( empty( $product_statuses ) ) {
149 $product_statuses = $this->get_saved_statuses_for_type( $product_type );
150 if ( empty( $product_statuses ) ) {
151 return null;
152 }
153 }
154
155 $cache_keys = array_map(
156 fn( $product_status ) => $this->get_cache_key( $product_type, $product_status ),
157 $product_statuses
158 );
159
160 $cache_values = wp_cache_get_multiple( $cache_keys );
161 $status_values = array();
162
163 $cache_key_prefix = $this->get_cache_key( $product_type, '' );
164 foreach ( $cache_values as $key => $value ) {
165 // Defensive perimeter: purge corrupted cache entries (external cache modification);
166 // As some object caching plugins are pushing integer but pulling string, we use is_numeric here.
167 if ( false !== $value && ! is_numeric( $value ) ) {
168 $value = false;
169 wp_cache_delete( $key );
170 }
171
172 // Return null for the entire cache if any of the requested statuses are not found because they fell out of cache.
173 if ( false === $value ) {
174 return null;
175 }
176
177 $status = substr( $key, strlen( $cache_key_prefix ) );
178 $status_values[ $status ] = (int) $value;
179 }
180
181 return $status_values;
182 }
183
184 /**
185 * Increment the cache value for a given product type and status.
186 *
187 * @param string $product_type The post type (e.g. 'product', 'product_variation').
188 * @param string $product_status The status of the product.
189 * @param int $offset The amount to increment by.
190 *
191 * @return int|false
192 */
193 public function increment( string $product_type, string $product_status, int $offset = 1 ) {
194 return wp_cache_incr( $this->get_cache_key( $product_type, $product_status ), $offset );
195 }
196
197 /**
198 * Decrement the cache value for a given product type and status.
199 *
200 * @param string $product_type The post type (e.g. 'product', 'product_variation').
201 * @param string $product_status The status of the product.
202 * @param int $offset The amount to decrement by.
203 *
204 * @return int|false
205 */
206 public function decrement( string $product_type, string $product_status, int $offset = 1 ) {
207 return wp_cache_decr( $this->get_cache_key( $product_type, $product_status ), $offset );
208 }
209
210 /**
211 * Flush the cache for a given product type and statuses.
212 *
213 * @param string $product_type The post type (e.g. 'product', 'product_variation').
214 * @param string[] $product_statuses The statuses to flush. Flushes all known statuses if empty.
215 *
216 * @return void
217 */
218 public function flush( string $product_type = 'product', array $product_statuses = array() ): void {
219 $flush_saved_statuses = false;
220 if ( empty( $product_statuses ) ) {
221 $product_statuses = $this->get_saved_statuses_for_type( $product_type );
222 $flush_saved_statuses = true;
223 }
224
225 $cache_keys = array_map(
226 fn ( $product_status ) => $this->get_cache_key( $product_type, $product_status ),
227 $product_statuses
228 );
229
230 if ( $flush_saved_statuses ) {
231 // If all statuses are being flushed, go ahead and flush the status list so any permanently removed statuses are cleared out.
232 $cache_keys[] = $this->get_saved_statuses_cache_key( $product_type );
233 }
234
235 wp_cache_delete_multiple( $cache_keys );
236 }
237 }
238