From 77f8689081a8d54bd07de1238044ba5de7e9658f Mon Sep 17 00:00:00 2001 From: Sandip Baikare Date: Sat, 25 Apr 2026 18:03:14 +0530 Subject: [PATCH 1/4] feat: Introduce 'wp_ai_client_cache_group' filter hook --- .../adapters/class-wp-ai-client-cache.php | 37 +++++++++++++++---- .../tests/ai-client/wpAiClientCache.php | 36 ++++++++++++++++++ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php index 45504897485f7..f8c35bf4d748e 100644 --- a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php +++ b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php @@ -29,6 +29,27 @@ class WP_AI_Client_Cache implements CacheInterface { */ private const CACHE_GROUP = 'wp_ai_client'; + /** + * Retrieves the cache group used for cache operations, applying a filter for customization. + * + * @since 7.1.0 + * + * @return string Cache group name. + */ + private function get_cache_group(): string { + /** + * Filter the cache group used by the WP AI Client cache adapter. + * + * Allows integrators to change the object cache group under which AI client + * items are stored. + * + * @since 7.1.0 + * + * @param string $group The cache group. + */ + return (string) apply_filters( 'wp_ai_client_cache_group', self::CACHE_GROUP ); + } + /** * Fetches a value from the cache. * @@ -40,7 +61,7 @@ class WP_AI_Client_Cache implements CacheInterface { */ public function get( $key, $default_value = null ) { $found = false; - $value = wp_cache_get( $key, self::CACHE_GROUP, false, $found ); + $value = wp_cache_get( $key, $this->get_cache_group(), false, $found ); if ( ! $found ) { return $default_value; @@ -62,7 +83,7 @@ public function get( $key, $default_value = null ) { public function set( $key, $value, $ttl = null ): bool { $expire = $this->ttl_to_seconds( $ttl ); - return wp_cache_set( $key, $value, self::CACHE_GROUP, $expire ); + return wp_cache_set( $key, $value, $this->get_cache_group(), $expire ); } /** @@ -74,7 +95,7 @@ public function set( $key, $value, $ttl = null ): bool { * @return bool True if the item was successfully removed. False if there was an error. */ public function delete( $key ): bool { - return wp_cache_delete( $key, self::CACHE_GROUP ); + return wp_cache_delete( $key, $this->get_cache_group() ); } /** @@ -92,7 +113,7 @@ public function clear(): bool { return false; } - return wp_cache_flush_group( self::CACHE_GROUP ); + return wp_cache_flush_group( $this->get_cache_group() ); } /** @@ -111,7 +132,7 @@ public function getMultiple( $keys, $default_value = null ): array { * @var array $keys_array */ $keys_array = $this->iterable_to_array( $keys ); - $values = wp_cache_get_multiple( $keys_array, self::CACHE_GROUP ); + $values = wp_cache_get_multiple( $keys_array, $this->get_cache_group() ); $result = array(); foreach ( $keys_array as $key ) { @@ -138,7 +159,7 @@ public function getMultiple( $keys, $default_value = null ): array { public function setMultiple( $values, $ttl = null ): bool { $values_array = $this->iterable_to_array( $values ); $expire = $this->ttl_to_seconds( $ttl ); - $results = wp_cache_set_multiple( $values_array, self::CACHE_GROUP, $expire ); + $results = wp_cache_set_multiple( $values_array, $this->get_cache_group(), $expire ); // Return true only if all operations succeeded. return ! in_array( false, $results, true ); @@ -154,7 +175,7 @@ public function setMultiple( $values, $ttl = null ): bool { */ public function deleteMultiple( $keys ): bool { $keys_array = $this->iterable_to_array( $keys ); - $results = wp_cache_delete_multiple( $keys_array, self::CACHE_GROUP ); + $results = wp_cache_delete_multiple( $keys_array, $this->get_cache_group() ); // Return true only if all operations succeeded. return ! in_array( false, $results, true ); @@ -170,7 +191,7 @@ public function deleteMultiple( $keys ): bool { */ public function has( $key ): bool { $found = false; - wp_cache_get( $key, self::CACHE_GROUP, false, $found ); + wp_cache_get( $key, $this->get_cache_group(), false, $found ); return (bool) $found; } diff --git a/tests/phpunit/tests/ai-client/wpAiClientCache.php b/tests/phpunit/tests/ai-client/wpAiClientCache.php index 690b8b9668981..e214e73b75915 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientCache.php +++ b/tests/phpunit/tests/ai-client/wpAiClientCache.php @@ -218,4 +218,40 @@ public function test_ttl_with_date_interval() { $this->assertTrue( $this->cache->set( 'key1', 'value1', $ttl ) ); $this->assertSame( 'value1', $this->cache->get( 'key1' ) ); } + + /** + * Test that the cache group filter is respected. + * + * @ticket 64591 + */ + public function test_cache_group_filter_is_respected() { + add_filter( 'wp_ai_client_cache_group', function( $group ) { + return 'wp_ai_client_tests_group'; + } ); + + $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); + $this->assertTrue( $set ); + + // Directly read from the underlying object cache using the expected group. + $value = wp_cache_get( 'ai_test_key', 'wp_ai_client_tests_group' ); + $this->assertSame( 'ai_value', $value ); + } + + /** + * Test that a non-string cache group filter value is cast to string. + * + * @ticket 64591 + */ + public function test_cache_group_filter_returns_non_string() { + add_filter( 'wp_ai_client_cache_group', function( $group ) { + return 12345; // Non-string value. + } ); + + $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); + $this->assertTrue( $set ); + + // Directly read from the underlying object cache using the expected group (cast to string). + $value = wp_cache_get( 'ai_test_key', '12345' ); + $this->assertSame( 'ai_value', $value ); + } } From 5a1fade3917edfe944f379851307fc75145ea1eb Mon Sep 17 00:00:00 2001 From: Sandip Baikare Date: Sat, 25 Apr 2026 18:09:53 +0530 Subject: [PATCH 2/4] fix: trac ticket number --- tests/phpunit/tests/ai-client/wpAiClientCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/ai-client/wpAiClientCache.php b/tests/phpunit/tests/ai-client/wpAiClientCache.php index e214e73b75915..3977d062e39a0 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientCache.php +++ b/tests/phpunit/tests/ai-client/wpAiClientCache.php @@ -222,7 +222,7 @@ public function test_ttl_with_date_interval() { /** * Test that the cache group filter is respected. * - * @ticket 64591 + * @ticket 65127 */ public function test_cache_group_filter_is_respected() { add_filter( 'wp_ai_client_cache_group', function( $group ) { @@ -240,7 +240,7 @@ public function test_cache_group_filter_is_respected() { /** * Test that a non-string cache group filter value is cast to string. * - * @ticket 64591 + * @ticket 65127 */ public function test_cache_group_filter_returns_non_string() { add_filter( 'wp_ai_client_cache_group', function( $group ) { From 271778f976c6c2b1b68135d24bce8d00772f076d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 25 Apr 2026 18:35:16 +0530 Subject: [PATCH 3/4] fix: coding standards --- .../adapters/class-wp-ai-client-cache.php | 2 +- .../tests/ai-client/wpAiClientCache.php | 34 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php index f8c35bf4d748e..97782b8379209 100644 --- a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php +++ b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php @@ -36,7 +36,7 @@ class WP_AI_Client_Cache implements CacheInterface { * * @return string Cache group name. */ - private function get_cache_group(): string { + private function get_cache_group(): string { /** * Filter the cache group used by the WP AI Client cache adapter. * diff --git a/tests/phpunit/tests/ai-client/wpAiClientCache.php b/tests/phpunit/tests/ai-client/wpAiClientCache.php index 3977d062e39a0..f32e79f102dd8 100644 --- a/tests/phpunit/tests/ai-client/wpAiClientCache.php +++ b/tests/phpunit/tests/ai-client/wpAiClientCache.php @@ -225,28 +225,34 @@ public function test_ttl_with_date_interval() { * @ticket 65127 */ public function test_cache_group_filter_is_respected() { - add_filter( 'wp_ai_client_cache_group', function( $group ) { - return 'wp_ai_client_tests_group'; - } ); + add_filter( + 'wp_ai_client_cache_group', + function ( $group ) { + return 'wp_ai_client_tests_group'; + } + ); - $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); - $this->assertTrue( $set ); + $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); + $this->assertTrue( $set ); - // Directly read from the underlying object cache using the expected group. - $value = wp_cache_get( 'ai_test_key', 'wp_ai_client_tests_group' ); - $this->assertSame( 'ai_value', $value ); - } + // Directly read from the underlying object cache using the expected group. + $value = wp_cache_get( 'ai_test_key', 'wp_ai_client_tests_group' ); + $this->assertSame( 'ai_value', $value ); + } /** * Test that a non-string cache group filter value is cast to string. - * + * * @ticket 65127 */ public function test_cache_group_filter_returns_non_string() { - add_filter( 'wp_ai_client_cache_group', function( $group ) { - return 12345; // Non-string value. - } ); - + add_filter( + 'wp_ai_client_cache_group', + function ( $group ) { + return 12345; // Non-string value. + } + ); + $set = $this->cache->set( 'ai_test_key', 'ai_value', 3600 ); $this->assertTrue( $set ); From f7121959a5562707aeb1b99667d0a803781bf63e Mon Sep 17 00:00:00 2001 From: Sandip Baikare Date: Sat, 25 Apr 2026 21:51:28 +0530 Subject: [PATCH 4/4] fix: update the filter hook comment --- .../ai-client/adapters/class-wp-ai-client-cache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php index 97782b8379209..11c544d2f42e6 100644 --- a/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php +++ b/src/wp-includes/ai-client/adapters/class-wp-ai-client-cache.php @@ -38,10 +38,11 @@ class WP_AI_Client_Cache implements CacheInterface { */ private function get_cache_group(): string { /** - * Filter the cache group used by the WP AI Client cache adapter. + * Filters the cache group used by the WP AI Client cache adapter. * * Allows integrators to change the object cache group under which AI client - * items are stored. + * items are stored. This is useful for avoiding key collisions, creating + * environment-specific caches, or adapting to backend constraints. * * @since 7.1.0 *