diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index 06927635d..26cc64127 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -18,6 +18,36 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { //} ); } + /** + * @param $object + * @param $attributes + * + * @return array + */ + protected function filter_object_by_attributes($object, $attributes){ + $object_filtered = []; + + if (is_array($attributes)) { + foreach ( $attributes as $attribute ) { + try { + $get_ = 'get_' . $attribute; + $object_filtered[$attribute] = $object->$get_(); + } catch ( \Error $error ) { + // Do nothing + } + } + } else { + try{ + $get_ = 'get_' . $attributes; + $object_filtered[$attributes] = $object->$get_(); + } catch (\Error $error){ + // Do nothing + } + } + + return $object_filtered; + } + /** * @param $object * @param $new_values diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index 3052e38da..f4f3908c1 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -133,11 +133,20 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller { */ public function prepare_item_for_response($item, $request){ if(!empty($item)){ - $item_arr = $item->__toArray(); - if($request['context'] === 'edit'){ - $item_arr['current_user_can_edit'] = $item->can_edit(); - } + if(!isset($request['fetch_only'])) { + + $item_arr = $item->__toArray(); + + if ( $request['context'] === 'edit' ) { + $item_arr['current_user_can_edit'] = $item->can_edit(); + } + + } else { + $attributes_to_filter = $request['fetch_only']; + + $item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter); + } return $item_arr; } diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 8b891410f..c7c85586d 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -107,13 +107,20 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { */ public function prepare_item_for_response( $item, $request ) { if(!empty($item)){ - $item_arr = $item->__toArray(); - if($request['context'] === 'edit'){ - $item_arr['current_user_can_edit'] = $item->can_edit(); + if(!isset($request['fetch_only'])) { + $item_arr = $item->__toArray(); + + if ( $request['context'] === 'edit' ) { + $item_arr['current_user_can_edit'] = $item->can_edit(); + } + + return $this->add_metadata_to_item( $item, $item_arr ); } - return $this->add_metadata_to_item($item, $item_arr); + $attributes_to_filter = $request['fetch_only']; + + return $this->filter_object_by_attributes($item, $attributes_to_filter); } return $item; diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php index 9ddc5b365..2cc9c0cea 100644 --- a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -82,10 +82,16 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller { */ public function prepare_item_for_response( $item, $request ) { if(!empty($item)) { - $item_arr = $item->__toArray(); + if(!isset($request['fetch_only'])) { + $item_arr = $item->__toArray(); - if($request['context'] === 'edit'){ - $item_arr['current_user_can_edit'] = $item->can_edit(); + if ( $request['context'] === 'edit' ) { + $item_arr['current_user_can_edit'] = $item->can_edit(); + } + } else { + $attributes_to_filter = $request['fetch_only']; + + $item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter); } return $item_arr; diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index a267f895d..7b0891b92 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -251,10 +251,16 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller { */ public function prepare_item_for_response( $item, $request ) { if(!empty($item)){ - $item_arr = $item->__toArray(); + if(!isset($request['fetch_only'])) { + $item_arr = $item->__toArray(); - if($request['context'] === 'edit'){ - $item_arr['current_user_can_edit'] = $item->can_edit(); + if ( $request['context'] === 'edit' ) { + $item_arr['current_user_can_edit'] = $item->can_edit(); + } + } else { + $attributes_to_filter = $request['fetch_only']; + + $item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter); } return $item_arr;