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 |