Make WordPress Core


Ignore:
Timestamp:
10/27/2020 04:42:38 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-request.php

    r48945 r49329  
    325325
    326326        /**
     327         * Checks if the request has specified a JSON content-type.
     328         *
     329         * @since 5.6.0
     330         *
     331         * @return bool True if the content-type header is JSON.
     332         */
     333        public function is_json_content_type() {
     334                $content_type = $this->get_content_type();
     335
     336                return isset( $content_type['value'] ) && wp_is_json_media_type( $content_type['value'] );
     337        }
     338
     339        /**
    327340         * Retrieves the parameter priority order.
    328341         *
     
    336349                $order = array();
    337350
    338                 $content_type = $this->get_content_type();
    339                 if ( isset( $content_type['value'] ) && 'application/json' === $content_type['value'] ) {
     351                if ( $this->is_json_content_type() ) {
    340352                        $order[] = 'JSON';
    341353                }
     
    659671
    660672                // Check that we actually got JSON.
    661                 $content_type = $this->get_content_type();
    662 
    663                 if ( empty( $content_type ) || 'application/json' !== $content_type['value'] ) {
     673                if ( ! $this->is_json_content_type() ) {
    664674                        return true;
    665675                }
Note: See TracChangeset for help on using the changeset viewer.