Introduce get_main_network_id()

Expand on the logic previously available as part of `is_main_network()` and provide a way to obtain the ID of the main network. Most useful in multi-network configurations.

Props @johnjamesjacoby for the initial patch.
Fixes #30294.

Built from https://develop.svn.wordpress.org/trunk@32775


git-svn-id: http://core.svn.wordpress.org/trunk@32746 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2015-06-14 21:45:25 +00:00
parent e3ae4f9d7e
commit 947eef9468
2 changed files with 42 additions and 17 deletions

View File

@ -3873,38 +3873,63 @@ function is_main_site( $site_id = null ) {
*
* @since 3.7.0
*
* @global wpdb $wpdb
*
* @param int $network_id Optional. Network ID to test. Defaults to current network.
* @return bool True if $network_id is the main network, or if not running Multisite.
*/
function is_main_network( $network_id = null ) {
global $wpdb;
if ( ! is_multisite() )
if ( ! is_multisite() ) {
return true;
}
$current_network_id = (int) get_current_site()->id;
if ( ! $network_id )
if ( null === $network_id ) {
$network_id = $current_network_id;
}
$network_id = (int) $network_id;
if ( defined( 'PRIMARY_NETWORK_ID' ) )
return $network_id === (int) PRIMARY_NETWORK_ID;
return ( $network_id === get_main_network_id() );
}
if ( 1 === $current_network_id )
return $network_id === $current_network_id;
/**
* Get the main network ID.
*
* @since 4.3.0
*
* @global wpdb $wpdb
*
* @return int The ID of the main network.
*/
function get_main_network_id() {
global $wpdb;
$primary_network_id = (int) wp_cache_get( 'primary_network_id', 'site-options' );
if ( ! is_multisite() ) {
return 1;
}
if ( $primary_network_id )
return $network_id === $primary_network_id;
if ( defined( 'PRIMARY_NETWORK_ID' ) ) {
$main_network_id = PRIMARY_NETWORK_ID;
} elseif ( 1 === (int) get_current_site()->id ) {
// If the current network has an ID of 1, assume it is the main network.
$main_network_id = 1;
} else {
$main_network_id = wp_cache_get( 'primary_network_id', 'site-options' );
$primary_network_id = (int) $wpdb->get_var( "SELECT id FROM $wpdb->site ORDER BY id LIMIT 1" );
wp_cache_add( 'primary_network_id', $primary_network_id, 'site-options' );
if ( false === $main_network_id ) {
$main_network_id = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1" );
wp_cache_add( 'primary_network_id', $main_network_id, 'site-options' );
}
}
return $network_id === $primary_network_id;
/**
* Filter the main network ID.
*
* @since 4.3.0
*
* @param int $main_network_id The ID of the main network.
*/
return (int) apply_filters( 'get_main_network_id', $main_network_id );
}
/**

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.3-alpha-32774';
$wp_version = '4.3-alpha-32775';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.