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:
parent
2a6793c7b2
commit
16b4096779
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
/**
|
||||
* Network API: WP_Network object class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Multisite
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used for interacting with a multisite network.
|
||||
*
|
||||
* This class is used during load to populate the `$current_site` global and
|
||||
* setup the current network.
|
||||
*
|
||||
* This class is most useful in WordPress multi-network installations where the
|
||||
* ability to interact with any network of sites is required.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
class WP_Network {
|
||||
|
||||
/**
|
||||
* Network ID.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* Domain of the network.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $domain = '';
|
||||
|
||||
/**
|
||||
* Path of the network.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $path = '';
|
||||
|
||||
/**
|
||||
* The ID of the network's main site.
|
||||
*
|
||||
* Named "blog" vs. "site" for legacy reasons. A main site is mapped to
|
||||
* the network when the network is created.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $blog_id = 0;
|
||||
|
||||
/**
|
||||
* Domain used to set cookies for this network.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $cookie_domain = '';
|
||||
|
||||
/**
|
||||
* Name of this network.
|
||||
*
|
||||
* Named "site" vs. "network" for legacy reasons.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $site_name = '';
|
||||
|
||||
/**
|
||||
* Retrieve a network from the database by its ID.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param int $network_id The ID of the network to retrieve.
|
||||
* @return WP_Network|bool The network's object if found. False if not.
|
||||
*/
|
||||
public static function get_instance( $network_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$network_id = (int) $network_id;
|
||||
if ( ! $network_id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$_network = wp_cache_get( $network_id, 'networks' );
|
||||
|
||||
if ( ! $_network ) {
|
||||
$_network = $wpdb->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 );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.4-alpha-34096';
|
||||
$wp_version = '4.4-alpha-34097';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue