From b077123a9892c5b8f40d6f937ed60e78d22ff3e9 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Wed, 29 Jun 2016 19:32:27 +0000 Subject: [PATCH] Multisite: Lazy load extended `WP_Site` properties when requested. In the past, `get_blog_details()` has been used to retrieve the `home`, `siteurl`, `blogname`, and `post_count` options for a site. By lazy loading properties in a `WP_Site` object, we can avoid having to use `get_blog_details()` and instead provide the properties as needed. This introduces the global `site-details` cache group in which standard objects representing the site are stored. This will one day be a replacement for the `blog-details` cache group that is currently used in `get_blog_details()`. This relies on the `ms_loaded` action introduced in [37916] as properties are not available via `get_option()` until multisite has been fully loaded. Props flixos90. Fixes #36935. Built from https://develop.svn.wordpress.org/trunk@37918 git-svn-id: http://core.svn.wordpress.org/trunk@37859 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-site.php | 73 +++++++++++++++++++++++++++++++++++ wp-includes/load.php | 2 +- wp-includes/ms-blogs.php | 5 ++- wp-includes/version.php | 2 +- 4 files changed, 78 insertions(+), 4 deletions(-) diff --git a/wp-includes/class-wp-site.php b/wp-includes/class-wp-site.php index 4aac6c1030..d839c59600 100644 --- a/wp-includes/class-wp-site.php +++ b/wp-includes/class-wp-site.php @@ -222,6 +222,7 @@ final class WP_Site { * Getter. * * Allows current multisite naming conventions when getting properties. + * Allows access to extended site properties. * * @since 4.6.0 * @access public @@ -239,6 +240,15 @@ final class WP_Site { return $this->site_id; case 'network_id': return (int) $this->site_id; + case 'blogname': + case 'siteurl': + case 'post_count': + case 'home': + if ( ! did_action( 'ms_loaded' ) ) { + return null; + } + $details = $this->get_details(); + return $details->$key; } return null; @@ -248,6 +258,7 @@ final class WP_Site { * Isset-er. * * Allows current multisite naming conventions when checking for properties. + * Checks for extended site properties. * * @since 4.6.0 * @access public @@ -262,6 +273,14 @@ final class WP_Site { case 'site_id': case 'network_id': return true; + case 'blogname': + case 'siteurl': + case 'post_count': + case 'home': + if ( ! did_action( 'ms_loaded' ) ) { + return false; + } + return true; } return false; @@ -292,4 +311,58 @@ final class WP_Site { $this->$key = $value; } } + + /** + * Retrieve the details for this site. + * + * This method is used internally to lazy-load the extended properties of a site. + * + * @since 4.6.0 + * @access private + * + * @see WP_Site::__get() + * + * @return object A raw site object with all details included. + */ + private function get_details() { + $details = wp_cache_get( $this->blog_id, 'site-details' ); + + if ( false === $details ) { + + switch_to_blog( $this->blog_id ); + // Create a raw copy of the object for backwards compatibility with the filter below. + $details = new stdClass(); + foreach ( get_object_vars( $this ) as $key => $value ) { + $details->$key = $value; + } + $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(); + + $cache_details = true; + foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $field ) { + if ( false === $details->$field ) { + $cache_details = false; + break; + } + } + + if ( $cache_details ) { + wp_cache_set( $this->blog_id, $details, 'site-details' ); + } + } + + /** + * Filters a site's extended properties. + * + * @since 4.6.0 + * + * @param object $details The site details. + */ + $details = apply_filters( 'site_details', $details ); + + return $details; + } } diff --git a/wp-includes/load.php b/wp-includes/load.php index 80654f1bff..62d033a7c7 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -509,7 +509,7 @@ function wp_start_object_cache() { wp_cache_init(); if ( function_exists( 'wp_cache_add_global_groups' ) ) { - wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) ); + wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) ); wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); } } diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index 3032f2e031..cb1c89af57 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -455,6 +455,7 @@ function clean_blog_cache( $blog ) { $domain_path_key = md5( $blog->domain . $blog->path ); wp_cache_delete( $blog_id, 'sites' ); + wp_cache_delete( $blog_id, 'site-details' ); wp_cache_delete( $blog_id , 'blog-details' ); wp_cache_delete( $blog_id . 'short' , 'blog-details' ); wp_cache_delete( $domain_path_key, 'blog-lookup' ); @@ -822,7 +823,7 @@ function switch_to_blog( $new_blog, $deprecated = null ) { if ( is_array( $global_groups ) ) { wp_cache_add_global_groups( $global_groups ); } else { - wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) ); + wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); } wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); } @@ -893,7 +894,7 @@ function restore_current_blog() { if ( is_array( $global_groups ) ) { wp_cache_add_global_groups( $global_groups ); } else { - wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) ); + wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); } wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 7ca026306c..e8232e19f7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.6-alpha-37917'; +$wp_version = '4.6-alpha-37918'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.