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.

Built from https://develop.svn.wordpress.org/trunk@34097


git-svn-id: http://core.svn.wordpress.org/trunk@34065 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt
2015-09-13 23:31:26 +00:00
parent 2a6793c7b2
commit 16b4096779
4 changed files with 175 additions and 22 deletions

View File

@@ -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 );