Make WordPress Core

Ticket #29684: 29684.14.diff

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