From c63c4d9345a3de1d11014585b9d3795a336d4755 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Thu, 9 Jun 2016 20:34:28 +0000 Subject: [PATCH] Multisite: Introduce `get_current_network_id()` Similar to `get_current_blog_id`, this can be used to get the ID of the `$current_site` global. If not available, it will fallback to the main network ID. In single site, this will return 1. Props spacedmonkey, flixos90. Fixes #33900. Built from https://develop.svn.wordpress.org/trunk@37670 git-svn-id: http://core.svn.wordpress.org/trunk@37636 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 4 +++- wp-includes/load.php | 23 +++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index f64a226ba4..55e732a51d 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -4255,9 +4255,11 @@ function get_main_network_id() { return 1; } + $current_site = get_current_site(); + if ( defined( 'PRIMARY_NETWORK_ID' ) ) { $main_network_id = PRIMARY_NETWORK_ID; - } elseif ( 1 === (int) get_current_site()->id ) { + } elseif ( isset( $current_site->id ) && 1 === (int) $current_site->id ) { // If the current network has an ID of 1, assume it is the main network. $main_network_id = 1; } else { diff --git a/wp-includes/load.php b/wp-includes/load.php index 9ab44f018f..f474c5c87e 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -807,6 +807,29 @@ function get_current_blog_id() { return absint($blog_id); } +/** + * Retrieves the current network ID. + * + * @since 4.6.0 + * + * @global WP_Network $current_site The current network. + * + * @return int The ID of the current network. + */ +function get_current_network_id() { + if ( ! is_multisite() ) { + return 1; + } + + $current_site = get_current_site(); + + if ( ! isset( $current_site->id ) ) { + return get_main_network_id(); + } + + return absint( $current_site->id ); +} + /** * Attempt an early load of translations. * diff --git a/wp-includes/version.php b/wp-includes/version.php index a424090958..969702982f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.6-alpha-37669'; +$wp_version = '4.6-alpha-37670'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.