WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 5 months ago

#42061 assigned enhancement

Add new utility function for checking if the current request is a REST API request.

Reported by: lots.0.logs Owned by: TimothyBlynJacobs
Milestone: Future Release Priority: normal
Severity: normal Version: 4.9
Component: Bootstrap/Load Keywords: has-patch dev-feedback
Focuses: rest-api, performance Cc:

Description (last modified by joehoyle)

This patch adds a new function: wp_doing_rest() and filter by the same name that work exactly like the current wp_doing_ajax/cron() functions but for Rest API requests.

Attachments (2)

42061-add-wp-doing-rest-filterable-function.patch (1.7 KB) - added by lots.0.logs 4 years ago.
42061.diff (3.1 KB) - added by flixos90 3 years ago.

Download all attachments as: .zip

Change History (26)

#1 @lots.0.logs
4 years ago

  • Keywords has-patch added

#2 @joehoyle
4 years ago

  • Description modified (diff)
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

See https://github.com/WP-API/WP-API/issues/926 for the previous story on this.

Though we want to discourage use of global state functions like these, there _is_ a REST_REQUEST constant for this purpose. As mentioned in that ticket, the idea of "the current request is a REST one" breaks down for things like _embed requests, and internal rest_do_request calls, therefore we don't want to encourage the use of "is the current request a REST one" as it's not always in that context.

@lots.0.logs perhaps you could define why you need such a function? wp_doing_ajax/cron() are inherently process-level functions, but the REST API callback methods are not intended to scoped to the whole "page load" request.

#3 @flixos90
4 years ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

I think this needs to be revisited, for the same purpose we have functions like wp_doing_ajax() etc. Every function in WordPress can be misused, but a wp_doing_rest() or wp_is_rest_request() would allow fixing weird hacks necessary when working with unit tests. See #43751 for an example.

#4 @SergeyBiryukov
4 years ago

  • Milestone set to Awaiting Review

#5 @swissspidy
4 years ago

  • Milestone changed from Awaiting Review to 5.0

+1 for consistency.

See #25669, #39591.

@flixos90
3 years ago

#6 @lots.0.logs
3 years ago

I see you changed the name to wp_is_rest_request(). That is inconsistent with existing similar utility functions. IMO, wp_doing_rest() is the most appropriate name for this function.

#7 follow-up: @flixos90
3 years ago

  • Keywords dev-feedback added
  • Owner set to flixos90
  • Status changed from reopened to assigned

As usual, this is more complicated than it may seem at first glance. Other than AJAX and Cron, which are controlled by constants, REST is controlled by a query variable, meaning it relies on the current request to be parsed. The REST_REQUEST constant is only set for an easy check, but is not available from the beginning of the request's lifecycle.

It is therefore impossible to detect a REST request before the parse_request action, at least with the means currently in place. That action is triggered rather late, after init, wp_loaded etc.

There is actually a bug in core which doesn't take the above into account: in wp_debug_mode(), it is checked whether the current request is a REST request in order to not display errors in such a case. However, this check will always result in false, since the function is executed even before most of WordPress files are even loaded. Therefore, currently REST API responses will still be messed up by debugging messages if enabled. This could be fixed in a separate issue though, depending on what we decide here.

I see two possibilities:

  • We could embrace the current functionality, relying on the query variable and introduce the function in a way that is triggers _doing_it_wrong() if called too early. In that case, it should be named differently from the existing wp_doing_ajax() and wp_doing_cron(), to indicate its different behavior (I suggest wp_is_rest_request() because in WordPress terms that connects it to the request which is associated with parsing the current request).
  • We could find a way to detect a REST request through another way and aim for making the process similar to AJAX and Cron so that it can be detected really early. In that case, the function could also be named accordingly, wp_doing_rest().

I'm in favor of the first alternative, and this is what I implemented in 42061.diff:

  • It introduces wp_is_rest_request() in wp-includes/functions.php. No point to put it in wp-includes/load.php under these circumstances.
  • If parse_request has not been executed yet and REST_REQUEST is not set (this check would allow custom setups to still set it early), but the function is called already, a _doing_it_wrong() is triggered.
  • The check for a REST request in wp_debug_mode() has been removed, since it doesn't do anything. Since I adjusted that line anyway, I also fixed a direct check of WP_INSTALLING, which should instead rely on wp_installing().
  • Other checks of REST_REQUEST has been adjusted as well, and the additional check in wp-admin/includes/ms.php is something I detected during #43751 (it could currently simply kill REST requests).

#8 @flixos90
3 years ago

  • Milestone changed from 5.0 to 5.1

#9 @swissspidy
3 years ago

Worth noting that #44534 introduced wp_is_json_request()

#10 @pento
3 years ago

wp_is_json_request() replaced some of the use cases for this function.

@flixos90: Do you think wp_doing_rest() is still needed?

#11 @ocean90
3 years ago

#45875 was marked as a duplicate.

#12 @pento
3 years ago

  • Milestone changed from 5.1 to Future Release

Punting this, as it requires further consideration.

#14 @mikejolley
3 years ago

@pento We could use this in WooCommerce. We do things such as load the cart after wp_loaded action, but we're unable to disable this based on REST_REQUEST due to that constant not being set until parse_request.

A function, available earlier, would be very useful.

ref https://github.com/woocommerce/woocommerce/issues/20937

#15 in reply to: ↑ 7 @iandunn
19 months ago

Replying to flixos90:

REST is controlled by a query variable...
It is therefore impossible to detect a REST request before the parse_request action, at least with the means currently in place...
We could find a way to detect a REST request through another way and aim for making the process similar to AJAX and Cron so that it can be detected really early.

I think that'd be ideal. One benefit would be that WP_USE_THEMES could be set to false for REST API requests, and wp_using_themes() could be used to detect the front end before template_redirect executes.

One way to do that might be something like the solution that Woo experiment with. I haven't looked deeply to see if there are cases where that wouldn't work, though.

#16 @TimothyBlynJacobs
12 months ago

  • Focuses performance added
  • Milestone changed from Future Release to 5.7

I'm going to attempt to take a look at this for 5.7 I agree with @iandunn that we'd need to do something like that to allow for checking if this is a REST API request early enough.

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


9 months ago

#18 @hellofromTonya
9 months ago

  • Keywords needs-refresh added

Though this ticket has a patch, it's still in the exploration phase. Marking it for refresh. As Timothy noted in today's scrub:

We have some code that does this that I’m going to see if will work in a Core context. So it’ll need testing and review soon.

This ticket was mentioned in PR #916 on WordPress/wordpress-develop by TimothyBJacobs.


9 months ago

  • Keywords needs-refresh removed

#20 @TimothyBlynJacobs
9 months ago

  • Owner changed from flixos90 to TimothyBlynJacobs

As @flixos90 mentioned, checking if this is a REST API request "properly" requires parsing rewrite rules. As such, if we want to support checking this early, we essentially need to duplicate some of the logic in WP::parse_request. I've attached a PR that does this and works in my testing so far with the built-in dev environment for.

  • http://localhost:8889/wp-json
  • http://localhost:8889/index.php/wp-json
  • http://localhost:8889/index.php?rest_route=/
  • http://localhost:8889/?rest_route=/

Just because we are replicating the logic doesn't mean this guarantees correctness since the query could be modified with a number of hooks that would change behavior and the rewrite rules could be setup to be something completely different from rest_api_register_rewrites. I think there may also be issues if WP_Rewrite::$index is overwritten and the function is called before WP_Rewrite is setup.

There is also the handling for when the REST API is accessed without pretty permalink. An issue here is that essentially we are trying to detect if wp will be called, but there is no way to guarantee wp will or will not be called before it is called. I implemented this by checking for the WP_USE_THEMES constant which works for vanilla core AFAICT, but again customized setups might have issues.,

However, I think we should be able to make it work for all "out of the box" server setups, ie where those advanced customizations haven't been made. If a site is making those adjustments, they could use the filter to provide more accurate detection.

If we want to continue with this, it'd need to be manually tested in a number of different server environments. And we'd have to be careful about usage. I'd think Core should probably only use the function post parse_request. I think it is still useful to allow for it to be called prior to that for usage in plugins.

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


9 months ago

#22 @hellofromTonya
9 months ago

  • Milestone changed from 5.7 to 5.8

Per Timothy during today's scrub:

Definitely :football: . It could use some opinions on my latest comment too if anyone is interested.

Moving to 5.8.

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


5 months ago

#24 @desrosj
5 months ago

  • Milestone changed from 5.8 to Future Release

I'm going to move this to Future Release. It's still waiting for feedback and will likely need refinement. The 5.8 feature freeze date is also today.

Note: See TracTickets for help on using tickets.