Make WordPress Core

Ticket #29684: 29684.5.diff

File 29684.5.diff, 7.9 KB (added by flixos90, 6 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

     
    43894389 * Determine whether a site is the main site of the current network.
    43904390 *
    43914391 * @since 3.0.0
     4392 * @since 4.9.0 The $network_id parameter has been added.
    43924393 *
    4393  * @param int $site_id Optional. Site ID to test. Defaults to current site.
     4394 * @param int $site_id    Optional. Site ID to test. Defaults to current site.
     4395 * @param int $network_id Optional. Network ID of the network to check for.
     4396 *                        Defaults to current network.
    43944397 * @return bool True if $site_id is the main site of the network, or if not
    43954398 *              running Multisite.
    43964399 */
    4397 function is_main_site( $site_id = null ) {
    4398         if ( ! is_multisite() )
     4400function is_main_site( $site_id = null, $network_id = null ) {
     4401        if ( ! is_multisite() ) {
    43994402                return true;
     4403        }
    44004404
    4401         if ( ! $site_id )
     4405        if ( ! $site_id ) {
    44024406                $site_id = get_current_blog_id();
     4407        }
     4408
     4409        $site_id = (int) $site_id;
     4410
     4411        return $site_id === get_main_site_id( $network_id );
     4412}
     4413
     4414/**
     4415 * Gets the main site ID.
     4416 *
     4417 * @since 4.9.0
     4418 *
     4419 * @param int $network_id Optional. The ID of the network for which to get the main site.
     4420 *                        Defaults to the current network.
     4421 * @return int The ID of the main site.
     4422 */
     4423function get_main_site_id( $network_id = null ) {
     4424        if ( ! is_multisite() ) {
     4425                return 1;
     4426        }
    44034427
    4404         return (int) $site_id === (int) get_network()->site_id;
     4428        $network = get_network( $network_id );
     4429        if ( ! $network ) {
     4430                return 0;
     4431        }
     4432
     4433        if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE )
     4434                || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) {
     4435                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     4436                        return BLOG_ID_CURRENT_SITE;
     4437                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     4438                        return BLOGID_CURRENT_SITE;
     4439                }
     4440        }
     4441
     4442        $site = get_site();
     4443        if ( $site->domain === $network->domain && $site->path === $network->path ) {
     4444                $main_site_id = (int) $site->id;
     4445        } else {
     4446                $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' );
     4447                if ( false === $main_site_id ) {
     4448                        $_sites = get_sites( array(
     4449                                'fields'     => 'ids',
     4450                                'number'     => 1,
     4451                                'domain'     => $network->domain,
     4452                                'path'       => $network->path,
     4453                                'network_id' => $network->id,
     4454                        ) );
     4455                        $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
     4456
     4457                        wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' );
     4458                }
     4459        }
     4460
     4461        /**
     4462         * Filters the main site ID.
     4463         *
     4464         * @since 4.9.0
     4465         *
     4466         * @param int $main_site_id The ID of the main site.
     4467         * @param int $network_id   The ID of the network for which the main site was detected.
     4468         */
     4469        return (int) apply_filters( 'get_main_site_id', $main_site_id, $network->id );
    44054470}
    44064471
    44074472/**
  • 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/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
     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
    744845        function test_switch_upload_dir() {
    745846                $this->assertTrue( is_main_site() );
    746847