From 0b69f2cd63dbe6339d6e92307841e6672b46a4cc Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Mon, 25 Jan 2016 21:51:26 +0000 Subject: [PATCH] Multisite: Introduce the WP_Site class. * A `WP_Site` object initially matches a row from `wp_blogs`. * A site can be retrieved by its ID through `WP_Site::get_instance()`. * Adds `sites` to the global cache group and captures instance lookups. * The multisite bootstrap now ensures `$current_blog` is an instance of `WP_Site`. Props johnjamesjacoby, jeremyfelt. See #32450. Built from https://develop.svn.wordpress.org/trunk@36393 git-svn-id: http://core.svn.wordpress.org/trunk@36360 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-site.php | 200 ++++++++++++++++++++++++++++++++++ wp-includes/ms-blogs.php | 9 +- wp-includes/ms-settings.php | 7 ++ wp-includes/version.php | 2 +- 4 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 wp-includes/class-wp-site.php diff --git a/wp-includes/class-wp-site.php b/wp-includes/class-wp-site.php new file mode 100644 index 0000000000..7858c7dbbb --- /dev/null +++ b/wp-includes/class-wp-site.php @@ -0,0 +1,200 @@ +get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) ); + + if ( empty( $_site ) || is_wp_error( $_site ) ) { + return false; + } + + wp_cache_add( $site_id, $_site, 'sites' ); + } + + return new WP_Site( $_site ); + } + + /** + * Create a new WP_Site object. + * + * Will populate object properties from the object provided and assign other + * default properties based on that information. + * + * @since 4.5.0 + * @access public + * + * @param WP_Site|object $site A site object. + */ + public function __construct( $site ) { + foreach( get_object_vars( $site ) as $key => $value ) { + $this->$key = $value; + } + } +} diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index f45d544543..586d56bab4 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -210,7 +210,7 @@ function get_blog_details( $fields = null, $get_all = true ) { } if ( empty($details) ) { - $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) ); + $details = WP_Site::get_instance( $blog_id ); if ( ! $details ) { // Set the full cache. wp_cache_set( $blog_id, -1, 'blog-details' ); @@ -218,6 +218,10 @@ function get_blog_details( $fields = null, $get_all = true ) { } } + if ( ! $details instanceof WP_Site ) { + $details = new WP_Site( $details ); + } + if ( ! $get_all ) { wp_cache_set( $blog_id . $all, $details, 'blog-details' ); return $details; @@ -442,6 +446,7 @@ function clean_blog_cache( $blog ) { $blog_id = $blog->blog_id; $domain_path_key = md5( $blog->domain . $blog->path ); + wp_cache_delete( $blog_id, 'sites' ); wp_cache_delete( $blog_id , 'blog-details' ); wp_cache_delete( $blog_id . 'short' , 'blog-details' ); wp_cache_delete( $domain_path_key, 'blog-lookup' ); @@ -655,7 +660,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' ) ); + 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_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); } diff --git a/wp-includes/ms-settings.php b/wp-includes/ms-settings.php index aad6269f3f..351a926b49 100644 --- a/wp-includes/ms-settings.php +++ b/wp-includes/ms-settings.php @@ -13,6 +13,9 @@ /** WP_Network class */ require_once( ABSPATH . WPINC . '/class-wp-network.php' ); +/** WP_Site class */ +require_once( ABSPATH . WPINC . '/class-wp-site.php' ); + /** Multisite loader */ require_once( ABSPATH . WPINC . '/ms-load.php' ); @@ -222,5 +225,9 @@ if ( ! $current_site instanceof WP_Network ) { $current_site = new WP_Network( $current_site ); } +if ( ! $current_blog instanceof WP_Site ) { + $current_blog = new WP_Site( $current_blog ); +} + // Define upload directory constants ms_upload_constants(); diff --git a/wp-includes/version.php b/wp-includes/version.php index 19ef249679..95ce125856 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36392'; +$wp_version = '4.5-alpha-36393'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.