Make WordPress Core

Changeset 60992


Ignore:
Timestamp:
10/20/2025 09:00:45 PM (5 weeks ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

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

    r60989 r60992  
    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';
    1207 
    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;
    1214     }
    1215 
    1216     return false;
     1199
     1200    if ( 1 === $blog_id ) {
     1201        $capabilities_key = $wpdb->base_prefix . 'capabilities';
     1202    } else {
     1203        $capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
     1204    }
     1205    $has_cap = get_user_meta( $user_id, $capabilities_key, true );
     1206
     1207    return is_array( $has_cap );
    12171208}
    12181209
Note: See TracChangeset for help on using the changeset viewer.