From 591a9aa73cb960af9c5324c4446ef696a25923bb Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Sun, 13 Sep 2015 23:30:57 +0000 Subject: [PATCH] Multisite: Introduce the `WP_Network` class. A `WP_Network` object initially matches a row from `wp_site` and is populated with additional properties used by WordPress core. The first iteration is used to retrieve an existing network based on data passed to the class. * A network can be retrieved by its ID through `WP_Network::get_instance()`, following in the steps of `WP_Post` and `WP_Comment`. * A network object can be created or completed by passing initial properties in as a standard object to `new WP_Network()`. Using these methods, we are now able to populate the global `$current_site` during load via this class. Props johnjamesjacoby, jeremyfelt, drewapicture, wonderboymusic. See #31985. git-svn-id: https://develop.svn.wordpress.org/trunk@34097 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-network.php | 154 +++++++++++++++++++++++++++ src/wp-includes/ms-load.php | 14 +-- src/wp-includes/ms-settings.php | 27 ++--- 3 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 src/wp-includes/class-wp-network.php diff --git a/src/wp-includes/class-wp-network.php b/src/wp-includes/class-wp-network.php new file mode 100644 index 0000000000..979e08341c --- /dev/null +++ b/src/wp-includes/class-wp-network.php @@ -0,0 +1,154 @@ +get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) ); + + if ( empty( $_network ) || is_wp_error( $_network ) ) { + return false; + } + + wp_cache_add( $network_id, $_network, 'networks' ); + } + + return new WP_Network( $_network ); + } + + /** + * Create a new WP_Network object. + * + * Will populate object properties from the object provided and assign other + * default properties based on that information. + * + * @since 4.4.0 + * @access public + * + * @param WP_Network|object $network A network object. + */ + public function __construct( $network ) { + foreach( get_object_vars( $network ) as $key => $value ) { + $this->$key = $value; + } + + $this->_set_cookie_domain(); + } + + /** + * Set the cookie domain based on the network domain if one has + * not been populated. + * + * @todo What if the domain of the network doesn't match the current site? + * + * @since 4.4.0 + * @access private + */ + private function _set_cookie_domain() { + if ( ! empty( $this->cookie_domain ) ) { + return; + } + + $this->cookie_domain = $this->domain; + if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) { + $this->cookie_domain = substr( $this->cookie_domain, 4 ); + } + } +} diff --git a/src/wp-includes/ms-load.php b/src/wp-includes/ms-load.php index dd1024accd..9690e1f94c 100644 --- a/src/wp-includes/ms-load.php +++ b/src/wp-includes/ms-load.php @@ -258,20 +258,16 @@ function get_network_by_path( $domain, $path, $segments = null ) { * Retrieve an object containing information about the requested network. * * @since 3.9.0 - * - * @global wpdb $wpdb + * @since 4.4.0 Converted to leverage WP_Network * * @param object|int $network The network's database row or ID. - * @return object|false Object containing network information if found, false if not. + * @return WP_Network|false Object containing network information if found, false if not. */ function wp_get_network( $network ) { - global $wpdb; - if ( ! is_object( $network ) ) { - $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) ); - if ( ! $network ) { - return false; - } + $network = WP_Network::get_instance( $network ); + } else { + $network = new WP_Network( $network ); } return $network; diff --git a/src/wp-includes/ms-settings.php b/src/wp-includes/ms-settings.php index 88e7629887..a231473fb5 100644 --- a/src/wp-includes/ms-settings.php +++ b/src/wp-includes/ms-settings.php @@ -10,8 +10,13 @@ * @since 3.0.0 */ -/** Include Multisite initialization functions */ +/** WP_Network class */ +require_once( ABSPATH . WPINC . '/class-wp-network.php' ); + +/** Multisite loader */ require_once( ABSPATH . WPINC . '/ms-load.php' ); + +/** Default Multisite constants */ require_once( ABSPATH . WPINC . '/ms-default-constants.php' ); if ( defined( 'SUNRISE' ) ) { @@ -75,7 +80,7 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) { // Are there even two networks installed? $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic] if ( 1 === $wpdb->num_rows ) { - $current_site = wp_get_network( $one_network ); + $current_site = new WP_Network( $one_network ); wp_cache_add( 'current_network', $current_site, 'site-options' ); } elseif ( 0 === $wpdb->num_rows ) { ms_not_installed( $domain, $path ); @@ -110,7 +115,7 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) { // Find the site by the domain and at most the first path segment. $current_blog = get_site_by_path( $domain, $path, 1 ); if ( $current_blog ) { - $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 ); + $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 ); } else { // If you don't have a site with the same domain/path as a network, you're pretty screwed, but: $current_site = get_network_by_path( $domain, $path, 1 ); @@ -119,7 +124,7 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) { // The network declared by the site trumps any constants. if ( $current_blog && $current_blog->site_id != $current_site->id ) { - $current_site = wp_get_network( $current_blog->site_id ); + $current_site = WP_Network::get_instance( $current_blog->site_id ); } // No network has been found, bail. @@ -179,14 +184,8 @@ if ( !isset( $current_site ) || !isset( $current_blog ) ) { exit; } - // @todo What if the domain of the network doesn't match the current site? - $current_site->cookie_domain = $current_site->domain; - if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) { - $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 ); - } - // Figure out the current network's main site. - if ( ! isset( $current_site->blog_id ) ) { + if ( empty( $current_site->blog_id ) ) { if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { $current_site->blog_id = $current_blog->blog_id; } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) { @@ -218,7 +217,11 @@ $switched = false; // need to init cache again after blog_id is set wp_start_object_cache(); -if ( ! isset( $current_site->site_name ) ) { +if ( ! $current_site instanceof WP_Network ) { + $current_site = new WP_Network( $current_site ); +} + +if ( empty( $current_site->site_name ) ) { $current_site->site_name = get_site_option( 'site_name' ); if ( ! $current_site->site_name ) { $current_site->site_name = ucfirst( $current_site->domain );