Update terms

This commit is contained in:
weryques 2018-01-17 12:33:28 -02:00
parent e37d4a6465
commit b3d92b7abb
3 changed files with 98 additions and 2 deletions

View File

@ -39,7 +39,12 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
array( array(
'methods' => WP_REST_Server::DELETABLE, 'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'), '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 ) { 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); 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);
}
} }
?> ?>

View File

@ -156,7 +156,20 @@ class Terms extends Repository {
} }
public function update($object){ 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){ public function delete($args){

View File

@ -70,6 +70,48 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
$this->assertNull($term); $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']);
}
} }
?> ?>