From 30c51901f9a0ceadb6631767168f2df9b7c32edf Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 15 Feb 2018 10:46:16 -0200 Subject: [PATCH 1/2] Start change on Item metadata api --- .../class-tainacan-rest-item-metadata-controller.php | 9 ++++++--- .../field-type/class-tainacan-field-type.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php index 16a67ccce..25cf426e6 100644 --- a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -87,10 +87,13 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { public function prepare_item_for_response( $item, $request ) { $field_as = []; - foreach ( $item as $field ) { - $field_as[] = $field->__toArray(); + foreach ( $item as $item_metadata ) { + $field_as[] = $item_metadata->__toArray(); + //$field_as['field']['field_type_object'] = $item_metadata->get_field()->get_field_type_object()->__toArray(); } - + + + return $field_as; } diff --git a/src/classes/field-types/field-type/class-tainacan-field-type.php b/src/classes/field-types/field-type/class-tainacan-field-type.php index 21197827f..81a70a737 100644 --- a/src/classes/field-types/field-type/class-tainacan-field-type.php +++ b/src/classes/field-types/field-type/class-tainacan-field-type.php @@ -82,5 +82,17 @@ abstract class Field_Type { public function form(){ } + + public function __toArray(){ + $attributes = []; + + $attributes['className'] = get_class($this); + $attributes['core'] = $this->core; + $attributes['component'] = $this->get_component(); + $attributes['primitive_type'] = $this->get_primitive_type(); + + return $attributes; + + } } \ No newline at end of file From 01a7c41d719d6234c3cfd41f19da47f9d3716cd7 Mon Sep 17 00:00:00 2001 From: weryques Date: Thu, 15 Feb 2018 11:27:45 -0200 Subject: [PATCH 2/2] Field type object is now returned with item metadata --- ...tainacan-rest-item-metadata-controller.php | 30 ++++++------- .../class-tainacan-item-metadata.php | 44 ++++--------------- tests/test-api-metadata.php | 4 +- 3 files changed, 23 insertions(+), 55 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php index 25cf426e6..34c66354a 100644 --- a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -85,16 +85,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { * @return array|WP_Error|WP_REST_Response */ public function prepare_item_for_response( $item, $request ) { - $field_as = []; - - foreach ( $item as $item_metadata ) { - $field_as[] = $item_metadata->__toArray(); - //$field_as['field']['field_type_object'] = $item_metadata->get_field()->get_field_type_object()->__toArray(); - } - - - - return $field_as; + return $item->__toArray(); } /** @@ -105,11 +96,16 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { public function get_items( $request ) { $item_id = $request['item_id']; - $item = new Entities\Item($item_id); + $item = $this->item_repository->fetch($item_id); - $item_metadata = $item->get_fields(); + $items_metadata = $item->get_fields(); - $prepared_item = $this->prepare_item_for_response($item_metadata, $request); + $prepared_item = []; + + foreach ($items_metadata as $item_metadata){ + $index = array_push($prepared_item, $this->prepare_item_for_response($item_metadata, $request)); + $prepared_item[$index-1]['field']['field_type_object'] = $item_metadata->get_field()->get_field_type_object()->__toArray(); + } return new WP_REST_Response($prepared_item, 200); } @@ -185,18 +181,18 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { if($body) { - $item_id = $request['item_id']; + $item_id = $request['item_id']; $field_id = $request['metadata_id']; - $value = $body['values']; + $value = $body['values']; - $item = $this->item_repository->fetch( $item_id ); + $item = $this->item_repository->fetch( $item_id ); $field = $this->field_repository->fetch( $field_id ); $item_metadata = new Entities\Item_Metadata_Entity( $item, $field ); $item_metadata->set_value( $value ); if ( $item_metadata->validate() ) { - $field_updated = $this->item_metadata_repository->update( $item_metadata ); + $field_updated = $this->item_metadata_repository->insert( $item_metadata ); return new WP_REST_Response( $field_updated->__toArray(), 200 ); } else { diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 06d65028b..be004a305 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -17,7 +17,7 @@ class Item_Metadata extends Repository { if ($field_type->core) { $this->save_core_field_value($item_metadata); } else { - add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); + update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); } } else { @@ -38,40 +38,6 @@ class Item_Metadata extends Repository { return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field()); } - public function update($item_metadata, $new_values = null){ - $unique = !$item_metadata->is_multiple(); - - if ($unique) { - $field_type = $item_metadata->get_field()->get_field_type_object(); - if ($field_type->core) { - $this->save_core_field_value($item_metadata); - } else { - update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); - } - } else { - delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id()); - - if (is_array($item_metadata->get_value())){ - $values = $item_metadata->get_value(); - - foreach ($values as $value){ - if(array_key_exists('prev', $value) && !empty($value['prev'])) { - update_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value['new'] ), wp_slash($value['prev']) ); - } else { - add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value['new'] )); - } - } - } - } - - do_action('tainacan-update', $item_metadata); - do_action('tainacan-update-Item_Metadata_Entity', $item_metadata); - - $item_metadata = new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field()); - - return $item_metadata; - } - /** * Delete Item Field * @@ -166,5 +132,11 @@ class Item_Metadata extends Repository { public function get_map() { return []; } public function get_default_properties($map) { return []; } - + + /** + * @param $object + * + * @return mixed + */ + public function update( $object, $new_values = null ) {} } \ No newline at end of file diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 8b1c33b91..e80130f99 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -156,8 +156,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $meta_values = json_encode( array( 'values' => array( - array('new' => '19/01/2018', 'prev' => ''), - array('new' => '19/02/2018', 'prev' => '') + '19/01/2018', + '19/02/2018', ) ) );