/**
 * Restore all blog associations for a given user
 *
 * @since BuddyPress (2.2.0)
 *
 * @param int $user_id ID whose blog data should be restored.
 */
function bp_blogs_restore_data($user_id = 0)
{
    if (!is_multisite()) {
        return;
    }
    // Get the user's blogs
    $user_blogs = get_blogs_of_user($user_id);
    if (empty($user_blogs)) {
        return;
    }
    $blogs = array_keys($user_blogs);
    foreach ($blogs as $blog_id) {
        bp_blogs_add_user_to_blog($user_id, false, $blog_id);
    }
}
/**
 * Populate the BP blogs table with existing blogs.
 *
 * @since BuddyPress (1.0.0)
 *
 * @global object $wpdb WordPress database object
 * @uses get_users()
 * @uses bp_blogs_record_blog()
 */
function bp_blogs_record_existing_blogs()
{
    global $wpdb;
    // Query for all sites in network
    if (is_multisite()) {
        // Get blog ID's if not a large network
        if (!wp_is_large_network()) {
            $blog_ids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $wpdb->siteid));
            // If error running this query, set blog ID's to false
            if (is_wp_error($blog_ids)) {
                $blog_ids = false;
            }
            // Large networks are not currently supported
        } else {
            $blog_ids = false;
        }
        // Record a single site
    } else {
        $blog_ids = $wpdb->blogid;
    }
    // Bail if there are no blogs in the network
    if (empty($blog_ids)) {
        return false;
    }
    // Get BuddyPress
    $bp = buddypress();
    // Truncate user blogs table
    $truncate = $wpdb->query("TRUNCATE {$bp->blogs->table_name}");
    if (is_wp_error($truncate)) {
        return false;
    }
    // Truncate user blogsmeta table
    $truncate = $wpdb->query("TRUNCATE {$bp->blogs->table_name_blogmeta}");
    if (is_wp_error($truncate)) {
        return false;
    }
    // Loop through users of blogs and record the relationship
    foreach ((array) $blog_ids as $blog_id) {
        // Ensure that the cache is clear after the table TRUNCATE above
        wp_cache_delete($blog_id, 'blog_meta');
        // Get all users
        $users = get_users(array('blog_id' => $blog_id));
        // Continue on if no users exist for this site (how did this happen?)
        if (empty($users)) {
            continue;
        }
        // Loop through users and record their relationship to this blog
        foreach ((array) $users as $user) {
            bp_blogs_add_user_to_blog($user->ID, false, $blog_id);
        }
    }
    // No errors
    return true;
}