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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 / ProductCountCache.php
woocommerce / src / Caches Last commit date
OrderCache.php 1 year ago OrderCacheController.php 1 year ago OrderCountCache.php 2 days ago OrderCountCacheService.php 2 days ago ProductCountCache.php 2 days ago ProductCountCacheService.php 2 days ago
ProductCountCache.php
229 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 $statuses = wp_cache_get( $this->get_saved_statuses_cache_key( $product_type ) );
38
39 return is_array( $statuses ) ? $statuses : array();
40 }
41
42 /**
43 * Adds the given statuses to the cached statuses array for the product type if they are not already stored.
44 *
45 * @param string $product_type The post type (e.g. 'product', 'product_variation').
46 * @param string[] $product_statuses One or more statuses to add.
47 *
48 * @return void
49 */
50 private function ensure_statuses_for_type( string $product_type, array $product_statuses ): void {
51 if ( empty( $product_statuses ) ) {
52 return;
53 }
54
55 $existing = $this->get_saved_statuses_for_type( $product_type );
56 $new_statuses = array_diff( $product_statuses, $existing );
57 if ( empty( $new_statuses ) ) {
58 return;
59 }
60
61 $merged = array_unique( array_merge( $existing, $new_statuses ) );
62 wp_cache_set( $this->get_saved_statuses_cache_key( $product_type ), $merged, '', $this->expiration );
63 }
64
65 /**
66 * Get the cache key for a given product type and status.
67 *
68 * @param string $product_type The post type (e.g. 'product', 'product_variation').
69 * @param string $product_status The status of the product.
70 *
71 * @return string
72 */
73 private function get_cache_key( string $product_type, string $product_status ): string {
74 return $this->cache_prefix . '_' . $product_type . '_' . $product_status;
75 }
76
77 /**
78 * Get the cache key for saved statuses of the given product type.
79 *
80 * @param string $product_type The post type (e.g. 'product', 'product_variation').
81 *
82 * @return string
83 */
84 private function get_saved_statuses_cache_key( string $product_type ): string {
85 return $this->cache_prefix . '_' . $product_type . '_statuses';
86 }
87
88 /**
89 * Check if the cache has a value for a given product type and status.
90 *
91 * @param string $product_type The post type (e.g. 'product', 'product_variation').
92 * @param string $product_status The status of the product.
93 *
94 * @return bool
95 */
96 public function is_cached( string $product_type, string $product_status ): bool {
97 return false !== wp_cache_get( $this->get_cache_key( $product_type, $product_status ) );
98 }
99
100 /**
101 * Set the cache value for a given product type and status.
102 *
103 * @param string $product_type The post type (e.g. 'product', 'product_variation').
104 * @param string $product_status The status slug of the product.
105 * @param int $value The value to set.
106 *
107 * @return bool
108 */
109 public function set( string $product_type, string $product_status, int $value ): bool {
110 $this->ensure_statuses_for_type( $product_type, array( $product_status ) );
111 return wp_cache_set( $this->get_cache_key( $product_type, $product_status ), $value, '', $this->expiration );
112 }
113
114 /**
115 * Set the cache count value for multiple statuses at once.
116 *
117 * @param string $product_type The post type (e.g. 'product', 'product_variation').
118 * @param array<string,int> $counts Counts keyed by status slug (e.g. [ 'publish' => 10, 'draft' => 5 ]).
119 *
120 * @return array<string,bool>
121 */
122 public function set_multiple( string $product_type, array $counts ) {
123 if ( empty( $counts ) ) {
124 return array();
125 }
126
127 $this->ensure_statuses_for_type( $product_type, array_keys( $counts ) );
128
129 $mapped_counts = array();
130 foreach ( $counts as $status => $count ) {
131 $mapped_counts[ $this->get_cache_key( $product_type, $status ) ] = (int) $count;
132 }
133
134 return wp_cache_set_multiple( $mapped_counts, '', $this->expiration );
135 }
136
137 /**
138 * Get the cache value for a given product type and set of statuses.
139 *
140 * @param string $product_type The post type (e.g. 'product', 'product_variation').
141 * @param string[] $product_statuses The statuses to retrieve.
142 *
143 * @return array<string,int>|null
144 */
145 public function get( string $product_type, array $product_statuses = array() ) {
146 if ( empty( $product_statuses ) ) {
147 $product_statuses = $this->get_saved_statuses_for_type( $product_type );
148 if ( empty( $product_statuses ) ) {
149 return null;
150 }
151 }
152
153 $cache_keys = array_map(
154 fn( $product_status ) => $this->get_cache_key( $product_type, $product_status ),
155 $product_statuses
156 );
157
158 $cache_values = wp_cache_get_multiple( $cache_keys );
159 $status_values = array();
160
161 $cache_key_prefix = $this->get_cache_key( $product_type, '' );
162 foreach ( $cache_values as $key => $value ) {
163 // Return null for the entire cache if any of the requested statuses are not found because they fell out of cache.
164 if ( false === $value ) {
165 return null;
166 }
167
168 $status = substr( $key, strlen( $cache_key_prefix ) );
169 $status_values[ $status ] = $value;
170 }
171
172 return $status_values;
173 }
174
175 /**
176 * Increment the cache value for a given product type and status.
177 *
178 * @param string $product_type The post type (e.g. 'product', 'product_variation').
179 * @param string $product_status The status of the product.
180 * @param int $offset The amount to increment by.
181 *
182 * @return int|false
183 */
184 public function increment( string $product_type, string $product_status, int $offset = 1 ) {
185 return wp_cache_incr( $this->get_cache_key( $product_type, $product_status ), $offset );
186 }
187
188 /**
189 * Decrement the cache value for a given product type and status.
190 *
191 * @param string $product_type The post type (e.g. 'product', 'product_variation').
192 * @param string $product_status The status of the product.
193 * @param int $offset The amount to decrement by.
194 *
195 * @return int|false
196 */
197 public function decrement( string $product_type, string $product_status, int $offset = 1 ) {
198 return wp_cache_decr( $this->get_cache_key( $product_type, $product_status ), $offset );
199 }
200
201 /**
202 * Flush the cache for a given product type and statuses.
203 *
204 * @param string $product_type The post type (e.g. 'product', 'product_variation').
205 * @param string[] $product_statuses The statuses to flush. Flushes all known statuses if empty.
206 *
207 * @return void
208 */
209 public function flush( string $product_type = 'product', array $product_statuses = array() ): void {
210 $flush_saved_statuses = false;
211 if ( empty( $product_statuses ) ) {
212 $product_statuses = $this->get_saved_statuses_for_type( $product_type );
213 $flush_saved_statuses = true;
214 }
215
216 $cache_keys = array_map(
217 fn ( $product_status ) => $this->get_cache_key( $product_type, $product_status ),
218 $product_statuses
219 );
220
221 if ( $flush_saved_statuses ) {
222 // If all statuses are being flushed, go ahead and flush the status list so any permanently removed statuses are cleared out.
223 $cache_keys[] = $this->get_saved_statuses_cache_key( $product_type );
224 }
225
226 wp_cache_delete_multiple( $cache_keys );
227 }
228 }
229