diff --git a/readme.txt b/readme.txt index 1ee48ee7ce..b43352a0fa 100644 --- a/readme.txt +++ b/readme.txt @@ -3,6 +3,7 @@ = [TBD] TBD = * Fix - The Decorated repository was not returning values from `save()` and other methods. Now they return as expected. [BTRIA-2310] +* Fix - Resolved an issue where transient notices would disappear given a certain order of operations. [ECP-1804] = [5.2.7] 2024-05-14 = diff --git a/src/Tribe/Admin/Notices.php b/src/Tribe/Admin/Notices.php index 3cd150b421..278180bde3 100644 --- a/src/Tribe/Admin/Notices.php +++ b/src/Tribe/Admin/Notices.php @@ -808,6 +808,8 @@ protected function get_nonce_action( string $slug ): string { protected function set_transients( $notices ) { $transient = self::$transient_notices_name; set_transient( $transient, $notices, MONTH_IN_SECONDS ); + // Manually memoize the value so we don't have to fetch it again. + tribe( 'cache' )['transient_admin_notices'] = $notices; } /** diff --git a/tests/wpunit/Tribe/Notices/NoticesTest.php b/tests/wpunit/Tribe/Notices/NoticesTest.php new file mode 100644 index 0000000000..77e544542a --- /dev/null +++ b/tests/wpunit/Tribe/Notices/NoticesTest.php @@ -0,0 +1,74 @@ +assertEmpty( $cache[ $cache_key ] ); + + // Register a notice. + $notice = Tribe__Admin__Notices::instance(); + $message = '

test

'; + $message_slug = 'test'; + $notice->register_transient( $message_slug, $message ); + + // The memoized notice should be in the Cache. + $this->assertEquals( $message, $cache[ $cache_key ][ $message_slug ][0] ); + } + + /** + * Take the Admin Notice instance and verify the notice cache busts. + * + * @test + */ + public function should_clear_memoize_notice() { + // Verify the notice is not memoized in the Cache. + $cache = tribe( 'cache' ); + $cache_key = 'transient_admin_notices'; + $this->assertEmpty( $cache[ $cache_key ] ); + + // Register a notice. + $notice = Tribe__Admin__Notices::instance(); + $message = '

test

'; + $message_slug = 'test'; + $notice->register_transient( $message_slug, $message ); + $notice->remove_transient( $message_slug ); + + // The memoized notice should not be in the Cache. + $this->assertFalse( isset( $cache[ $cache_key ][ $message_slug ] ) ); + } + + /** + * Take the Admin Notice instance and verify the notice updates. + * + * @test + */ + public function should_update_memoize_notice() { + // Verify the notice is not memoized in the Cache. + $cache = tribe( 'cache' ); + $cache_key = 'transient_admin_notices'; + $this->assertEmpty( $cache[ $cache_key ] ); + + // Register a notice. + $notice = Tribe__Admin__Notices::instance(); + $message = '

test

'; + $message_slug = 'test'; + $notice->register_transient( $message_slug, $message ); + + // Register another notice. + $message2 = '

test 2

'; + $message_slug2 = 'test 2'; + $notice->register_transient( $message_slug2, $message2 ); + + // The Notice store should be updated with both. + $this->assertTrue( $notice->showing_transient_notice( $message_slug ) ); + $this->assertTrue( $notice->showing_transient_notice( $message_slug2 ) ); + $this->assertFalse( $notice->showing_transient_notice( 'nonsense-faux-slug' ) ); + } +}