Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions includes/utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,17 @@ 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();

if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
$wp_object_cache->__remoteset(); // important
}
// 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();
}
}

Expand Down
28 changes: 28 additions & 0 deletions tests/UtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,32 @@ 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 );
}

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_supports' ) && wp_cache_supports( 'flush_runtime' ) ) {
// We can't easily test the internal behavior without mocking, but we can verify
// 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
$result = clear_caches();
$this->assertNull( $result );
}
}