WP_REST_Controller::get_fields_for_response( WP_REST_Request $request )
Gets an array of fields to be included on the response.
Description
Included fields are based on item schema and _fields=
request argument.
Parameters
- $request
-
(Required) Full details about the request.
Return
(array) Fields to be included in the response.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-controller.php
public function get_fields_for_response( $request ) {
$schema = $this->get_item_schema();
$fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array();
if ( ! isset( $request['_fields'] ) ) {
return $fields;
}
$requested_fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] );
if ( 0 === count( $requested_fields ) ) {
return $fields;
}
// Trim off outside whitespace from the comma delimited list.
$requested_fields = array_map( 'trim', $requested_fields );
// Always persist 'id', because it can be needed for add_additional_fields_to_object().
if ( in_array( 'id', $fields, true ) ) {
$requested_fields[] = 'id';
}
return array_intersect( $fields, $requested_fields );
}
Changelog
Version | Description |
---|---|
WP-4.9.6 | Introduced. |