OrderCache.php
1 year ago
OrderCacheController.php
1 year ago
OrderCountCache.php
1 month ago
OrderCountCacheService.php
1 month ago
ProductCountCache.php
1 month ago
ProductCountCacheService.php
1 month ago
ProductCountCacheService.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Caches; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\Utilities\ProductUtil; |
| 8 | use WC_Product; |
| 9 | use WP_Post; |
| 10 | |
| 11 | /** |
| 12 | * A service class to help with updates to the aggregate product counts cache. |
| 13 | * |
| 14 | * @internal |
| 15 | */ |
| 16 | class ProductCountCacheService { |
| 17 | |
| 18 | public const BACKGROUND_EVENT_HOOK = 'woocommerce_refresh_product_count_cache'; |
| 19 | |
| 20 | /** |
| 21 | * ProductCountCache instance. |
| 22 | * |
| 23 | * @var ProductCountCache |
| 24 | */ |
| 25 | private ProductCountCache $product_count_cache; |
| 26 | |
| 27 | /** |
| 28 | * Array of product IDs with their last transitioned status as key value pairs. |
| 29 | * Guarantees idempotency for product status transitions when multiple hooks fire for the same product. |
| 30 | * |
| 31 | * @var array<int,string> |
| 32 | */ |
| 33 | private array $product_statuses = array(); |
| 34 | |
| 35 | /** |
| 36 | * Array of product IDs with their initial status as key value pairs. |
| 37 | * Guarantees idempotency for product status transitions when multiple hooks fire for the same product. |
| 38 | * |
| 39 | * @var array<int,string> |
| 40 | */ |
| 41 | private array $initial_product_statuses = array(); |
| 42 | |
| 43 | /** |
| 44 | * Set of product IDs currently being created in this request (keyed by ID; detected via old_status='new'). |
| 45 | * |
| 46 | * @var array<int,true> |
| 47 | */ |
| 48 | private array $products_in_creation = array(); |
| 49 | |
| 50 | /** |
| 51 | * Class initialization, invoked by the DI container. |
| 52 | * |
| 53 | * @internal |
| 54 | */ |
| 55 | final public function init(): void { |
| 56 | $this->product_count_cache = new ProductCountCache(); |
| 57 | |
| 58 | add_action( 'action_scheduler_ensure_recurring_actions', array( $this, 'unschedule_background_actions' ) ); |
| 59 | add_action( self::BACKGROUND_EVENT_HOOK, array( $this, 'prime_cache_if_cold' ) ); |
| 60 | if ( defined( 'WC_PLUGIN_BASENAME' ) ) { |
| 61 | add_action( 'deactivate_' . WC_PLUGIN_BASENAME, array( $this, 'unschedule_background_actions' ) ); |
| 62 | } |
| 63 | |
| 64 | // Until persistent counters reactivated, disable callbacks for woocommerce_new_product, transition_post_status, before_delete_post hooks. |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Primes the product count cache for a given post type when it is cold. |
| 69 | * |
| 70 | * @param string $product_type The product post type. |
| 71 | * @return void |
| 72 | */ |
| 73 | public function prime_cache_if_cold( string $product_type = 'product' ): void { |
| 74 | // Until persistent counters reactivated, this task is no-op. |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Register background caching for each product type. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function schedule_background_actions(): void { |
| 83 | $frequency = HOUR_IN_SECONDS * 12; |
| 84 | $timestamp = time() + $frequency; |
| 85 | as_schedule_recurring_action( $timestamp, $frequency, self::BACKGROUND_EVENT_HOOK, array( 'product' ), 'count', true ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Unschedules background actions. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public function unschedule_background_actions(): void { |
| 94 | WC()->queue()->cancel_all( self::BACKGROUND_EVENT_HOOK ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Update the cache when a new product is created. |
| 99 | * |
| 100 | * @param int $product_id Product ID. |
| 101 | * @param WC_Product $product The product. |
| 102 | * @return void |
| 103 | */ |
| 104 | public function update_on_new_product( int $product_id, WC_Product $product ): void { |
| 105 | // transition_post_status already counted this product — reverse any errant decrement from a cold step 1 and stop. |
| 106 | // In-memory status may diverge from DB after a mid-creation wp_update_post; do not increment here. |
| 107 | if ( isset( $this->product_statuses[ $product_id ] ) ) { |
| 108 | $this->maybe_restore_initial_status_count( $product_id ); |
| 109 | unset( $this->products_in_creation[ $product_id ] ); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // Cache was cold throughout creation — transition_post_status never fired; use in-memory status as the sole count. |
| 114 | $product_status = $product->get_status(); |
| 115 | if ( $this->product_count_cache->is_cached( 'product', $product_status ) ) { |
| 116 | $this->product_statuses[ $product_id ] = $product_status; |
| 117 | $this->product_count_cache->increment( 'product', $product_status ); |
| 118 | } |
| 119 | unset( $this->products_in_creation[ $product_id ] ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Update the cache whenever a product status changes. |
| 124 | * |
| 125 | * @param string $new_status The new post status. |
| 126 | * @param string $old_status The previous post status. |
| 127 | * @param WP_Post $post The post object. |
| 128 | * |
| 129 | * @return void |
| 130 | */ |
| 131 | public function update_on_product_status_changed( string $new_status, string $old_status, WP_Post $post ): void { |
| 132 | if ( 'product' !== $post->post_type ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $product_id = $post->ID; |
| 137 | |
| 138 | // WordPress uses 'new' as old_status exclusively on the first transition_post_status of a newly inserted post. |
| 139 | if ( 'new' === $old_status ) { |
| 140 | $this->products_in_creation[ $product_id ] = true; |
| 141 | } |
| 142 | |
| 143 | $is_new_cached = $this->product_count_cache->is_cached( 'product', $new_status ); |
| 144 | $is_old_cached = $this->product_count_cache->is_cached( 'product', $old_status ); |
| 145 | if ( ! $is_new_cached && ! $is_old_cached ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | // If the status count has already been incremented for this product, skip. |
| 150 | if ( ( $this->product_statuses[ $product_id ] ?? null ) === $new_status ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | $previously_tracked = isset( $this->product_statuses[ $product_id ] ); |
| 155 | $this->product_statuses[ $product_id ] = $new_status; |
| 156 | $was_decremented = $is_old_cached && false !== $this->product_count_cache->decrement( 'product', $old_status ); |
| 157 | if ( $is_new_cached ) { |
| 158 | $this->product_count_cache->increment( 'product', $new_status ); |
| 159 | } |
| 160 | |
| 161 | // Record old status for creation-time correction only; existing-product decrements are correct and must not be reversed. |
| 162 | // If $previously_tracked, an earlier transition already counted the old status — decrement is legitimate. |
| 163 | if ( ! $previously_tracked && $was_decremented && ! isset( $this->initial_product_statuses[ $product_id ] ) && isset( $this->products_in_creation[ $product_id ] ) ) { |
| 164 | $this->initial_product_statuses[ $product_id ] = $old_status; |
| 165 | } elseif ( ( $this->initial_product_statuses[ $product_id ] ?? null ) === $new_status ) { |
| 166 | unset( $this->initial_product_statuses[ $product_id ] ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Update the cache when a product is permanently deleted. |
| 172 | * |
| 173 | * @param int $post_id Post ID. |
| 174 | * @param WP_Post $post The post object. |
| 175 | * |
| 176 | * @return void |
| 177 | */ |
| 178 | public function update_on_product_deleted( int $post_id, WP_Post $post ): void { |
| 179 | if ( 'product' === $post->post_type ) { |
| 180 | // Reverse any errant decrement from a mid-creation status transition that update_on_new_product will never get to correct. |
| 181 | $this->maybe_restore_initial_status_count( $post_id ); |
| 182 | |
| 183 | $product_status = $post->post_status; |
| 184 | if ( $this->product_count_cache->is_cached( 'product', $product_status ) ) { |
| 185 | $this->product_count_cache->decrement( 'product', $product_status ); |
| 186 | } |
| 187 | |
| 188 | unset( $this->product_statuses[ $post_id ], $this->products_in_creation[ $post_id ] ); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Reverses an errant decrement recorded in initial_product_statuses for a given product, if any. |
| 194 | * |
| 195 | * @param int $product_id Product ID. |
| 196 | * |
| 197 | * @return void |
| 198 | */ |
| 199 | private function maybe_restore_initial_status_count( int $product_id ): void { |
| 200 | if ( isset( $this->initial_product_statuses[ $product_id ] ) ) { |
| 201 | $initial_status = $this->initial_product_statuses[ $product_id ]; |
| 202 | unset( $this->initial_product_statuses[ $product_id ] ); |
| 203 | if ( $this->product_count_cache->is_cached( 'product', $initial_status ) ) { |
| 204 | $this->product_count_cache->increment( 'product', $initial_status ); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 |