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