-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Conversation
… whether a REST endpoint request is currently being handled.
src/wp-includes/functions.php
Outdated
* | ||
* @return bool True if it's a WordPress REST API request, false otherwise. | ||
*/ | ||
function wp_is_rest_request(): bool { |
There was a problem hiding this comment.
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%
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
src/wp-includes/rest-api.php
Outdated
* | ||
* @return bool True if a REST endpoint request is currently being handled, false otherwise. | ||
*/ | ||
function wp_is_rest_endpoint(): bool { |
There was a problem hiding this comment.
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' ) ) {...}
There was a problem hiding this comment.
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.
@TimothyBJacobs Is there any chance you can get to reviewing this? :) |
I know we document expect usage timing in the function docs, but it feels so similar to things like Maybe |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Thanks @TimothyBJacobs, I've updated the PR based on your feedback. |
There was a problem hiding this 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 ); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in e09b4a0
Committed in https://core.trac.wordpress.org/changeset/57312 |
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. viarest_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.