diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 7e16de469..d58ce5007 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -39,7 +39,12 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array($this, 'delete_item'), - 'permission_callbacl' => array($this, 'delete_item_permissions_check') + 'permission_callback' => array($this, 'delete_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array($this, 'update_item'), + 'permission_callback' => array($this, 'update_item_permissions_check') ) ) ); @@ -132,9 +137,45 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { } public function delete_item_permissions_check( $request ) { - $term = $this->terms_repository->fetch($request['term_id']); + $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); return $this->terms_repository->can_delete($term); } + + public function update_item( $request ) { + $term_id = $request['term_id']; + $taxonomy_id = $request['taxonomy_id']; + + $body = json_decode($request->get_body(), true); + + if(!empty($body)){ + $taxonomy_name = $this->taxonomy_repository->fetch($taxonomy_id)->get_db_identifier(); + + $identifiers = [ + 'term_id' => $term_id, + 'tax_name' => $taxonomy_name + ]; + + $attributes = []; + + foreach ($body as $att => $value){ + $attributes[$att] = $value; + } + + $updated_term = $this->terms_repository->update([$attributes, $identifiers]); + + return new WP_REST_Response($updated_term->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => 'The body could not be empty', + 'body' => $body + ], 400); + } + + public function update_item_permissions_check( $request ) { + $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); + return $this->terms_repository->can_edit($term); + } } ?> \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 3852a5819..d65f482f7 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -156,7 +156,20 @@ class Terms extends Repository { } public function update($object){ + $map = $this->get_map(); + $entity = []; + + foreach ($object[0] as $key => $value) { + if($key != 'ID') { + $entity[$map[$key]['map']] = $value ; + } + } + + $term_tax_ids = wp_update_term($object[1]['term_id'], $object[1]['tax_name'], $entity); + $term_id = (int) $term_tax_ids['term_id']; + + return new Entities\Term($term_id, $object[1]['tax_name']); } public function delete($args){ diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php index b67d3fae3..b434c6dc7 100644 --- a/tests/test-api-terms.php +++ b/tests/test-api-terms.php @@ -70,6 +70,48 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { $this->assertNull($term); } + + public function test_update_term(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => '1genero', + 'description' => 'tipos de musica', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $term = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Rock', + 'user' => get_current_user_id(), + ), + true + ); + + $new_attributes = json_encode([ + 'name' => 'Trap' + ]); + + $request = new \WP_REST_Request( + 'PATCH', $this->namespace . '/terms/' . $term . '/taxonomy/' . $taxonomy->get_id() + ); + + $request->set_body($new_attributes); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertNotEquals('Rock', $data['name']); + $this->assertEquals('Trap', $data['name']); + + $this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']); + } } ?> \ No newline at end of file