Make WordPress Core


Ignore:
Timestamp:
09/15/2017 11:14:49 AM (7 years ago)
Author:
flixos90
Message:

Multisite: Introduce get_main_site_id().

This function can be used to easily get the main site ID of a given network via the optional $network_id parameter, which defaults to the current network. The existing is_main_site() now uses the new function internally and now accepts an optional $network_id parameter as well.

The main purpose of the new function at this point is to ensure that the WP_Network::$blog_id property is always set. Magic getters in the class have been adjusted to auto-fill the property when it is accessed and empty. Furthermore the function encapsulates logic that was previously part of ms_load_current_site_and_network() and has been replaced with a call to the function now.

Props spacedmonkey, jeremyfelt, johnjamesjacoby, flixos90.
Fixes #29684.

File:
1 edited

Legend:

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

    r41289 r41380  
    136136/**
    137137 * Retrieves the closest matching site object by its domain and path.
    138  * 
     138 *
    139139 * This will not necessarily return an exact match for a domain and path. Instead, it
    140140 * breaks the domain and path into pieces that are then used to match the closest
     
    425425    // Figure out the current network's main site.
    426426    if ( empty( $current_site->blog_id ) ) {
    427         if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    428             $current_site->blog_id = $current_blog->blog_id;
    429         } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
    430             $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    431                 $current_site->domain, $current_site->path ) );
    432             wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
    433         }
     427        $current_site->blog_id = get_main_site_id( $current_site->id );
    434428    }
    435429
Note: See TracChangeset for help on using the changeset viewer.