From 8e4eb58685a8ba34cc430e3ba20d2cb6267ab661 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Wed, 13 Aug 2025 17:41:26 -0700 Subject: [PATCH 1/8] Fix Object Cache Pro compatibility issue - Replace direct access to object cache properties with WordPress's wp_cache_* functions - This fixes the fatal error when using Object Cache Pro or other object cache drop-ins - Follows WordPress best practices for plugin development - Add test to ensure clear_caches function works without errors - Resolves #95 --- includes/utility.php | 18 +++++++++--------- tests/UtilityTest.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/includes/utility.php b/includes/utility.php index 201c8b5..51b5cc8 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -76,19 +76,19 @@ function stop_bulk_operation() { * @props VIP */ function clear_caches() { - global $wpdb, $wp_object_cache; + global $wpdb; $wpdb->queries = array(); - if ( is_object( $wp_object_cache ) ) { - $wp_object_cache->group_ops = array(); - $wp_object_cache->stats = array(); - $wp_object_cache->memcache_debug = array(); - $wp_object_cache->cache = array(); + // Use WordPress's built-in cache functions instead of accessing object cache properties directly + // This ensures compatibility with all object cache implementations including Object Cache Pro - if ( method_exists( $wp_object_cache, '__remoteset' ) ) { - $wp_object_cache->__remoteset(); // important - } + // Clear all cache groups + wp_cache_flush(); + + // Reset any custom cache stats if the object cache supports it + if ( function_exists( 'wp_cache_stats' ) ) { + wp_cache_stats(); } } diff --git a/tests/UtilityTest.php b/tests/UtilityTest.php index d6557e5..8746f56 100644 --- a/tests/UtilityTest.php +++ b/tests/UtilityTest.php @@ -70,4 +70,17 @@ function test_it_can_override_default_capability() { $this->assertEquals( 'manage_options', get_required_capability() ); } + + function test_clear_caches_does_not_cause_errors() { + // Test that clear_caches function can be called without errors + // This is especially important for compatibility with Object Cache Pro and other cache implementations + $result = clear_caches(); + + // The function should not return anything (void function) + $this->assertNull( $result ); + + // The function should not cause any fatal errors or exceptions + // If we reach this point, the test passes + $this->assertTrue( true ); + } } From babc585c07cbf63a1217c7f5a847bf73b408f2fd Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 14 Aug 2025 16:09:37 -0700 Subject: [PATCH 2/8] Improve cache clearing to use wp_cache_flush_runtime() for better OOM prevention - Use wp_cache_flush_runtime() when available (WordPress 6.0+) as it's more appropriate for preventing out of memory errors - Add fallback to wp_cache_flush() for older WordPress versions - This function specifically clears runtime cache without affecting persistent storage - Add test to verify appropriate cache function usage - Maintains backward compatibility while improving performance --- includes/utility.php | 14 ++++++++++---- tests/UtilityTest.php | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/includes/utility.php b/includes/utility.php index 51b5cc8..e8eab1f 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -82,10 +82,16 @@ function clear_caches() { // Use WordPress's built-in cache functions instead of accessing object cache properties directly // This ensures compatibility with all object cache implementations including Object Cache Pro - - // Clear all cache groups - wp_cache_flush(); - + + // Clear runtime cache to prevent out of memory errors during bulk operations + // wp_cache_flush_runtime() was introduced in WordPress 6.0.0 and is more appropriate for OOM prevention + if ( function_exists( 'wp_cache_flush_runtime' ) ) { + wp_cache_flush_runtime(); + } else { + // Fallback to wp_cache_flush() for older WordPress versions + wp_cache_flush(); + } + // Reset any custom cache stats if the object cache supports it if ( function_exists( 'wp_cache_stats' ) ) { wp_cache_stats(); diff --git a/tests/UtilityTest.php b/tests/UtilityTest.php index 8746f56..eeb6b31 100644 --- a/tests/UtilityTest.php +++ b/tests/UtilityTest.php @@ -83,4 +83,18 @@ function test_clear_caches_does_not_cause_errors() { // If we reach this point, the test passes $this->assertTrue( true ); } + + function test_clear_caches_uses_appropriate_cache_function() { + // Test that the function uses wp_cache_flush_runtime() when available (WordPress 6.0+) + // This is more appropriate for preventing out of memory errors during bulk operations + if ( function_exists( 'wp_cache_flush_runtime' ) ) { + // We can't easily test the internal behavior without mocking, but we can verify + // the function exists and our function can call it without errors + $this->assertTrue( function_exists( 'wp_cache_flush_runtime' ) ); + } + + // The function should always work regardless of WordPress version + $result = clear_caches(); + $this->assertNull( $result ); + } } From 2da5c083e2b43024141600486d53f0ee273f061b Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 14 Aug 2025 16:11:31 -0700 Subject: [PATCH 3/8] Add function existence check for wp_cache_flush() fallback - Add function_exists check for wp_cache_flush() as additional safety measure - While wp_cache_flush() is guaranteed to exist in WordPress 6.6+, this check provides extra robustness for different cache implementations - Maintains backward compatibility and improves error handling - Follows WordPress best practices for cache function usage --- includes/utility.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/utility.php b/includes/utility.php index e8eab1f..40ef790 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -80,16 +80,16 @@ function clear_caches() { $wpdb->queries = array(); - // Use WordPress's built-in cache functions instead of accessing object cache properties directly - // This ensures compatibility with all object cache implementations including Object Cache Pro - // Clear runtime cache to prevent out of memory errors during bulk operations // wp_cache_flush_runtime() was introduced in WordPress 6.0.0 and is more appropriate for OOM prevention if ( function_exists( 'wp_cache_flush_runtime' ) ) { wp_cache_flush_runtime(); } else { // Fallback to wp_cache_flush() for older WordPress versions - wp_cache_flush(); + // wp_cache_flush() has been available since early WordPress versions + if ( function_exists( 'wp_cache_flush' ) ) { + wp_cache_flush(); + } } // Reset any custom cache stats if the object cache supports it From 3ff69f14962077cf9337d0f7fd132dbb4694a5ca Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 14 Aug 2025 16:13:15 -0700 Subject: [PATCH 4/8] Refactor cache clearing logic in clear_caches function - Remove redundant comments and streamline the cache clearing process - Use wp_cache_flush() directly as a fallback for older WordPress versions - Simplify the function existence checks while maintaining backward compatibility - Enhances code clarity and adheres to best practices for cache management --- includes/utility.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/includes/utility.php b/includes/utility.php index 40ef790..a2f2e40 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -80,19 +80,12 @@ function clear_caches() { $wpdb->queries = array(); - // Clear runtime cache to prevent out of memory errors during bulk operations - // wp_cache_flush_runtime() was introduced in WordPress 6.0.0 and is more appropriate for OOM prevention if ( function_exists( 'wp_cache_flush_runtime' ) ) { wp_cache_flush_runtime(); } else { - // Fallback to wp_cache_flush() for older WordPress versions - // wp_cache_flush() has been available since early WordPress versions - if ( function_exists( 'wp_cache_flush' ) ) { - wp_cache_flush(); - } + wp_cache_flush(); } - // Reset any custom cache stats if the object cache supports it if ( function_exists( 'wp_cache_stats' ) ) { wp_cache_stats(); } From e6777b8f86a58ba2dd647acaa236b98a1a431f3e Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 14 Aug 2025 16:15:32 -0700 Subject: [PATCH 5/8] Use wp_cache_supports() for runtime flush capability check - Replace function_exists check with wp_cache_supports('flush_runtime') for better reliability - This follows WordPress best practices for checking cache capabilities - Ensures the cache implementation actually supports runtime flushing, not just that the function exists - Improves compatibility with different object cache implementations - Updates test to verify capability support rather than just function existence --- includes/utility.php | 6 +++++- tests/UtilityTest.php | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/includes/utility.php b/includes/utility.php index a2f2e40..b1c7c9e 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -80,12 +80,16 @@ function clear_caches() { $wpdb->queries = array(); - if ( function_exists( 'wp_cache_flush_runtime' ) ) { + // Clear runtime cache to prevent out of memory errors during bulk operations + // Use wp_cache_supports() to check for runtime flush capability (WordPress 6.0+) + if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_runtime' ) ) { wp_cache_flush_runtime(); } else { + // Fallback to wp_cache_flush() for older WordPress versions or implementations without runtime support wp_cache_flush(); } + // Reset any custom cache stats if the object cache supports it if ( function_exists( 'wp_cache_stats' ) ) { wp_cache_stats(); } diff --git a/tests/UtilityTest.php b/tests/UtilityTest.php index eeb6b31..3568adf 100644 --- a/tests/UtilityTest.php +++ b/tests/UtilityTest.php @@ -87,10 +87,11 @@ function test_clear_caches_does_not_cause_errors() { function test_clear_caches_uses_appropriate_cache_function() { // Test that the function uses wp_cache_flush_runtime() when available (WordPress 6.0+) // This is more appropriate for preventing out of memory errors during bulk operations - if ( function_exists( 'wp_cache_flush_runtime' ) ) { + if ( function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_runtime' ) ) { // We can't easily test the internal behavior without mocking, but we can verify - // the function exists and our function can call it without errors - $this->assertTrue( function_exists( 'wp_cache_flush_runtime' ) ); + // the capability check works and our function can call it without errors + $this->assertTrue( function_exists( 'wp_cache_supports' ) ); + $this->assertTrue( wp_cache_supports( 'flush_runtime' ) ); } // The function should always work regardless of WordPress version From 3eabc4b7b4ac4e8188e5e874b3378a55a7484447 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 15 Aug 2025 13:00:41 +1000 Subject: [PATCH 6/8] CS: Remove trailing whitespace. --- includes/utility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/utility.php b/includes/utility.php index b1c7c9e..66677e2 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -88,7 +88,7 @@ function clear_caches() { // Fallback to wp_cache_flush() for older WordPress versions or implementations without runtime support wp_cache_flush(); } - + // Reset any custom cache stats if the object cache supports it if ( function_exists( 'wp_cache_stats' ) ) { wp_cache_stats(); From 3321e3b1539a647b988197c381e1fce689a6d237 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 14 Aug 2025 20:30:08 -0700 Subject: [PATCH 7/8] =?UTF-8?q?fix:=20remove=20cache=20stat=20=E2=80=9Cres?= =?UTF-8?q?et=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- includes/utility.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/includes/utility.php b/includes/utility.php index 66677e2..1a13ebc 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -89,10 +89,6 @@ function clear_caches() { wp_cache_flush(); } - // Reset any custom cache stats if the object cache supports it - if ( function_exists( 'wp_cache_stats' ) ) { - wp_cache_stats(); - } } /** From f4fc67e23df863b27ad0f73afc862fb7024e8a58 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 15 Aug 2025 13:48:01 +1000 Subject: [PATCH 8/8] CS: Remove extra line break. --- includes/utility.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/utility.php b/includes/utility.php index 1a13ebc..702d11f 100755 --- a/includes/utility.php +++ b/includes/utility.php @@ -88,7 +88,6 @@ function clear_caches() { // Fallback to wp_cache_flush() for older WordPress versions or implementations without runtime support wp_cache_flush(); } - } /**