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
This commit is contained in:
Jeremy Felt 2016-06-29 19:32:27 +00:00
parent b4a1b39a95
commit b077123a98
4 changed files with 78 additions and 4 deletions

View File

@ -222,6 +222,7 @@ final class WP_Site {
* Getter. * Getter.
* *
* Allows current multisite naming conventions when getting properties. * Allows current multisite naming conventions when getting properties.
* Allows access to extended site properties.
* *
* @since 4.6.0 * @since 4.6.0
* @access public * @access public
@ -239,6 +240,15 @@ final class WP_Site {
return $this->site_id; return $this->site_id;
case 'network_id': case 'network_id':
return (int) $this->site_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; return null;
@ -248,6 +258,7 @@ final class WP_Site {
* Isset-er. * Isset-er.
* *
* Allows current multisite naming conventions when checking for properties. * Allows current multisite naming conventions when checking for properties.
* Checks for extended site properties.
* *
* @since 4.6.0 * @since 4.6.0
* @access public * @access public
@ -262,6 +273,14 @@ final class WP_Site {
case 'site_id': case 'site_id':
case 'network_id': case 'network_id':
return true; return true;
case 'blogname':
case 'siteurl':
case 'post_count':
case 'home':
if ( ! did_action( 'ms_loaded' ) ) {
return false;
}
return true;
} }
return false; return false;
@ -292,4 +311,58 @@ final class WP_Site {
$this->$key = $value; $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;
}
} }

View File

@ -509,7 +509,7 @@ function wp_start_object_cache() {
wp_cache_init(); wp_cache_init();
if ( function_exists( 'wp_cache_add_global_groups' ) ) { 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' ) ); wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
} }
} }

View File

@ -455,6 +455,7 @@ function clean_blog_cache( $blog ) {
$domain_path_key = md5( $blog->domain . $blog->path ); $domain_path_key = md5( $blog->domain . $blog->path );
wp_cache_delete( $blog_id, 'sites' ); 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 , 'blog-details' );
wp_cache_delete( $blog_id . 'short' , 'blog-details' ); wp_cache_delete( $blog_id . 'short' , 'blog-details' );
wp_cache_delete( $domain_path_key, 'blog-lookup' ); 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 ) ) { if ( is_array( $global_groups ) ) {
wp_cache_add_global_groups( $global_groups ); wp_cache_add_global_groups( $global_groups );
} else { } 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' ) ); wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
} }
@ -893,7 +894,7 @@ function restore_current_blog() {
if ( is_array( $global_groups ) ) { if ( is_array( $global_groups ) ) {
wp_cache_add_global_groups( $global_groups ); wp_cache_add_global_groups( $global_groups );
} else { } 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' ) ); wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.