Ticket #29684: 29684.15.diff
File 29684.15.diff, 8.7 KB (added by , 7 years ago) |
---|
-
src/wp-includes/functions.php
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; 4413 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 /** 4437 * Filters the main site ID. 4438 * 4439 * Returning anything other than null will effectively short-circuit the function, returning 4440 * the result parsed as an integer immediately. 4441 * 4442 * @since 4.9.0 4443 * 4444 * @param int|null $main_site_id If anything other than null is returned, it is interpreted as the main site ID. 4445 * @param int $network_id The ID of the network for which the main site was detected. 4446 */ 4447 $main_site_id = apply_filters( 'pre_get_main_site_id', null, $network->id ); 4448 if ( null !== $main_site_id ) { 4449 return (int) $main_site_id; 4450 } 4451 4452 if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE ) 4453 || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) { 4454 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 4455 return BLOG_ID_CURRENT_SITE; 4456 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 4457 return BLOGID_CURRENT_SITE; 4458 } 4459 } 4460 4461 $site = get_site(); 4462 if ( $site->domain === $network->domain && $site->path === $network->path ) { 4463 $main_site_id = (int) $site->id; 4464 } else { 4465 $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' ); 4466 if ( false === $main_site_id ) { 4467 $_sites = get_sites( array( 4468 'fields' => 'ids', 4469 'number' => 1, 4470 'domain' => $network->domain, 4471 'path' => $network->path, 4472 'network_id' => $network->id, 4473 ) ); 4474 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; 4475 4476 wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' ); 4477 } 4478 } 4479 4480 $main_site_id = (int) $main_site_id; 4406 4481 4407 return (int) $site_id === (int) get_network()->site_id;4482 return $main_site_id; 4408 4483 } 4409 4484 4410 4485 /** -
src/wp-includes/ms-load.php
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. … … 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
1 <?php 2 3 if ( is_multisite() ) : 4 5 /** 6 * Tests for the get_main_site_id() function. 7 * 8 * @group ms-site 9 * @group multisite 10 */ 11 class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase { 12 protected static $network_ids; 13 protected static $site_ids; 14 15 public static function wpSetUpBeforeClass( $factory ) { 16 self::$network_ids = array( 17 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 18 'wp.org/' => array( 'domain' => 'wp.org', 'path' => '/' ), // A network with no sites. 19 ); 20 21 foreach ( self::$network_ids as &$id ) { 22 $id = $factory->network->create( $id ); 23 } 24 unset( $id ); 25 26 self::$site_ids = array( 27 'www.w.org/' => array( 'domain' => 'www.w.org', 'path' => '/' ), 28 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/', 'site_id' => self::$network_ids['wordpress.org/'] ), 29 'wordpress.org/foo/' => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ), 30 ); 31 32 foreach ( self::$site_ids as &$id ) { 33 $id = $factory->blog->create( $id ); 34 } 35 unset( $id ); 36 } 37 38 public static function wpTearDownAfterClass() { 39 foreach( self::$site_ids as $id ) { 40 wpmu_delete_blog( $id, true ); 41 } 42 43 global $wpdb; 44 45 foreach( self::$network_ids as $id ) { 46 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) ); 47 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) ); 48 } 49 50 wp_update_network_site_counts(); 51 } 52 53 /** 54 * @ticket 29684 55 */ 56 public function test_get_main_site_id_on_main_site_returns_self() { 57 $this->assertSame( get_current_blog_id(), get_main_site_id() ); 58 } 59 60 /** 61 * @ticket 29684 62 */ 63 public function test_get_main_site_id_returns_main_site_in_switched_context() { 64 $main_site_id = get_current_blog_id(); 65 $other_site_id = self::$site_ids['www.w.org/']; 66 67 switch_to_blog( $other_site_id ); 68 $result = get_main_site_id(); 69 restore_current_blog(); 70 71 $this->assertSame( $main_site_id, $result ); 72 } 73 74 /** 75 * @ticket 29684 76 */ 77 public function test_get_main_site_id_with_different_network_returns_correct_id() { 78 $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) ); 79 } 80 81 /** 82 * @ticket 29684 83 */ 84 public function test_get_main_site_id_on_network_without_site_returns_0() { 85 $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) ); 86 } 87 88 /** 89 * @ticket 29684 90 */ 91 public function test_get_main_site_id_on_invalid_network_returns_0() { 92 $this->assertSame( 0, get_main_site_id( 333 ) ); 93 } 94 95 /** 96 * @ticket 29684 97 */ 98 public function test_get_main_site_id_filtered() { 99 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 100 $result = get_main_site_id(); 101 102 $this->assertSame( 333, $result ); 103 } 104 105 public function filter_get_main_site_id() { 106 return 333; 107 } 108 } 109 110 endif; -
tests/phpunit/tests/multisite/site.php
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