mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-12-06 12:51:58 +00:00
REST API: Filter responses based on the _fields parameter, before data is processed.
Historically, the REST API would generate the entire response object, including running expensive filters, then it would apply the `_fields` parameter, discarding the fields that weren't specificed. This change causes `_fields` to be applied earlier, so that only requested fields are processed. Props danielbachhuber. See #43874. git-svn-id: https://develop.svn.wordpress.org/trunk@43087 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -199,15 +199,36 @@ class WP_REST_Post_Statuses_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
public function prepare_item_for_response( $status, $request ) {
|
||||
|
||||
$data = array(
|
||||
'name' => $status->label,
|
||||
'private' => (bool) $status->private,
|
||||
'protected' => (bool) $status->protected,
|
||||
'public' => (bool) $status->public,
|
||||
'queryable' => (bool) $status->publicly_queryable,
|
||||
'show_in_list' => (bool) $status->show_in_admin_all_list,
|
||||
'slug' => $status->name,
|
||||
);
|
||||
$fields = $this->get_fields_for_response( $request );
|
||||
$data = array();
|
||||
|
||||
if ( in_array( 'name', $fields, true ) ) {
|
||||
$data['name'] = $status->label;
|
||||
}
|
||||
|
||||
if ( in_array( 'private', $fields, true ) ) {
|
||||
$data['private'] = (bool) $status->private;
|
||||
}
|
||||
|
||||
if ( in_array( 'protected', $fields, true ) ) {
|
||||
$data['protected'] = (bool) $status->protected;
|
||||
}
|
||||
|
||||
if ( in_array( 'public', $fields, true ) ) {
|
||||
$data['public'] = (bool) $status->public;
|
||||
}
|
||||
|
||||
if ( in_array( 'queryable', $fields, true ) ) {
|
||||
$data['queryable'] = (bool) $status->publicly_queryable;
|
||||
}
|
||||
|
||||
if ( in_array( 'show_in_list', $fields, true ) ) {
|
||||
$data['show_in_list'] = (bool) $status->show_in_admin_all_list;
|
||||
}
|
||||
|
||||
if ( in_array( 'slug', $fields, true ) ) {
|
||||
$data['slug'] = $status->name;
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
|
||||
Reference in New Issue
Block a user