#35329 closed enhancement (fixed)
Permit rest_do_request() to be used in non-REST API contexts
| Reported by: | danielbachhuber | Owned by: | rmccue |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.5 |
| Component: | REST API | Version: | 4.4 |
| Severity: | normal | Keywords: | has-patch commit has-unit-tests |
| Cc: | Focuses: |
Description
rest_do_request() currently requires $wp_rest_server to be properly instantiated:
function rest_do_request( $request ) {
global $wp_rest_server;
$request = rest_ensure_request( $request );
return $wp_rest_server->dispatch( $request );
}
We should improve it such that it initializes $wp_rest_server if it isn't already loaded, such that you could use rest_do_request() in a non-REST API context.
@joehoyle wrote an equivalent function we can crib from.
Attachments (4)
Change History (18)
#3
in reply to: ↑ 2
@
11 years ago
Replying to TimothyBlynJacobs:
Would it be worth it to have a
rest_get_server()function?
I think this is a good idea.
#4
@
10 years ago
- Keywords has-patch added; needs-patch removed
35329.diff adds the rest_internal_request() function created by joehoyle and a new rest_get_server() function to get (and maybe set) the server instance. Not sure if the former is needed, but rest_get_server() feels like a great idea. Just think of all the places where global $wp_rest_server wouldn't be needed anymore.
#5
@
10 years ago
I think we should add the logic of rest_internal_request() to rest_do_request(), given rest_do_request() already exists in core.
#6
@
10 years ago
- Owner set to
- Status new → accepted
New patch added. I dropped rest_internal_request() (I don't think we need this given the other pieces) and replaced the rest_api_loaded() internals to avoid duplication of the API initialisation.
I don't think we need unit tests for this version (now that we don't have rest_internal_request())?
#7
@
10 years ago
Just that it's not forgotten, the version used in the @since docs for the hooks of course shouldn't change.
This ticket was mentioned in Slack in #core-restapi by danielbachhuber. View the logs.
10 years ago
#9
@
10 years ago
- Keywords commit added
@rmccue @joehoyle @swissspidy The latest patch here looks good to me, but I understand the use-case well enough to be comfortable committing it. Can you help get this in 4.5?
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
+1
In my looong career with the REST API (*wink*), I've already had to write an equivalent to this
rest_internal_request()function a couple times to ensure the$wp_rest_serveris constructed for internal requests.