WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/18/2010 05:11:00 PM (9 years ago)
Author:
ryan
Message:

Cache invalidation for get_blogs_of_user(). fixes #14379

File:
1 edited

Legend:

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

    r15768 r15833  
    576576
    577577/**
    578  * Get the blogs a user belong to.
    579  *
    580  * $since 3.0.0
     578 * Get the blogs a user belongs to.
     579 *
     580 * @since 3.0.0
    581581 *
    582582 * @param int $id User Id
    583  * @param bool $all Whether to retrieve all blog details or an abbreviated set of details. Default is abbreviated.
    584  * @return array A list of the user's blogs.
     583 * @param bool $all Whether to retrieve all blogs or only blogs that are not marked as deleted, archived, or spam.
     584 * @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.
    585585 */
    586586function get_blogs_of_user( $id, $all = false ) {
     
    599599    }
    600600
    601     $cache_suffix = $all ? '_all' : '_short';
    602     $return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
    603     if ( $return )
    604         return apply_filters( 'get_blogs_of_user', $return, $id, $all );
    605 
    606     $user = get_userdata( (int) $id );
    607     if ( !$user )
    608         return false;
    609 
    610     $blogs = $match = array();
    611     $prefix_length = strlen($wpdb->base_prefix);
    612     foreach ( (array) $user as $key => $value ) {
    613         if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
    614             continue;
    615         if ( substr($key, -12, 12) != 'capabilities' )
    616             continue;
    617         if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
    618             if ( count( $match ) > 2 )
    619                 $blog_id = $match[ 2 ];
    620             else
    621                 $blog_id = 1;
    622             $blog = get_blog_details( $blog_id );
    623             if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
    624                 $blogs[ $blog_id ]->userblog_id = $blog_id;
    625                 $blogs[ $blog_id ]->blogname        = $blog->blogname;
    626                 $blogs[ $blog_id ]->domain      = $blog->domain;
    627                 $blogs[ $blog_id ]->path            = $blog->path;
    628                 $blogs[ $blog_id ]->site_id     = $blog->site_id;
    629                 $blogs[ $blog_id ]->siteurl     = $blog->siteurl;
     601    $blogs = wp_cache_get( 'blogs_of_user-' . $id, 'users' );
     602    if ( false === $blogs ) {
     603        $user = get_userdata( (int) $id );
     604        if ( !$user )
     605            return false;
     606
     607        $blogs = $match = array();
     608        $prefix_length = strlen($wpdb->base_prefix);
     609        foreach ( (array) $user as $key => $value ) {
     610            if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
     611                continue;
     612            if ( substr($key, -12, 12) != 'capabilities' )
     613                continue;
     614            if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
     615                if ( count( $match ) > 2 )
     616                    $blogs[] = (int) $match[ 2 ];
     617                else
     618                    $blogs[] = 1;
    630619            }
    631620        }
    632     }
    633 
    634     wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 );
    635     return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
     621        wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
     622    }
     623
     624    $blog_deets = array();
     625    foreach ( (array) $blogs as $blog_id ) {
     626        $blog = get_blog_details( $blog_id );
     627        if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
     628            $blog_deets[ $blog_id ]->userblog_id    = $blog_id;
     629            $blog_deets[ $blog_id ]->blogname       = $blog->blogname;
     630            $blog_deets[ $blog_id ]->domain     = $blog->domain;
     631            $blog_deets[ $blog_id ]->path           = $blog->path;
     632            $blog_deets[ $blog_id ]->site_id        = $blog->site_id;
     633            $blog_deets[ $blog_id ]->siteurl        = $blog->siteurl;
     634        }
     635    }
     636
     637    return apply_filters( 'get_blogs_of_user', $blog_deets, $id, $all );
    636638}
    637639
     
    12691271    wp_cache_delete($user->user_email, 'useremail');
    12701272    wp_cache_delete($user->user_nicename, 'userslugs');
     1273    wp_cache_delete('blogs_of_user-' . $id, 'users');
    12711274}
    12721275
Note: See TracChangeset for help on using the changeset viewer.