There are a couple of patterns in use in the Jetpack WP REST API endpoint code which would be better avoided.
-
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.
-
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.
There are a couple of patterns in use in the Jetpack WP REST API endpoint code which would be better avoided.
Remove the
self::routehelper used to register endpoint routes, defined here and used (for example) here. Each endpoint should have its ownregister_rest_routecall; using a helper function makes the code much harder to follow and makes it harder to tell what endpoints the code is making available.Do not call
get_json_params()in callback functions (done multiple times inclass.core-rest-api-endpoints.phpandclass.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 onlyget_json_paramsremoves 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
$requestparameter instead of$datato make it clear that they're actually getting aWP_REST_Requestobject.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.