add endpoint methed `DELETE` for remoce a value of metadata compound #17

This commit is contained in:
vnmedeiros 2020-04-10 00:08:37 -03:00
parent 591495db6e
commit c015b37ac8
3 changed files with 82 additions and 1 deletions

View File

@ -47,7 +47,12 @@ class REST_Item_Metadata_Controller extends REST_Controller {
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::EDITABLE)
)
),
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check')
),
)
);
register_rest_route($this->namespace, '/item/(?P<item_id>[\d]+)/'. $this->rest_base,
@ -310,6 +315,56 @@ class REST_Item_Metadata_Controller extends REST_Controller {
return $query_params;
}
/**
* Verify if current user has permission to delete a item metadata value
*
* @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']);
$metadatum = $this->metadatum_repository->fetch( $request['metadatum_id'] );
if ( $item instanceof Entities\Item && $metadatum instanceof Entities\Metadatum ) {
if( $item->can_edit() && $metadatum->can_read() ) {
return true;
}
else {
// not yet implemented
// return 'publish' === $metadatum->get_status() && $metadatum->get_accept_suggestion();
}
}
}
return false;
}
public function delete_item( $request ) {
$body = json_decode( $request->get_body(), true );
if($body) {
$item_id = $request['item_id'];
$metadatum_id = $request['metadatum_id'];
$parent_meta_id = isset( $body['parent_meta_id'] ) && $body['parent_meta_id'] > 0 ? $body['parent_meta_id'] : null;
if($parent_meta_id == null) {
return new \WP_REST_Response( [
'error_message' => __( 'Please verify, invalid value(s)', 'tainacan' ),
'errors' => "operation permitted only compound metadata"
], 400 );
}
$item = $this->item_repository->fetch($request['item_id']);
$remove_item_metadata = $this->item_metadata_repository->remove_compound_value($item, $metadatum_id, $parent_meta_id);
return new \WP_REST_Response(["item_metadata_removed" => $remove_item_metadata], 200);
}
}
}
?>

View File

@ -208,7 +208,23 @@ class Item_Metadata extends Repository {
return add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_parent(), $current_value );
}
}
public function remove_compound_value(\Tainacan\Entities\Item $item, $metadatum_id, $parent_meta_id ) {
$post_id = $item->get_id();
$current_childrens = get_metadata_by_mid( 'post', $parent_meta_id );
if ( is_object( $current_childrens ) ) {
$current_childrens = $current_childrens->meta_value;
}
if ( ! is_array( $current_childrens ) ) {
$current_childrens = [];
}
foreach($current_childrens as $meta_children) {
delete_post_meta('post', $meta_children);
}
delete_metadata_by_mid('post', $parent_meta_id);
return $current_childrens;
}
/**

View File

@ -223,6 +223,16 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase {
$this->assertEquals( 'Green', $compoundValue[1][$metadatum_child1->get_id()]->get_value() , 'First element of value should have "Red" value' );
$this->assertEquals( 'Yellow', $compoundValue[1][$metadatum_child2->get_id()]->get_value() , 'Second element of value should have "Blue" value' );
$item_metadata_removed = $Tainacan_Item_Metadata->remove_compound_value($i, $metadatum, $item_metadata3->get_parent_meta_id());
$compoundItem = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum);
$compoundValue = $compoundItem->get_value();
$this->assertTrue( is_array($compoundValue), 'value of a compound should return array' );
$this->assertEquals( 1, sizeof($compoundValue), 'value should have 1 values' );
$this->assertEquals( 'Red', $compoundValue[0][$metadatum_child1->get_id()]->get_value() , 'First element of value should have "Red" value' );
$this->assertEquals( 'Blue', $compoundValue[0][$metadatum_child2->get_id()]->get_value() , 'Second element of value should have "Blue" value' );
}
function test_validations_taxonomy_in_multiple() {