Skip to content

Commit d45dc6b

Browse files
committed
Cache API: Handle numerically-keyed global_groups when switching sites.
This commit protects against another multisite cache edge-case where a persistent object-cache drop-in plugin (namely memcached) may use a numerically-keyed `global_groups` array instead of `'key' => true` like the default object-cache class, and includes the following changes: * Use `wp_is_numeric_array()` inside of `wp_cache_switch_to_blog_fallback()` so that the global group names array are always properly formatted regardless of the caching back-end in use * Add private helper methods to `Tests_Multisite_WpCacheSwitchToBlogFallback` to properly format global group names, and tweak a few tests to make them more resilient to different caching back-ends Follow up to r61760. See #23290. git-svn-id: https://develop.svn.wordpress.org/trunk@61772 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5ce0e86 commit d45dc6b

2 files changed

Lines changed: 130 additions & 79 deletions

File tree

src/wp-includes/ms-blogs.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,28 @@ function wp_cache_switch_to_blog_fallback() {
614614
$global_groups = false;
615615
$non_persistent_groups = false;
616616

617-
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
618-
$global_groups = array_keys( $wp_object_cache->global_groups );
619-
$all_groups = array_fill_keys( array_keys( $wp_object_cache->cache ), true );
620-
$non_persistent_groups = array_keys( array_diff_key( $all_groups, $wp_object_cache->global_groups ) );
617+
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) && is_array( $wp_object_cache->global_groups ) ) {
618+
619+
// Get the global groups as they are.
620+
$group_names = $wp_object_cache->global_groups;
621+
622+
// Get global group keys if non-numeric array.
623+
if ( ! wp_is_numeric_array( $group_names ) ) {
624+
$group_names = array_keys( $group_names );
625+
}
626+
627+
$global_groups = $group_names;
628+
629+
/*
630+
* Non-persistent groups: Check for no_mc_groups first (memcached drop-in).
631+
* Fall back to cache structure (default cache).
632+
*/
633+
if ( isset( $wp_object_cache->no_mc_groups ) && is_array( $wp_object_cache->no_mc_groups ) && ! empty( $wp_object_cache->no_mc_groups ) ) {
634+
$non_persistent_groups = $wp_object_cache->no_mc_groups;
635+
} elseif ( isset( $wp_object_cache->cache ) && is_array( $wp_object_cache->cache ) ) {
636+
$all_groups = array_keys( $wp_object_cache->cache );
637+
$non_persistent_groups = array_values( array_diff( $all_groups, $global_groups ) );
638+
}
621639
}
622640

623641
wp_cache_init();

0 commit comments

Comments
 (0)