diff --git a/src/wp-includes/class-wp-site.php b/src/wp-includes/class-wp-site.php index f438264370..5bfc6430f2 100644 --- a/src/wp-includes/class-wp-site.php +++ b/src/wp-includes/class-wp-site.php @@ -330,7 +330,14 @@ final class WP_Site { wp_cache_set( $this->blog_id, $details, 'site-details' ); } - /** This filter is documented in wp-includes/ms-blogs.php */ + /** + * Filters a blog's details. + * + * @since MU (3.0.0) + * @deprecated 4.7.0 Use site_details + * + * @param object $details The blog details. + */ $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); /** diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index e1f9c14d6f..f25fa6ec6a 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -108,8 +108,7 @@ function get_id_from_blogname( $slug ) { * Retrieve the details for a blog from the blogs table and blog options. * * @since MU (3.0.0) - * - * @global wpdb $wpdb WordPress database abstraction object. + * @since 4.9.0 Use get_site_by() internally. * * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against. * If not specified the current blog ID is used. @@ -118,138 +117,46 @@ function get_id_from_blogname( $slug ) { * @return WP_Site|false Blog details on success. False on failure. */ function get_blog_details( $fields = null, $get_all = true ) { - global $wpdb; - - if ( is_array($fields ) ) { - if ( isset($fields['blog_id']) ) { - $blog_id = $fields['blog_id']; - } elseif ( isset($fields['domain']) && isset($fields['path']) ) { - $key = md5( $fields['domain'] . $fields['path'] ); - $blog = wp_cache_get($key, 'blog-lookup'); - if ( false !== $blog ) - return $blog; - if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { - $nowww = substr( $fields['domain'], 4 ); - $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'] ) ); - } else { - $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); - } - if ( $blog ) { - wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); - $blog_id = $blog->blog_id; - } else { - return false; - } - } elseif ( isset($fields['domain']) && is_subdomain_install() ) { - $key = md5( $fields['domain'] ); - $blog = wp_cache_get($key, 'blog-lookup'); - if ( false !== $blog ) - return $blog; - if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { - $nowww = substr( $fields['domain'], 4 ); - $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); - } else { - $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); - } - if ( $blog ) { - wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); - $blog_id = $blog->blog_id; - } else { - return false; - } + if ( is_array( $fields ) ) { + if ( isset( $fields['blog_id'] ) ) { + $field = 'id'; + $value = (int) $fields['blog_id']; + } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) { + $field = 'url'; + $value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' ); + } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) { + $field = 'domain'; + $value = $fields['domain']; } else { return false; } } else { - if ( ! $fields ) - $blog_id = get_current_blog_id(); - elseif ( ! is_numeric( $fields ) ) - $blog_id = get_id_from_blogname( $fields ); - else - $blog_id = $fields; - } - - $blog_id = (int) $blog_id; - - $all = $get_all == true ? '' : 'short'; - $details = wp_cache_get( $blog_id . $all, 'blog-details' ); - - if ( $details ) { - if ( ! is_object( $details ) ) { - if ( $details == -1 ) { - return false; - } else { - // Clear old pre-serialized objects. Cache clients do better with that. - wp_cache_delete( $blog_id . $all, 'blog-details' ); - unset($details); - } + if ( ! $fields ) { + $field = 'id'; + $value = get_current_blog_id(); + } elseif ( ! is_numeric( $fields ) ) { + $field = 'slug'; + $value = $fields; } else { - return $details; + $field = 'id'; + $value = (int) $fields; } } - // Try the other cache. + $site = get_site_by( $field, $value ); + + if ( ! $site ) { + return false; + } + if ( $get_all ) { - $details = wp_cache_get( $blog_id . 'short', 'blog-details' ); - } else { - $details = wp_cache_get( $blog_id, 'blog-details' ); - // If short was requested and full cache is set, we can return. - if ( $details ) { - if ( ! is_object( $details ) ) { - if ( $details == -1 ) { - return false; - } else { - // Clear old pre-serialized objects. Cache clients do better with that. - wp_cache_delete( $blog_id, 'blog-details' ); - unset($details); - } - } else { - return $details; - } + // Prepopulate magic properties for backward compatibility. + foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) { + $site->$detail = $site->$detail; } } - if ( empty($details) ) { - $details = WP_Site::get_instance( $blog_id ); - if ( ! $details ) { - // Set the full cache. - wp_cache_set( $blog_id, -1, 'blog-details' ); - return false; - } - } - - if ( ! $details instanceof WP_Site ) { - $details = new WP_Site( $details ); - } - - if ( ! $get_all ) { - wp_cache_set( $blog_id . $all, $details, 'blog-details' ); - return $details; - } - - switch_to_blog( $blog_id ); - $details->blogname = get_option( 'blogname' ); - $details->siteurl = get_option( 'siteurl' ); - $details->post_count = get_option( 'post_count' ); - $details->home = get_option( 'home' ); - restore_current_blog(); - - /** - * Filters a blog's details. - * - * @since MU (3.0.0) - * @deprecated 4.7.0 Use site_details - * - * @param object $details The blog details. - */ - $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); - - wp_cache_set( $blog_id . $all, $details, 'blog-details' ); - - $key = md5( $details->domain . $details->path ); - wp_cache_set( $key, $details, 'blog-lookup' ); - - return $details; + return $site; } /** diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index d0b460b752..0804778c55 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -118,22 +118,21 @@ class Tests_Multisite_Site extends WP_UnitTestCase { // $get_all = false, only retrieve details from the blogs table $details = get_blog_details( $blog_id, false ); - // Combine domain and path for a site specific cache key. - $key = md5( $details->domain . $details->path ); - - $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); + $cached_details = wp_cache_get( $blog_id, 'sites' ); + $this->assertNotFalse( $cached_details ); + $this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) ); // get_blogaddress_by_name() $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) ); - // These are empty until get_blog_details() is called with $get_all = true - $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); + // This is empty until get_blog_details() is called with $get_all = true + $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); // $get_all = true, populate the full blog-details cache and the blog slug lookup cache $details = get_blog_details( $blog_id, true ); - $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) ); - $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) ); + $cached_details = wp_cache_get( $blog_id, 'site-details' ); + $this->assertNotFalse( $cached_details ); + $this->assertEqualSets( get_object_vars( $details ), get_object_vars( $cached_details ) ); // Check existence of each database table for the created site. foreach ( $wpdb->tables( 'blog', false ) as $table ) { @@ -196,9 +195,8 @@ class Tests_Multisite_Site extends WP_UnitTestCase { // Delete the site without forcing a table drop. wpmu_delete_blog( $blog_id, false ); - $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); + $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); + $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); } @@ -234,9 +232,8 @@ class Tests_Multisite_Site extends WP_UnitTestCase { // Delete the site and force a table drop. wpmu_delete_blog( $blog_id, true ); - $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); + $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); + $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); } @@ -272,9 +269,8 @@ class Tests_Multisite_Site extends WP_UnitTestCase { // Delete the site and force a table drop. wpmu_delete_blog( $blog_id, true ); - $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $blog_id . 'short', 'blog-details' ) ); - $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) ); + $this->assertEquals( false, wp_cache_get( $blog_id, 'sites' ) ); + $this->assertEquals( false, wp_cache_get( $blog_id, 'site-details' ) ); $this->assertEquals( false, wp_cache_get( $key, 'blog-id-cache' ) ); } @@ -390,19 +386,21 @@ class Tests_Multisite_Site extends WP_UnitTestCase { // Prime the cache for an invalid site. get_blog_details( $blog_id ); - // When the cache is primed with an invalid site, the value is set to -1. - $this->assertEquals( -1, wp_cache_get( $blog_id, 'blog-details' ) ); + // When the cache is primed with an invalid site, the value is not set. + $this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) ); // Create a site in the invalid site's place. self::factory()->blog->create(); // When a new site is created, its cache is cleared through refresh_blog_details. - $this->assertFalse( wp_cache_get( $blog_id, 'blog-details' ) ); + $this->assertFalse( wp_cache_get( $blog_id, 'site-details' ) ); $blog = get_blog_details( $blog_id ); // When the cache is refreshed, it should now equal the site data. - $this->assertEquals( $blog, wp_cache_get( $blog_id, 'blog-details' ) ); + $cached_blog = wp_cache_get( $blog_id, 'site-details' ); + $this->assertNotFalse( $cached_blog ); + $this->assertEqualSets( get_object_vars( $blog ), get_object_vars( $cached_blog ) ); } /** diff --git a/tests/phpunit/tests/multisite/siteDetails.php b/tests/phpunit/tests/multisite/siteDetails.php index bce6aaead4..5a64fb9df1 100644 --- a/tests/phpunit/tests/multisite/siteDetails.php +++ b/tests/phpunit/tests/multisite/siteDetails.php @@ -27,25 +27,6 @@ class Tests_Multisite_Site_Details extends WP_UnitTestCase { $this->assertFalse( $cached_result ); } - /** - * @dataProvider data_whitelisted_options - * - * @ticket 40063 - */ - public function test_update_whitelisted_option_deletes_blog_details_cache( $whitelisted_option, $temporary_value ) { - $blog_details = get_blog_details(); - - $original_value = $blog_details->$whitelisted_option; - update_option( $whitelisted_option, $temporary_value ); - - $cached_result = wp_cache_get( $blog_details->id, 'blog-details' ); - - /* Reset to original value. */ - update_option( $whitelisted_option, $original_value ); - - $this->assertFalse( $cached_result ); - } - /** * @dataProvider data_whitelisted_options * @@ -65,25 +46,6 @@ class Tests_Multisite_Site_Details extends WP_UnitTestCase { $this->assertNotFalse( $cached_result ); } - /** - * @dataProvider data_whitelisted_options - * - * @ticket 40063 - */ - public function test_update_whitelisted_option_does_not_delete_short_blog_details_cache( $whitelisted_option, $temporary_value ) { - $blog_details = get_blog_details( null, false ); - - $original_value = get_option( $whitelisted_option ); - update_option( $whitelisted_option, $temporary_value ); - - $cached_result = wp_cache_get( $blog_details->id . 'short', 'blog-details' ); - - /* Reset to original value. */ - update_option( $whitelisted_option, $original_value ); - - $this->assertNotFalse( $cached_result ); - } - /** * @dataProvider data_whitelisted_options *