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
+29
View File
@@ -11,6 +11,7 @@ if ( is_multisite() ) :
class Tests_Multisite_Site extends WP_UnitTestCase {
protected $suppress = false;
protected static $network_ids;
protected static $site_ids;
function setUp() {
global $wpdb;
@@ -33,11 +34,25 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
$id = $factory->network->create( $id );
}
unset( $id );
self::$site_ids = array(
'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['make.wordpress.org/'] ),
'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['make.wordpress.org/'] ),
);
foreach ( self::$site_ids as &$id ) {
$id = $factory->blog->create( $id );
}
unset( $id );
}
public static function wpTearDownAfterClass() {
global $wpdb;
foreach( self::$site_ids as $id ) {
wpmu_delete_blog( $id, true );
}
foreach( self::$network_ids as $id ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
@@ -1028,6 +1043,20 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
function filter_allow_unavailable_languages( $value, $option, $original_value ) {
return $original_value;
}
/**
* @ticket 29684
*/
public function test_is_main_site_different_network() {
$this->assertTrue( is_main_site( self::$site_ids['make.wordpress.org/'], self::$network_ids['make.wordpress.org/'] ) );
}
/**
* @ticket 29684
*/
public function test_is_main_site_different_network_random_site() {
$this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) );
}
}
endif;