Changeset 41719
- Timestamp:
- 10/03/2017 07:40:01 PM (7 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-site.php
r41162 r41719 331 331 } 332 332 333 /** This filter is documented in wp-includes/ms-blogs.php */ 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 */ 334 341 $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); 335 342 -
trunk/src/wp-includes/ms-blogs.php
r41717 r41719 109 109 * 110 110 * @since MU (3.0.0) 111 * 112 * @global wpdb $wpdb WordPress database abstraction object. 111 * @since 4.9.0 Use get_site_by() internally. 113 112 * 114 113 * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against. … … 119 118 */ 120 119 function get_blog_details( $fields = null, $get_all = true ) { 121 global $wpdb; 122 123 if ( is_array($fields ) ) { 124 if ( isset($fields['blog_id']) ) { 125 $blog_id = $fields['blog_id']; 126 } elseif ( isset($fields['domain']) && isset($fields['path']) ) { 127 $key = md5( $fields['domain'] . $fields['path'] ); 128 $blog = wp_cache_get($key, 'blog-lookup'); 129 if ( false !== $blog ) 130 return $blog; 131 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { 132 $nowww = substr( $fields['domain'], 4 ); 133 $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'] ) ); 134 } else { 135 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); 136 } 137 if ( $blog ) { 138 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); 139 $blog_id = $blog->blog_id; 140 } else { 141 return false; 142 } 143 } elseif ( isset($fields['domain']) && is_subdomain_install() ) { 144 $key = md5( $fields['domain'] ); 145 $blog = wp_cache_get($key, 'blog-lookup'); 146 if ( false !== $blog ) 147 return $blog; 148 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { 149 $nowww = substr( $fields['domain'], 4 ); 150 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); 151 } else { 152 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); 153 } 154 if ( $blog ) { 155 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); 156 $blog_id = $blog->blog_id; 157 } else { 158 return false; 159 } 120 if ( is_array( $fields ) ) { 121 if ( isset( $fields['blog_id'] ) ) { 122 $field = 'id'; 123 $value = (int) $fields['blog_id']; 124 } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) { 125 $field = 'url'; 126 $value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' ); 127 } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) { 128 $field = 'domain'; 129 $value = $fields['domain']; 160 130 } else { 161 131 return false; 162 132 } 163 133 } else { 164 if ( ! $fields ) 165 $blog_id = get_current_blog_id(); 166 elseif ( ! is_numeric( $fields ) ) 167 $blog_id = get_id_from_blogname( $fields ); 168 else 169 $blog_id = $fields; 170 } 171 172 $blog_id = (int) $blog_id; 173 174 $all = $get_all == true ? '' : 'short'; 175 $details = wp_cache_get( $blog_id . $all, 'blog-details' ); 176 177 if ( $details ) { 178 if ( ! is_object( $details ) ) { 179 if ( $details == -1 ) { 180 return false; 181 } else { 182 // Clear old pre-serialized objects. Cache clients do better with that. 183 wp_cache_delete( $blog_id . $all, 'blog-details' ); 184 unset($details); 185 } 134 if ( ! $fields ) { 135 $field = 'id'; 136 $value = get_current_blog_id(); 137 } elseif ( ! is_numeric( $fields ) ) { 138 $field = 'slug'; 139 $value = $fields; 186 140 } else { 187 return $details; 188 } 189 } 190 191 // Try the other cache. 141 $field = 'id'; 142 $value = (int) $fields; 143 } 144 } 145 146 $site = get_site_by( $field, $value ); 147 148 if ( ! $site ) { 149 return false; 150 } 151 192 152 if ( $get_all ) { 193 $details = wp_cache_get( $blog_id . 'short', 'blog-details' ); 194 } else { 195 $details = wp_cache_get( $blog_id, 'blog-details' ); 196 // If short was requested and full cache is set, we can return. 197 if ( $details ) { 198 if ( ! is_object( $details ) ) { 199 if ( $details == -1 ) { 200 return false; 201 } else { 202 // Clear old pre-serialized objects. Cache clients do better with that. 203 wp_cache_delete( $blog_id, 'blog-details' ); 204 unset($details); 205 } 206 } else { 207 return $details; 208 } 209 } 210 } 211 212 if ( empty($details) ) { 213 $details = WP_Site::get_instance( $blog_id ); 214 if ( ! $details ) { 215 // Set the full cache. 216 wp_cache_set( $blog_id, -1, 'blog-details' ); 217 return false; 218 } 219 } 220 221 if ( ! $details instanceof WP_Site ) { 222 $details = new WP_Site( $details ); 223 } 224 225 if ( ! $get_all ) { 226 wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 227 return $details; 228 } 229 230 switch_to_blog( $blog_id ); 231 $details->blogname = get_option( 'blogname' ); 232 $details->siteurl = get_option( 'siteurl' ); 233 $details->post_count = get_option( 'post_count' ); 234 $details->home = get_option( 'home' ); 235 restore_current_blog(); 236 237 /** 238 * Filters a blog's details. 239 * 240 * @since MU (3.0.0) 241 * @deprecated 4.7.0 Use site_details 242 * 243 * @param object $details The blog details. 244 */ 245 $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); 246 247 wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 248 249 $key = md5( $details->domain . $details->path ); 250 wp_cache_set( $key, $details, 'blog-lookup' ); 251 252 return $details; 153 // Prepopulate magic properties for backward compatibility. 154 foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) { 155 $site->$detail = $site->$detail; 156 } 157 } 158 159 return $site; 253 160 } 254 161 -
trunk/tests/phpunit/tests/multisite/site.php
r41716 r41719 119 119 $details = get_blog_details( $blog_id, false ); 120 120 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' ) ); 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 ) ); 125 124 126 125 // get_blogaddress_by_name() 127 126 $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) ); 128 127 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' ) ); 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' ) ); 132 130 133 131 // $get_all = true, populate the full blog-details cache and the blog slug lookup cache 134 132 $details = get_blog_details( $blog_id, true ); 135 $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) ); 136 $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) ); 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 ) ); 137 136 138 137 // Check existence of each database table for the created site. … … 197 196 wpmu_delete_blog( $blog_id, false ); 198 197 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' ) ); 198 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); 199 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 202 200 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 203 201 } … … 235 233 wpmu_delete_blog( $blog_id, true ); 236 234 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' ) ); 235 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); 236 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 240 237 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 241 238 } … … 273 270 wpmu_delete_blog( $blog_id, true ); 274 271 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' ) ); 272 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); 273 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 278 274 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 279 275 } … … 391 387 get_blog_details( $blog_id ); 392 388 393 // When the cache is primed with an invalid site, the value is set to -1.394 $this->assert Equals( -1, wp_cache_get( $blog_id, 'blog-details' ) );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' ) ); 395 391 396 392 // Create a site in the invalid site's place. … … 398 394 399 395 // When a new site is created, its cache is cleared through refresh_blog_details. 400 $this->assertFalse( wp_cache_get( $blog_id, ' blog-details' ) );396 $this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) ); 401 397 402 398 $blog = get_blog_details( $blog_id ); 403 399 404 400 // When the cache is refreshed, it should now equal the site data. 405 $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) ); 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 ) ); 406 404 } 407 405 -
trunk/tests/phpunit/tests/multisite/siteDetails.php
r40478 r41719 33 33 * @ticket 40063 34 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 49 /**50 * @dataProvider data_whitelisted_options51 *52 * @ticket 4006353 */54 35 public function test_update_whitelisted_option_does_not_delete_site_cache( $whitelisted_option, $temporary_value ) { 55 36 $site = get_site(); … … 59 40 60 41 $cached_result = wp_cache_get( $site->id, 'sites' ); 61 62 /* Reset to original value. */63 update_option( $whitelisted_option, $original_value );64 65 $this->assertNotFalse( $cached_result );66 }67 68 /**69 * @dataProvider data_whitelisted_options70 *71 * @ticket 4006372 */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 42 81 43 /* Reset to original value. */
Note: See TracChangeset
for help on using the changeset viewer.