Make WordPress Core


Ignore:
Timestamp:
05/20/2016 08:56:54 PM (9 years ago)
Author:
jeremyfelt
Message:

Multisite: Wrap the main bootstrap process in a function

Introduce ms_load_current_site_and_network. This is used by core during the multisite bootstrap process to populate the $current_site and $current_blog globals based on a requested domain and path.

Return values from this function inform ms-settings.php as to whether a page view should continue, ms_not_installed() should fire, or a redirect to a new location should occur.

This was previously a procedural block in ms-settings.php. Wrapping this code and providing specific return values allows us to write tests that do not rely on the manual and repeated inclusion of ms-settings.php.

This should not be used by plugins or themes. Please.

See #34941.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-load.php

    r35668 r37475  
    259259
    260260    return false;
     261}
     262
     263/**
     264 * Identifies the network and site of a requested domain and path and populates the
     265 * corresponding network and site global objects as part of the multisite bootstrap process.
     266 *
     267 * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into
     268 * a function to facilitate unit tests. It should not be used outside of core.
     269 *
     270 * Usually, it's easier to query the site first, which then declares its network.
     271 * In limited situations, we either can or must find the network first.
     272 *
     273 * If a network and site are found, a `true` response will be returned so that the
     274 * request can continue.
     275 *
     276 * If neither a network or site is found, `false` or a URL string will be returned
     277 * so that either an error can be shown or a redirect can occur.
     278 *
     279 * @since 4.6.0
     280 * @access private
     281 *
     282 * @global wpdb       $wpdb         WordPress database abstraction object.
     283 * @global WP_Network $current_site The current network.
     284 * @global WP_Site    $current_blog The current site.
     285 *
     286 * @param string $domain    The requested domain.
     287 * @param string $path      The requested path.
     288 * @param bool   $subdomain Whether a subdomain (true) or subdirectory (false) configuration.
     289 * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
     290 *                     False if bootstrap could not be properly completed.
     291 *                     Redirect URL if parts exist, but the request as a whole can not be fulfilled.
     292 */
     293function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
     294    global $wpdb, $current_site, $current_blog;
     295
     296    // If the network is defined in wp-config.php, we can simply use that.
     297    if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
     298        $current_site = new stdClass;
     299        $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
     300        $current_site->domain = DOMAIN_CURRENT_SITE;
     301        $current_site->path = PATH_CURRENT_SITE;
     302        if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
     303            $current_site->blog_id = BLOG_ID_CURRENT_SITE;
     304        } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
     305            $current_site->blog_id = BLOGID_CURRENT_SITE;
     306        }
     307
     308        if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
     309            $current_blog = get_site_by_path( $domain, $path );
     310        } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
     311            // If the current network has a path and also matches the domain and path of the request,
     312            // we need to look for a site using the first path segment following the network's path.
     313            $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
     314        } else {
     315            // Otherwise, use the first path segment (as usual).
     316            $current_blog = get_site_by_path( $domain, $path, 1 );
     317        }
     318
     319    } elseif ( ! $subdomain ) {
     320        /*
     321         * A "subdomain" install can be re-interpreted to mean "can support any domain".
     322         * If we're not dealing with one of these installs, then the important part is determining
     323         * the network first, because we need the network's path to identify any sites.
     324         */
     325        if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
     326            // Are there even two networks installed?
     327            $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
     328            if ( 1 === $wpdb->num_rows ) {
     329                $current_site = new WP_Network( $one_network );
     330                wp_cache_add( 'current_network', $current_site, 'site-options' );
     331            } elseif ( 0 === $wpdb->num_rows ) {
     332                // A network not found hook should fire here.
     333                return false;
     334            }
     335        }
     336
     337        if ( empty( $current_site ) ) {
     338            $current_site = WP_Network::get_by_path( $domain, $path, 1 );
     339        }
     340
     341        if ( empty( $current_site ) ) {
     342            /**
     343             * Fires when a network cannot be found based on the requested domain and path.
     344             *
     345             * At the time of this action, the only recourse is to redirect somewhere
     346             * and exit. If you want to declare a particular network, do so earlier.
     347             *
     348             * @since 4.4.0
     349             *
     350             * @param string $domain       The domain used to search for a network.
     351             * @param string $path         The path used to search for a path.
     352             */
     353            do_action( 'ms_network_not_found', $domain, $path );
     354
     355            return false;
     356        } elseif ( $path === $current_site->path ) {
     357            $current_blog = get_site_by_path( $domain, $path );
     358        } else {
     359            // Search the network path + one more path segment (on top of the network path).
     360            $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
     361        }
     362    } else {
     363        // Find the site by the domain and at most the first path segment.
     364        $current_blog = get_site_by_path( $domain, $path, 1 );
     365        if ( $current_blog ) {
     366            $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
     367        } else {
     368            // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
     369            $current_site = WP_Network::get_by_path( $domain, $path, 1 );
     370        }
     371    }
     372
     373    // The network declared by the site trumps any constants.
     374    if ( $current_blog && $current_blog->site_id != $current_site->id ) {
     375        $current_site = WP_Network::get_instance( $current_blog->site_id );
     376    }
     377
     378    // No network has been found, bail.
     379    if ( empty( $current_site ) ) {
     380        /** This action is documented in wp-includes/ms-settings.php */
     381        do_action( 'ms_network_not_found', $domain, $path );
     382
     383        return false;
     384    }
     385
     386    // During activation of a new subdomain, the requested site does not yet exist.
     387    if ( empty( $current_blog ) && wp_installing() ) {
     388        $current_blog = new stdClass;
     389        $current_blog->blog_id = $blog_id = 1;
     390        $current_blog->public = 1;
     391    }
     392
     393    // No site has been found, bail.
     394    if ( empty( $current_blog ) ) {
     395        // We're going to redirect to the network URL, with some possible modifications.
     396        $scheme = is_ssl() ? 'https' : 'http';
     397        $destination = "$scheme://{$current_site->domain}{$current_site->path}";
     398
     399        /**
     400         * Fires when a network can be determined but a site cannot.
     401         *
     402         * At the time of this action, the only recourse is to redirect somewhere
     403         * and exit. If you want to declare a particular site, do so earlier.
     404         *
     405         * @since 3.9.0
     406         *
     407         * @param object $current_site The network that had been determined.
     408         * @param string $domain       The domain used to search for a site.
     409         * @param string $path         The path used to search for a site.
     410         */
     411        do_action( 'ms_site_not_found', $current_site, $domain, $path );
     412
     413        if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
     414            // For a "subdomain" install, redirect to the signup form specifically.
     415            $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
     416        } elseif ( $subdomain ) {
     417            // For a "subdomain" install, the NOBLOGREDIRECT constant
     418            // can be used to avoid a redirect to the signup form.
     419            // Using the ms_site_not_found action is preferred to the constant.
     420            if ( '%siteurl%' !== NOBLOGREDIRECT ) {
     421                $destination = NOBLOGREDIRECT;
     422            }
     423        } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
     424            /*
     425             * If the domain we were searching for matches the network's domain,
     426             * it's no use redirecting back to ourselves -- it'll cause a loop.
     427             * As we couldn't find a site, we're simply not installed.
     428             */
     429            return false;
     430        }
     431
     432        return $destination;
     433    }
     434
     435    // Figure out the current network's main site.
     436    if ( empty( $current_site->blog_id ) ) {
     437        if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     438            $current_site->blog_id = $current_blog->blog_id;
     439        } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
     440            $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     441                $current_site->domain, $current_site->path ) );
     442            wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
     443        }
     444    }
     445
     446    return true;
    261447}
    262448
Note: See TracChangeset for help on using the changeset viewer.