Skip to content

Commit 2223743

Browse files
committed
fix: Fixes the broken caching issue in get_blog_details()
When get_blog_details() is called with $get_all=true first, the full result is cached. A subsequent call with $get_all=false incorrectly returns the full cached result instead of a short one. The "try the other cache" block now discards the full cache hit when short is requested, allowing WP_Site::get_instance() to build a clean short result without an extra DB query. Ref: https://core.trac.wordpress.org/ticket/63518#ticket
1 parent fdafcd3 commit 2223743

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/wp-includes/ms-blogs.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ function get_blog_details( $fields = null, $get_all = true ) {
209209
$details = wp_cache_get( $blog_id . 'short', 'blog-details' );
210210
} else {
211211
$details = wp_cache_get( $blog_id, 'blog-details' );
212-
// If short was requested and full cache is set, we can return.
213212
if ( $details ) {
214213
if ( ! is_object( $details ) ) {
215214
if ( -1 === $details ) {
@@ -220,7 +219,9 @@ function get_blog_details( $fields = null, $get_all = true ) {
220219
unset( $details );
221220
}
222221
} else {
223-
return $details;
222+
// Full cache is set but short was requested. Discard so a clean
223+
// short result is built from WP_Site::get_instance() below.
224+
unset( $details );
224225
}
225226
}
226227
}

tests/phpunit/tests/multisite/getBlogDetails.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,48 @@ public function test_get_blog_details_iterate_over_result( $get_all ) {
193193
$this->assertSameSets( $this->get_fields( $get_all ), $result );
194194
}
195195

196+
/**
197+
* @ticket 63518
198+
*/
199+
public function test_get_blog_details_get_all_true_then_false_returns_short() {
200+
$site_id = self::$site_ids['wordpress.org/'];
201+
202+
// Ensure a clean cache state.
203+
wp_cache_delete( $site_id, 'blog-details' );
204+
wp_cache_delete( $site_id . 'short', 'blog-details' );
205+
206+
// Populate the full cache first.
207+
$full = get_blog_details( $site_id, true );
208+
$this->assertSameSets( $this->get_fields( true ), array_keys( get_object_vars( $full ) ) );
209+
210+
// A subsequent short request must not return the full result.
211+
$short = get_blog_details( $site_id, false );
212+
$this->assertSameSets( $this->get_fields( false ), array_keys( get_object_vars( $short ) ) );
213+
}
214+
215+
/**
216+
* @ticket 63518
217+
*/
218+
public function test_get_blog_details_false_true_false_returns_correct_fields() {
219+
$site_id = self::$site_ids['wordpress.org/'];
220+
221+
// Ensure a clean cache state.
222+
wp_cache_delete( $site_id, 'blog-details' );
223+
wp_cache_delete( $site_id . 'short', 'blog-details' );
224+
225+
// Short first.
226+
$short1 = get_blog_details( $site_id, false );
227+
$this->assertSameSets( $this->get_fields( false ), array_keys( get_object_vars( $short1 ) ) );
228+
229+
// Full second.
230+
$full = get_blog_details( $site_id, true );
231+
$this->assertSameSets( $this->get_fields( true ), array_keys( get_object_vars( $full ) ) );
232+
233+
// Short again — must still return short fields.
234+
$short2 = get_blog_details( $site_id, false );
235+
$this->assertSameSets( $this->get_fields( false ), array_keys( get_object_vars( $short2 ) ) );
236+
}
237+
196238
public function data_get_all() {
197239
return array(
198240
array( false ),

0 commit comments

Comments
 (0)