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

Try setting targetHints automatically. #7139

Closed
wants to merge 9 commits into from

Conversation

TimothyBJacobs
Copy link
Member

@TimothyBJacobs TimothyBJacobs commented Aug 5, 2024

This needs to be refactored to be less ugly, and handle errors from WP_REST_Request::from_url. But this is my thinking on how we could automatically add a targetHint.

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


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.

@TimothyBJacobs TimothyBJacobs requested a review from Mamaduka August 5, 2024 20:24
@TimothyBJacobs TimothyBJacobs self-assigned this Aug 5, 2024
Copy link

github-actions bot commented Aug 5, 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.

@noisysocks
Copy link
Member

@TimothyBJacobs: How's this going? It's marked as a backport for 6.7. Beta 1 is 4 weeks and I'll sync packages in just over 2 weeks.

@TimothyBJacobs TimothyBJacobs marked this pull request as ready for review September 2, 2024 18:54
Copy link

github-actions bot commented Sep 2, 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 timothyblynjacobs, mamaduka, peterwilsoncc, swissspidy, noisysocks.

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

@TimothyBJacobs
Copy link
Member Author

Refreshed with the latest changes from GB. I'll leave it open a few days for feedback.

Mamaduka
Mamaduka previously approved these changes Sep 3, 2024
peterwilsoncc
peterwilsoncc previously approved these changes Sep 10, 2024
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

This looks good to me based on a read through of the code.

Copy link
Member

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

⚠️ ⚠️

WordPress/gutenberg#65280 needs to be fixed here as well!

@TimothyBJacobs
Copy link
Member Author

Merged in r59032.

@swissspidy swissspidy closed this Sep 17, 2024
@freibergergarcia
Copy link

@TimothyBJacobs @swissspidy I would like to validate something about this change.

The change impacts performance depending on the number of users, posts, and how you hit the users endpoint.

When rest_send_allow_header is called from get_target_hints_for_link it ends up calling the user's permission callback: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php#L458 and count_user_posts( $user->ID, $types ) for each user.

Depending on the number of $types and data (users, posts), this call can be expensive. An example could be: /wp-json/wp/v2/users?per_page=x&pagination=true&page=1. The higher per_page is set, the worse it performs.

Questions:

  • what's the best way to "disable" it?
  • is there a different way to implement this without having to call count_user_posts
  • have you considered the performance implications?
  • does it need to be "enabled" by default?

One alternative to work around this is the following. Do you think it's the most appropriate way?

/**
 * Add targetHints to the self-links in the user response to prevent COUNT slow queries.
 * 
 * @see get_target_hints_for_link core method in WordPress version 6.7
 */
add_filter( 'rest_prepare_user', function( $response, $user, $request ) {
	$links = $response->get_links();
	if ( empty( $links ) ) {
		return $response;
	}

	$response->remove_link('self');

	foreach( $links as $rel => $set ) {
		if ( 'self' !== $rel ) {
			continue;
		}

		foreach( $set as $link ) {
			if ( ! isset( $link['href'] ) ) {
				continue;
			}

			$response->add_link( 
				$rel,
				$link['href'],
				array_merge( 
					$link['attributes'] ?? [], 
					[
						'targetHints' => [
							'allow' => [],
						]
					] 
				) 
			);
		}
	}

	return $response;
}, 10, 3 );

I'm looking forward to hearing your thoughts on this.

@swissspidy
Copy link
Member

Could you perhaps open a Trac ticket to increase visibility of this?

Would you say that caching count_user_posts would mostly solve this? There's a ticket for that which would be nice to land: https://core.trac.wordpress.org/ticket/39242

@swissspidy
Copy link
Member

Actually, looks like https://core.trac.wordpress.org/ticket/46249 seems to basically have covered this case, but was pointed to https://core.trac.wordpress.org/ticket/39242 as well.. So yeah, maybe let's focus on the caching?

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.

6 participants