Changeset 41380
- Timestamp:
- 09/15/2017 11:14:49 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-network.php
r41289 r41380 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 … … 200 200 $this->$key = $value; 201 201 } 202 } 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 'site_id' 208 * properties. 209 * 210 * @since 4.9.0 211 * @see get_main_site_id() 212 * 213 * @return string Main site ID as numeric string, for compatibility reasons. 214 */ 215 private function get_main_site_id() { 216 if ( empty( $this->blog_id ) ) { 217 $this->blog_id = (string) get_main_site_id( $this->id ); 218 } 219 220 return $this->blog_id; 202 221 } 203 222 -
trunk/src/wp-includes/functions.php
r41369 r41380 4391 4391 * 4392 4392 * @since 3.0.0 4393 * 4394 * @param int $site_id Optional. Site ID to test. Defaults to current site. 4393 * @since 4.9.0 The $network_id parameter has been added. 4394 * 4395 * @param int $site_id Optional. Site ID to test. Defaults to current site. 4396 * @param int $network_id Optional. Network ID of the network to check for. 4397 * Defaults to current network. 4395 4398 * @return bool True if $site_id is the main site of the network, or if not 4396 4399 * running Multisite. 4397 4400 */ 4398 function is_main_site( $site_id = null ) {4399 if ( ! is_multisite() ) 4401 function is_main_site( $site_id = null, $network_id = null ) { 4402 if ( ! is_multisite() ) { 4400 4403 return true; 4401 4402 if ( ! $site_id ) 4404 } 4405 4406 if ( ! $site_id ) { 4403 4407 $site_id = get_current_blog_id(); 4404 4405 return (int) $site_id === (int) get_network()->site_id; 4408 } 4409 4410 $site_id = (int) $site_id; 4411 4412 return $site_id === get_main_site_id( $network_id ); 4413 } 4414 4415 /** 4416 * Gets the main site ID. 4417 * 4418 * @since 4.9.0 4419 * 4420 * @param int $network_id Optional. The ID of the network for which to get the main site. 4421 * Defaults to the current network. 4422 * @return int The ID of the main site. 4423 */ 4424 function get_main_site_id( $network_id = null ) { 4425 if ( ! is_multisite() ) { 4426 return 1; 4427 } 4428 4429 $network = get_network( $network_id ); 4430 if ( ! $network ) { 4431 return 0; 4432 } 4433 4434 /** 4435 * Filters the main site ID. 4436 * 4437 * Returning anything other than null will effectively short-circuit the function, returning 4438 * the result parsed as an integer immediately. 4439 * 4440 * @since 4.9.0 4441 * 4442 * @param int|null $main_site_id If anything other than null is returned, it is interpreted as the main site ID. 4443 * @param int $network_id The ID of the network for which the main site was detected. 4444 */ 4445 $main_site_id = apply_filters( 'pre_get_main_site_id', null, $network->id ); 4446 if ( null !== $main_site_id ) { 4447 return (int) $main_site_id; 4448 } 4449 4450 if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE ) 4451 || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) { 4452 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 4453 return BLOG_ID_CURRENT_SITE; 4454 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 4455 return BLOGID_CURRENT_SITE; 4456 } 4457 } 4458 4459 $site = get_site(); 4460 if ( $site->domain === $network->domain && $site->path === $network->path ) { 4461 $main_site_id = (int) $site->id; 4462 } else { 4463 $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' ); 4464 if ( false === $main_site_id ) { 4465 $_sites = get_sites( array( 4466 'fields' => 'ids', 4467 'number' => 1, 4468 'domain' => $network->domain, 4469 'path' => $network->path, 4470 'network_id' => $network->id, 4471 ) ); 4472 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; 4473 4474 wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' ); 4475 } 4476 } 4477 4478 return (int) $main_site_id; 4406 4479 } 4407 4480 -
trunk/src/wp-includes/ms-load.php
r41289 r41380 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 … … 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 -
trunk/tests/phpunit/tests/multisite/network.php
r41242 r41380 584 584 $this->assertEquals( $original_count + 1, $result ); 585 585 } 586 587 /** 588 * @ticket 29684 589 */ 590 public function test_network_blog_id_set() { 591 $network = get_network( self::$different_network_id ); 592 593 $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id ); 594 } 586 595 } 587 596 -
trunk/tests/phpunit/tests/multisite/site.php
r41058 r41380 12 12 protected $suppress = false; 13 13 protected static $network_ids; 14 protected static $site_ids; 14 15 15 16 function setUp() { … … 34 35 } 35 36 unset( $id ); 37 38 self::$site_ids = array( 39 'make.wordpress.org/' => array( 'domain' => 'make.wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['make.wordpress.org/'] ), 40 'make.wordpress.org/foo/' => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['make.wordpress.org/'] ), 41 ); 42 43 foreach ( self::$site_ids as &$id ) { 44 $id = $factory->blog->create( $id ); 45 } 46 unset( $id ); 36 47 } 37 48 38 49 public static function wpTearDownAfterClass() { 39 50 global $wpdb; 51 52 foreach( self::$site_ids as $id ) { 53 wpmu_delete_blog( $id, true ); 54 } 40 55 41 56 foreach( self::$network_ids as $id ) { … … 1029 1044 return $original_value; 1030 1045 } 1046 1047 /** 1048 * @ticket 29684 1049 */ 1050 public function test_is_main_site_different_network() { 1051 $this->assertTrue( is_main_site( self::$site_ids['make.wordpress.org/'], self::$network_ids['make.wordpress.org/'] ) ); 1052 } 1053 1054 /** 1055 * @ticket 29684 1056 */ 1057 public function test_is_main_site_different_network_random_site() { 1058 $this->assertFalse( is_main_site( self::$site_ids['make.wordpress.org/foo/'], self::$network_ids['make.wordpress.org/'] ) ); 1059 } 1031 1060 } 1032 1061
Note: See TracChangeset
for help on using the changeset viewer.