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.


git-svn-id: https://develop.svn.wordpress.org/trunk@41380 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2017-09-15 11:14:49 +00:00
parent 95a2632121
commit e23004c344
6 changed files with 249 additions and 15 deletions
+2 -8
View File
@@ -135,7 +135,7 @@ function get_network_by_path( $domain, $path, $segments = null ) {
/**
* Retrieves the closest matching site object by its domain and path.
*
*
* This will not necessarily return an exact match for a domain and path. Instead, it
* breaks the domain and path into pieces that are then used to match the closest
* possibility from a query.
@@ -424,13 +424,7 @@ function ms_load_current_site_and_network( $domain, $path, $subdomain = false )
// Figure out the current network's main site.
if ( empty( $current_site->blog_id ) ) {
if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
$current_site->blog_id = $current_blog->blog_id;
} elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
$current_site->domain, $current_site->path ) );
wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
}
$current_site->blog_id = get_main_site_id( $current_site->id );
}
return true;