Now is possible fetch only especifcs attributes of a object

This commit is contained in:
weryques 2018-03-07 14:44:40 -03:00
parent 1bc977a890
commit 561e9db834
5 changed files with 72 additions and 14 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;