Make WordPress Core

Ticket #29684: 29684.17.diff

File 29684.17.diff, 19.2 KB (added by spacedmonkey, 7 years ago)
  • src/wp-includes/class-wp-network.php

     
    148148                        case 'id':
    149149                                return (int) $this->id;
    150150                        case 'blog_id':
    151                                 return $this->blog_id;
    152151                        case 'site_id':
     152                                $this->get_main_site_id();
    153153                                return (int) $this->blog_id;
    154154                }
    155155
     
    202202        }
    203203
    204204        /**
     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                /*
     216                 * Filters the main site ID.
     217                 *
     218                 * Returning anything other than null will effectively short-circuit the function, returning
     219                                                                                                   * the result parsed as an integer immediately.
     220                 *
     221                 * @since 4.9.0
     222                             *
     223                 * @param int|null $main_site_id If anything other than null is returned, it is interpreted as the main site ID.
     224                 * @param int $network_id The ID of the network for which the main site was detected.
     225                 */
     226                $main_site_id = apply_filters( 'pre_get_main_site_id', null, $this->id );
     227                if ( null !== $main_site_id ) {
     228                        $this->blog_id = (int) $main_site_id;
     229                        return;
     230                }
     231               
     232                if ( ! empty( $this->blog_id ) ) {
     233                        return;
     234                }
     235
     236
     237
     238                if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE )
     239                     || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) {
     240                        if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     241                                $this->blog_id = BLOG_ID_CURRENT_SITE;
     242                                return;
     243                        } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     244                                $this->blog_id = BLOGID_CURRENT_SITE;
     245                                return;
     246                        }
     247                }
     248
     249                $site = get_site();
     250                if ( $site->domain === $this->domain && $site->path === $this->path ) {
     251                        $main_site_id = $site->id;
     252                } else {
     253                        $main_site_id = wp_cache_get( 'network:' . $this->id . ':main_site', 'site-options' );
     254                        if ( false === $main_site_id ) {
     255                                $_sites       = get_sites( array(
     256                                        'fields'     => 'ids',
     257                                        'number'     => 1,
     258                                        'domain'     => $this->domain,
     259                                        'path'       => $this->path,
     260                                        'network_id' => $this->id,
     261                                ) );
     262                                $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
     263
     264                                wp_cache_add( 'network:' . $this->id . ':main_site', $main_site_id, 'site-options' );
     265                        }
     266                }
     267
     268                $this->blog_id = (int) $main_site_id;
     269
     270                return;
     271        }
     272
     273        /**
    205274         * Set the site name assigned to the network if one has not been populated.
    206275         *
    207276         * @since 4.4.0
  • src/wp-includes/functions.php

     
    43924392 * Determine whether a site is the main site of the current network.
    43934393 *
    43944394 * @since 3.0.0
     4395 * @since 4.9.0 The $network_id parameter has been added.
    43954396 *
    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.
    43974400 * @return bool True if $site_id is the main site of the network, or if not
    43984401 *              running Multisite.
    43994402 */
    4400 function is_main_site( $site_id = null ) {
    4401         if ( ! is_multisite() )
     4403function is_main_site( $site_id = null, $network_id = null ) {
     4404        if ( ! is_multisite() ) {
    44024405                return true;
     4406        }
    44034407
    4404         if ( ! $site_id )
     4408        if ( ! $site_id ) {
    44054409                $site_id = get_current_blog_id();
     4410        }
    44064411
    4407         return (int) $site_id === (int) get_network()->site_id;
     4412        return (int) $site_id === get_main_site_id( $network_id );
    44084413}
    44094414
    44104415/**
     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 */
     4424function 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        return $network->site_id;
     4435}
     4436
     4437/**
    44114438 * Determine whether a network is the main network of the Multisite installation.
    44124439 *
    44134440 * @since 3.7.0
  • src/wp-includes/ms-load.php

     
    135135
    136136/**
    137137 * Retrieves the closest matching site object by its domain and path.
    138  * 
     138 *
    139139 * This will not necessarily return an exact match for a domain and path. Instead, it
    140140 * breaks the domain and path into pieces that are then used to match the closest
    141141 * possibility from a query.
     
    289289                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    290290                $current_site->domain = DOMAIN_CURRENT_SITE;
    291291                $current_site->path = PATH_CURRENT_SITE;
    292                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
    293                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    294                 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
    295                         $current_site->blog_id = BLOGID_CURRENT_SITE;
    296                 }
    297 
    298292                if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
    299293                        $current_blog = get_site_by_path( $domain, $path );
    300294                } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
     
    424418
    425419        // Figure out the current network's main site.
    426420        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                 }
     421                $current_site->blog_id = get_main_site_id( $current_site );
    434422        }
    435423
    436424        return true;
  • tests/phpunit/tests/multisite/getMainSiteId.php

     
     1<?php
     2
     3class 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() {
     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_with_different_network_returns_correct_id() {
     70                $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
     71        }
     72
     73        /**
     74         * @ticket 29684
     75         */
     76        public function test_get_main_site_id_on_network_without_site_returns_0() {
     77                $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
     78        }
     79
     80        /**
     81         * @ticket 29684
     82         */
     83        public function test_get_main_site_id_on_invalid_network_returns_0() {
     84                $this->assertSame( 0, get_main_site_id( 333 ) );
     85        }
     86
     87        /**
     88         * @ticket 29684
     89         */
     90        public function test_get_main_site_id_filtered() {
     91                add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
     92                $result = get_main_site_id();
     93
     94                $this->assertSame( 333, $result );
     95        }
     96
     97        public function filter_get_main_site_id() {
     98                return 333;
     99        }
     100}
     101<?php
     102
     103class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase {
     104        protected static $network_ids;
     105        protected static $site_ids;
     106
     107        public static function wpSetUpBeforeClass( $factory ) {
     108                self::$network_ids = array(
     109                        'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
     110                        'wp.org/'        => array( 'domain' => 'wp.org',        'path' => '/' ), // A network with no sites.
     111                );
     112
     113                foreach ( self::$network_ids as &$id ) {
     114                        $id = $factory->network->create( $id );
     115                }
     116                unset( $id );
     117
     118                self::$site_ids = array(
     119                        'www.w.org/'                   => array( 'domain' => 'www.w.org',     'path' => '/' ),
     120                        'wordpress.org/'               => array( 'domain' => 'wordpress.org', 'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
     121                        'wordpress.org/foo/'           => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ),
     122                );
     123
     124                foreach ( self::$site_ids as &$id ) {
     125                        $id = $factory->blog->create( $id );
     126                }
     127                unset( $id );
     128        }
     129
     130        public static function wpTearDownAfterClass() {
     131                foreach( self::$site_ids as $id ) {
     132                        wpmu_delete_blog( $id, true );
     133                }
     134
     135                global $wpdb;
     136
     137                foreach( self::$network_ids as $id ) {
     138                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     139                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     140                }
     141
     142                wp_update_network_site_counts();
     143        }
     144
     145        /**
     146         * @ticket 29684
     147         */
     148        public function test_get_main_site_id_on_main_site_returns_self() {
     149                $this->assertSame( get_current_blog_id(), get_main_site_id() );
     150        }
     151
     152        /**
     153         * @ticket 29684
     154         */
     155        public function test_get_main_site_id_returns_main_site_in_switched_context() {
     156                $main_site_id = get_current_blog_id();
     157                $other_site_id = self::$site_ids['www.w.org/'];
     158
     159                switch_to_blog( $other_site_id );
     160                $result = get_main_site_id();
     161                restore_current_blog();
     162
     163                $this->assertSame( $main_site_id, $result );
     164        }
     165
     166        /**
     167         * @ticket 29684
     168         */
     169        public function test_get_main_site_id_with_different_network_returns_correct_id() {
     170                $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
     171        }
     172
     173        /**
     174         * @ticket 29684
     175         */
     176        public function test_get_main_site_id_on_network_without_site_returns_0() {
     177                $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
     178        }
     179
     180        /**
     181         * @ticket 29684
     182         */
     183        public function test_get_main_site_id_on_invalid_network_returns_0() {
     184                $this->assertSame( 0, get_main_site_id( 333 ) );
     185        }
     186
     187        /**
     188         * @ticket 29684
     189         */
     190        public function test_get_main_site_id_filtered() {
     191                add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
     192                $result = get_main_site_id();
     193
     194                $this->assertSame( 333, $result );
     195        }
     196
     197        public function filter_get_main_site_id() {
     198                return 333;
     199        }
     200}
     201<?php
     202
     203class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase {
     204        protected static $network_ids;
     205        protected static $site_ids;
     206
     207        public static function wpSetUpBeforeClass( $factory ) {
     208                self::$network_ids = array(
     209                        'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
     210                        'wp.org/'        => array( 'domain' => 'wp.org',        'path' => '/' ), // A network with no sites.
     211                );
     212
     213                foreach ( self::$network_ids as &$id ) {
     214                        $id = $factory->network->create( $id );
     215                }
     216                unset( $id );
     217
     218                self::$site_ids = array(
     219                        'www.w.org/'                   => array( 'domain' => 'www.w.org',     'path' => '/' ),
     220                        'wordpress.org/'               => array( 'domain' => 'wordpress.org', 'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
     221                        'wordpress.org/foo/'           => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ),
     222                );
     223
     224                foreach ( self::$site_ids as &$id ) {
     225                        $id = $factory->blog->create( $id );
     226                }
     227                unset( $id );
     228        }
     229
     230        public static function wpTearDownAfterClass() {
     231                foreach( self::$site_ids as $id ) {
     232                        wpmu_delete_blog( $id, true );
     233                }
     234
     235                global $wpdb;
     236
     237                foreach( self::$network_ids as $id ) {
     238                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     239                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     240                }
     241
     242                wp_update_network_site_counts();
     243        }
     244
     245        /**
     246         * @ticket 29684
     247         */
     248        public function test_get_main_site_id_on_main_site_returns_self() {
     249                $this->assertSame( get_current_blog_id(), get_main_site_id() );
     250        }
     251
     252        /**
     253         * @ticket 29684
     254         */
     255        public function test_get_main_site_id_returns_main_site_in_switched_context() {
     256                $main_site_id = get_current_blog_id();
     257                $other_site_id = self::$site_ids['www.w.org/'];
     258
     259                switch_to_blog( $other_site_id );
     260                $result = get_main_site_id();
     261                restore_current_blog();
     262
     263                $this->assertSame( $main_site_id, $result );
     264        }
     265
     266        /**
     267         * @ticket 29684
     268         */
     269        public function test_get_main_site_id_with_different_network_returns_correct_id() {
     270                $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
     271        }
     272
     273        /**
     274         * @ticket 29684
     275         */
     276        public function test_get_main_site_id_on_network_without_site_returns_0() {
     277                $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
     278        }
     279
     280        /**
     281         * @ticket 29684
     282         */
     283        public function test_get_main_site_id_on_invalid_network_returns_0() {
     284                $this->assertSame( 0, get_main_site_id( 333 ) );
     285        }
     286
     287        /**
     288         * @ticket 29684
     289         */
     290        public function test_get_main_site_id_filtered() {
     291                add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
     292                $result = get_main_site_id();
     293
     294                $this->assertSame( 333, $result );
     295        }
     296
     297        public function filter_get_main_site_id() {
     298                return 333;
     299        }
     300}
     301<?php
     302
     303class Tests_Multisite_Get_Main_Site_ID extends WP_UnitTestCase {
     304        protected static $network_ids;
     305        protected static $site_ids;
     306
     307        public static function wpSetUpBeforeClass( $factory ) {
     308                self::$network_ids = array(
     309                        'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ),
     310                        'wp.org/'        => array( 'domain' => 'wp.org',        'path' => '/' ), // A network with no sites.
     311                );
     312
     313                foreach ( self::$network_ids as &$id ) {
     314                        $id = $factory->network->create( $id );
     315                }
     316                unset( $id );
     317
     318                self::$site_ids = array(
     319                        'www.w.org/'                   => array( 'domain' => 'www.w.org',     'path' => '/' ),
     320                        'wordpress.org/'               => array( 'domain' => 'wordpress.org', 'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
     321                        'wordpress.org/foo/'           => array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ),
     322                );
     323
     324                foreach ( self::$site_ids as &$id ) {
     325                        $id = $factory->blog->create( $id );
     326                }
     327                unset( $id );
     328        }
     329
     330        public static function wpTearDownAfterClass() {
     331                foreach( self::$site_ids as $id ) {
     332                        wpmu_delete_blog( $id, true );
     333                }
     334
     335                global $wpdb;
     336
     337                foreach( self::$network_ids as $id ) {
     338                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     339                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     340                }
     341
     342                wp_update_network_site_counts();
     343        }
     344
     345        /**
     346         * @ticket 29684
     347         */
     348        public function test_get_main_site_id_on_main_site_returns_self() {
     349                $this->assertSame( get_current_blog_id(), get_main_site_id() );
     350        }
     351
     352        /**
     353         * @ticket 29684
     354         */
     355        public function test_get_main_site_id_returns_main_site_in_switched_context() {
     356                $main_site_id = get_current_blog_id();
     357                $other_site_id = self::$site_ids['www.w.org/'];
     358
     359                switch_to_blog( $other_site_id );
     360                $result = get_main_site_id();
     361                restore_current_blog();
     362
     363                $this->assertSame( $main_site_id, $result );
     364        }
     365
     366        /**
     367         * @ticket 29684
     368         */
     369        public function test_get_main_site_id_with_different_network_returns_correct_id() {
     370                $this->assertSame( self::$site_ids['wordpress.org/'], get_main_site_id( self::$network_ids['wordpress.org/'] ) );
     371        }
     372
     373        /**
     374         * @ticket 29684
     375         */
     376        public function test_get_main_site_id_on_network_without_site_returns_0() {
     377                $this->assertSame( 0, get_main_site_id( self::$network_ids['wp.org/'] ) );
     378        }
     379
     380        /**
     381         * @ticket 29684
     382         */
     383        public function test_get_main_site_id_on_invalid_network_returns_0() {
     384                $this->assertSame( 0, get_main_site_id( 333 ) );
     385        }
     386
     387        /**
     388         * @ticket 29684
     389         */
     390        public function test_get_main_site_id_filtered() {
     391                add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) );
     392                $result = get_main_site_id();
     393
     394                $this->assertSame( 333, $result );
     395        }
     396
     397        public function filter_get_main_site_id() {
     398                return 333;
     399        }
     400}
  • tests/phpunit/tests/multisite/site.php

    Property changes on: tests/phpunit/tests/multisite/getMainSiteId.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    741741                restore_current_blog();
    742742        }
    743743
     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
    744765        function test_switch_upload_dir() {
    745766                $this->assertTrue( is_main_site() );
    746767