Changeset 32775
- Timestamp:
- 06/14/2015 09:44:45 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r32650 r32775 3874 3874 * @since 3.7.0 3875 3875 * 3876 * @global wpdb $wpdb3877 *3878 3876 * @param int $network_id Optional. Network ID to test. Defaults to current network. 3879 3877 * @return bool True if $network_id is the main network, or if not running Multisite. 3880 3878 */ 3881 3879 function is_main_network( $network_id = null ) { 3880 if ( ! is_multisite() ) { 3881 return true; 3882 } 3883 3884 $current_network_id = (int) get_current_site()->id; 3885 3886 if ( null === $network_id ) { 3887 $network_id = $current_network_id; 3888 } 3889 3890 $network_id = (int) $network_id; 3891 3892 return ( $network_id === get_main_network_id() ); 3893 } 3894 3895 /** 3896 * Get the main network ID. 3897 * 3898 * @since 4.3.0 3899 * 3900 * @global wpdb $wpdb 3901 * 3902 * @return int The ID of the main network. 3903 */ 3904 function get_main_network_id() { 3882 3905 global $wpdb; 3883 3906 3884 if ( ! is_multisite() ) 3885 return true; 3886 3887 $current_network_id = (int) get_current_site()->id; 3888 3889 if ( ! $network_id ) 3890 $network_id = $current_network_id; 3891 $network_id = (int) $network_id; 3892 3893 if ( defined( 'PRIMARY_NETWORK_ID' ) ) 3894 return $network_id === (int) PRIMARY_NETWORK_ID; 3895 3896 if ( 1 === $current_network_id ) 3897 return $network_id === $current_network_id; 3898 3899 $primary_network_id = (int) wp_cache_get( 'primary_network_id', 'site-options' ); 3900 3901 if ( $primary_network_id ) 3902 return $network_id === $primary_network_id; 3903 3904 $primary_network_id = (int) $wpdb->get_var( "SELECT id FROM $wpdb->site ORDER BY id LIMIT 1" ); 3905 wp_cache_add( 'primary_network_id', $primary_network_id, 'site-options' ); 3906 3907 return $network_id === $primary_network_id; 3907 if ( ! is_multisite() ) { 3908 return 1; 3909 } 3910 3911 if ( defined( 'PRIMARY_NETWORK_ID' ) ) { 3912 $main_network_id = PRIMARY_NETWORK_ID; 3913 } elseif ( 1 === (int) get_current_site()->id ) { 3914 // If the current network has an ID of 1, assume it is the main network. 3915 $main_network_id = 1; 3916 } else { 3917 $main_network_id = wp_cache_get( 'primary_network_id', 'site-options' ); 3918 3919 if ( false === $main_network_id ) { 3920 $main_network_id = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1" ); 3921 wp_cache_add( 'primary_network_id', $main_network_id, 'site-options' ); 3922 } 3923 } 3924 3925 /** 3926 * Filter the main network ID. 3927 * 3928 * @since 4.3.0 3929 * 3930 * @param int $main_network_id The ID of the main network. 3931 */ 3932 return (int) apply_filters( 'get_main_network_id', $main_network_id ); 3908 3933 } 3909 3934 -
trunk/tests/phpunit/tests/multisite/network.php
r31623 r32775 22 22 23 23 function tearDown() { 24 global $wpdb ;24 global $wpdb, $current_site; 25 25 $wpdb->suppress_errors( $this->suppress ); 26 $current_site->id = 1; 26 27 parent::tearDown(); 28 } 29 30 /** 31 * By default, only one network exists and has a network ID of 1. 32 */ 33 function test_get_main_network_id_default() { 34 $this->assertEquals( 1, get_main_network_id() ); 35 } 36 37 /** 38 * If a second network is created, network ID 1 should still be returned 39 * as the main network ID. 40 */ 41 function test_get_main_network_id_two_networks() { 42 $this->factory->network->create(); 43 44 $this->assertEquals( 1, get_main_network_id() ); 45 } 46 47 /** 48 * When the `$current_site` global is populated with another network, the 49 * main network should still return as 1. 50 */ 51 function test_get_main_network_id_after_network_switch() { 52 global $current_site; 53 54 $id = $this->factory->network->create(); 55 56 $current_site->id = (int) $id; 57 58 $this->assertEquals( 1, get_main_network_id() ); 59 } 60 61 /** 62 * When the first network is removed, the next should return as the main 63 * network ID. 64 * 65 * @todo In the future, we'll have a smarter way of deleting a network. For now, 66 * fake the process with UPDATE queries. 67 */ 68 function test_get_main_network_id_after_network_delete() { 69 global $wpdb, $current_site; 70 $id = $this->factory->network->create(); 71 72 $current_site->id = (int) $id; 73 $wpdb->query( "UPDATE {$wpdb->site} SET id=100 WHERE id=1" ); 74 $this->assertEquals( $id, get_main_network_id() ); 75 $wpdb->query( "UPDATE {$wpdb->site} SET id=1 WHERE id=100" ); 76 } 77 78 function test_get_main_network_id_filtered() { 79 add_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) ); 80 $this->assertEquals( 3, get_main_network_id() ); 81 remove_filter( 'get_main_network_id', array( $this, '_get_main_network_id' ) ); 82 } 83 84 function _get_main_network_id() { 85 return 3; 27 86 } 28 87
Note: See TracChangeset
for help on using the changeset viewer.