Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load user capabilities in WP_User object #5098

Open
wants to merge 11 commits into
base: trunk
Choose a base branch
from

Conversation

spacedmonkey
Copy link
Member

@spacedmonkey spacedmonkey commented Aug 26, 2023

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@spacedmonkey spacedmonkey changed the title Try/lazy load Lazy load user capabilities in WP_User object Sep 4, 2023
Copy link

github-actions bot commented Jul 1, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props spacedmonkey, flixos90, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

The number of expected queries in the cacheResults test has been decreased from two to one. Specifically, the user data query is no longer expected, only the user meta data query remains. The test function has been updated accordingly to reflect this change.
Copy link

github-actions bot commented Jul 1, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spacedmonkey I think there is at least one bug with the code that needs to be addressed (related to switching to another site in Multisite).

Other than that, I think it would be great if you could separate the overall metadata lazyloader integration and handle that in another PR. The role and capability logic itself is expensive enough to be lazy-loaded, which is what most of this PR is about. That in itself is already complex and may have some quirks to consider, so I think separating the two concerns makes the PRs easier to review.

As an aside, I'm not very familiar with metadata lazy-loading, but I am familiar with how WP_User works, so I only feel comfortable with reviewing the latter.

src/wp-includes/class-wp-user.php Show resolved Hide resolved
src/wp-includes/class-wp-user.php Show resolved Hide resolved
src/wp-includes/class-wp-user.php Show resolved Hide resolved
src/wp-includes/class-wp-user.php Show resolved Hide resolved
Comment on lines -879 to -881
$this->caps = $this->get_caps_data();

$this->get_role_caps();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing this without replacing it with some other mechanism to "reset" the flag that capability and role data are loaded will lead to problems. For example, if capabilities and roles were already loaded and then someone calls WP_User::for_site() with another site ID, the WP_User object would still have the old capabilities and roles, not the ones for the site requested.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this appears to be the cause of the test failures for MS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

$this->caps = $this->get_caps_data();

$this->get_role_caps();
wp_lazyload_user_meta( array( $this->ID ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with how metadata lazy-loading works, so I feel like I'm not the right person to review this part. For example, one potential caveat here is that user metadata is generally global, but some meta keys only apply to specific sites.

I wonder for instance why this method is called here. Since user metadata is global, why would it need to be loaded (again) when the site is switched? Shouldn't this call be somewhere else, or only be called if it hadn't been called before?

@peterwilsoncc peterwilsoncc removed their request for review January 19, 2025 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants