Skip to content

Commit 363445a

Browse files
committed
Multisite: Revert [41719].
While `get_site_by()` makes sense as a more explicit and less complex replacement for `get_blog_details()`, it is not ready yet in terms of caching, where it currently falls short of the older function under specific circumstances. See #40180, #40228. git-svn-id: https://develop.svn.wordpress.org/trunk@41883 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 652c632 commit 363445a

4 files changed

Lines changed: 182 additions & 56 deletions

File tree

src/wp-includes/class-wp-site.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,7 @@ private function get_details() {
330330
wp_cache_set( $this->blog_id, $details, 'site-details' );
331331
}
332332

333-
/**
334-
* Filters a blog's details.
335-
*
336-
* @since MU (3.0.0)
337-
* @deprecated 4.7.0 Use site_details
338-
*
339-
* @param object $details The blog details.
340-
*/
333+
/** This filter is documented in wp-includes/ms-blogs.php */
341334
$details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
342335

343336
/**

src/wp-includes/ms-blogs.php

Lines changed: 121 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ function get_id_from_blogname( $slug ) {
9393
* Retrieve the details for a blog from the blogs table and blog options.
9494
*
9595
* @since MU (3.0.0)
96-
* @since 4.9.0 Use get_site_by() internally.
96+
*
97+
* @global wpdb $wpdb WordPress database abstraction object.
9798
*
9899
* @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against.
99100
* If not specified the current blog ID is used.
@@ -102,46 +103,138 @@ function get_id_from_blogname( $slug ) {
102103
* @return WP_Site|false Blog details on success. False on failure.
103104
*/
104105
function get_blog_details( $fields = null, $get_all = true ) {
105-
if ( is_array( $fields ) ) {
106-
if ( isset( $fields['blog_id'] ) ) {
107-
$field = 'id';
108-
$value = (int) $fields['blog_id'];
109-
} elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
110-
$field = 'url';
111-
$value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' );
112-
} elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
113-
$field = 'domain';
114-
$value = $fields['domain'];
106+
global $wpdb;
107+
108+
if ( is_array($fields ) ) {
109+
if ( isset($fields['blog_id']) ) {
110+
$blog_id = $fields['blog_id'];
111+
} elseif ( isset($fields['domain']) && isset($fields['path']) ) {
112+
$key = md5( $fields['domain'] . $fields['path'] );
113+
$blog = wp_cache_get($key, 'blog-lookup');
114+
if ( false !== $blog )
115+
return $blog;
116+
if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
117+
$nowww = substr( $fields['domain'], 4 );
118+
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
119+
} else {
120+
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
121+
}
122+
if ( $blog ) {
123+
wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
124+
$blog_id = $blog->blog_id;
125+
} else {
126+
return false;
127+
}
128+
} elseif ( isset($fields['domain']) && is_subdomain_install() ) {
129+
$key = md5( $fields['domain'] );
130+
$blog = wp_cache_get($key, 'blog-lookup');
131+
if ( false !== $blog )
132+
return $blog;
133+
if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
134+
$nowww = substr( $fields['domain'], 4 );
135+
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
136+
} else {
137+
$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
138+
}
139+
if ( $blog ) {
140+
wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
141+
$blog_id = $blog->blog_id;
142+
} else {
143+
return false;
144+
}
115145
} else {
116146
return false;
117147
}
118148
} else {
119-
if ( ! $fields ) {
120-
$field = 'id';
121-
$value = get_current_blog_id();
122-
} elseif ( ! is_numeric( $fields ) ) {
123-
$field = 'slug';
124-
$value = $fields;
125-
} else {
126-
$field = 'id';
127-
$value = (int) $fields;
128-
}
149+
if ( ! $fields )
150+
$blog_id = get_current_blog_id();
151+
elseif ( ! is_numeric( $fields ) )
152+
$blog_id = get_id_from_blogname( $fields );
153+
else
154+
$blog_id = $fields;
129155
}
130156

131-
$site = get_site_by( $field, $value );
157+
$blog_id = (int) $blog_id;
132158

133-
if ( ! $site ) {
134-
return false;
159+
$all = $get_all == true ? '' : 'short';
160+
$details = wp_cache_get( $blog_id . $all, 'blog-details' );
161+
162+
if ( $details ) {
163+
if ( ! is_object( $details ) ) {
164+
if ( $details == -1 ) {
165+
return false;
166+
} else {
167+
// Clear old pre-serialized objects. Cache clients do better with that.
168+
wp_cache_delete( $blog_id . $all, 'blog-details' );
169+
unset($details);
170+
}
171+
} else {
172+
return $details;
173+
}
135174
}
136175

176+
// Try the other cache.
137177
if ( $get_all ) {
138-
// Prepopulate magic properties for backward compatibility.
139-
foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) {
140-
$site->$detail = $site->$detail;
178+
$details = wp_cache_get( $blog_id . 'short', 'blog-details' );
179+
} else {
180+
$details = wp_cache_get( $blog_id, 'blog-details' );
181+
// If short was requested and full cache is set, we can return.
182+
if ( $details ) {
183+
if ( ! is_object( $details ) ) {
184+
if ( $details == -1 ) {
185+
return false;
186+
} else {
187+
// Clear old pre-serialized objects. Cache clients do better with that.
188+
wp_cache_delete( $blog_id, 'blog-details' );
189+
unset($details);
190+
}
191+
} else {
192+
return $details;
193+
}
141194
}
142195
}
143196

144-
return $site;
197+
if ( empty($details) ) {
198+
$details = WP_Site::get_instance( $blog_id );
199+
if ( ! $details ) {
200+
// Set the full cache.
201+
wp_cache_set( $blog_id, -1, 'blog-details' );
202+
return false;
203+
}
204+
}
205+
206+
if ( ! $details instanceof WP_Site ) {
207+
$details = new WP_Site( $details );
208+
}
209+
210+
if ( ! $get_all ) {
211+
wp_cache_set( $blog_id . $all, $details, 'blog-details' );
212+
return $details;
213+
}
214+
215+
switch_to_blog( $blog_id );
216+
$details->blogname = get_option( 'blogname' );
217+
$details->siteurl = get_option( 'siteurl' );
218+
$details->post_count = get_option( 'post_count' );
219+
$details->home = get_option( 'home' );
220+
restore_current_blog();
221+
222+
/**
223+
* Filters a blog's details.
224+
*
225+
* @since MU (3.0.0)
226+
* @deprecated 4.7.0 Use site_details
227+
*
228+
* @param object $details The blog details.
229+
*/
230+
$details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
231+
232+
wp_cache_set( $blog_id . $all, $details, 'blog-details' );
233+
234+
$key = md5( $details->domain . $details->path );
235+
wp_cache_set( $key, $details, 'blog-lookup' );
236+
237+
return $details;
145238
}
146239

147240
/**

tests/phpunit/tests/multisite/site.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,22 @@ function test_created_site_details() {
118118
// $get_all = false, only retrieve details from the blogs table
119119
$details = get_blog_details( $blog_id, false );
120120

121-
$cached_details = wp_cache_get( $blog_id, 'sites' );
122-
$this->assertNotFalse( $cached_details );
123-
$this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) );
121+
// Combine domain and path for a site specific cache key.
122+
$key = md5( $details->domain . $details->path );
123+
124+
$this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
124125

125126
// get_blogaddress_by_name()
126127
$this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
127128

128-
// This is empty until get_blog_details() is called with $get_all = true
129-
$this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
129+
// These are empty until get_blog_details() is called with $get_all = true
130+
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
131+
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
130132

131133
// $get_all = true, populate the full blog-details cache and the blog slug lookup cache
132134
$details = get_blog_details( $blog_id, true );
133-
$cached_details = wp_cache_get( $blog_id, 'site-details' );
134-
$this->assertNotFalse( $cached_details );
135-
$this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) );
135+
$this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
136+
$this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
136137

137138
// Check existence of each database table for the created site.
138139
foreach ( $wpdb->tables( 'blog', false ) as $table ) {
@@ -195,8 +196,9 @@ function test_data_in_cache_after_wpmu_delete_blog_drop_false() {
195196
// Delete the site without forcing a table drop.
196197
wpmu_delete_blog( $blog_id, false );
197198

198-
$this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
199-
$this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
199+
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
200+
$this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
201+
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
200202
$this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
201203
}
202204

@@ -232,8 +234,9 @@ function test_data_in_cache_after_wpmu_delete_blog_drop_true() {
232234
// Delete the site and force a table drop.
233235
wpmu_delete_blog( $blog_id, true );
234236

235-
$this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
236-
$this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
237+
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
238+
$this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
239+
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
237240
$this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
238241
}
239242

@@ -269,8 +272,9 @@ function test_data_in_cache_after_wpmu_delete_blog_main_site_drop_true() {
269272
// Delete the site and force a table drop.
270273
wpmu_delete_blog( $blog_id, true );
271274

272-
$this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) );
273-
$this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) );
275+
$this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
276+
$this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
277+
$this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
274278
$this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) );
275279
}
276280

@@ -386,21 +390,19 @@ function test_get_blog_details_when_site_does_not_exist() {
386390
// Prime the cache for an invalid site.
387391
get_blog_details( $blog_id );
388392

389-
// When the cache is primed with an invalid site, the value is not set.
390-
$this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) );
393+
// When the cache is primed with an invalid site, the value is set to -1.
394+
$this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) );
391395

392396
// Create a site in the invalid site's place.
393397
self::factory()->blog->create();
394398

395399
// When a new site is created, its cache is cleared through refresh_blog_details.
396-
$this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) );
400+
$this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) );
397401

398402
$blog = get_blog_details( $blog_id );
399403

400404
// When the cache is refreshed, it should now equal the site data.
401-
$cached_blog = wp_cache_get( $blog_id, 'site-details' );
402-
$this->assertNotFalse( $cached_blog );
403-
$this->assertEqualSets( get_object_vars( $blog ), get_object_vars( $cached_blog ) );
405+
$this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) );
404406
}
405407

406408
/**

tests/phpunit/tests/multisite/siteDetails.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ public function test_update_whitelisted_option_deletes_site_details_cache( $whit
2727
$this->assertFalse( $cached_result );
2828
}
2929

30+
/**
31+
* @dataProvider data_whitelisted_options
32+
*
33+
* @ticket 40063
34+
*/
35+
public function test_update_whitelisted_option_deletes_blog_details_cache( $whitelisted_option, $temporary_value ) {
36+
$blog_details = get_blog_details();
37+
38+
$original_value = $blog_details->$whitelisted_option;
39+
update_option( $whitelisted_option, $temporary_value );
40+
41+
$cached_result = wp_cache_get( $blog_details->id, 'blog-details' );
42+
43+
/* Reset to original value. */
44+
update_option( $whitelisted_option, $original_value );
45+
46+
$this->assertFalse( $cached_result );
47+
}
48+
3049
/**
3150
* @dataProvider data_whitelisted_options
3251
*
@@ -46,6 +65,25 @@ public function test_update_whitelisted_option_does_not_delete_site_cache( $whit
4665
$this->assertNotFalse( $cached_result );
4766
}
4867

68+
/**
69+
* @dataProvider data_whitelisted_options
70+
*
71+
* @ticket 40063
72+
*/
73+
public function test_update_whitelisted_option_does_not_delete_short_blog_details_cache( $whitelisted_option, $temporary_value ) {
74+
$blog_details = get_blog_details( null, false );
75+
76+
$original_value = get_option( $whitelisted_option );
77+
update_option( $whitelisted_option, $temporary_value );
78+
79+
$cached_result = wp_cache_get( $blog_details->id . 'short', 'blog-details' );
80+
81+
/* Reset to original value. */
82+
update_option( $whitelisted_option, $original_value );
83+
84+
$this->assertNotFalse( $cached_result );
85+
}
86+
4987
/**
5088
* @dataProvider data_whitelisted_options
5189
*

0 commit comments

Comments
 (0)