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 |