Update taxonomies

This commit is contained in:
weryques 2018-01-17 10:02:22 -02:00
parent 4b2103f800
commit e37d4a6465
3 changed files with 95 additions and 0 deletions

View File

@ -48,6 +48,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'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')
)
)
);
@ -227,6 +232,43 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
return $this->taxonomy_repository->can_edit($this->taxonomy);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$taxonomy_id = $request['taxonomy_id'];
$body = json_decode($request->get_body(), true);
if(!empty($body)){
$attributes = ['ID' => $taxonomy_id];
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$updated_taxonomy = $this->taxonomy_repository->update($attributes);
return new WP_REST_Response($updated_taxonomy->__toArray(), 200);
}
return new WP_REST_Response([
'error_message' => 'The body could not be empty',
'body' => $body
], 400);
}
/**
* @param WP_REST_Request $request
*
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
return $this->taxonomy_repository->can_edit($taxonomy);
}
}
?>

View File

@ -180,7 +180,22 @@ class Taxonomies extends Repository {
}
public function update($object){
$map = $this->get_map();
$entity = [];
foreach ($object as $key => $value) {
if($key != 'ID') {
$entity[$map[$key]['map']] = $value ;
} elseif ($key == 'ID'){
$entity[$key] = (int) $value;
}
}
$updated_taxonomy = new Entities\Taxonomy(wp_update_post($entity));
$updated_taxonomy->register_taxonomy();
return $updated_taxonomy;
}
public function delete($args){

View File

@ -122,6 +122,44 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals($taxonomy1->get_name(), $data[1]['name']);
$this->assertEquals($taxonomy2->get_name(), $data[0]['name']);
}
public function test_update_taxonomy(){
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Gender',
'description' => 'Music types',
'allow_insert' => 'yes',
'status' => 'publish'
),
true
);
$new_attributes = json_encode([
'name' => 'People',
'description' => 'Male or Female'
]);
$request = new \WP_REST_Request('PATCH', $this->namespace . '/taxonomies/' . $taxonomy->get_id());
$request->set_body($new_attributes);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$args=array(
'name' => $taxonomy->get_db_identifier()
);
$output = 'objects';
$tax = get_taxonomies($args, $output);
$this->assertNotEquals($taxonomy->get_name(), $data['name']);
$this->assertEquals('People', $data['name']);
$this->assertEquals('People', $tax[$taxonomy->get_db_identifier()]->label);
}
}
?>