Protect against redundant cache and RUCSS table entries when multisite switch_to_blog is used
Describe the bug
User reported an issue in their multi-site setup in which they use switch_to_blog() and restore_current_blog() to fetch common data from their various blogs.
During switch_to_blog() the $wpdb->tables array is filled with redundant wpr_rocket_cache and wpr_rucss_used_css entries.
This is happening because of this line inside berlinDB (the package that we use to handle the database): https://github.com/wp-media/wp-rocket/blob/3b6300ec350c3fa60bdae03c32bc8d5c4b9cb7f9/inc/Dependencies/Database/Table.php#L929
To Reproduce User reported this could be reproduced with the following:
global $wpdb;
var_dump( $wpdb->tables );
switch_to_blog( get_current_blog_id() );
var_dump( $wpdb->tables );
switch_to_blog( get_current_blog_id() );
var_dump( $wpdb->tables );
switch_to_blog( get_current_blog_id() );
var_dump( $wpdb->tables );
Expected behavior
We should not create duplicate entries in the DB tables while switch_to_blog() and restore_current_blog() are used.
The user reported this worked to prevent the issue:
// Add the table to the global table array
if( array_search( $this->prefixed_name, $db->{$tables} ) === false )
{
$db->{$tables}[] = $this->prefixed_name;
}
Additional context https://secure.helpscout.net/conversation/2553115996/483411 https://wp-media.slack.com/archives/C43T1AYMQ/p1711631770988099
Acceptance Criteria (for WP Media team use only) Clear instructions for developers, to be added before the grooming
I'd like to comment that the 'fix' was just an interim attempt to prevent the array being added to, I'm sure there's bound to be a more holistic change to how this function is triggered and what it does. But adding the simple wrapper prevented the issues in my instance. so thank you again for the ticket.
This is not something that can be implemented soon. We'll reopen it when working on multisite.