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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 / OrderCountCache.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
OrderCountCache.php
256 lines
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Automattic\WooCommerce\Caches;
6
7 use Automattic\WooCommerce\Enums\OrderStatus;
8
9 /**
10 * A class to cache counts for various order statuses.
11 */
12 class OrderCountCache {
13
14 /**
15 * Cache prefix.
16 *
17 * @var string
18 */
19 private $cache_prefix = 'order-count';
20
21 /**
22 * Default value for the duration of the objects in the cache, in seconds
23 * (may not be used depending on the cache engine used WordPress cache implementation).
24 *
25 * @var int
26 */
27 protected $expiration = DAY_IN_SECONDS;
28
29 /**
30 * Retrieves the list of known statuses by order type. A cached array of statuses is saved per order type for
31 * improved backward compatibility with some of the extensions that don't register all statuses they use with
32 * WooCommerce.
33 *
34 * @param string $order_type The type of order.
35 *
36 * @return string[]
37 */
38 private function get_saved_statuses_for_type( string $order_type ) {
39 $statuses = wp_cache_get( $this->get_saved_statuses_cache_key( $order_type ) );
40 if ( ! is_array( $statuses ) ) {
41 $statuses = array();
42 }
43
44 return $statuses;
45 }
46
47 /**
48 * Adds the given statuses to the cached statuses array for the order type if they are not already stored.
49 *
50 * @param string $order_type The order type to save with.
51 * @param string[] $order_statuses One or more normalised statuses to add.
52 *
53 * @return void
54 */
55 private function ensure_statuses_for_type( string $order_type, array $order_statuses ) {
56 if ( empty( $order_statuses ) ) {
57 return;
58 }
59
60 $existing = $this->get_saved_statuses_for_type( $order_type );
61 $new_statuses = array_diff( $order_statuses, $existing );
62 if ( empty( $new_statuses ) ) {
63 return;
64 }
65 $merged = array_unique( array_merge( $existing, $new_statuses ) );
66
67 wp_cache_set( $this->get_saved_statuses_cache_key( $order_type ), $merged, '', $this->expiration );
68 }
69
70 /**
71 * Get the default statuses.
72 *
73 * @return string[]
74 *
75 * @deprecated 10.1.0 This method will be removed in the future.
76 */
77 public function get_default_statuses() {
78 return array_merge(
79 array_keys( wc_get_order_statuses() ),
80 array( OrderStatus::TRASH )
81 );
82 }
83
84 /**
85 * Get the cache key for a given order type and status.
86 *
87 * @param string $order_type The type of order.
88 * @param string $order_status The status of the order.
89 *
90 * @return string The cache key.
91 */
92 private function get_cache_key( $order_type, $order_status ) {
93 return $this->cache_prefix . '_' . $order_type . '_' . $order_status;
94 }
95
96 /**
97 * Get the cache key for saved statuses of the given order type.
98 *
99 * @param string $order_type The type of order.
100 *
101 * @return string The cache key.
102 */
103 private function get_saved_statuses_cache_key( string $order_type ) {
104 return $this->cache_prefix . '_' . $order_type . '_statuses';
105 }
106
107 /**
108 * Check if the cache has a value for a given order type and status.
109 *
110 * @param string $order_type The type of order.
111 * @param string $order_status The status of the order.
112 *
113 * @return bool True if the cache has a value, false otherwise.
114 */
115 public function is_cached( $order_type, $order_status ) {
116 $cache_key = $this->get_cache_key( $order_type, $order_status );
117 return wp_cache_get( $cache_key ) !== false;
118 }
119
120 /**
121 * Set the cache value for a given order type and status.
122 *
123 * @param string $order_type The type of order.
124 * @param string $order_status The status slug of the order.
125 * @param int $value The value to set.
126 *
127 * @return bool True if the value was set, false otherwise.
128 */
129 public function set( $order_type, $order_status, int $value ): bool {
130 $this->ensure_statuses_for_type( (string) $order_type, array( (string) $order_status ) );
131 $cache_key = $this->get_cache_key( $order_type, $order_status );
132 return wp_cache_set( $cache_key, $value, '', $this->expiration );
133 }
134
135
136 /**
137 * Set the cache count value for multiple statuses at once.
138 *
139 * @param string $order_type The order type being set.
140 * @param array<string,int> $counts Normalized counts keyed by status slug (e.g. [ 'wc-processing' => 10, 'wc-pending' => 5 ]).
141 *
142 * @return array<string,bool> Success map from wp_cache_set_multiple().
143 */
144 public function set_multiple( string $order_type, array $counts ) {
145 if ( empty( $counts ) ) {
146 return array();
147 }
148
149 $this->ensure_statuses_for_type( $order_type, array_keys( $counts ) );
150
151 $mapped_counts = array();
152 foreach ( $counts as $status => $count ) {
153 $mapped_counts[ $this->get_cache_key( $order_type, $status ) ] = (int) $count;
154 }
155
156 return wp_cache_set_multiple( $mapped_counts, '', $this->expiration );
157 }
158
159
160 /**
161 * Get the cache value for a given order type and set of statuses.
162 *
163 * @param string $order_type The type of order.
164 * @param string[] $order_statuses The statuses to retrieve.
165 *
166 * @return array<string,int>|null The cache value.
167 */
168 public function get( $order_type, $order_statuses = array() ) {
169 $order_type = (string) $order_type;
170 if ( empty( $order_statuses ) ) {
171 $order_statuses = $this->get_saved_statuses_for_type( $order_type );
172 if ( empty( $order_statuses ) ) {
173 return null;
174 }
175 }
176
177 $cache_keys = array_map(
178 fn( $order_statuses ) => $this->get_cache_key( $order_type, $order_statuses ),
179 $order_statuses
180 );
181
182 $cache_values = wp_cache_get_multiple( $cache_keys );
183 $status_values = array();
184
185 $cache_key_prefix = $this->get_cache_key( $order_type, '' );
186 foreach ( $cache_values as $key => $value ) {
187 // Return null for the entire cache if any of the requested statuses are not found because they fell out of cache.
188 if ( false === $value ) {
189 return null;
190 }
191
192 $status = substr( $key, strlen( $cache_key_prefix ) );
193 $status_values[ $status ] = $value;
194 }
195
196 return $status_values;
197 }
198
199 /**
200 * Increment the cache value for a given order type and status.
201 *
202 * @param string $order_type The type of order.
203 * @param string $order_status The status of the order.
204 * @param int $offset The amount to increment by.
205 *
206 * @return int|false The new value of the cache.
207 */
208 public function increment( $order_type, $order_status, $offset = 1 ) {
209 $cache_key = $this->get_cache_key( $order_type, $order_status );
210 return wp_cache_incr( $cache_key, $offset );
211 }
212
213 /**
214 * Decrement the cache value for a given order type and status.
215 *
216 * @param string $order_type The type of order.
217 * @param string $order_status The status of the order.
218 * @param int $offset The amount to decrement by.
219 *
220 * @return int|false The new value of the cache.
221 */
222 public function decrement( $order_type, $order_status, $offset = 1 ) {
223 $cache_key = $this->get_cache_key( $order_type, $order_status );
224 return wp_cache_decr( $cache_key, $offset );
225 }
226
227 /**
228 * Flush the cache for a given order type and statuses.
229 *
230 * @param string $order_type The type of order.
231 * @param string[] $order_statuses The statuses to flush. Flushes all known statuses if empty.
232 *
233 * @return void
234 */
235 public function flush( $order_type = 'shop_order', $order_statuses = array() ) {
236 $order_type = (string) $order_type;
237 $flush_saved_statuses = false;
238 if ( empty( $order_statuses ) ) {
239 $order_statuses = $this->get_saved_statuses_for_type( $order_type );
240 $flush_saved_statuses = true;
241 }
242
243 $cache_keys = array_map(
244 fn( $order_statuses ) => $this->get_cache_key( $order_type, $order_statuses ),
245 $order_statuses
246 );
247
248 if ( $flush_saved_statuses ) {
249 // If all statuses are being flushed, go ahead and flush the status list so any permanently removed statuses are cleared out.
250 $cache_keys[] = $this->get_saved_statuses_cache_key( $order_type );
251 }
252
253 wp_cache_delete_multiple( $cache_keys );
254 }
255 }
256