Ticket #29684: 29684.7.diff
File 29684.7.diff, 10.2 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-network.php
diff --git a/src/wp-includes/class-wp-network.php b/src/wp-includes/class-wp-network.php index 311f3378a5..e942a218b9 100644
a b class WP_Network { 148 148 case 'id': 149 149 return (int) $this->id; 150 150 case 'blog_id': 151 return $this-> blog_id;151 return $this->get_main_site_id(); 152 152 case 'site_id': 153 return (int) $this-> blog_id;153 return (int) $this->get_main_site_id(); 154 154 } 155 155 156 156 return null; … … class WP_Network { 201 201 } 202 202 } 203 203 204 /** 205 * Returns the main site ID for the network. 206 * 207 * Internal method used by the magic getter for the 'blog_id' and 208 * 'site_id' properties. 209 * 210 * @since 4.9.0 211 * 212 * @return string|int Main site ID as numeric string or integer. 213 */ 214 private function get_main_site_id() { 215 if ( empty( $this->blog_id ) ) { 216 $this->blog_id = get_main_site_id( $this->id ); 217 } 218 219 return $this->blog_id; 220 } 221 204 222 /** 205 223 * Set the site name assigned to the network if one has not been populated. 206 224 * -
src/wp-includes/functions.php
diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 03c6126d5b..651327136c 100644
a b function wp_suspend_cache_invalidation( $suspend = true ) { 4392 4392 * Determine whether a site is the main site of the current network. 4393 4393 * 4394 4394 * @since 3.0.0 4395 * @since 4.9.0 The $network_id parameter has been added. 4395 4396 * 4396 * @param int $site_id Optional. Site ID to test. Defaults to current site. 4397 * @param int $site_id Optional. Site ID to test. Defaults to current site. 4398 * @param int $network_id Optional. Network ID of the network to check for. 4399 * Defaults to current network. 4397 4400 * @return bool True if $site_id is the main site of the network, or if not 4398 4401 * running Multisite. 4399 4402 */ 4400 function is_main_site( $site_id = null ) {4401 if ( ! is_multisite() ) 4403 function is_main_site( $site_id = null, $network_id = null ) { 4404 if ( ! is_multisite() ) { 4402 4405 return true; 4406 } 4403 4407 4404 if ( ! $site_id ) 4408 if ( ! $site_id ) { 4405 4409 $site_id = get_current_blog_id(); 4410 } 4411 4412 $site_id = (int) $site_id; 4406 4413 4407 return (int) $site_id === (int) get_network()->site_id; 4414 return $site_id === get_main_site_id( $network_id ); 4415 } 4416 4417 /** 4418 * Gets the main site ID. 4419 * 4420 * @since 4.9.0 4421 * 4422 * @param int $network_id Optional. The ID of the network for which to get the main site. 4423 * Defaults to the current network. 4424 * @return int The ID of the main site. 4425 */ 4426 function get_main_site_id( $network_id = null ) { 4427 if ( ! is_multisite() ) { 4428 return 1; 4429 } 4430 4431 $network = get_network( $network_id ); 4432 if ( ! $network ) { 4433 return 0; 4434 } 4435 4436 if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE ) 4437 || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) { 4438 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 4439 return BLOG_ID_CURRENT_SITE; 4440 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 4441 return BLOGID_CURRENT_SITE; 4442 } 4443 } 4444 4445 $site = get_site(); 4446 if ( $site->domain === $network->domain && $site->path === $network->path ) { 4447 $main_site_id = (int) $site->id; 4448 } else { 4449 $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' ); 4450 if ( false === $main_site_id ) { 4451 $_sites = get_sites( array( 4452 'fields' => 'ids', 4453 'number' => 1, 4454 'domain' => $network->domain, 4455 'path' => $network->path, 4456 'network_id' => $network->id, 4457 ) ); 4458 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; 4459 4460 wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' ); 4461 } 4462 } 4463 4464 /** 4465 * Filters the main site ID. 4466 * 4467 * @since 4.9.0 4468 * 4469 * @param int $main_site_id The ID of the main site. 4470 * @param int $network_id The ID of the network for which the main site was detected. 4471 */ 4472 return (int) apply_filters( 'get_main_site_id', $main_site_id, $network->id ); 4408 4473 } 4409 4474 4410 4475 /** -
src/wp-includes/ms-load.php
diff --git a/src/wp-includes/ms-load.php b/src/wp-includes/ms-load.php index 3c51b3d158..7a00745a95 100644
a b function get_network_by_path( $domain, $path, $segments = null ) { 135 135 136 136 /** 137 137 * Retrieves the closest matching site object by its domain and path. 138 * 138 * 139 139 * This will not necessarily return an exact match for a domain and path. Instead, it 140 140 * breaks the domain and path into pieces that are then used to match the closest 141 141 * possibility from a query. … … function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) 424 424 425 425 // Figure out the current network's main site. 426 426 if ( empty( $current_site->blog_id ) ) { 427 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { 428 $current_site->blog_id = $current_blog->blog_id; 429 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) { 430 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", 431 $current_site->domain, $current_site->path ) ); 432 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' ); 433 } 427 $current_site->blog_id = get_main_site_id( $current_site->id ); 434 428 } 435 429 436 430 return true; -
tests/phpunit/tests/multisite/getMainSiteId.php
diff --git a/tests/phpunit/tests/multisite/getMainSiteId.php b/tests/phpunit/tests/multisite/getMainSiteId.php index e69de29bb2..7011a59660 100644
a b 1 <?php 2 3 class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { 4 protected static $network_ids; 5 protected static $site_ids; 6 7 public static function wpSetUpBeforeClass( $factory ) { 8 self::$network_ids = array( 9 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 10 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. 11 ); 12 13 foreach ( self::$network_ids as &$id ) { 14 $id = $factory->network->create( $id ); 15 } 16 unset( $id ); 17 18 self::$site_ids = array( 19 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), 20 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), 21 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), 22 ); 23 24 foreach ( self::$site_ids as &$id ) { 25 $id = $factory->blog->create( $id ); 26 } 27 unset( $id ); 28 } 29 30 public static function wpTearDownAfterClass() { 31 foreach( self::$site_ids as $id ) { 32 wpmu_delete_blog( $id, true ); 33 } 34 35 global $wpdb; 36 37 foreach( self::$network_ids as $id ) { 38 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); 39 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); 40 } 41 42 wp_update_network_site_counts(); 43 } 44 45 /** 46 * @ticket 29684 47 */ 48 public function test_get_main_site_id_on_main_site_returns_self() { 49 $this->assertSame( get_current_blog_id(), get_main_site_id() ); 50 } 51 52 /** 53 * @ticket 29684 54 */ 55 public function test_get_main_site_id_returns_main_site_in_switched_context_same_network() { 56 $main_site_id = get_current_blog_id(); 57 $other_site_id = self::$site_ids['www.w.org/']; 58 59 switch_to_blog( $other_site_id ); 60 $result = get_main_site_id(); 61 restore_current_blog(); 62 63 $this->assertSame( $main_site_id, $result ); 64 } 65 66 /** 67 * @ticket 29684 68 */ 69 public function test_get_main_site_id_returns_main_site_from_different_network_without_network_id_while_switched() { 70 switch_to_blog( self::$site_ids['wordpress.org/foo/'] ); 71 $result = get_main_site_id(); 72 restore_current_blog(); 73 74 $this->assertSame( self::$site_ids['wordpress.org/'], $result ); 75 } 76 77 /** 78 * @ticket 29684 79 */ 80 public function test_get_main_site_id_returns_main_site_from_different_network_with_network_id_while_switched() { 81 switch_to_blog( self::$site_ids['wordpress.org/foo/'] ); 82 $result = get_main_site_id( self::$network_ids['wordpress.org/'] ); 83 restore_current_blog(); 84 85 $this->assertSame( self::$site_ids['wordpress.org/'], $result ); 86 } 87 88 /** 89 * @ticket 29684 90 */ 91 public function test_get_main_site_id_with_different_network_returns_correct_id() { 92 $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); 93 } 94 95 /** 96 * @ticket 29684 97 */ 98 public function test_get_main_site_id_on_network_without_site_returns_0() { 99 $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); 100 } 101 102 /** 103 * @ticket 29684 104 */ 105 public function test_get_main_site_id_on_invalid_network_returns_0() { 106 $this->assertSame( 0, get_main_site_id( 333 ) ); 107 } 108 109 /** 110 * @ticket 29684 111 */ 112 public function test_get_main_site_id_filtered() { 113 add_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 114 $result = get_main_site_id(); 115 remove_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 116 117 $this->assertSame( 333, $result ); 118 } 119 120 public function filter_get_main_site_id( $main_site_id ) { 121 return 333; 122 } 123 } -
tests/phpunit/tests/multisite/site.php
diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 64dc67d48e..01dd24e24b 100644
a b class Tests_Multisite_Site extends WP_UnitTestCase { 741 741 restore_current_blog(); 742 742 } 743 743 744 /** 745 * @ticket 29684 746 */ 747 function test_is_main_site_different_network() { 748 $args = array( 749 'domain' => 'wordpress.org', 750 'path' => '/', 751 ); 752 753 $network_id = self::factory()->network->create( $args ); 754 755 $args['site_id'] = $network_id; 756 757 $site_id = self::factory()->blog->create( $args ); 758 $other_site_id = self::factory()->blog->create( array_merge( $args, array( 759 'path' => '/foo/', 760 ) ) ); 761 762 $this->assertTrue( is_main_site( $site_id, $network_id ) ); 763 } 764 744 765 function test_switch_upload_dir() { 745 766 $this->assertTrue( is_main_site() ); 746 767