Make WordPress Core

Ticket #31985: 31985.2.diff

File 31985.2.diff, 6.8 KB (added by jeremyfelt, 10 years ago)
  • src/wp-includes/class-wp-network.php

     
     1<?php
     2
     3/**
     4 * Main network class
     5 *
     6 * This class contains the values and functional methods for interacting with
     7 * a network of WordPress sites. Traditionally used for multisite, this class
     8 * is used by the $current_site global to setup the current network
     9 *
     10 * This class is most useful in WordPress multi-network installations where the
     11 * ability to interact with any network of sites is required.
     12 *
     13 * @since 4.3.0
     14 */
     15class WP_Network {
     16
     17        /**
     18         * Unique numerical ID.
     19         *
     20         * @since 4.3.0
     21         *
     22         * @var int
     23         */
     24        public $id = 0;
     25
     26        /**
     27         * Domain of this network.
     28         *
     29         * @since 4.3.0
     30         *
     31         * @var string
     32         */
     33        public $domain = '';
     34
     35        /**
     36         * Path of this network.
     37         *
     38         * @since 4.3.0
     39         *
     40         * @var string
     41         */
     42        public $path = '';
     43
     44        /**
     45         * The ID of the main site mapped to this network.
     46         *
     47         * Named "blog" vs. "site" for legacy reasons.
     48         *
     49         * @since 4.3.0
     50         *
     51         * @var int
     52         */
     53        public $blog_id = 0;
     54
     55        /**
     56         * Domain used for cookies for this network.
     57         *
     58         * @since 4.3.0
     59         *
     60         * @var int
     61         */
     62        public $cookie_domain = '';
     63
     64        /**
     65         * Create a new WP_Network object
     66         *
     67         * Will populate all relevant data if a network ID or object is passed
     68         *
     69         * @since 4.3.0
     70         *
     71         * @param mixed $network stdClass object or network ID
     72         */
     73        public function __construct( $network = false ) {
     74
     75                // Bail if not populating from an existing network.
     76                if ( empty( $network ) ) {
     77                        return false;
     78                }
     79
     80                // Get the network ID.
     81                if ( is_object( $network ) && isset( $network->id ) ) {
     82                        $network_id = $network->id;
     83                } elseif ( is_numeric( $network ) ) {
     84                        $network_id = (int) $network;
     85                } else {
     86                        return false;
     87                }
     88
     89                $this->populate( $network_id );
     90
     91                return $this;
     92        }
     93
     94        /**
     95         * @since 4.3.0
     96         *
     97         * @param int $network_id
     98         * @return bool
     99         */
     100        public function populate( $network_id = 0 ) {
     101                // Query for network
     102                $network = self::get_by_id( $network_id );
     103
     104                // Bail if no network to update
     105                if ( false === $network ) {
     106                        return false;
     107                }
     108
     109                // Set the main data
     110                $this->set_main_data( $network );
     111                $this->set_cookie_domain();
     112        }
     113
     114        /**
     115         * Set the network ID, domain, and path
     116         *
     117         * @since 4.3.0
     118         */
     119        private function set_main_data( $network ) {
     120                $this->id     = $network->id;
     121                $this->domain = $network->domain;
     122                $this->path   = $network->path;
     123        }
     124
     125        /**
     126         * Set the cookie domain, based on the network domain
     127         *
     128         * @todo What if the domain of the network doesn't match the current site?
     129         *
     130         * @since WordPress (4.3.0)
     131         */
     132        private function set_cookie_domain() {
     133                $this->cookie_domain = $this->domain;
     134                if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
     135                        $this->cookie_domain = substr( $this->cookie_domain, 4 );
     136                }
     137        }
     138
     139        /**
     140         * Retreive a network from the database by its ID.
     141         *
     142         * @since 4.3.0
     143         *
     144         * @param int $network_id
     145         *
     146         * @return bool|mixed
     147         */
     148        public static function get_by_id( $network_id = 0 ) {
     149                global $wpdb;
     150
     151                // Check cache
     152                $network = wp_cache_get( $network_id, 'networks' );
     153
     154                // Cache miss
     155                if ( false === $network ) {
     156
     157                        // Query for network data
     158                        $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d", $network_id ) );
     159
     160                        // Bail if network cannot be found
     161                        if ( empty( $network ) || is_wp_error( $network ) ) {
     162                                $network = false;
     163                        }
     164
     165                        // Add network to cache
     166                        wp_cache_add( $network_id, $network, 'networks' );
     167                }
     168
     169                // Return the network
     170                return $network;
     171        }
     172}
     173 No newline at end of file
  • src/wp-includes/ms-load.php

     
    120120 * @param string   $domain   Domain to check.
    121121 * @param string   $path     Path to check.
    122122 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
    123  * @return object|bool Network object if successful. False when no network is found.
     123 * @return WP_Network|bool Network object if successful. False when no network is found.
    124124 */
    125125function get_network_by_path( $domain, $path, $segments = null ) {
    126126        global $wpdb;
     
    264264 * @return object|bool Object containing network information if found, false if not.
    265265 */
    266266function wp_get_network( $network ) {
    267         global $wpdb;
    268 
    269         if ( ! is_object( $network ) ) {
    270                 $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
    271                 if ( ! $network ) {
    272                         return false;
    273                 }
    274         }
    275 
     267        $network = new WP_Network( $network );
    276268        return $network;
    277269}
    278270
  • src/wp-includes/ms-settings.php

     
    1111 */
    1212
    1313/** Include Multisite initialization functions */
     14require_once( ABSPATH . WPINC . '/class-wp-network.php' );
    1415require_once( ABSPATH . WPINC . '/ms-load.php' );
    1516require_once( ABSPATH . WPINC . '/ms-default-constants.php' );
    1617
     
    2122/** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    2223ms_subdomain_constants();
    2324
    24 if ( !isset( $current_site ) || !isset( $current_blog ) ) {
     25if ( ! isset( $current_site ) || ! isset( $current_blog ) ) {
    2526
    2627        // Given the domain and path, let's try to identify the network and site.
    2728        // Usually, it's easier to query the site first, which declares its network.
     
    4445
    4546        // If the network is defined in wp-config.php, we can simply use that.
    4647        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    47                 $current_site = new stdClass;
     48                $current_site = new WP_Network();
    4849                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    4950                $current_site->domain = DOMAIN_CURRENT_SITE;
    5051                $current_site->path = PATH_CURRENT_SITE;
     
    163164                exit;
    164165        }
    165166
    166         // @todo What if the domain of the network doesn't match the current site?
    167         $current_site->cookie_domain = $current_site->domain;
    168         if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    169                 $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
    170         }
    171 
    172167        // Figure out the current network's main site.
    173         if ( ! isset( $current_site->blog_id ) ) {
     168        if ( ! isset( $current_site->blog_id ) || 0 === $current_site->blog_id ) {
    174169                if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    175170                        $current_site->blog_id = $current_blog->blog_id;
    176171                } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {