Make WordPress Core

Changeset 25147


Ignore:
Timestamp:
08/28/2013 03:34:50 AM (11 years ago)
Author:
nacin
Message:

Introduce is_main_network().

By default, a network ID of 1 is assumed to be the main network.
Otherwise, it is the first network listed in the wp_site table.

If PRIMARY_NETWORK_ID is defined, it is considered main network.

props jeremyfelt.
see #25030.

File:
1 edited

Legend:

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

    r25101 r25147  
    32473247
    32483248/**
    3249  * Is main site?
    3250  *
     3249 * Whether a site is the main site of the current network.
    32513250 *
    32523251 * @since 3.0.0
    3253  * @package WordPress
    3254  *
    3255  * @param int $blog_id optional blog id to test (default current blog)
    3256  * @return bool True if not multisite or $blog_id is main site
    3257  */
    3258 function is_main_site( $blog_id = '' ) {
     3252 *
     3253 * @param int $site_id Optional. Site ID to test. Defaults to current site.
     3254 * @return bool True if $site_id is the main site of the network, or if not running multisite.
     3255 */
     3256function is_main_site( $site_id = null ) {
     3257    // This is the current network's information; 'site' is old terminology.
    32593258    global $current_site;
    32603259
     
    32623261        return true;
    32633262
    3264     if ( ! $blog_id )
    3265         $blog_id = get_current_blog_id();
    3266 
    3267     return $blog_id == $current_site->blog_id;
     3263    if ( ! $site_id )
     3264        $site_id = get_current_blog_id();
     3265
     3266    return (int) $site_id === (int) $current_site->blog_id;
     3267}
     3268
     3269/**
     3270 * Whether a network is the main network of the multisite install.
     3271 *
     3272 * @since 3.7.0
     3273 *
     3274 * @param int $network_id Optional. Network ID to test. Defaults to current network.
     3275 * @return bool True if $network_id is the main network, or if not running multisite.
     3276 */
     3277function is_main_network( $network_id = null ) {
     3278    global $current_site, $wpdb;
     3279
     3280    $current_network_id = (int) $current_site->id;
     3281
     3282    if ( ! is_multisite() )
     3283        return true;
     3284
     3285    if ( ! $network_id )
     3286        $network_id = $current_network_id;
     3287    $network_id = (int) $network_id;
     3288
     3289    if ( defined( 'PRIMARY_NETWORK_ID' ) )
     3290        return $network_id === (int) PRIMARY_NETWORK_ID;
     3291
     3292    if ( 1 === $current_network_id )
     3293        return $network_id === $current_network_id;
     3294
     3295    $primary_network_id = (int) wp_cache_get( 'primary_network_id', 'site-options' );
     3296
     3297    if ( $primary_network_id )
     3298        return $network_id === $primary_network_id;
     3299
     3300    $primary_network_id = (int) $wpdb->get_var( "SELECT id FROM $wpdb->site ORDER BY id LIMIT 1" );
     3301    wp_cache_add( 'primary_network_id', $primary_network_id, 'site-options' );
     3302
     3303    return $network_id === $primary_network_id;
    32683304}
    32693305
Note: See TracChangeset for help on using the changeset viewer.