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

Implement functions to check whether the current request is a REST request and whether a REST endpoint request is currently being handled #5658

Closed

Conversation

felixarntz
Copy link
Member

@felixarntz felixarntz commented Nov 10, 2023

This PR introduces two new functions:

  • wp_is_rest_request() checks whether the current request is an actual "standalone" REST request. This function does not provide any filter, similar to how e.g. it is not possible to filter whether a request is a WP Admin request. There's probably no good reason to change that now.
  • wp_is_rest_endpoint() checks whether a REST request to an endpoint is currently being handled. This is the case for any standalone REST request, as well as any internal REST requests made via another regular WordPress request (e.g. via rest_preload_api_request()). This function provides a filter as it can allow custom implementations for handling a REST request to remain compatible with this check.

The rationale for two separate functions is that those two things are distinct aspects. Most current core usage checking the REST_REQUEST constant in core is actually about whether a REST endpoint is being handled. Only a few general infrastructure checks depend on whether the current request is actually a "standalone" REST request.

All relevant usages of REST_REQUEST have been updated, or a comment has been added why they should not be updated.

To do: Add unit test coverage (once there's alignment on the approach).

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


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.

… whether a REST endpoint request is currently being handled.
*
* @return bool True if it's a WordPress REST API request, false otherwise.
*/
function wp_is_rest_request(): bool {
Copy link

Choose a reason for hiding this comment

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

Setting a return type slows this function down by about 20%

Copy link
Member Author

Choose a reason for hiding this comment

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

@sybrew I'm not sure where you get this from. In any case, I am sure the time this function takes is irrelevant in the grand scheme of things, since it's just a constant check.

Copy link

Choose a reason for hiding this comment

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

@felixarntz every time a value is returned, its type is checked. This requires more processing time. Simply allowing to pass the value unchecked executes no additional code, so that's faster.

I'm trying to prevent this from becoming the norm. PHP isn't typed and nothing has been done to optimize type hinting thus far for userland. It's only a waste of resources.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sybrew That's fair. It probably requires a larger discussion, not in a specific ticket like this. I'm not sure what I think about it yet, but it looks like this would be the first return type in core anyway, so I'll just leave it out. Done in 32a27c6

Copy link

@sybrew sybrew Jan 18, 2024

Choose a reason for hiding this comment

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

(Crosstalk) There's also no need to crash a user's site because a wrong type is used during development. Just make sure this doesn't happen; it's easily achievable. We have PHPDoc, static code checks, and unit testing to help us with this. We write the code, so we can ascertain the return type without type hinting.

*
* @return bool True if a REST endpoint request is currently being handled, false otherwise.
*/
function wp_is_rest_endpoint(): bool {
Copy link

@petitphp petitphp Jan 15, 2024

Choose a reason for hiding this comment

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

Since we have access to the full REST request object, we could add a $endpoint parameter to this function to allow developers to check which endpoint if currently dispatched, like what is possible with is_post_type_archive.

if ( wp_is_rest_endpoint( 'wp/v2/post' ) ) {...}

Copy link
Member Author

Choose a reason for hiding this comment

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

This could be done, but it's not in scope of this ticket/PR. Checking for a specific endpoint is a separate use-case, which I think should be explored in a separate ticket. It could be implemented later.

@felixarntz
Copy link
Member Author

@TimothyBJacobs Is there any chance you can get to reviewing this? :)

@TimothyBJacobs
Copy link
Member

I know we document expect usage timing in the function docs, but it feels so similar to things like wp_doing_ajax or wp_doing_cron that I think developers will expect they can use it early.

Maybe wp_is_serving_rest_request would make that a bit clearer that it won't return true until the REST API Server is actually handling it?

Copy link

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.

@felixarntz
Copy link
Member Author

Thanks @TimothyBJacobs, I've updated the PR based on your feedback.

Copy link
Member

@TimothyBJacobs TimothyBJacobs left a comment

Choose a reason for hiding this comment

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

looks great to me!

@@ -1068,7 +1068,7 @@ public function dispatch( $request ) {
* @return bool Whether the REST server is currently handling a request.
*/
public function is_dispatching() {
return empty( $this->dispatching_requests );
return ! empty( $this->dispatching_requests );
Copy link

Choose a reason for hiding this comment

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

Since the property is registered, return (bool) $this->dispatching_requests; would also work (and is about 15% faster).

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated in e09b4a0

@felixarntz
Copy link
Member Author

@felixarntz felixarntz closed this Jan 19, 2024
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.

4 participants