wp_cache_switch_to_blog_fallback()

In this article

Fallback logic for switching cache context when an object cache drop-in lacks a switch_to_blog() method.

Description

Reinitializes the cache and restores global/non-persistent groups.

Used by the wp_cache_switch_to_blog() compatibility function, abstracted only to allow for unit testing outside of the drop-in plugin inclusion circus.

Source

function wp_cache_switch_to_blog_fallback() {
	global $wp_object_cache;

	$global_groups         = false;
	$non_persistent_groups = false;

	if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) && is_array( $wp_object_cache->global_groups ) ) {

		// Get the global groups as they are.
		$group_names = $wp_object_cache->global_groups;

		// Get global group keys if non-numeric array.
		if ( ! wp_is_numeric_array( $group_names ) ) {
			$group_names = array_keys( $group_names );
		}

		$global_groups = $group_names;

		/*
		 * Non-persistent groups: Check for no_mc_groups first (memcached drop-in).
		 * Fall back to cache structure (default cache).
		 */
		if ( isset( $wp_object_cache->no_mc_groups ) && is_array( $wp_object_cache->no_mc_groups ) && ! empty( $wp_object_cache->no_mc_groups ) ) {
			$non_persistent_groups = $wp_object_cache->no_mc_groups;
		} elseif ( isset( $wp_object_cache->cache ) && is_array( $wp_object_cache->cache ) ) {
			$all_groups            = array_keys( $wp_object_cache->cache );
			$non_persistent_groups = array_values( array_diff( $all_groups, $global_groups ) );
		}
	}

	wp_cache_init();

	if ( function_exists( 'wp_cache_add_global_groups' ) ) {
		if ( ! is_array( $global_groups ) || empty( $global_groups ) ) {
			$global_groups = array(
				'blog-details',
				'blog-id-cache',
				'blog-lookup',
				'blog_meta',
				'global-posts',
				'image_editor',
				'networks',
				'network-queries',
				'sites',
				'site-details',
				'site-options',
				'site-queries',
				'site-transient',
				'theme_files',
				'translation_files',
				'rss',
				'users',
				'user-queries',
				'user_meta',
				'useremail',
				'userlogins',
				'userslugs',
			);
		}

		wp_cache_add_global_groups( $global_groups );
	}

	if ( function_exists( 'wp_cache_add_non_persistent_groups' ) ) {
		if ( ! is_array( $non_persistent_groups ) || empty( $non_persistent_groups ) ) {
			$non_persistent_groups = array(
				'counts',
				'plugins',
				'theme_json',
			);
		}

		wp_cache_add_non_persistent_groups( $non_persistent_groups );
	}
}

Changelog

VersionDescription
7.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.