Skip to content

Improvements to REST API endpoints #5973

@nylen

Description

@nylen

There are a couple of patterns in use in the Jetpack WP REST API endpoint code which would be better avoided.

  1. Remove the self::route helper used to register endpoint routes, defined here and used (for example) here. Each endpoint should have its own register_rest_route call; using a helper function makes the code much harder to follow and makes it harder to tell what endpoints the code is making available.

  2. Do not call get_json_params() in callback functions (done multiple times in class.core-rest-api-endpoints.php and class.jetpack-core-api-module-endpoints.php. By default, WP REST API endpoints support multiple kinds of parameters equally, including query string and url-encoded POST data. Calling only get_json_params removes support for these other ways of calling the endpoints.

These two issues can be fixed together - all endpoint callbacks should be registered directly, and they should receive a $request parameter instead of $data to make it clear that they're actually getting a WP_REST_Request object.

Then, almost all endpoints should access parameters directly like $request['parameter_name'] so that they can come from any source and still be treated correctly. I can't think of a reason why an endpoint would still need to get all its parameters at once, but in that case it should use $request->get_params() to get all the parameters from every source, rather than only accepting JSON.

Metadata

Metadata

Assignees

Labels

Admin PageReact-powered dashboard under the Jetpack menuBugWhen a feature is broken and / or not performing as intended[Feature] WP REST API

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions