[wp-trac] [WordPress Trac] #35590: Add filters to allow creating REST API middleware plugins

WordPress Trac noreply at wordpress.org
Sat Jan 23 22:42:56 UTC 2016


#35590: Add filters to allow creating REST API middleware plugins
-------------------------+-----------------------------
 Reporter:  jnylen0      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  REST API     |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 At WordPress.com we're working on v2 of our REST API which will be powered
 by WP-API.  To make OAuth easier, and for historical reasons, we'll need
 to serve API requests for all our sites from public-api.wordpress.com.

 We're testing some logic to make this work, but right now it is pretty
 fragile:  we add a `rest_endpoints` filter, loop through all the
 endpoints, and wrap the endpoint callbacks with the logic to switch to the
 correct blog and then switch back afterwards.

 It'd be much cleaner to have a pair of filters that are called immediately
 before and after endpoint callbacks.  We can't use `rest_pre_dispatch`
 here because the request isn't matched to an endpoint when that filter is
 applied, and we're not really filtering the response, so
 `rest_dispatch_request` isn't appropriate either.

 This will enable other middleware-style use cases where plugins need to
 perform validation or run logic before and/or after endpoint callbacks.

 For cases where the new `rest_request_after_callbacks` filter is used to
 clean up after the `before_callbacks` filter, we need to ensure that it is
 always called, even if an error response will be sent.  Therefore we need
 to meet the following conditions:

 - If `rest_request_before_callbacks` is called, then we need to always
 call `rest_request_after_callbacks` as well.
 - There cannot ever be any `return` statements in between these two
 filters (right now we are OK here).

 Thanks to @mdawaffe and @joehoyle for working on and discussing this
 change with me.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35590>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list