WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 5 weeks ago

#42061 reopened enhancement

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

Reported by: lots.0.logs Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.9
Component: Bootstrap/Load Keywords: has-patch
Focuses: rest-api 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 (1)

42061-add-wp-doing-rest-filterable-function.patch (1.7 KB) - added by lots.0.logs 8 months ago.

Download all attachments as: .zip

Change History (6)

#1 @lots.0.logs
8 months ago

  • Keywords has-patch added

#2 @joehoyle
6 months 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
5 weeks 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
5 weeks ago

  • Milestone set to Awaiting Review

#5 @swissspidy
5 weeks ago

  • Milestone changed from Awaiting Review to 5.0

+1 for consistency.

See #25669, #39591.

Note: See TracTickets for help on using tickets.