Index: src/wp-includes/class-wp-xmlrpc-server.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/class-wp-xmlrpc-server.php	(date 1479535152000)
+++ src/wp-includes/class-wp-xmlrpc-server.php	(revision )
@@ -626,6 +626,8 @@
 			$primary_blog_id = (int) $active_blog->blog_id;
 		}
 
+		$site_state = get_site_state();
+
 		foreach ( $blogs as $blog ) {
 			// Don't include blogs that aren't hosted at this site.
 			if ( $blog->site_id != get_current_network_id() )
@@ -646,9 +648,9 @@
 				'blogName'  => get_option( 'blogname' ),
 				'xmlrpc'    => site_url( 'xmlrpc.php', 'rpc' ),
 			);
-
-			restore_current_blog();
 		}
+
+		restore_site_state( $site_state );
 
 		return $struct;
 	}
Index: src/wp-includes/class-wp-site-state.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/class-wp-site-state.php	(revision )
+++ src/wp-includes/class-wp-site-state.php	(revision )
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Site API: WP_Site_State class
+ *
+ * @package WordPress
+ * @subpackage Multisite
+ * @since 4.8.0
+ */
+
+/**
+ * Core class used for storing and restoring site states.
+ *
+ * @since 4.8.0
+ */
+class WP_Site_State {
+
+	/**
+	 * @var int
+	 */
+	private $site_id;
+
+	/**
+	 * @var array
+	 */
+	private $stack;
+
+	/**
+	 * Constructor. Sets up the properties.
+	 *
+	 * @since 4.8.0
+	 *
+	 * @global array $_wp_switched_stack
+	 */
+	public function __construct() {
+		$this->site_id = get_current_blog_id();
+		$this->stack = $GLOBALS['_wp_switched_stack'];
+	}
+
+	/**
+	 * Restores the stored site state.
+	 *
+	 * @since 4.8.0
+	 *
+	 * @global array $_wp_switched_stack
+	 * @global bool $switched
+	 *
+	 * @return int The current site ID.
+	 */
+	public function restore() {
+		switch_to_blog( $this->site_id );
+		$GLOBALS['_wp_switched_stack'] = $this->stack;
+		$GLOBALS['switched'] = ! empty( $this->stack );
+
+		return get_current_blog_id();
+	}
+}
Index: src/wp-admin/includes/ms.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-admin/includes/ms.php	(date 1479535152000)
+++ src/wp-admin/includes/ms.php	(revision )
@@ -218,6 +218,8 @@
 	$blogs = get_blogs_of_user( $id );
 
 	if ( ! empty( $blogs ) ) {
+		$site_state = get_site_state();
+
 		foreach ( $blogs as $blog ) {
 			switch_to_blog( $blog->userblog_id );
 			remove_user_from_blog( $id, $blog->userblog_id );
@@ -234,9 +236,9 @@
 				foreach ( $link_ids as $link_id )
 					wp_delete_link( $link_id );
 			}
-
-			restore_current_blog();
 		}
+
+		restore_site_state( $site_state );
 	}
 
 	$meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
Index: src/wp-includes/ms-load.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/ms-load.php	(date 1479535152000)
+++ src/wp-includes/ms-load.php	(revision )
@@ -556,3 +556,27 @@
 
 	return $network;
 }
+
+/**
+ * Returns a new object holding the current site state.
+ *
+ * @since 4.8.0
+ *
+ * @return WP_Site_State A new site state object.
+ */
+function get_site_state() {
+	return new WP_Site_State();
+}
+
+/**
+ * Restores the given site state.
+ *
+ * @since 4.8.0
+ *
+ * @param WP_Site_State $site_state The object holding the site state to be restored.
+ *
+ * @return int The current site ID.
+ */
+function restore_site_state( WP_Site_State $site_state ) {
+	return $site_state->restore();
+}
Index: src/wp-includes/admin-bar.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/admin-bar.php	(date 1479535152000)
+++ src/wp-includes/admin-bar.php	(revision )
@@ -490,6 +490,8 @@
 		),
 	) );
 
+	$site_state = get_site_state();
+
 	foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
 		switch_to_blog( $blog->userblog_id );
 
@@ -541,9 +543,9 @@
 			'title'  => __( 'Visit Site' ),
 			'href'   => home_url( '/' ),
 		) );
-
-		restore_current_blog();
 	}
+
+	restore_site_state( $site_state );
 }
 
 /**
Index: src/wp-includes/ms-settings.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/ms-settings.php	(date 1479535152000)
+++ src/wp-includes/ms-settings.php	(revision )
@@ -28,6 +28,9 @@
 /** WP_Site class */
 require_once( ABSPATH . WPINC . '/class-wp-site.php' );
 
+/** WP_Site_State class */
+require_once( ABSPATH . WPINC . '/class-wp-site-state.php' );
+
 /** Multisite loader */
 require_once( ABSPATH . WPINC . '/ms-load.php' );
 
