Make WordPress Core


Ignore:
Timestamp:
05/20/2016 08:56:54 PM (10 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-settings.php

    r37226 r37475  
    4242ms_subdomain_constants();
    4343
     44// This block will process a request if the current network or current site objects
     45// have not been populated in the global scope through something like `sunrise.php`.
    4446if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    45 
    46         // Given the domain and path, let's try to identify the network and site.
    47         // Usually, it's easier to query the site first, which declares its network.
    48         // In limited situations, though, we either can or must find the network first.
    4947
    5048        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
    … …  
    6361        list( $path ) = explode( '?', $path );
    6462
    65         // If the network is defined in wp-config.php, we can simply use that.
    66         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    67                 $current_site = new stdClass;
    68                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    69                 $current_site->domain = DOMAIN_CURRENT_SITE;
    70                 $current_site->path = PATH_CURRENT_SITE;
    71                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
    72                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    73                 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
    74                         $current_site->blog_id = BLOGID_CURRENT_SITE;
    75                 }
     63        $bootstrap_result = ms_load_current_site_and_network( $domain, $path, is_subdomain_install() );
    7664
    77                 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
    78                         $current_blog = get_site_by_path( $domain, $path );
    79                 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
    80                         // If the current network has a path and also matches the domain and path of the request,
    81                         // we need to look for a site using the first path segment following the network's path.
    82                         $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
    83                 } else {
    84                         // Otherwise, use the first path segment (as usual).
    85                         $current_blog = get_site_by_path( $domain, $path, 1 );
    86                 }
    87 
    88         } elseif ( ! is_subdomain_install() ) {
    89                 /*
    90                  * A "subdomain" install can be re-interpreted to mean "can support any domain".
    91                  * If we're not dealing with one of these installs, then the important part is determining
    92                  * the network first, because we need the network's path to identify any sites.
    93                  */
    94                 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
    95                         // Are there even two networks installed?
    96                         $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
    97                         if ( 1 === $wpdb->num_rows ) {
    98                                 $current_site = new WP_Network( $one_network );
    99                                 wp_cache_add( 'current_network', $current_site, 'site-options' );
    100                         } elseif ( 0 === $wpdb->num_rows ) {
    101                                 ms_not_installed( $domain, $path );
    102                         }
    103                 }
    104                 if ( empty( $current_site ) ) {
    105                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
    106                 }
    107 
    108                 if ( empty( $current_site ) ) {
    109                         /**
    110                          * Fires when a network cannot be found based on the requested domain and path.
    111                          *
    112                          * At the time of this action, the only recourse is to redirect somewhere
    113                          * and exit. If you want to declare a particular network, do so earlier.
    114                          *
    115                          * @since 4.4.0
    116                          *
    117                          * @param string $domain       The domain used to search for a network.
    118                          * @param string $path         The path used to search for a path.
    119                          */
    120                         do_action( 'ms_network_not_found', $domain, $path );
    121 
    122                         ms_not_installed( $domain, $path );
    123                 } elseif ( $path === $current_site->path ) {
    124                         $current_blog = get_site_by_path( $domain, $path );
    125                 } else {
    126                         // Search the network path + one more path segment (on top of the network path).
    127                         $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
    128                 }
     65        if ( true === $bootstrap_result ) {
     66                // `$current_blog` and `$current_site are now populated.
     67        } elseif ( false === $bootstrap_result ) {
     68                ms_not_installed( $domain, $path );
    12969        } else {
    130                 // Find the site by the domain and at most the first path segment.
    131                 $current_blog = get_site_by_path( $domain, $path, 1 );
    132                 if ( $current_blog ) {
    133                         $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
    134                 } else {
    135                         // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
    136                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
    137                 }
    138         }
    139 
    140         // The network declared by the site trumps any constants.
    141         if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    142                 $current_site = WP_Network::get_instance( $current_blog->site_id );
    143         }
    144 
    145         // No network has been found, bail.
    146         if ( empty( $current_site ) ) {
    147                 /** This action is documented in wp-includes/ms-settings.php */
    148                 do_action( 'ms_network_not_found', $domain, $path );
    149 
    150                 ms_not_installed( $domain, $path );
    151         }
    152 
    153         // During activation of a new subdomain, the requested site does not yet exist.
    154         if ( empty( $current_blog ) && wp_installing() ) {
    155                 $current_blog = new stdClass;
    156                 $current_blog->blog_id = $blog_id = 1;
    157                 $current_blog->public = 1;
    158         }
    159 
    160         // No site has been found, bail.
    161         if ( empty( $current_blog ) ) {
    162                 // We're going to redirect to the network URL, with some possible modifications.
    163                 $scheme = is_ssl() ? 'https' : 'http';
    164                 $destination = "$scheme://{$current_site->domain}{$current_site->path}";
    165 
    166                 /**
    167                  * Fires when a network can be determined but a site cannot.
    168                  *
    169                  * At the time of this action, the only recourse is to redirect somewhere
    170                  * and exit. If you want to declare a particular site, do so earlier.
    171                  *
    172                  * @since 3.9.0
    173                  *
    174                  * @param object $current_site The network that had been determined.
    175                  * @param string $domain       The domain used to search for a site.
    176                  * @param string $path         The path used to search for a site.
    177                  */
    178                 do_action( 'ms_site_not_found', $current_site, $domain, $path );
    179 
    180                 if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) {
    181                         // For a "subdomain" install, redirect to the signup form specifically.
    182                         $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
    183                 } elseif ( is_subdomain_install() ) {
    184                         // For a "subdomain" install, the NOBLOGREDIRECT constant
    185                         // can be used to avoid a redirect to the signup form.
    186                         // Using the ms_site_not_found action is preferred to the constant.
    187                         if ( '%siteurl%' !== NOBLOGREDIRECT ) {
    188                                 $destination = NOBLOGREDIRECT;
    189                         }
    190                 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
    191                         /*
    192                          * If the domain we were searching for matches the network's domain,
    193                          * it's no use redirecting back to ourselves -- it'll cause a loop.
    194                          * As we couldn't find a site, we're simply not installed.
    195                          */
    196                         ms_not_installed( $domain, $path );
    197                 }
    198 
    199                 header( 'Location: ' . $destination );
     70                header( 'Location: ' . $bootstrap_result );
    20071                exit;
    20172        }
    202 
    203         // Figure out the current network's main site.
    204         if ( empty( $current_site->blog_id ) ) {
    205                 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    206                         $current_site->blog_id = $current_blog->blog_id;
    207                 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
    208                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    209                                 $current_site->domain, $current_site->path ) );
    210                         wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
    211                 }
    212         }
     73        unset( $bootstrap_result );
    21374
    21475        $blog_id = $current_blog->blog_id;
Note: See TracChangeset for help on using the changeset viewer.