Delete Term

This commit is contained in:
weryques 2018-01-15 17:47:27 -02:00
parent 533f07f965
commit eac7db72c4
3 changed files with 67 additions and 6 deletions

View File

@ -25,7 +25,7 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
}
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<taxonomy_id>[\d]+)',
register_rest_route($this->namespace, '/' . $this->rest_base . '/taxonomy/(?P<taxonomy_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
@ -34,6 +34,15 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
)
)
);
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<term_id>[\d]+)/taxonomy/(?P<taxonomy_id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callbacl' => array($this, 'delete_item_permissions_check')
)
)
);
}
/**
@ -104,11 +113,27 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
}
public function delete_item( $request ) {
return parent::delete_item( $request ); // TODO: Change the autogenerated stub
$term_id = $request['term_id'];
$taxonomy_id = $request['taxonomy_id'];
$taxonomy_name = $this->taxonomy_repository->fetch( $taxonomy_id )->get_db_identifier();
if(!$taxonomy_name){
return new WP_REST_Response([
'error_message' => 'The ID of taxonomy may be incorrect.'
]);
}
$args = [$term_id, $taxonomy_name];
$is_deleted = $this->terms_repository->delete($args);
return new WP_REST_Response($is_deleted, 200);
}
public function delete_item_permissions_check( $request ) {
return parent::delete_item_permissions_check( $request ); // TODO: Change the autogenerated stub
$term = $this->terms_repository->fetch($request['term_id']);
return $this->terms_repository->can_delete($term);
}
}

View File

@ -159,8 +159,8 @@ class Terms extends Repository {
}
public function delete($object){
public function delete($args){
return wp_delete_term($args[0], $args[1]);
}
public function register_post_type() { }

View File

@ -17,7 +17,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
);
$request = new \WP_REST_Request(
'POST', $this->namespace . '/terms/' . $taxonomy->get_id()
'POST', $this->namespace . '/terms/taxonomy/' . $taxonomy->get_id()
);
$term = json_encode([
@ -34,6 +34,42 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
$this->assertEquals('Termo teste', $data['name']);
$this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']);
}
public function test_delete(){
$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
);
$request = new \WP_REST_Request('DELETE', $this->namespace . '/terms/' . $term . '/taxonomy/' . $taxonomy->get_id());
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertTrue($data);
$term = get_term($term);
$this->assertNull($term);
}
}
?>