restore_current_blog()

最后更新于:2021-11-27 23:31:33

restore_current_blog()

Restore the current blog, after calling switch_to_blog().

响应

(bool) True on success, false if we’re already on the current blog.

源文件

文件: gc-includes/ms-blogs.php

function restore_current_blog() {
	global $gcdb;

	if ( empty( $GLOBALS['_gc_switched_stack'] ) ) {
		return false;
	}

	$new_blog_id  = array_pop( $GLOBALS['_gc_switched_stack'] );
	$prev_blog_id = get_current_blog_id();

	if ( $new_blog_id == $prev_blog_id ) {
		/** This filter is documented in gc-includes/ms-blogs.php */
		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );

		// If we still have items in the switched stack, consider ourselves still 'switched'.
		$GLOBALS['switched'] = ! empty( $GLOBALS['_gc_switched_stack'] );

		return true;
	}

	$gcdb->set_blog_id( $new_blog_id );
	$GLOBALS['blog_id']      = $new_blog_id;
	$GLOBALS['table_prefix'] = $gcdb->get_blog_prefix();

	if ( function_exists( 'gc_cache_switch_to_blog' ) ) {
		gc_cache_switch_to_blog( $new_blog_id );
	} else {
		global $gc_object_cache;

		if ( is_object( $gc_object_cache ) && isset( $gc_object_cache->global_groups ) ) {
			$global_groups = $gc_object_cache->global_groups;
		} else {
			$global_groups = false;
		}

		gc_cache_init();

		if ( function_exists( 'gc_cache_add_global_groups' ) ) {
			if ( is_array( $global_groups ) ) {
				gc_cache_add_global_groups( $global_groups );
			} else {
				gc_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
			}

			gc_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
		}
	}

	/** This filter is documented in gc-includes/ms-blogs.php */
	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );

	// If we still have items in the switched stack, consider ourselves still 'switched'.
	$GLOBALS['switched'] = ! empty( $GLOBALS['_gc_switched_stack'] );

	return true;
}