Add filters to allow creating REST API middleware plugins
|Reported by:||jnylen0||Owned by:||DrewAPicture|
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.
Change History (25)
in reply to:
15 months ago
- Milestone changed from Awaiting Review to 4.5
- Focuses docs added
- Milestone changed from Future Release to 4.7
- Resolution fixed deleted
- Status changed from closed to reopened
- Owner changed from rmccue to DrewAPicture
- Status changed from reopened to reviewing