WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/04/2017 06:44:41 PM (3 years ago)
Author:
kadamwhite
Message:

REST API: Add _fields parameter to selectively include fields in response JSON.

Allows REST API consumers to specify the specific fields needed in their application code, whitelisting those fields and omitting all others from the returned JSON response object.
This permits applications that only need for example the ID and title of posts to avoid having to transfer the entire rendered post content over the wire alongside the desired fields.
While this whitelisting has no affect on the queries run when preparing the response, it can yield significant reductions in the bandwidth required to transfer a response payload for simple applications.

Props adamsilverstein, TimothyBlynJacobs, svrooij.
Fixes #38131.

File:
1 edited

Legend:

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

    r41727 r41744  
    166166    add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
    167167    add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
     168    add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
    168169
    169170    add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
     
    629630        $response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) );
    630631    }
     632
     633    return $response;
     634}
     635
     636/**
     637 * Filter the API response to include only a white-listed set of response object fields.
     638 *
     639 * @since 4.8.0
     640 *
     641 * @param WP_REST_Response $response Current response being served.
     642 * @param WP_REST_Server   $server   ResponseHandler instance (usually WP_REST_Server).
     643 * @param WP_REST_Request  $request  The request that was used to make current response.
     644 *
     645 * @return WP_REST_Response Response to be served, trimmed down to contain a subset of fields.
     646 */
     647function rest_filter_response_fields( $response, $server, $request ) {
     648    if ( ! isset( $request['_fields'] ) || $response->is_error() ) {
     649        return $response;
     650    }
     651
     652    $data = $response->get_data();
     653
     654    $fields = is_array( $request['_fields']  ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] );
     655
     656    if ( 0 === count( $fields ) ) {
     657        return $response;
     658    }
     659
     660    // Trim off outside whitespace from the comma delimited list.
     661    $fields = array_map( 'trim', $fields );
     662
     663    $fields_as_keyed = array_combine( $fields, array_fill( 0, count( $fields ), true ) );
     664
     665    if ( wp_is_numeric_array( $data ) ) {
     666        $new_data = array();
     667        foreach ( $data as $item ) {
     668            $new_data[] = array_intersect_key( $item, $fields_as_keyed );
     669        }
     670    } else {
     671        $new_data = array_intersect_key( $data, $fields_as_keyed );
     672    }
     673
     674    $response->set_data( $new_data );
    631675
    632676    return $response;
Note: See TracChangeset for help on using the changeset viewer.