Cache invalidation for get_blogs_of_user(). fixes #14379

git-svn-id: http://svn.automattic.com/wordpress/trunk@15833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-10-18 17:11:00 +00:00
parent a034198225
commit 54ae23aa08
1 changed files with 37 additions and 34 deletions

View File

@ -575,13 +575,13 @@ function get_users_of_blog( $id = '' ) {
} }
/** /**
* Get the blogs a user belong to. * Get the blogs a user belongs to.
* *
* $since 3.0.0 * @since 3.0.0
* *
* @param int $id User Id * @param int $id User Id
* @param bool $all Whether to retrieve all blog details or an abbreviated set of details. Default is abbreviated. * @param bool $all Whether to retrieve all blogs or only blogs that are not marked as deleted, archived, or spam.
* @return array A list of the user's blogs. * @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.
*/ */
function get_blogs_of_user( $id, $all = false ) { function get_blogs_of_user( $id, $all = false ) {
global $wpdb; global $wpdb;
@ -598,41 +598,43 @@ function get_blogs_of_user( $id, $all = false ) {
return $blogs; return $blogs;
} }
$cache_suffix = $all ? '_all' : '_short'; $blogs = wp_cache_get( 'blogs_of_user-' . $id, 'users' );
$return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' ); if ( false === $blogs ) {
if ( $return ) $user = get_userdata( (int) $id );
return apply_filters( 'get_blogs_of_user', $return, $id, $all ); if ( !$user )
return false;
$user = get_userdata( (int) $id ); $blogs = $match = array();
if ( !$user ) $prefix_length = strlen($wpdb->base_prefix);
return false; foreach ( (array) $user as $key => $value ) {
if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
$blogs = $match = array(); continue;
$prefix_length = strlen($wpdb->base_prefix); if ( substr($key, -12, 12) != 'capabilities' )
foreach ( (array) $user as $key => $value ) { continue;
if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix ) if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
continue; if ( count( $match ) > 2 )
if ( substr($key, -12, 12) != 'capabilities' ) $blogs[] = (int) $match[ 2 ];
continue; else
if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) { $blogs[] = 1;
if ( count( $match ) > 2 )
$blog_id = $match[ 2 ];
else
$blog_id = 1;
$blog = get_blog_details( $blog_id );
if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
$blogs[ $blog_id ]->userblog_id = $blog_id;
$blogs[ $blog_id ]->blogname = $blog->blogname;
$blogs[ $blog_id ]->domain = $blog->domain;
$blogs[ $blog_id ]->path = $blog->path;
$blogs[ $blog_id ]->site_id = $blog->site_id;
$blogs[ $blog_id ]->siteurl = $blog->siteurl;
} }
} }
wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
}
$blog_deets = array();
foreach ( (array) $blogs as $blog_id ) {
$blog = get_blog_details( $blog_id );
if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
$blog_deets[ $blog_id ]->userblog_id = $blog_id;
$blog_deets[ $blog_id ]->blogname = $blog->blogname;
$blog_deets[ $blog_id ]->domain = $blog->domain;
$blog_deets[ $blog_id ]->path = $blog->path;
$blog_deets[ $blog_id ]->site_id = $blog->site_id;
$blog_deets[ $blog_id ]->siteurl = $blog->siteurl;
}
} }
wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 ); return apply_filters( 'get_blogs_of_user', $blog_deets, $id, $all );
return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
} }
function get_ordered_blogs_of_user( $user_id, $visibility = true ) { function get_ordered_blogs_of_user( $user_id, $visibility = true ) {
@ -1268,6 +1270,7 @@ function clean_user_cache($id) {
wp_cache_delete($user->user_login, 'userlogins'); wp_cache_delete($user->user_login, 'userlogins');
wp_cache_delete($user->user_email, 'useremail'); wp_cache_delete($user->user_email, 'useremail');
wp_cache_delete($user->user_nicename, 'userslugs'); wp_cache_delete($user->user_nicename, 'userslugs');
wp_cache_delete('blogs_of_user-' . $id, 'users');
} }
?> ?>