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 |