WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/12/2018 08:50:22 PM (22 months ago)
Author:
desrosj
Message:

REST API: Don't add fields to object when not included in ?_fields=.

In [43087], we improved REST API performance by only rendering the fields specified in the request. Similarly, any fields registered with register_rest_field() should only be rendered when included in ?_fields=.

Props dlh, danielbachhuber.

Merges [43736] to trunk.

Fixes #45099.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tests/phpunit/tests/rest-api/rest-test-controller.php

    r42343 r43986  
    1111 */
    1212class WP_REST_Test_Controller extends WP_REST_Controller {
     13    /**
     14     * Prepares the item for the REST response.
     15     *
     16     * @param mixed           $item    WordPress representation of the item.
     17     * @param WP_REST_Request $request Request object.
     18     * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     19     */
     20    public function prepare_item_for_response( $item, $request ) {
     21        $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
     22        $item     = $this->add_additional_fields_to_object( $item, $request );
     23        $item     = $this->filter_response_by_context( $item, $context );
     24        $response = rest_ensure_response( $item );
     25        return $response;
     26    }
    1327
    1428    /**
    15      * Get the Post type's schema, conforming to JSON Schema
     29     * Get the item's schema, conforming to JSON Schema.
    1630     *
    1731     * @return array
     
    7387        );
    7488
    75         return $schema;
     89        return $this->add_additional_fields_schema( $schema );
    7690    }
    7791
Note: See TracChangeset for help on using the changeset viewer.