Skip to content

Commit 458daed

Browse files
committed
Users: Avoid fetching all user meta keys in is_user_member_of_blog()
In [33771], is_user_member_of_blog() was optimised to improve the performance of get_blogs_of_user(). That change used $meta_key = '' to fetch all user meta, which can cause unnecessary data loading and makes it difficult to use the get_{$meta_type}_metadata filter. When all meta keys are retrieved, it’s not possible to tell which specific meta value is being requested for short-circuiting or custom handling. This commit updates the logic to request only the meta key related to the blog’s capability check, reducing overhead and improving compatibility with metadata filters. Props rinatkhaziev, spacedmonkey. Fixes #63989. git-svn-id: https://develop.svn.wordpress.org/trunk@60992 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f20b265 commit 458daed

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/wp-includes/user.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,24 +1196,15 @@ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
11961196
return false;
11971197
}
11981198

1199-
$keys = get_user_meta( $user_id );
1200-
if ( empty( $keys ) ) {
1201-
return false;
1202-
}
1203-
1204-
// No underscore before capabilities in $base_capabilities_key.
1205-
$base_capabilities_key = $wpdb->base_prefix . 'capabilities';
1206-
$site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
12071199

1208-
if ( isset( $keys[ $base_capabilities_key ] ) && 1 === $blog_id ) {
1209-
return true;
1210-
}
1211-
1212-
if ( isset( $keys[ $site_capabilities_key ] ) ) {
1213-
return true;
1200+
if ( 1 === $blog_id ) {
1201+
$capabilities_key = $wpdb->base_prefix . 'capabilities';
1202+
} else {
1203+
$capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
12141204
}
1205+
$has_cap = get_user_meta( $user_id, $capabilities_key, true );
12151206

1216-
return false;
1207+
return is_array( $has_cap );
12171208
}
12181209

12191210
/**

0 commit comments

Comments
 (0)