Ticket #40228: 40228.2.diff
File 40228.2.diff, 12.1 KB (added by , 7 years ago) |
---|
-
src/wp-includes/ms-blogs.php
110 110 * Retrieve the details for a blog from the blogs table and blog options. 111 111 * 112 112 * @since MU (3.0.0) 113 * 114 * @global wpdb $wpdb WordPress database abstraction object. 113 * @since 4.9.0 Use get_site_by() internally. 115 114 * 116 115 * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against. 117 116 * If not specified the current blog ID is used. … … 120 119 * @return WP_Site|false Blog details on success. False on failure. 121 120 */ 122 121 function get_blog_details( $fields = null, $get_all = true ) { 123 global $wpdb; 124 125 if ( is_array($fields ) ) { 126 if ( isset($fields['blog_id']) ) { 127 $blog_id = $fields['blog_id']; 128 } elseif ( isset($fields['domain']) && isset($fields['path']) ) { 129 $key = md5( $fields['domain'] . $fields['path'] ); 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) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) ); 136 } else { 137 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); 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 } 145 } elseif ( isset($fields['domain']) && is_subdomain_install() ) { 146 $key = md5( $fields['domain'] ); 147 $blog = wp_cache_get($key, 'blog-lookup'); 148 if ( false !== $blog ) 149 return $blog; 150 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { 151 $nowww = substr( $fields['domain'], 4 ); 152 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); 153 } else { 154 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); 155 } 156 if ( $blog ) { 157 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); 158 $blog_id = $blog->blog_id; 159 } else { 160 return false; 161 } 122 if ( is_array( $fields ) ) { 123 if ( isset( $fields['blog_id'] ) ) { 124 $field = 'id'; 125 $value = (int) $fields['blog_id']; 126 } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) { 127 $field = 'url'; 128 $value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' ); 129 } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) { 130 $field = 'domain'; 131 $value = $fields['domain']; 162 132 } else { 163 133 return false; 164 134 } 165 135 } else { 166 if ( ! $fields ) 167 $blog_id = get_current_blog_id(); 168 elseif ( ! is_numeric( $fields ) ) 169 $blog_id = get_id_from_blogname( $fields ); 170 else 171 $blog_id = $fields; 172 } 173 174 $blog_id = (int) $blog_id; 175 176 $all = $get_all == true ? '' : 'short'; 177 $details = wp_cache_get( $blog_id . $all, 'blog-details' ); 178 179 if ( $details ) { 180 if ( ! is_object( $details ) ) { 181 if ( $details == -1 ) { 182 return false; 183 } else { 184 // Clear old pre-serialized objects. Cache clients do better with that. 185 wp_cache_delete( $blog_id . $all, 'blog-details' ); 186 unset($details); 187 } 136 if ( ! $fields ) { 137 $field = 'id'; 138 $value = get_current_blog_id(); 139 } elseif ( ! is_numeric( $fields ) ) { 140 $field = 'slug'; 141 $value = $fields; 188 142 } else { 189 return $details; 143 $field = 'id'; 144 $value = (int) $fields; 190 145 } 191 146 } 192 147 193 // Try the other cache. 194 if ( $get_all ) { 195 $details = wp_cache_get( $blog_id . 'short', 'blog-details' ); 196 } else { 197 $details = wp_cache_get( $blog_id, 'blog-details' ); 198 // If short was requested and full cache is set, we can return. 199 if ( $details ) { 200 if ( ! is_object( $details ) ) { 201 if ( $details == -1 ) { 202 return false; 203 } else { 204 // Clear old pre-serialized objects. Cache clients do better with that. 205 wp_cache_delete( $blog_id, 'blog-details' ); 206 unset($details); 207 } 208 } else { 209 return $details; 210 } 211 } 212 } 213 214 if ( empty($details) ) { 215 $details = WP_Site::get_instance( $blog_id ); 216 if ( ! $details ) { 217 // Set the full cache. 218 wp_cache_set( $blog_id, -1, 'blog-details' ); 219 return false; 220 } 221 } 148 $site = get_site_by( $field, $value ); 222 149 223 if ( ! $ details instanceof WP_Site ) {224 $details = new WP_Site( $details );150 if ( ! $site ) { 151 return false; 225 152 } 226 153 227 if ( ! $get_all ) { 228 wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 229 return $details; 154 if ( $get_all ) { 155 // Prepopulate site details for backward compatibility. 156 foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) { 157 $site->$detail = $site->$detail; 158 } 230 159 } 231 160 232 switch_to_blog( $blog_id ); 233 $details->blogname = get_option( 'blogname' ); 234 $details->siteurl = get_option( 'siteurl' ); 235 $details->post_count = get_option( 'post_count' ); 236 $details->home = get_option( 'home' ); 237 restore_current_blog(); 238 239 /** 240 * Filters a blog's details. 241 * 242 * @since MU (3.0.0) 243 * @deprecated 4.7.0 Use site_details 244 * 245 * @param object $details The blog details. 246 */ 247 $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); 248 249 wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 250 251 $key = md5( $details->domain . $details->path ); 252 wp_cache_set( $key, $details, 'blog-lookup' ); 253 254 return $details; 161 return $site; 255 162 } 256 163 257 164 /** -
tests/phpunit/tests/multisite/site.php
118 118 // $get_all = false, only retrieve details from the blogs table 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. 139 138 foreach ( $wpdb->tables( 'blog', false ) as $table ) { … … 196 195 // Delete the site without forcing a table drop. 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 } 204 202 … … 234 232 // Delete the site and force a table drop. 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 } 242 239 … … 272 269 // Delete the site and force a table drop. 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 } 280 276 … … 390 386 // Prime the cache for an invalid site. 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. 397 393 self::factory()->blog->create(); 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 408 406 /** -
tests/phpunit/tests/multisite/siteDetails.php
32 32 * 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(); 56 37 … … 70 51 * 71 52 * @ticket 40063 72 53 */ 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 87 /**88 * @dataProvider data_whitelisted_options89 *90 * @ticket 4006391 */92 54 public function test_update_whitelisted_option_does_not_update_sites_last_changed( $whitelisted_option, $temporary_value ) { 93 55 $last_changed = wp_cache_get_last_changed( 'sites' ); 94 56