<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[34097] trunk/src/wp-includes: Multisite: Introduce the `WP_Network` class.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/34097">34097</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/34097","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jeremyfelt</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-09-13 23:30:57 +0000 (Sun, 13 Sep 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/31985">#31985</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmsloadphp">trunk/src/wp-includes/ms-load.php</a></li>
<li><a href="#trunksrcwpincludesmssettingsphp">trunk/src/wp-includes/ms-settings.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpnetworkphp">trunk/src/wp-includes/class-wp-network.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpnetworkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/class-wp-network.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-network.php                                (rev 0)
+++ trunk/src/wp-includes/class-wp-network.php  2015-09-13 23:30:57 UTC (rev 34097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,154 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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 );
+               }
+       }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesmsloadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-load.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-load.php 2015-09-13 16:47:54 UTC (rev 34096)
+++ trunk/src/wp-includes/ms-load.php   2015-09-13 23:30:57 UTC (rev 34097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -258,20 +258,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Retrieve an object containing information about the requested network.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.4.0 Converted to leverage WP_Network
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global wpdb $wpdb
- *
</del><span class="cx" style="display: block; padding: 0 10px">  * @param object|int $network The network's database row or ID.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return object|false Object containing network information if found, false if not.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_Network|false Object containing network information if found, false if not.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_get_network( $network ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        global $wpdb;
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( ! is_object( $network ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
-               if ( ! $network ) {
-                       return false;
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $network = WP_Network::get_instance( $network );
+       } else {
+               $network = new WP_Network( $network );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $network;
</span></span></pre></div>
<a id="trunksrcwpincludesmssettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-settings.php     2015-09-13 16:47:54 UTC (rev 34096)
+++ trunk/src/wp-includes/ms-settings.php       2015-09-13 23:30:57 UTC (rev 34097)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,8 +10,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.0.0
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/** Include Multisite initialization functions */
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/** WP_Network class */
+require_once( ABSPATH . WPINC . '/class-wp-network.php' );
+
+/** Multisite loader */
</ins><span class="cx" style="display: block; padding: 0 10px"> require_once( ABSPATH . WPINC . '/ms-load.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/** Default Multisite constants */
</ins><span class="cx" style="display: block; padding: 0 10px"> require_once( ABSPATH . WPINC . '/ms-default-constants.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> if ( defined( 'SUNRISE' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,7 +80,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        // Are there even two networks installed?
</span><span class="cx" style="display: block; padding: 0 10px">                        $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 1 === $wpdb->num_rows ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $current_site = wp_get_network( $one_network );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $current_site = new WP_Network( $one_network );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 wp_cache_add( 'current_network', $current_site, 'site-options' );
</span><span class="cx" style="display: block; padding: 0 10px">                        } elseif ( 0 === $wpdb->num_rows ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                ms_not_installed( $domain, $path );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,7 +115,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Find the site by the domain and at most the first path segment.
</span><span class="cx" style="display: block; padding: 0 10px">                $current_blog = get_site_by_path( $domain, $path, 1 );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $current_blog ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
</span><span class="cx" style="display: block; padding: 0 10px">                        $current_site = get_network_by_path( $domain, $path, 1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,7 +124,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // The network declared by the site trumps any constants.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $current_blog && $current_blog->site_id != $current_site->id ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $current_site = wp_get_network( $current_blog->site_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $current_site = WP_Network::get_instance( $current_blog->site_id );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // No network has been found, bail.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -179,14 +184,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                exit;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // @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 );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         // Figure out the current network's main site.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! isset( $current_site->blog_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $current_site->blog_id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $current_site->blog_id = $current_blog->blog_id;
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -218,7 +217,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // need to init cache again after blog_id is set
</span><span class="cx" style="display: block; padding: 0 10px"> wp_start_object_cache();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( ! isset( $current_site->site_name ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! $current_site instanceof WP_Network ) {
+       $current_site = new WP_Network( $current_site );
+}
+
+if ( empty( $current_site->site_name ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         $current_site->site_name = get_site_option( 'site_name' );
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $current_site->site_name ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $current_site->site_name = ucfirst( $current_site->domain );
</span></span></pre>
</div>
</div>

</body>
</html>