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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Caches / ProductCacheController.php
woocommerce / src / Internal / Caches Last commit date
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