OrdersVersionStringInvalidator.php
4 months ago
ProductCache.php
5 months ago
ProductCacheController.php
5 months ago
ProductVersionStringInvalidator.php
4 months ago
TaxRateVersionStringInvalidator.php
4 months ago
VersionStringGenerator.php
4 months ago
ProductCacheController.php
176 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ProductCacheController class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Caches; |
| 9 | |
| 10 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 11 | |
| 12 | /** |
| 13 | * Controller for product caching functionality. |
| 14 | * |
| 15 | * @since 10.5.0 |
| 16 | */ |
| 17 | class ProductCacheController { |
| 18 | |
| 19 | /** |
| 20 | * Feature flag name for product instance caching. |
| 21 | * |
| 22 | * @since 10.5.0 |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | public const FEATURE_NAME = 'product_instance_caching'; |
| 27 | |
| 28 | /** |
| 29 | * The product cache instance. |
| 30 | * |
| 31 | * @since 10.5.0 |
| 32 | * |
| 33 | * @var ProductCache |
| 34 | */ |
| 35 | private ProductCache $product_cache; |
| 36 | |
| 37 | /** |
| 38 | * Class initialization, invoked by the DI container. |
| 39 | * |
| 40 | * @since 10.5.0 |
| 41 | * |
| 42 | * @internal |
| 43 | * |
| 44 | * @param ProductCache $product_cache The product cache instance. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | final public function init( ProductCache $product_cache ): void { |
| 49 | $this->product_cache = $product_cache; |
| 50 | |
| 51 | // Mark cache group as non-persistent immediately to ensure it's set |
| 52 | // regardless of when this controller is instantiated relative to other hooks. |
| 53 | $this->set_product_cache_group_as_non_persistent(); |
| 54 | |
| 55 | // Defer feature check to 'init' to avoid triggering translations too early. |
| 56 | add_action( 'init', array( $this, 'on_init' ), 0 ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Check feature flag and register hooks on WordPress init. |
| 61 | * |
| 62 | * @since 10.5.0 |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function on_init(): void { |
| 67 | if ( ! FeaturesUtil::feature_is_enabled( self::FEATURE_NAME ) ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $this->register_hooks(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register the cache invalidation hooks. |
| 76 | * |
| 77 | * This method is separated from on_init() to allow tests to call it directly |
| 78 | * after enabling the feature flag. |
| 79 | * |
| 80 | * @since 10.5.0 |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | public function register_hooks(): void { |
| 85 | // Handle direct WordPress post updates (bypassing CRUD). |
| 86 | add_action( 'clean_post_cache', array( $this, 'invalidate_product_cache_on_clean' ), 10, 2 ); |
| 87 | |
| 88 | // Handle post meta updates (third-party plugins updating via postmeta API). |
| 89 | add_action( 'updated_post_meta', array( $this, 'invalidate_product_cache_by_meta' ), 10, 2 ); |
| 90 | add_action( 'added_post_meta', array( $this, 'invalidate_product_cache_by_meta' ), 10, 2 ); |
| 91 | add_action( 'deleted_post_meta', array( $this, 'invalidate_product_cache_by_meta' ), 10, 2 ); |
| 92 | |
| 93 | // Handle direct stock/sales updates (which uses direct SQL and cache manipulation, bypassing standard meta hooks) |
| 94 | // In the future, update WC_Product_Data_Store_CPT::update_product_stock() and |
| 95 | // update_product_sales() to trigger standard WordPress updated_post_meta hooks instead |
| 96 | // of requiring specific hooks here. |
| 97 | add_action( 'woocommerce_updated_product_stock', array( $this, 'invalidate_product_cache' ), 10, 1 ); |
| 98 | add_action( 'woocommerce_updated_product_sales', array( $this, 'invalidate_product_cache' ), 10, 1 ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set the `product_objects` cache group as non-persistent. |
| 103 | * |
| 104 | * With product instance caching enabled, products are cached in-memory during a request |
| 105 | * rather than being persisted to external cache backends. If WC_Data::__sleep()/::__wakeup() methods are eventually |
| 106 | * removed or changed so that the entire object is stored instead of just the ID, this should be revisited and evaluated |
| 107 | * performance impact. |
| 108 | * |
| 109 | * @since 10.5.0 |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public function set_product_cache_group_as_non_persistent(): void { |
| 114 | wp_cache_add_non_persistent_groups( array( $this->product_cache->get_object_type() ) ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Invalidate the product cache when the post cache is cleaned. |
| 119 | * |
| 120 | * @since 10.5.0 |
| 121 | * |
| 122 | * @param int $post_id The post ID. |
| 123 | * @param \WP_Post $post The post object. |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function invalidate_product_cache_on_clean( $post_id, $post ): void { |
| 128 | $post_id = (int) $post_id; |
| 129 | /** |
| 130 | * It's important not to trigger get_post() during this callback as some extensions may attempt to clean cache |
| 131 | * prior to updating the database and a call to get_post() would cause the post to be added back to cache before the update. |
| 132 | */ |
| 133 | if ( ! ( $post instanceof \WP_Post ) || ! in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | $this->product_cache->remove( $post_id ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Invalidate the product cache for a given post ID if it's a product or product variation. |
| 142 | * |
| 143 | * @since 10.5.0 |
| 144 | * |
| 145 | * @param int $post_id The post ID to check and invalidate. |
| 146 | * |
| 147 | * @return void |
| 148 | */ |
| 149 | public function invalidate_product_cache( $post_id ): void { |
| 150 | $post_id = (int) $post_id; |
| 151 | $post_type = get_post_type( $post_id ); |
| 152 | if ( ! $post_type || ! in_array( $post_type, array( 'product', 'product_variation' ), true ) ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $this->product_cache->remove( $post_id ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Invalidate the product cache when post meta is updated. |
| 161 | * |
| 162 | * @since 10.5.0 |
| 163 | * |
| 164 | * @param int $meta_id The ID of the metadata entry. |
| 165 | * @param int $object_id The ID of the object the metadata is for. |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | public function invalidate_product_cache_by_meta( $meta_id, $object_id ): void { |
| 170 | $object_id = (int) $object_id; |
| 171 | if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) { |
| 172 | $this->invalidate_product_cache( $object_id ); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 |