Index: src/wp-includes/ms-load.php
===================================================================
--- src/wp-includes/ms-load.php	(revision 40568)
+++ src/wp-includes/ms-load.php	(working copy)
@@ -135,7 +135,7 @@
 
 /**
  * Retrieves the closest matching site object by its domain and path.
- * 
+ *
  * This will not necessarily return an exact match for a domain and path. Instead, it
  * breaks the domain and path into pieces that are then used to match the closest
  * possibility from a query.
@@ -437,6 +437,161 @@
 }
 
 /**
+ * Detects the current site and network if they haven't been set up yet.
+ *
+ * Prior to 4.8.0, this was a procedural block in `ms-settings.php`. It was wrapped into a
+ * function to facilitate unit tests. It should not be used outside of core.
+ *
+ * If the 'SUNRISE' constant is defined, the code in the custom `sunrise.php` will be executed
+ * prior to this function.
+ *
+ * @since 4.8.0
+ *
+ * @global WP_Network $current_site The current network.
+ * @global WP_Site    $current_blog The current site.
+ *
+ * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`,
+ *                     or if they had already been populated prior.
+ *                     False if bootstrap could not be properly completed.
+ *                     Redirect URL if parts exist, but the request as a whole can not be fulfilled.
+ */
+function ms_detect_current_site_and_network() {
+	global $current_site, $current_blog;
+
+	if ( isset( $current_site ) && isset( $current_blog ) ) {
+		// Ensure objects populated in custom `sunrise.php` are proper class instances.
+		if ( ! $current_site instanceof WP_Network ) {
+			$current_site = new WP_Network( $current_site );
+		}
+
+		if ( ! $current_blog instanceof WP_Site ) {
+			$current_blog = new WP_Site( $current_blog );
+		}
+
+		return true;
+	}
+
+	$domain = ms_get_request_domain();
+	$path   = ms_get_request_path();
+
+	return ms_load_current_site_and_network( $domain, $path, is_subdomain_install() );
+}
+
+/**
+ * Returns the domain from the current request.
+ *
+ * @since 4.8.0
+ *
+ * @return string Current domain.
+ */
+function ms_get_request_domain() {
+	$domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
+	if ( substr( $domain, -3 ) == ':80' ) {
+		$domain = substr( $domain, 0, -3 );
+		$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
+	} elseif ( substr( $domain, -4 ) == ':443' ) {
+		$domain = substr( $domain, 0, -4 );
+		$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
+	}
+
+	return $domain;
+}
+
+/**
+ * Returns the path from the current request.
+ *
+ * @since 4.8.0
+ *
+ * @return string Current path.
+ */
+function ms_get_request_path() {
+	$path = stripslashes( $_SERVER['REQUEST_URI'] );
+	if ( is_admin() ) {
+		$path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
+	}
+	list( $path ) = explode( '?', $path );
+
+	return $path;
+}
+
+/**
+ * Handles the multisite bootstrap result from loading the current site and network.
+ *
+ * It may exit or redirect depending on the result.
+ *
+ * @since 4.8.0
+ *
+ * @param bool|string @bootstrap_result True if bootstrap successfully populated `$current_blog` and `$current_site`.
+ *                                      False if bootstrap could not be properly completed.
+ *                                      Redirect URL if parts exist, but the request as a whole can not be fulfilled.
+ */
+function ms_handle_bootstrap_result( $bootstrap_result ) {
+	if ( true === $bootstrap_result ) {
+		return;
+	}
+
+	if ( false === $bootstrap_result ) {
+		$domain = ms_get_request_domain();
+		$path   = ms_get_request_path();
+
+		ms_not_installed( $domain, $path );
+	}
+
+	header( 'Location: ' . $bootstrap_result );
+	exit;
+}
+
+/**
+ * Sets up related globals after the current site and network have been populated successfully.
+ *
+ * The database and cache are properly setup as well as the network options.
+ *
+ * @since 4.8.0
+ *
+ * @param WP_Site    $site    Current site.
+ * @param WP_Network $network Current network.
+ *
+ * @global wpdb   $wpdb               WordPress database abstraction object.
+ * @global string $table_prefix       The database table prefix.
+ * @global int    $site_id            The current network ID.
+ * @global int    $blog_id            The current site ID.
+ * @global int    $public             Whether the current site is public.
+ * @global array  $_wp_switched_stack Stack for storing site IDs when switching sites.
+ * @global bool   $switched           Whether sites are currently switched.
+ */
+function ms_setup_vars( $site, $network ) {
+	global $wpdb, $table_prefix, $site_id, $blog_id, $public, $_wp_switched_stack, $switched;
+
+	if ( ! isset( $blog_id ) ) {
+		$blog_id = $site->id;
+	}
+
+	if ( ! isset( $public ) ) {
+		$public = $site->public;
+	}
+
+	if ( empty( $site->network_id ) ) {
+		$site->network_id = 1;
+	}
+
+	if ( ! isset( $site_id ) ) {
+		$site_id = $site->network_id;
+	}
+
+	wp_load_core_site_options( $site_id );
+
+	$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
+	$wpdb->set_blog_id( $site->id, $site->network_id );
+
+	$table_prefix = $wpdb->get_blog_prefix();
+	$_wp_switched_stack = array();
+	$switched = false;
+
+	// need to init cache again after blog_id is set
+	wp_start_object_cache();
+}
+
+/**
  * Displays a failure message.
  *
  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
Index: src/wp-includes/ms-settings.php
===================================================================
--- src/wp-includes/ms-settings.php	(revision 40568)
+++ src/wp-includes/ms-settings.php	(working copy)
@@ -10,18 +10,6 @@
  * @since 3.0.0
  */
 
-/**
- * Objects representing the current network and current site.
- *
- * These may be populated through a custom `sunrise.php`. If not, then this
- * file will attempt to populate them based on the current request.
- *
- * @global WP_Network $current_site The current network.
- * @global object     $current_blog The current site.
- * @since 3.0.0
- */
-global $current_site, $current_blog;
-
 /** WP_Network class */
 require_once( ABSPATH . WPINC . '/class-wp-network.php' );
 
@@ -41,66 +29,15 @@
 /** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
 ms_subdomain_constants();
 
-// This block will process a request if the current network or current site objects
-// have not been populated in the global scope through something like `sunrise.php`.
-if ( !isset( $current_site ) || !isset( $current_blog ) ) {
-
-	$domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
-	if ( substr( $domain, -3 ) == ':80' ) {
-		$domain = substr( $domain, 0, -3 );
-		$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
-	} elseif ( substr( $domain, -4 ) == ':443' ) {
-		$domain = substr( $domain, 0, -4 );
-		$_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
-	}
-
-	$path = stripslashes( $_SERVER['REQUEST_URI'] );
-	if ( is_admin() ) {
-		$path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
-	}
-	list( $path ) = explode( '?', $path );
-
-	$bootstrap_result = ms_load_current_site_and_network( $domain, $path, is_subdomain_install() );
-
-	if ( true === $bootstrap_result ) {
-		// `$current_blog` and `$current_site are now populated.
-	} elseif ( false === $bootstrap_result ) {
-		ms_not_installed( $domain, $path );
-	} else {
-		header( 'Location: ' . $bootstrap_result );
-		exit;
-	}
-	unset( $bootstrap_result );
-
-	$blog_id = $current_blog->blog_id;
-	$public  = $current_blog->public;
-
-	if ( empty( $current_blog->site_id ) ) {
-		// This dates to [MU134] and shouldn't be relevant anymore,
-		// but it could be possible for arguments passed to insert_blog() etc.
-		$current_blog->site_id = 1;
-	}
+/** Detect and populate the current site and network. */
+$bootstrap_result = ms_detect_current_site_and_network();
 
-	$site_id = $current_blog->site_id;
-	wp_load_core_site_options( $site_id );
-}
-
-$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php
-$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
-$table_prefix = $wpdb->get_blog_prefix();
-$_wp_switched_stack = array();
-$switched = false;
-
-// need to init cache again after blog_id is set
-wp_start_object_cache();
+/** Handle possible failures from the bootstrapping process. */
+ms_handle_bootstrap_result( $bootstrap_result );
+unset( $bootstrap_result );
 
-if ( ! $current_site instanceof WP_Network ) {
-	$current_site = new WP_Network( $current_site );
-}
-
-if ( ! $current_blog instanceof WP_Site ) {
-	$current_blog = new WP_Site( $current_blog );
-}
+/** Setup globals and initialize the database and cache. */
+ms_setup_vars( $current_blog, $current_site );
 
 // Define upload directory constants
 ms_upload_constants();
