Make WordPress Core


Ignore:
Timestamp:
10/10/2011 07:50:08 PM (13 years ago)
Author:
nacin
Message:

Remove caching from get_blogs_of_user(). Leave caching to usermeta and blog details. Speed up the loops. fixes #15458.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/user.php

    r18909 r18928  
    646646 * @since 3.0.0
    647647 *
    648  * @param int $id User Id
    649  * @param bool $all Whether to retrieve all blogs or only blogs that are not marked as deleted, archived, or spam.
     648 * @param int $user_id User ID
     649 * @param bool $all Whether to retrieve all blogs, or only blogs that are not marked as deleted, archived, or spam.
    650650 * @return array A list of the user's blogs. False if the user was not found or an empty array if the user has no blogs.
    651651 */
    652 function get_blogs_of_user( $id, $all = false ) {
     652function get_blogs_of_user( $user_id, $all = false ) {
    653653    global $wpdb;
    654654
    655     if ( !is_multisite() ) {
     655    if ( ! is_multisite() ) {
    656656        $blog_id = get_current_blog_id();
    657657        $blogs = array( $blog_id => new stdClass );
     
    665665    }
    666666
     667    $user_id = (int) $user_id;
     668
    667669    // Logged out users can't have blogs
    668     if ( 0 === $id )
     670    if ( empty( $user_id ) )
    669671        return false;
    670672
    671     $blogs = wp_cache_get( 'blogs_of_user-' . $id, 'users' );
    672 
    673     // Try priming the new cache from the old cache
    674     if ( false === $blogs ) {
    675         $cache_suffix = $all ? '_all' : '_short';
    676         $blogs = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
    677         if ( is_array( $blogs ) ) {
    678             $blogs = array_keys( $blogs );
    679             if ( $all )
    680                 wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
    681         }
    682     }
    683 
    684     if ( false === $blogs ) {
    685         $userkeys = get_user_meta( (int) $id );
    686         if ( empty( $userkeys ) )
    687             return false;
    688         $userkeys = array_keys( $userkeys );
    689 
    690         $blogs = $match = array();
    691         $prefix_length = strlen( $wpdb->base_prefix );
    692         foreach ( $userkeys as $key ) {
    693             if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
    694                 continue;
    695             if ( substr($key, -12, 12) != 'capabilities' )
    696                 continue;
    697             if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
    698                 if ( count( $match ) > 2 )
    699                     $blogs[] = (int) $match[ 2 ];
    700                 else
    701                     $blogs[] = 1;
    702             }
    703         }
    704         wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
    705     }
    706 
    707     $blog_deets = array();
    708     foreach ( (array) $blogs as $blog_id ) {
     673    $keys = get_user_meta( $user_id );
     674    if ( empty( $keys ) )
     675        return false;
     676
     677    $blogs = array();
     678
     679    if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
     680        $blog = get_blog_details( 1 );
     681        if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
     682            $blogs[ 1 ] = (object) array(
     683                'userblog_id' => 1,
     684                'blogname'    => $blog->blogname,
     685                'domain'      => $blog->domain,
     686                'path'        => $blog->path,
     687                'site_id'     => $blog->site_id,
     688                'siteurl'     => $blog->siteurl,
     689            );
     690        }
     691        unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
     692    }
     693
     694    $keys = array_keys( $keys );
     695
     696    foreach ( $keys as $key ) {
     697        if ( 'capabilities' !== substr( $key, -12 ) )
     698            continue;
     699        if ( 0 !== strpos( $key, $wpdb->base_prefix ) )
     700            continue;
     701        $blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
     702        if ( ! is_numeric( $blog_id ) )
     703            continue;
     704
     705        $blog_id = (int) $blog_id;
    709706        $blog = get_blog_details( $blog_id );
    710         if ( $blog && isset( $blog->domain ) && ( $all || ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
    711             $blog_deets[ $blog_id ] = new stdClass;
    712             $blog_deets[ $blog_id ]->userblog_id = $blog_id;
    713             $blog_deets[ $blog_id ]->blogname    = $blog->blogname;
    714             $blog_deets[ $blog_id ]->domain      = $blog->domain;
    715             $blog_deets[ $blog_id ]->path        = $blog->path;
    716             $blog_deets[ $blog_id ]->site_id     = $blog->site_id;
    717             $blog_deets[ $blog_id ]->siteurl     = $blog->siteurl;
    718         }
    719     }
    720 
    721     return apply_filters( 'get_blogs_of_user', $blog_deets, $id, $all );
     707        if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
     708            $blogs[ $blog_id ] = (object) array(
     709                'userblog_id' => $blog_id,
     710                'blogname'    => $blog->blogname,
     711                'domain'      => $blog->domain,
     712                'path'        => $blog->path,
     713                'site_id'     => $blog->site_id,
     714                'siteurl'     => $blog->siteurl,
     715            );
     716        }
     717    }
     718
     719    return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
    722720}
    723721
     
    11541152    wp_cache_delete($user->user_email, 'useremail');
    11551153    wp_cache_delete($user->user_nicename, 'userslugs');
    1156     wp_cache_delete('blogs_of_user-' . $id, 'users');
    11571154}
    11581155
Note: See TracChangeset for help on using the changeset viewer.