Ticket #29684: 29684.2.diff
File 29684.2.diff, 6.2 KB (added by , 8 years ago) |
---|
-
src/wp-includes/functions.php
4386 4386 * Determine whether a site is the main site of the current network. 4387 4387 * 4388 4388 * @since 3.0.0 4389 * @since 4.9.0 The $network_id parameter has been added. 4389 4390 * 4390 * @param int $site_id Optional. Site ID to test. Defaults to current site. 4391 * @param int $site_id Optional. Site ID to test. Defaults to current site. 4392 * @param int $network_id Optional. Network ID of the network to check for. 4393 * Defaults to current network. 4391 4394 * @return bool True if $site_id is the main site of the network, or if not 4392 4395 * running Multisite. 4393 4396 */ 4394 function is_main_site( $site_id = null ) {4395 if ( ! is_multisite() ) 4397 function is_main_site( $site_id = null, $network_id = null ) { 4398 if ( ! is_multisite() ) { 4396 4399 return true; 4400 } 4397 4401 4398 if ( ! $site_id ) 4402 if ( ! $site_id ) { 4399 4403 $site_id = get_current_blog_id(); 4404 } 4405 4406 $site_id = (int) $site_id; 4407 4408 return $site_id === get_main_site_id( $network_id ); 4409 } 4410 4411 /** 4412 * Gets the main site ID. 4413 * 4414 * @since 4.9.0 4415 * 4416 * @param int $network_id Optional. The ID of the network for which to get the main site. 4417 * Defaults to the current network. 4418 * @return int The ID of the main site. 4419 */ 4420 function get_main_site_id( $network_id = null ) { 4421 if ( ! is_multisite() ) { 4422 return 1; 4423 } 4424 4425 $network = get_network( $network_id ); 4426 if ( ! $network ) { 4427 return 0; 4428 } 4429 4430 $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' ); 4431 if ( false === $main_site_id ) { 4432 $_sites = get_sites( array( 4433 'fields' => 'ids', 4434 'number' => 1, 4435 'domain' => $network->domain, 4436 'path' => $network->path, 4437 'network_id' => $network->id, 4438 ) ); 4439 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; 4400 4440 4401 return (int) $site_id === (int) get_network()->site_id; 4441 wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' ); 4442 } 4443 4444 /** 4445 * Filters the main site ID. 4446 * 4447 * @since 4.9.0 4448 * 4449 * @param int $main_site_id The ID of the main site. 4450 * @param int $network_id The ID of the network for which the main site was detected. 4451 */ 4452 return (int) apply_filters( 'get_main_site_id', $main_site_id, $network->id ); 4402 4453 } 4403 4454 4404 4455 /** -
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. … … 426 426 if ( empty( $current_site->blog_id ) ) { 427 427 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { 428 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' ); 429 } else { 430 $current_site->blog_id = get_main_site_id( $current_site->id ); 433 431 } 434 432 } 435 433 -
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 765 /** 766 * @ticket 29684 767 */ 768 function test_get_main_site_id() { 769 $this->assertSame( get_current_blog_id(), get_main_site_id() ); 770 } 771 772 /** 773 * @ticket 29684 774 */ 775 function test_get_main_site_id_switched() { 776 $main_site_id = get_current_blog_id(); 777 778 $other_site_id = self::factory()->blog->create(); 779 switch_to_blog( $other_site_id ); 780 781 $result = get_main_site_id(); 782 783 restore_current_blog(); 784 785 $this->assertSame( $main_site_id, $result ); 786 } 787 788 /** 789 * @ticket 29684 790 */ 791 function test_get_main_site_id_different_network() { 792 $args = array( 793 'domain' => 'wordpress.org', 794 'path' => '/', 795 ); 796 797 $network_id = self::factory()->network->create( $args ); 798 799 $args['site_id'] = $network_id; 800 801 $site_id = self::factory()->blog->create( $args ); 802 $other_site_id = self::factory()->blog->create( array_merge( $args, array( 803 'path' => '/foo/', 804 ) ) ); 805 806 $this->assertSame( $site_id, get_main_site_id( $network_id ) ); 807 } 808 809 /** 810 * @ticket 29684 811 */ 812 function test_get_main_site_id_different_network_without_site() { 813 $args = array( 814 'domain' => 'wordpress.org', 815 'path' => '/', 816 ); 817 818 $network_id = self::factory()->network->create( $args ); 819 820 $this->assertSame( 0, get_main_site_id( $network_id ) ); 821 } 822 823 /** 824 * @ticket 29684 825 */ 826 function test_get_main_site_id_invalid_network() { 827 $this->assertSame( 0, get_main_site_id( 333 ) ); 828 } 829 830 /** 831 * @ticket 29684 832 */ 833 function test_get_main_site_id_filtered() { 834 add_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 835 $result = get_main_site_id(); 836 remove_filter( 'get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 837 838 $this->assertSame( 333, $result ); 839 } 840 841 function filter_get_main_site_id( $main_site_id ) { 842 return 333; 843 } 844 744 845 function test_switch_upload_dir() { 745 846 $this->assertTrue( is_main_site() ); 746 847