Make WordPress Core

Ticket #29684: 29684.13.diff

File 29684.13.diff, 9.6 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;
     151                                return $this->get_main_site_id();
    152152                        case 'site_id':
    153                                 return (int) $this->blog_id;
     153                                return (int) $this->get_main_site_id();
    154154                }
    155155
    156156                return null;
     
    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                        $this->blog_id = get_main_site_id( $this->id );
     217                }
     218
     219                return $this->blog_id;
     220        }
     221
     222        /**
    205223         * Set the site name assigned to the network if one has not been populated.
    206224         *
    207225         * @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 === (int) get_network( $network_id )->site_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        /**
     4430         * Filters the main site ID.
     4431         *
     4432         * Returning anything other than null will effectively short-circuit the function, returning
     4433         * the result parsed as an integer immediately.
     4434         *
     4435         * @since 4.9.0
     4436         *
     4437         * @param int|null $main_site_id If anything other than null is returned, it is interpreted as the main site ID.
     4438         * @param int $network_id The ID of the network for which the main site was detected.
     4439         */
     4440        $main_site_id = apply_filters( 'pre_get_main_site_id', null, $network_id );
     4441        if ( null !== $main_site_id ) {
     4442                return (int) $main_site_id;
     4443        }
     4444
     4445        $network = get_network( $network_id );
     4446        if ( ! $network ) {
     4447                return 0;
     4448        }
     4449
     4450        if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE )
     4451             || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) {
     4452                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     4453                        return BLOG_ID_CURRENT_SITE;
     4454                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     4455                        return BLOGID_CURRENT_SITE;
     4456                }
     4457        }
     4458
     4459        $site = get_site();
     4460        if ( $site->domain === $network->domain && $site->path === $network->path ) {
     4461                $main_site_id = (int) $site->id;
     4462        } else {
     4463                $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' );
     4464                if ( false === $main_site_id ) {
     4465                        $_sites       = get_sites( array(
     4466                                'fields'     => 'ids',
     4467                                'number'     => 1,
     4468                                'domain'     => $network->domain,
     4469                                'path'       => $network->path,
     4470                                'network_id' => $network->id,
     4471                        ) );
     4472                        $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
     4473
     4474                        wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' );
     4475                }
     4476        }
     4477
     4478        $main_site_id = (int) $main_site_id;
     4479
     4480        return $main_site_id;
    44084481}
    44094482
    44104483/**
  • 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.
     
    424424
    425425        // Figure out the current network's main site.
    426426        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 );
    434428        }
    435429
    436430        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}
  • tests/phpunit/tests/multisite/site.php

     
    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