diff --git a/src/api/endpoints/class-tainacan-rest-fields-controller.php b/src/api/endpoints/class-tainacan-rest-fields-controller.php index 8b2b3a6e3..578d07ea3 100644 --- a/src/api/endpoints/class-tainacan-rest-fields-controller.php +++ b/src/api/endpoints/class-tainacan-rest-fields-controller.php @@ -230,14 +230,13 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { * @return WP_Error|WP_REST_Response */ public function delete_item( $request ) { - if(!empty($request->get_body())){ - $body = json_decode($request->get_body()); + $field_id = $request['field_id']; - $collection_id = $request['collection_id']; - $field_id = $body['field_id']; + $field_trashed = $this->field_repository->delete($field_id); - return new WP_REST_Response(['error' => 'Not Implemented.'], 400); - } + $prepared = $this->prepare_item_for_response($field_trashed, $request); + + return new WP_REST_Response($prepared, 200); } /** 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 86619a16f..3e92b438c 100644 --- a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -142,41 +142,6 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { return parent::get_collection_params(); // TODO: Change the autogenerated stub } - /** - * @param WP_REST_Request $request - * - * @return WP_Error|WP_REST_Response - */ - public function delete_item( $request ) { - if(!empty($request->get_body())){ - $body = json_decode($request->get_body()); - - $collection_id = $request['collection_id']; - $field_id = $body['metadata_id']; - - return new WP_REST_Response(['error' => 'Not Implemented.'], 400); - } - } - - /** - * @param WP_REST_Request $request - * - * @return bool|WP_Error - * @throws Exception - */ - public function delete_item_permissions_check( $request ) { - if(isset($request['item_id'])){ - $item = $this->item_repository->fetch($request['item_id']); - - if($item instanceof Entities\Item) { - return $item->can_delete(); - } - - } - - return false; - } - /** * @param WP_REST_Request $request * diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index 99e6470dc..142123689 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -414,8 +414,8 @@ class Fields extends Repository { return $this->insert($object); } - public function delete($object){ - + public function delete($field_id){ + return new Entities\Field( wp_trash_post( $field_id ) ); } /** diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 19455a9d2..7a8a8cbfc 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -39,23 +39,11 @@ class Item_Metadata extends Repository { } /** - * Delete Item Field - * * @param $item_metadata * * @return mixed|void */ - public function delete($item_metadata){ -// if(is_array($item_metadata->get_value())){ -// $values = $item_metadata->get_value(); -// -// foreach ($values as $value){ -// delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash($value)); -// } -// } else { -// delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash($item_metadata->get_value())); -// } - } + public function delete($item_metadata){} public function save_core_field_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { $field_type = $item_metadata->get_field()->get_field_type_object(); diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 684bf2d2f..793ee6b9b 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -210,6 +210,43 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { } + public function test_trash_field(){ + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Statement', + 'description' => 'No Statement' + ), + true + ); + + $field = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'Field Statement', + 'description' => 'No Statement', + 'collection' => $collection, + 'status' => 'publish', + 'field_type' => 'Tainacan\Field_Types\Text', + 'multiple' => 'yes' + ), + true + ); + + $trash_field_request = new \WP_REST_Request( + 'DELETE', + $this->namespace . '/collection/'. $collection->get_id() . '/fields/' . $field->get_id() + ); + + $trash_field_response = $this->server->dispatch($trash_field_request); + $data1 = $trash_field_response->get_data(); + + $this->assertEquals($field->get_id(), $data1['id']); + + $field_trashed = get_post($data1['id']); + $this->assertEquals('trash', $field_trashed->post_status); + } + } ?> \ No newline at end of file