WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/14/2017 10:41:15 PM (3 years ago)
Author:
flixos90
Message:

Multisite: Take WP_Network::$blog_id into account in get_main_site_id().

When the WP_Network::$blog_id property is set manually, for example in the multisite bootstrap process, get_main_site_id() should use that value instead of running its own logic. The main logic for the function was therefore moved into the internal WP_Network::get_main_site_id() method, which is now being accessed by the function through the magic property handling for WP_Network::$blog_id (and its equivalent WP_Network::$site_id).

Props spacedmonkey, jeremyfelt.
Fixes #41936.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-network.php

    r41380 r41861  
    149149                return (int) $this->id;
    150150            case 'blog_id':
     151                return (string) $this->get_main_site_id();
     152            case 'site_id':
    151153                return $this->get_main_site_id();
    152             case 'site_id':
    153                 return (int) $this->get_main_site_id();
    154154        }
    155155
     
    209209     *
    210210     * @since 4.9.0
    211      * @see get_main_site_id()
    212      *
    213      * @return string Main site ID as numeric string, for compatibility reasons.
     211     *
     212     * @return int The ID of the main site.
    214213     */
    215214    private function get_main_site_id() {
    216         if ( empty( $this->blog_id ) ) {
    217             $this->blog_id = (string) get_main_site_id( $this->id );
    218         }
    219 
    220         return $this->blog_id;
     215        /**
     216         * Filters the main site ID.
     217         *
     218         * Returning a positive integer will effectively short-circuit the function.
     219         *
     220         * @since 4.9.0
     221         *
     222         * @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
     223         * @param int $network_id The ID of the network for which the main site was detected.
     224         */
     225        $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this->id );
     226        if ( 0 < $main_site_id ) {
     227            return $main_site_id;
     228        }
     229
     230        if ( 0 < (int) $this->blog_id ) {
     231            return (int) $this->blog_id;
     232        }
     233
     234        if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $this->domain === DOMAIN_CURRENT_SITE && $this->path === PATH_CURRENT_SITE )
     235            || ( defined( 'SITE_ID_CURRENT_SITE' ) && $this->id == SITE_ID_CURRENT_SITE ) ) {
     236            if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     237                $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
     238
     239                return (int) $this->blog_id;
     240            }
     241
     242            if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     243                $this->blog_id = (string) BLOGID_CURRENT_SITE;
     244
     245                return (int) $this->blog_id;
     246            }
     247        }
     248
     249        $site = get_site();
     250        if ( $site->domain === $this->domain && $site->path === $this->path ) {
     251            $main_site_id = (int) $site->id;
     252        } else {
     253            $cache_key = 'network:' . $this->id . ':main_site';
     254
     255            $main_site_id = wp_cache_get( $cache_key, 'site-options' );
     256            if ( false === $main_site_id ) {
     257                $_sites = get_sites( array(
     258                    'fields'     => 'ids',
     259                    'number'     => 1,
     260                    'domain'     => $this->domain,
     261                    'path'       => $this->path,
     262                    'network_id' => $this->id,
     263                ) );
     264                $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
     265
     266                wp_cache_add( $cache_key, $main_site_id, 'site-options' );
     267            }
     268        }
     269
     270        $this->blog_id = (string) $main_site_id;
     271
     272        return (int) $this->blog_id;
    221273    }
    222274
Note: See TracChangeset for help on using the changeset viewer.