Ticket #40228: 40228.diff
File 40228.diff, 11.8 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 113 * 114 * @global wpdb $wpdb WordPress database abstraction object. 113 * @since 4.8.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; 122 $field = $fields; 123 $value = null; 124 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 } 125 if ( ! is_array( $field ) ) { 126 if ( ! $field ) { 127 $field = 'id'; 128 } elseif ( is_numeric( $field ) ) { 129 $value = $field; 130 $field = 'id'; 162 131 } else { 163 return false; 132 $value = $field; 133 $field = 'slug'; 164 134 } 165 } 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; 135 } elseif ( isset( $field['blog_id'] ) ) { 136 $field['id'] = $field['blog_id']; 137 unset( $field['blog_id'] ); 172 138 } 173 139 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 } 188 } else { 189 return $details; 190 } 191 } 140 $site = get_site_by( $field, $value ); 192 141 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 } 222 223 if ( ! $details instanceof WP_Site ) { 224 $details = new WP_Site( $details ); 142 if ( ! $site ) { 143 return false; 225 144 } 226 145 227 146 if ( ! $get_all ) { 228 wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 229 return $details; 147 return $site; 230 148 } 231 149 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 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' ); 150 /** Prepopulate site details for backward compatibility. */ 151 foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) { 152 $site->$detail = $site->$detail; 153 } 253 154 254 return $ details;155 return $site; 255 156 } 256 157 257 158 /** -
tests/phpunit/tests/multisite/site.php
81 81 82 82 // $get_all = false, only retrieve details from the blogs table 83 83 $details = get_blog_details( $blog_id, false ); 84 85 // Combine domain and path for a site specific cache key. 86 $key = md5( $details->domain . $details->path ); 87 88 $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); 84 $cached_details = wp_cache_get( $blog_id, 'sites' ); 85 $this->assertNotFalse( $cached_details ); 86 $this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) ); 89 87 90 88 // get_blogaddress_by_name() 91 89 $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) ); 92 90 93 // These are empty until get_blog_details() is called with $get_all = true 94 $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); 95 $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); 91 // This is empty until get_blog_details() is called with $get_all = true 92 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 96 93 97 94 // $get_all = true, populate the full blog-details cache and the blog slug lookup cache 98 95 $details = get_blog_details( $blog_id, true ); 99 $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) ); 100 $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) ); 96 $cached_details = wp_cache_get( $blog_id, 'site-details' ); 97 $this->assertNotFalse( $cached_details ); 98 $this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) ); 101 99 102 100 // Check existence of each database table for the created site. 103 101 foreach ( $wpdb->tables( 'blog', false ) as $table ) { … … 134 132 // Delete the site without forcing a table drop. 135 133 wpmu_delete_blog( $blog_id, false ); 136 134 137 $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); 138 $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); 139 $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); 140 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 135 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); 136 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 141 137 } 142 138 143 139 /** … … 172 168 // Delete the site and force a table drop. 173 169 wpmu_delete_blog( $blog_id, true ); 174 170 175 $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); 176 $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); 177 $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); 178 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 171 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); 172 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 179 173 } 180 174 181 175 /** … … 210 204 // Delete the site and force a table drop. 211 205 wpmu_delete_blog( $blog_id, true ); 212 206 213 $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); 214 $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); 215 $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); 216 $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); 207 $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); 208 $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); 217 209 } 218 210 219 211 /** … … 328 320 // Prime the cache for an invalid site. 329 321 get_blog_details( $blog_id ); 330 322 331 // When the cache is primed with an invalid site, the value is set to -1.332 $this->assert Equals( -1, wp_cache_get( $blog_id, 'blog-details' ) );323 // When the cache is primed with an invalid site, the value is not set. 324 $this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) ); 333 325 334 326 // Create a site in the invalid site's place. 335 327 self::factory()->blog->create(); 336 328 337 329 // When a new site is created, its cache is cleared through refresh_blog_details. 338 $this->assertFalse( wp_cache_get( $blog_id, ' blog-details' ) );330 $this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) ); 339 331 340 332 $blog = get_blog_details( $blog_id ); 341 333 342 334 // When the cache is refreshed, it should now equal the site data. 343 $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) ); 335 $cached_blog = wp_cache_get( $blog_id, 'site-details' ); 336 $this->assertNotFalse( $cached_blog ); 337 $this->assertEqualSets( get_object_vars( $blog ), get_object_vars( $cached_blog ) ); 344 338 } 345 339 346 340 /** -
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 … … 61 42 62 43 /* Reset to original value. */ 63 44 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 81 /* Reset to original value. */82 update_option( $whitelisted_option, $original_value );83 45 84 46 $this->assertNotFalse( $cached_result ); 85 47 }