OrdersVersionStringInvalidator.php
4 months ago
ProductCache.php
5 months ago
ProductCacheController.php
4 days ago
ProductTransientsDeferrer.php
4 days ago
ProductVersionStringInvalidator.php
4 days ago
TaxRateVersionStringInvalidator.php
4 months ago
VersionStringGenerator.php
4 months ago
ProductTransientsDeferrer.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Caches; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\Utilities\ProductUtil; |
| 8 | |
| 9 | /** |
| 10 | * Coalesces repeated product transient deletions during bulk write operations. |
| 11 | * |
| 12 | * @internal |
| 13 | */ |
| 14 | class ProductTransientsDeferrer { |
| 15 | |
| 16 | /** |
| 17 | * Priority for the shutdown safety-net hook. |
| 18 | * |
| 19 | * This must run before WC_Post_Data::do_deferred_product_sync() at priority 10 |
| 20 | * so stale product transients are cleared before parent product sync reads them. |
| 21 | */ |
| 22 | private const SHUTDOWN_HOOK_PRIORITY = 0; |
| 23 | |
| 24 | /** |
| 25 | * The product utility instance. |
| 26 | * |
| 27 | * @var ProductUtil |
| 28 | */ |
| 29 | private ProductUtil $product_util; |
| 30 | |
| 31 | /** |
| 32 | * Nesting level of active deferrals. |
| 33 | * |
| 34 | * @var int |
| 35 | */ |
| 36 | private int $deferral_level = 0; |
| 37 | |
| 38 | /** |
| 39 | * Product IDs collected while deferral is active, as a set of id => true. |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | private array $deferred_product_ids = array(); |
| 44 | |
| 45 | /** |
| 46 | * Initialize the class instance. |
| 47 | * |
| 48 | * @internal |
| 49 | * |
| 50 | * @param ProductUtil $product_util The product utility instance. |
| 51 | * @return void |
| 52 | */ |
| 53 | final public function init( ProductUtil $product_util ): void { |
| 54 | $this->product_util = $product_util; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Start deferring product transient deletions. Calls can be nested. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function start_deferring(): void { |
| 63 | ++$this->deferral_level; |
| 64 | if ( 1 === $this->deferral_level ) { |
| 65 | add_action( 'shutdown', array( $this, 'handle_shutdown' ), self::SHUTDOWN_HOOK_PRIORITY ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Stop deferring product transient deletions. |
| 71 | * |
| 72 | * When the outermost deferral ends, all collected product IDs are flushed. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function stop_deferring(): void { |
| 77 | if ( 0 === $this->deferral_level ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | --$this->deferral_level; |
| 82 | if ( 0 === $this->deferral_level ) { |
| 83 | remove_action( 'shutdown', array( $this, 'handle_shutdown' ), self::SHUTDOWN_HOOK_PRIORITY ); |
| 84 | $this->flush(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Record a product ID for deferred transient deletion, if deferral is active. |
| 90 | * |
| 91 | * @param int $product_id Product ID whose transients were requested to be deleted. |
| 92 | * @return bool True if the deletion was deferred, false if deferral is not active. |
| 93 | */ |
| 94 | public function maybe_defer_deletion( int $product_id ): bool { |
| 95 | if ( 0 === $this->deferral_level ) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | $this->deferred_product_ids[ $product_id ] = true; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Handle the shutdown hook. |
| 105 | * |
| 106 | * Flushes pending deletions if deferral was not explicitly stopped. |
| 107 | * |
| 108 | * @internal |
| 109 | * |
| 110 | * @return void |
| 111 | */ |
| 112 | public function handle_shutdown(): void { |
| 113 | $this->deferral_level = 0; |
| 114 | $this->flush(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Delete transients for all collected product IDs and reset the collection. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | private function flush(): void { |
| 123 | if ( empty( $this->deferred_product_ids ) ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | $product_ids = array_keys( $this->deferred_product_ids ); |
| 128 | $this->deferred_product_ids = array(); |
| 129 | $this->product_util->delete_product_transients_for_products( $product_ids ); |
| 130 | } |
| 131 | } |
| 132 |