/**
  * Cache-safe switching in case any multi-site hiccups might occur.
  * Clears the cache after switching to the given blog to avoid using
  * another blog's cached values.
  * See  wp_cache_reset() in wp-includes/cache.php
  * @see wp_cache_reset()
  * @link http://core.trac.wordpress.org/ticket/14941
  *
  * @param int $blog_id
  */
 public static function switch_to_blog($blog_id)
 {
     switch_to_blog($blog_id);
     if (function_exists('wp_cache_switch_to_blog')) {
         wp_cache_switch_to_blog($blog_id);
         // introduced in WP 3.5.0
     } else {
         wp_cache_reset();
         // deprecated in WP 3.5.0
     }
 }
示例#2
0
/**
 * Start the WordPress object cache.
 *
 * If an object-cache.php file exists in the wp-content directory,
 * it uses that drop-in as an external object cache.
 *
 * @since 3.0.0
 * @access private
 *
 * @global int $blog_id Blog ID.
 */
function wp_start_object_cache()
{
    global $blog_id;
    $first_init = false;
    if (!function_exists('wp_cache_init')) {
        if (file_exists(WP_CONTENT_DIR . '/object-cache.php')) {
            require_once WP_CONTENT_DIR . '/object-cache.php';
            if (function_exists('wp_cache_init')) {
                wp_using_ext_object_cache(true);
            }
        }
        $first_init = true;
    } elseif (!wp_using_ext_object_cache() && file_exists(WP_CONTENT_DIR . '/object-cache.php')) {
        /*
         * Sometimes advanced-cache.php can load object-cache.php before
         * it is loaded here. This breaks the function_exists check above
         * and can result in `$_wp_using_ext_object_cache` being set
         * incorrectly. Double check if an external cache exists.
         */
        wp_using_ext_object_cache(true);
    }
    if (!wp_using_ext_object_cache()) {
        require_once ABSPATH . WPINC . '/cache.php';
    }
    /*
     * If cache supports reset, reset instead of init if already
     * initialized. Reset signals to the cache that global IDs
     * have changed and it may need to update keys and cleanup caches.
     */
    if (!$first_init && function_exists('wp_cache_switch_to_blog')) {
        wp_cache_switch_to_blog($blog_id);
    } elseif (function_exists('wp_cache_init')) {
        wp_cache_init();
    }
    if (function_exists('wp_cache_add_global_groups')) {
        wp_cache_add_global_groups(array('users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache'));
        wp_cache_add_non_persistent_groups(array('comment', 'counts', 'plugins'));
    }
}
示例#3
0
/**
 * Restore the current blog, after calling switch_to_blog()
 *
 * @see switch_to_blog()
 * @since MU
 *
 * @return bool True on success, false if we're already on the current blog
 */
function restore_current_blog()
{
    global $wpdb, $wp_roles;
    if (empty($GLOBALS['_wp_switched_stack'])) {
        return false;
    }
    $blog = array_pop($GLOBALS['_wp_switched_stack']);
    if ($GLOBALS['blog_id'] == $blog) {
        /** This filter is documented in wp-includes/ms-blogs.php */
        do_action('switch_blog', $blog, $blog);
        // If we still have items in the switched stack, consider ourselves still 'switched'
        $GLOBALS['switched'] = !empty($GLOBALS['_wp_switched_stack']);
        return true;
    }
    $wpdb->set_blog_id($blog);
    $prev_blog_id = $GLOBALS['blog_id'];
    $GLOBALS['blog_id'] = $blog;
    $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
    if (function_exists('wp_cache_switch_to_blog')) {
        wp_cache_switch_to_blog($blog);
    } else {
        global $wp_object_cache;
        if (is_object($wp_object_cache) && isset($wp_object_cache->global_groups)) {
            $global_groups = $wp_object_cache->global_groups;
        } else {
            $global_groups = false;
        }
        wp_cache_init();
        if (function_exists('wp_cache_add_global_groups')) {
            if (is_array($global_groups)) {
                wp_cache_add_global_groups($global_groups);
            } else {
                wp_cache_add_global_groups(array('users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache'));
            }
            wp_cache_add_non_persistent_groups(array('comment', 'counts', 'plugins'));
        }
    }
    if (did_action('init')) {
        $wp_roles->reinit();
        $current_user = wp_get_current_user();
        $current_user->for_blog($blog);
    }
    /** This filter is documented in wp-includes/ms-blogs.php */
    do_action('switch_blog', $blog, $prev_blog_id);
    // If we still have items in the switched stack, consider ourselves still 'switched'
    $GLOBALS['switched'] = !empty($GLOBALS['_wp_switched_stack']);
    return true;
}
示例#4
0
/**
 * Restore the current blog, after calling switch_to_blog()
 *
 * @see switch_to_blog()
 * @since MU
 *
 * @return bool True on success, False if we're already on the current blog
 */
function restore_current_blog()
{
    global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
    if (!$switched) {
        return false;
    }
    if (!is_array($switched_stack)) {
        return false;
    }
    $blog = array_pop($switched_stack);
    if ($blog_id == $blog) {
        do_action('switch_blog', $blog, $blog);
        /* If we still have items in the switched stack, consider ourselves still 'switched' */
        $switched = is_array($switched_stack) && count($switched_stack) > 0;
        return true;
    }
    $wpdb->set_blog_id($blog);
    $prev_blog_id = $blog_id;
    $blog_id = $blog;
    $table_prefix = $wpdb->prefix;
    if (is_object($wp_roles)) {
        $wpdb->suppress_errors();
        if (method_exists($wp_roles, '_init')) {
            $wp_roles->_init();
        } elseif (method_exists($wp_roles, '__construct')) {
            $wp_roles->__construct();
        }
        $wpdb->suppress_errors(false);
    }
    if (did_action('init')) {
        $current_user = wp_get_current_user();
        if (is_object($current_user)) {
            $current_user->for_blog($blog_id);
        }
    }
    if (function_exists('wp_cache_switch_to_blog')) {
        wp_cache_switch_to_blog($blog_id);
    } else {
        if (is_object($wp_object_cache) && isset($wp_object_cache->global_groups)) {
            $global_groups = $wp_object_cache->global_groups;
        } else {
            $global_groups = false;
        }
        wp_cache_init();
        if (function_exists('wp_cache_add_global_groups')) {
            if (is_array($global_groups)) {
                wp_cache_add_global_groups($global_groups);
            } else {
                wp_cache_add_global_groups(array('users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts'));
            }
            wp_cache_add_non_persistent_groups(array('comment', 'counts', 'plugins'));
        }
    }
    do_action('switch_blog', $blog_id, $prev_blog_id);
    /* If we still have items in the switched stack, consider ourselves still 'switched' */
    $switched = is_array($switched_stack) && count($switched_stack) > 0;
    return true;
}