WordPress.org

Make WordPress Core

Changeset 34097


Ignore:
Timestamp:
09/13/2015 11:30:57 PM (5 years ago)
Author:
jeremyfelt
Message:

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.

Location:
trunk/src/wp-includes
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-load.php

    r33999 r34097  
    259259 *
    260260 * @since 3.9.0
    261  *
    262  * @global wpdb $wpdb
     261 * @since 4.4.0 Converted to leverage WP_Network
    263262 *
    264263 * @param object|int $network The network's database row or ID.
    265  * @return object|false Object containing network information if found, false if not.
     264 * @return WP_Network|false Object containing network information if found, false if not.
    266265 */
    267266function wp_get_network( $network ) {
    268     global $wpdb;
    269 
    270267    if ( ! is_object( $network ) ) {
    271         $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
    272         if ( ! $network ) {
    273             return false;
    274         }
     268        $network = WP_Network::get_instance( $network );
     269    } else {
     270        $network = new WP_Network( $network );
    275271    }
    276272
  • trunk/src/wp-includes/ms-settings.php

    r33990 r34097  
    1111 */
    1212
    13 /** Include Multisite initialization functions */
     13/** WP_Network class */
     14require_once( ABSPATH . WPINC . '/class-wp-network.php' );
     15
     16/** Multisite loader */
    1417require_once( ABSPATH . WPINC . '/ms-load.php' );
     18
     19/** Default Multisite constants */
    1520require_once( ABSPATH . WPINC . '/ms-default-constants.php' );
    1621
     
    7681            $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
    7782            if ( 1 === $wpdb->num_rows ) {
    78                 $current_site = wp_get_network( $one_network );
     83                $current_site = new WP_Network( $one_network );
    7984                wp_cache_add( 'current_network', $current_site, 'site-options' );
    8085            } elseif ( 0 === $wpdb->num_rows ) {
     
    111116        $current_blog = get_site_by_path( $domain, $path, 1 );
    112117        if ( $current_blog ) {
    113             $current_site = wp_get_network( $current_blog->site_id ? $current_blog->site_id : 1 );
     118            $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
    114119        } else {
    115120            // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
     
    120125    // The network declared by the site trumps any constants.
    121126    if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    122         $current_site = wp_get_network( $current_blog->site_id );
     127        $current_site = WP_Network::get_instance( $current_blog->site_id );
    123128    }
    124129
     
    180185    }
    181186
    182     // @todo What if the domain of the network doesn't match the current site?
    183     $current_site->cookie_domain = $current_site->domain;
    184     if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    185         $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
    186     }
    187 
    188187    // Figure out the current network's main site.
    189     if ( ! isset( $current_site->blog_id ) ) {
     188    if ( empty( $current_site->blog_id ) ) {
    190189        if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    191190            $current_site->blog_id = $current_blog->blog_id;
     
    219218wp_start_object_cache();
    220219
    221 if ( ! isset( $current_site->site_name ) ) {
     220if ( ! $current_site instanceof WP_Network ) {
     221    $current_site = new WP_Network( $current_site );
     222}
     223
     224if ( empty( $current_site->site_name ) ) {
    222225    $current_site->site_name = get_site_option( 'site_name' );
    223226    if ( ! $current_site->site_name ) {
Note: See TracChangeset for help on using the changeset viewer.