Make WordPress Core


Ignore:
Timestamp:
09/19/2019 02:04:51 PM (5 years ago)
Author:
kadamwhite
Message:

REST API: Support dot.nested hierarchical properties in _fields query parameter.

Enable clients to opt-in to receipt of one or more specific sub-properties within a response, and not other sub-properties.
Skip potentially expensive filtering and processing for post resources which were explicitly not requested.

Props kadamwhite, TimothyBlynJacobs, dlh.
Fixes #42094.

File:
1 edited

Legend:

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

    r46069 r46184  
    563563            $requested_fields[] = 'id';
    564564        }
    565         return array_intersect( $fields, $requested_fields );
     565        // Return the list of all requested fields which appear in the schema.
     566        return array_reduce(
     567            $requested_fields,
     568            function( $response_fields, $field ) use ( $fields ) {
     569                if ( in_array( $field, $fields, true ) ) {
     570                    $response_fields[] = $field;
     571                    return $response_fields;
     572                }
     573                // Check for nested fields if $field is not a direct match.
     574                $nested_fields = explode( '.', $field );
     575                // A nested field is included so long as its top-level property is
     576                // present in the schema.
     577                if ( in_array( $nested_fields[0], $fields, true ) ) {
     578                    $response_fields[] = $field;
     579                }
     580                return $response_fields;
     581            },
     582            array()
     583        );
    566584    }
    567585
Note: See TracChangeset for help on using the changeset viewer.