REST API: Support a broader range of JSON media types.

Previously, we only supported `application/json` which prevented using subtypes like `application/activity+json`. This allows for the REST API to `json_decode` the body of requests using a JSON subtype `Content-Type`. Additionally, `wp_die()` now properly sends the error as JSON when a JSON subtype is specified in the `Accept` header.

Props pfefferle.
Fixes #49404.


git-svn-id: https://develop.svn.wordpress.org/trunk@49329 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2020-10-27 16:42:38 +00:00
parent fce8f2bf4d
commit d56906f5b0
4 changed files with 139 additions and 7 deletions
@@ -323,6 +323,19 @@ class WP_REST_Request implements ArrayAccess {
return $data;
}
/**
* Checks if the request has specified a JSON content-type.
*
* @since 5.6.0
*
* @return bool True if the content-type header is JSON.
*/
public function is_json_content_type() {
$content_type = $this->get_content_type();
return isset( $content_type['value'] ) && wp_is_json_media_type( $content_type['value'] );
}
/**
* Retrieves the parameter priority order.
*
@@ -335,8 +348,7 @@ class WP_REST_Request implements ArrayAccess {
protected function get_parameter_order() {
$order = array();
$content_type = $this->get_content_type();
if ( isset( $content_type['value'] ) && 'application/json' === $content_type['value'] ) {
if ( $this->is_json_content_type() ) {
$order[] = 'JSON';
}
@@ -658,9 +670,7 @@ class WP_REST_Request implements ArrayAccess {
$this->parsed_json = true;
// Check that we actually got JSON.
$content_type = $this->get_content_type();
if ( empty( $content_type ) || 'application/json' !== $content_type['value'] ) {
if ( ! $this->is_json_content_type() ) {
return true;
}