Make WordPress Core

Opened 12 months ago

Closed 11 months ago

Last modified 11 months ago

#63989 closed enhancement (fixed)

is_user_member_of_blogs fetches all user meta keys

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

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.


12 months 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
12 months ago

  • Description modified (diff)
  • Milestone Awaiting Review6.9

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


11 months ago

#4 @wildworks
11 months 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
11 months ago

  • Owner set to spacedmonkey
  • Status newreviewing

@rinatkhaziev commented on PR #9920:


11 months ago
#6

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

#7 @spacedmonkey
11 months ago

  • Resolutionfixed
  • Status reviewingclosed

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:


11 months ago
#8

Committed

#9 @westonruter
11 months 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.


11 months ago

Note: See TracTickets for help on using tickets.