From 947eef9468a25bbf44deae772bc4d29103578a14 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Sun, 14 Jun 2015 21:45:25 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 57 ++++++++++++++++++++++++++++----------- wp-includes/version.php | 2 +- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index b09ed844af..99cf913d9a 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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 ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 8498a3b6d5..8af736353d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.