Make WordPress Core

Ticket #34941: 34941.2.diff

File 34941.2.diff, 28.3 KB (added by jeremyfelt, 9 years ago)
  • src/wp-includes/ms-load.php

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

     
    1010 * @since 3.0.0
    1111 */
    1212
     13/**
     14 * Will hold objects representing the current network and current site.
     15 *
     16 * These may be populated through a custom `sunrise.php`. If not, then this
     17 * file will attempt to populate them based on the current request.
     18 *
     19 * @global WP_Network $current_site The current network.
     20 * @global object     $current_blog The current site.
     21 * @since 3.0.0
     22 */
     23global $current_site, $current_blog;
     24
    1325/** WP_Network class */
    1426require_once( ABSPATH . WPINC . '/class-wp-network.php' );
    1527
     
    2941/** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
    3042ms_subdomain_constants();
    3143
     44/**
     45 * This block will process a request when either the current network or current site
     46 * object is not yet populated in the global scope through something like `sunrise.php`.
     47 *
     48 * Possible outcomes:
     49 *
     50 * - `$current_site` and `$current_blog` are populated fully.
     51 * - In a subdirectory configuration, if 0 networks exist, show an error.
     52 * - In a subdirectory configuration, if no matching networks (domain/path) are found, show an error.
     53 * - In either configuration, if no matching network is found, show an error.
     54 * - In a subdomain configuration, if a network is found and a site is not found, redirect to signup.
     55 * - In a subdirectory configuration, if a network is found and a site is not found, redirect to main site.
     56 * - In a subdirectory configuration, if a network is found, a site is not found, and a redirect would
     57 *   cause a loop, show an error.
     58 */
    3259if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    3360
    34         // Given the domain and path, let's try to identify the network and site.
    35         // Usually, it's easier to query the site first, which declares its network.
    36         // In limited situations, though, we either can or must find the network first.
    37 
    3861        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
    3962        if ( substr( $domain, -3 ) == ':80' ) {
    4063                $domain = substr( $domain, 0, -3 );
     
    5073        }
    5174        list( $path ) = explode( '?', $path );
    5275
    53         // If the network is defined in wp-config.php, we can simply use that.
    54         if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    55                 $current_site = new stdClass;
    56                 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    57                 $current_site->domain = DOMAIN_CURRENT_SITE;
    58                 $current_site->path = PATH_CURRENT_SITE;
    59                 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
    60                         $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    61                 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
    62                         $current_site->blog_id = BLOGID_CURRENT_SITE;
    63                 }
     76        $bootstrap_result = wp_setup_current_site_and_blog( $domain, $path );
    6477
    65                 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
    66                         $current_blog = get_site_by_path( $domain, $path );
    67                 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
    68                         // If the current network has a path and also matches the domain and path of the request,
    69                         // we need to look for a site using the first path segment following the network's path.
    70                         $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
    71                 } else {
    72                         // Otherwise, use the first path segment (as usual).
    73                         $current_blog = get_site_by_path( $domain, $path, 1 );
    74                 }
    75 
    76         } elseif ( ! is_subdomain_install() ) {
    77                 /*
    78                  * A "subdomain" install can be re-interpreted to mean "can support any domain".
    79                  * If we're not dealing with one of these installs, then the important part is determining
    80                  * the network first, because we need the network's path to identify any sites.
    81                  */
    82                 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
    83                         // Are there even two networks installed?
    84                         $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
    85                         if ( 1 === $wpdb->num_rows ) {
    86                                 $current_site = new WP_Network( $one_network );
    87                                 wp_cache_add( 'current_network', $current_site, 'site-options' );
    88                         } elseif ( 0 === $wpdb->num_rows ) {
    89                                 ms_not_installed( $domain, $path );
    90                         }
    91                 }
    92                 if ( empty( $current_site ) ) {
    93                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
    94                 }
    95 
    96                 if ( empty( $current_site ) ) {
    97                         /**
    98                          * Fires when a network cannot be found based on the requested domain and path.
    99                          *
    100                          * At the time of this action, the only recourse is to redirect somewhere
    101                          * and exit. If you want to declare a particular network, do so earlier.
    102                          *
    103                          * @since 4.4.0
    104                          *
    105                          * @param string $domain       The domain used to search for a network.
    106                          * @param string $path         The path used to search for a path.
    107                          */
    108                         do_action( 'ms_network_not_found', $domain, $path );
    109 
    110                         ms_not_installed( $domain, $path );
    111                 } elseif ( $path === $current_site->path ) {
    112                         $current_blog = get_site_by_path( $domain, $path );
    113                 } else {
    114                         // Search the network path + one more path segment (on top of the network path).
    115                         $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
    116                 }
     78        if ( true === $bootstrap_result ) {
     79                // `$current_blog` and `$current_site are now populated.
     80        } elseif ( false === $bootstrap_result ) {
     81                ms_not_installed( $domain, $path );
    11782        } else {
    118                 // Find the site by the domain and at most the first path segment.
    119                 $current_blog = get_site_by_path( $domain, $path, 1 );
    120                 if ( $current_blog ) {
    121                         $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
    122                 } else {
    123                         // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
    124                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
    125                 }
    126         }
    127 
    128         // The network declared by the site trumps any constants.
    129         if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    130                 $current_site = WP_Network::get_instance( $current_blog->site_id );
    131         }
    132 
    133         // No network has been found, bail.
    134         if ( empty( $current_site ) ) {
    135                 /** This action is documented in wp-includes/ms-settings.php */
    136                 do_action( 'ms_network_not_found', $domain, $path );
    137 
    138                 ms_not_installed( $domain, $path );
    139         }
    140 
    141         // During activation of a new subdomain, the requested site does not yet exist.
    142         if ( empty( $current_blog ) && wp_installing() ) {
    143                 $current_blog = new stdClass;
    144                 $current_blog->blog_id = $blog_id = 1;
    145                 $current_blog->public = 1;
    146         }
    147 
    148         // No site has been found, bail.
    149         if ( empty( $current_blog ) ) {
    150                 // We're going to redirect to the network URL, with some possible modifications.
    151                 $scheme = is_ssl() ? 'https' : 'http';
    152                 $destination = "$scheme://{$current_site->domain}{$current_site->path}";
    153 
    154                 /**
    155                  * Fires when a network can be determined but a site cannot.
    156                  *
    157                  * At the time of this action, the only recourse is to redirect somewhere
    158                  * and exit. If you want to declare a particular site, do so earlier.
    159                  *
    160                  * @since 3.9.0
    161                  *
    162                  * @param object $current_site The network that had been determined.
    163                  * @param string $domain       The domain used to search for a site.
    164                  * @param string $path         The path used to search for a site.
    165                  */
    166                 do_action( 'ms_site_not_found', $current_site, $domain, $path );
    167 
    168                 if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) {
    169                         // For a "subdomain" install, redirect to the signup form specifically.
    170                         $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
    171                 } elseif ( is_subdomain_install() ) {
    172                         // For a "subdomain" install, the NOBLOGREDIRECT constant
    173                         // can be used to avoid a redirect to the signup form.
    174                         // Using the ms_site_not_found action is preferred to the constant.
    175                         if ( '%siteurl%' !== NOBLOGREDIRECT ) {
    176                                 $destination = NOBLOGREDIRECT;
    177                         }
    178                 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
    179                         /*
    180                          * If the domain we were searching for matches the network's domain,
    181                          * it's no use redirecting back to ourselves -- it'll cause a loop.
    182                          * As we couldn't find a site, we're simply not installed.
    183                          */
    184                         ms_not_installed( $domain, $path );
    185                 }
    186 
    187                 header( 'Location: ' . $destination );
     83                header( 'Location: ' . $bootstrap_result );
    18884                exit;
    18985        }
     86        unset( $bootstrap_result );
    19087
    191         // Figure out the current network's main site.
    192         if ( empty( $current_site->blog_id ) ) {
    193                 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    194                         $current_site->blog_id = $current_blog->blog_id;
    195                 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
    196                         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    197                                 $current_site->domain, $current_site->path ) );
    198                         wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
    199                 }
    200         }
    201 
    20288        $blog_id = $current_blog->blog_id;
    20389        $public  = $current_blog->public;
    20490
  • tests/phpunit/tests/multisite/bootstrap.php

     
    2323                parent::tearDown();
    2424        }
    2525
    26 
    2726        /**
    2827         * @ticket 27003
    2928         */
     
    186185                }
    187186                unset( $id );
    188187
    189                 $this->_setup_host_request( 'wordpress.org', '/' );
    190                 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    191                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     188                $requests = array(
     189                        'wordpress.org/' => array(
     190                                'network_id' => $network_ids['wordpress.org/'],
     191                                'site_id'    => $ids['wordpress.org/'],
     192                                'domain'     => 'wordpress.org',
     193                                'path'       => '/',
     194                                'response'   => true,
     195                                'message'    => false,
     196                        ),
     197                        'wordpress.org/2014/04/23/hello-world/' => array(
     198                                'network_id' => $network_ids['wordpress.org/'],
     199                                'site_id'    => $ids['wordpress.org/'],
     200                                'domain'     => 'wordpress.org',
     201                                'path'       => '/2014/04/23/hello-world/',
     202                                'response'   => true,
     203                                'message'    => 'A post or 404 on a site should result in the correct site and network.',
     204                        ),
     205                        'wordpress.org/sample-page/' => array(
     206                                'network_id' => $network_ids['wordpress.org/'],
     207                                'site_id'    => $ids['wordpress.org/'],
     208                                'domain'     => 'wordpress.org',
     209                                'path'       => '/sample-page/',
     210                                'response'   => true,
     211                                'message'    => 'A page or 404 on a site should result in the correct site and network.',
     212                        ),
     213                        'wordpress.org/?p=1' => array(
     214                                'network_id' => $network_ids['wordpress.org/'],
     215                                'site_id'    => $ids['wordpress.org/'],
     216                                'domain'     => 'wordpress.org',
     217                                'path'       => '/?p=1',
     218                                'response'   => true,
     219                                'message'    => 'A non pretty-permalink URL should result in the correct site and network.',
     220                        ),
     221                        'wordpress.org/wp-admin/' => array(
     222                                'network_id' => $network_ids['wordpress.org/'],
     223                                'site_id'    => $ids['wordpress.org/'],
     224                                'domain'     => 'wordpress.org',
     225                                'path'       => '/wp-admin/',
     226                                'response'   => true,
     227                                'message'    => 'The standard admin area of the main site should load the correct site and network.',
     228                        ),
     229                        'wordpress.org/wp-admin/network/' => array(
     230                                'network_id' => $network_ids['wordpress.org/'],
     231                                'site_id'    => $ids['wordpress.org/'],
     232                                'domain'     => 'wordpress.org',
     233                                'path'       => '/',
     234                                'response'   => true,
     235                                'message'    => 'The network admin area of the main site should load the correct site and network.',
     236                        ),
     237                        // See #17376, this is not correct behavior.
     238                        'wordpress.org/invalid/wp-admin/users.php' => array(
     239                                'network_id' => $network_ids['wordpress.org/'],
     240                                'site_id'    => $ids['wordpress.org/'],
     241                                'domain'     => 'wordpress.org',
     242                                'path'       => '/',
     243                                'response'   => true,
     244                                'message'    => 'If wp-admin is requested after an invalid path, the parent site and network should still load.',
     245                        ),
     246                        // See #17376, this is not correct behavior.
     247                        'wordpress.org/invalid/wp-admin/network/users.php' => array(
     248                                'network_id' => $network_ids['wordpress.org/'],
     249                                'site_id'    => $ids['wordpress.org/'],
     250                                'domain'     => 'wordpress.org',
     251                                'path'       => '/',
     252                                'response'   => true,
     253                                'message'    => 'Valid',
     254                        ),
     255                        'wordpress.org/foo/' => array(
     256                                'network_id' => $network_ids['wordpress.org/'],
     257                                'site_id'    => $ids['wordpress.org/foo/'],
     258                                'domain'     => 'wordpress.org',
     259                                'path'       => '/foo/',
     260                                'response'   => true,
     261                                'message'    => 'A valid sub-site should load the correct site and network.',
     262                        ),
     263                        'wordpress.org/FOO/' => array(
     264                                'network_id' => $network_ids['wordpress.org/'],
     265                                'site_id'    => $ids['wordpress.org/foo/'],
     266                                'domain'     => 'wordpress.org',
     267                                'path'       => '/FOO/',
     268                                'response'   => true,
     269                                'message'    => 'A valid sub-site should not be case sensitive.',
     270                        ),
     271                        'wordpress.org/foo/2014/04/23/hello-world/' => array(
     272                                'network_id' => $network_ids['wordpress.org/'],
     273                                'site_id'    => $ids['wordpress.org/foo/'],
     274                                'domain'     => 'wordpress.org',
     275                                'path'       => '/foo/2014/04/23/hello-world/',
     276                                'response'   => true,
     277                                'message'    => 'A post or 404 on a valid sub site should load the correct site and network.',
     278                        ),
     279                        'wordpress.org/foo/sample-page/' => array(
     280                                'network_id' => $network_ids['wordpress.org/'],
     281                                'site_id'    => $ids['wordpress.org/foo/'],
     282                                'domain'     => 'wordpress.org',
     283                                'path'       => '/foo/sample-page/',
     284                                'response'   => true,
     285                                'message'    => 'A page or 404 on a valid sub site should load the correct site and network.',
     286                        ),
     287                        'wordpress.org/foo/?p=1' => array(
     288                                'network_id' => $network_ids['wordpress.org/'],
     289                                'site_id'    => $ids['wordpress.org/foo/'],
     290                                'domain'     => 'wordpress.org',
     291                                'path'       => '/foo/?p=1',
     292                                'response'   => true,
     293                                'message'    => 'A non pretty-permalink URL on a valid sub site should load the correct site and network.',
     294                        ),
     295                        'wordpress.org/foo/wp-admin/' => array(
     296                                'network_id' => $network_ids['wordpress.org/'],
     297                                'site_id'    => $ids['wordpress.org/foo/'],
     298                                'domain'     => 'wordpress.org',
     299                                'path'       => '/foo/wp-admin/',
     300                                'response'   => true,
     301                                'message'    => 'If wp-admin is requested on a valid sub-site, the correct network and site should load.',
     302                        ),
     303                        // This will eventually redirect to the main site, but not during bootstrap.
     304                        'wordpress.org/foo/wp-admin/network/' => array(
     305                                'network_id' => $network_ids['wordpress.org/'],
     306                                'site_id'    => $ids['wordpress.org/foo/'],
     307                                'domain'    => 'wordpress.org',
     308                                'path'      => '/foo/wp-admin/network/',
     309                                'response'  => true,
     310                                'message'   => 'If the network admin is requested on a valid sub-site, the correct network and site should still populate.',
     311                        ),
     312                        'make.wordpress.org/' => array(
     313                                'network_id' => $network_ids['make.wordpress.org/'],
     314                                'site_id'    => $ids['make.wordpress.org/'],
     315                                'domain'     => 'make.wordpress.org',
     316                                'path'       => '/',
     317                                'response'   => true,
     318                                'message'    => false,
     319                        ),
     320                        'make.wordpress.org/foo/' => array(
     321                                'network_id' => $network_ids['make.wordpress.org/'],
     322                                'site_id'    => $ids['make.wordpress.org/foo/'],
     323                                'domain'     => 'make.wordpress.org',
     324                                'path'       => '/foo/',
     325                                'response'   => true,
     326                                'message'    => false,
     327                        ),
     328                        'core.wordpress.org/' => array(
     329                                'domain'   => 'core.wordpress.org',
     330                                'path'     => '/',
     331                                'response' => 'http://wordpress.org/',
     332                                'message'  => 'An invalid subdomain should redirect to the top level network address.',
     333                        ),
     334                        'invalidsite.org/' => array(
     335                                'domain'   => 'invalidsite.org',
     336                                'path'     => '/',
     337                                'response' => false,
     338                                'message'  => 'An invalid site/network should result in false.',
     339                        )
     340                );
    192341
    193                 $this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' );
    194                 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    195                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     342                /**
     343                 * Loop through the possible requests and assert that the proper string/true/false response is received
     344                 * from `ms_bootstrap`.
     345                 *
     346                 * In cases where a response is successful, the blog_id and site_id properties of `$current_blog`
     347                 * should also be checked to match expectations.
     348                 */
     349                foreach( $requests as $request_url => $request_data ) {
     350                        $message = $request_data['domain'] . $request_data['path'] . ' ' . $request_data['message'];
     351                        $this->assertEquals( $request_data['response'], wp_setup_current_site_and_blog( $request_data['domain'], $request_data['path'] ), $message );
    196352
    197                 $this->_setup_host_request( 'wordpress.org', '/sample-page/' );
    198                 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    199                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     353                        // For successful responses, also test the resulting site and network information.
     354                        if ( true === $request_data['response'] ) {
     355                                $this->assertEquals( $request_data['site_id'], $current_blog->blog_id );
     356                                $this->assertEquals( $request_data['network_id'], $current_blog->site_id );
     357                        }
     358                }
    200359
    201                 $this->_setup_host_request( 'wordpress.org', '/?p=1' );
    202                 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    203                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    204 
    205                 $this->_setup_host_request( 'wordpress.org', '/wp-admin/' );
    206                 $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    207                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    208 
    209                 $this->_setup_host_request( 'wordpress.org', '/foo/' );
    210                 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    211                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    212 
    213                 $this->_setup_host_request( 'wordpress.org', '/FOO/' );
    214                 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    215                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    216 
    217                 $this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' );
    218                 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    219                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    220 
    221                 $this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' );
    222                 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    223                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    224 
    225                 $this->_setup_host_request( 'wordpress.org', '/foo/?p=1' );
    226                 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    227                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    228 
    229                 $this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' );
    230                 $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    231                 $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    232 
    233360                // @todo not currently passing.
    234                 //$this->_setup_host_request( 'wordpress.org', '/foo/bar/' );
     361                //wp_setup_current_site_and_blog( 'wordpress.org', '/foo/bar/' );
    235362                //$this->assertEquals( $ids['wordpress.org/foo/bar/'], $current_blog->blog_id );
    236363                //$this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
    237364
    238                 $this->_setup_host_request( 'make.wordpress.org', '/' );
    239                 $this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id );
    240                 $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
    241 
    242                 $this->_setup_host_request( 'make.wordpress.org', '/foo/' );
    243                 $this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id );
    244                 $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
    245 
    246365                // Request the original tests domain and path to unpollute the stack.
    247                 $this->_setup_host_request( WP_TESTS_DOMAIN, '/' );
     366                wp_setup_current_site_and_blog( WP_TESTS_DOMAIN, '/' );
    248367        }
    249368
    250369        /**