Bust WC object cache on direct postmeta updates. (#50215)

This commit is contained in:
Vedanshu Jain 2024-08-29 17:12:26 +05:30 committed by GitHub
parent 4ab886ada0
commit f359c20f84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Bust WC object cache on direct post meta update to prevent stale cache read.

View File

@ -58,6 +58,8 @@ class WC_Post_Data {
// Meta cache flushing.
add_action( 'updated_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
add_action( 'added_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
add_action( 'deleted_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
add_action( 'updated_order_item_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
}

View File

@ -114,4 +114,67 @@ class WC_Product_Data_Store_CPT_Test extends WC_Unit_Test_Case {
$product2->save();
$product3->save();
}
/**
* @testDox Test that meta cache key is changed on direct post meta add.
*/
public function test_get_meta_data_is_busted_on_post_meta_add() {
$product = new WC_Product();
$product->save();
// Set the cache.
$product->get_meta_data();
$object_id_cache_key = WC_Cache_Helper::get_cache_prefix( 'object_' . $product->get_id() );
add_post_meta( $product->get_id(), 'test', 'value' );
$r_object_id_cache_key = WC_Cache_Helper::get_cache_prefix( 'object_' . $product->get_id() );
$this->assertNotEquals( $object_id_cache_key, $r_object_id_cache_key );
$product = wc_get_product( $product->get_id() );
$this->assertEquals( 'value', $product->get_meta( 'test', true ) );
}
/**
* @testDox Test that meta cache key is changed on direct post meta update.
*/
public function test_get_meta_data_is_busted_on_post_meta_update() {
$product = new WC_Product();
$product->add_meta_data( 'test', 'value' );
$product->save();
// Set the cache.
$product->get_meta_data();
$object_id_cache_key = WC_Cache_Helper::get_cache_prefix( 'object_' . $product->get_id() );
update_post_meta( $product->get_id(), 'test', 'value2' );
$r_object_id_cache_key = WC_Cache_Helper::get_cache_prefix( 'object_' . $product->get_id() );
$this->assertNotEquals( $object_id_cache_key, $r_object_id_cache_key );
$product = wc_get_product( $product->get_id() );
$this->assertEquals( 'value2', $product->get_meta( 'test', true ) );
}
/**
* @testDox Test that meta cache key is changed on direct post meta delete.
*/
public function test_get_meta_data_is_busted_on_post_meta_delete() {
$product = new WC_Product();
$product->add_meta_data( 'test', 'value' );
$product->save();
// Set the cache.
$product->get_meta_data();
$object_id_cache_key = WC_Cache_Helper::get_cache_prefix( 'object_' . $product->get_id() );
delete_post_meta( $product->get_id(), 'test' );
$r_object_id_cache_key = WC_Cache_Helper::get_cache_prefix( 'object_' . $product->get_id() );
$this->assertNotEquals( $object_id_cache_key, $r_object_id_cache_key );
$product = wc_get_product( $product->get_id() );
$this->assertEmpty( $product->get_meta( 'test', true ) );
}
}