Make WordPress Core

Opened 8 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#63989 closed enhancement (fixed)

is_user_member_of_blogs fetches all user meta keys

Reported by: rinatkhaziev's profile rinatkhaziev Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.9 Priority: normal
Severity: normal Version: trunk
Component: Users Keywords: has-patch needs-testing dev-feedback
Focuses: performance Cc:

Description (last modified by SergeyBiryukov)

[33771] introduced performance improvements to is_user_member_of_blog to combat the slowness of get_blogs_of_user

It does so by fetching all meta keys ($meta_key = ''). This makes it challenging to use "get_{$meta_type}_metadata" filter, as there's no good way to tell which part we want to short-circuit or apply special handling to.

The proposed approach is to change the logic do check for the specific blog's capability check name.

Roughly:

<?php
        // No underscore before capabilities in $base_capabilities_key.
        $base_capabilities_key = $wpdb->base_prefix . 'capabilities';
        $site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';

        if ( 1 === $blog_id ) {
                $has_cap = get_user_meta( $user_id, $base_capabilities_key, true );
        } else {
                $has_cap = get_user_meta( $user_id, $site_capabilities_key, true );
        }

        return is_array( $has_cap );

Change History (10)

This ticket was mentioned in PR #9920 on WordPress/wordpress-develop by rinatkhaziev.


8 weeks ago
#1

  • Keywords has-patch added

https://core.trac.wordpress.org/changeset/33771 introduced performance improvements to is_user_member_of_blog to combat the slowness of get_blogs_of_user

It does so by fetching all meta keys ($meta_key = ). This makes it challenging to use "get_{$meta_type}_metadata" filter, as there's no good way to tell which part we want to short-circuit or apply special handling to.

The proposed approach is to change the logic do check for the specific blog's capability check name.

Trac ticket: https://core.trac.wordpress.org/ticket/63989

#2 @SergeyBiryukov
8 weeks ago

  • Description modified (diff)
  • Milestone changed from Awaiting Review to 6.9

This ticket was mentioned in Slack in #core by wildworks. View the logs.


4 weeks ago

#4 @wildworks
4 weeks ago

  • Keywords needs-testing dev-feedback added

This ticket was featured on today's 6.9 Bug Scrub. This ticket has had no activity since the PR was submitted. Testing and feedback are needed to move this ticket forward.

#5 @westonruter
4 weeks ago

  • Owner set to spacedmonkey
  • Status changed from new to reviewing

@rinatkhaziev commented on PR #9920:


3 weeks ago
#6

@jonnynews thank you, makes perfect sense. I've applied the suggestions.

#7 @spacedmonkey
3 weeks ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 60992:

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.

@spacedmonkey commented on PR #9920:


3 weeks ago
#8

Committed

#9 @westonruter
3 weeks ago

In 60996:

Coding Standards: Remove extra line break which caused PHPCS to cry.

Sniff: Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines

Follow-up to [60992].

See #63989.

This ticket was mentioned in Slack in #core by westonruter. View the logs.


3 weeks ago

Note: See TracTickets for help on using tickets.