Refactoring delete methods (issue #89)

This commit is contained in:
weryques 2018-08-10 14:49:14 -03:00
parent d2c75b2d92
commit 4c3d9c8375
16 changed files with 214 additions and 143 deletions

View File

@ -328,9 +328,11 @@ class REST_Collections_Controller extends REST_Controller {
$collection_id = $request['collection_id'];
$permanently = $request['permanently'];
$args = [$collection_id, $permanently];
$collection = $this->collections_repository->delete($args);
if($permanently == true) {
$collection = $this->collections_repository->delete($collection_id);
} else {
$collection = $this->collections_repository->trash($collection_id);
}
$prepared_collection = $this->prepare_item_for_response($collection, $request);

View File

@ -226,9 +226,11 @@ class REST_Filters_Controller extends REST_Controller {
$filter_id = $request['filter_id'];
$permanently = $request['permanently'];
$args = [$filter_id, $permanently];
$filter = $this->filter_repository->delete($args);
if($permanently == true) {
$filter = $this->filter_repository->delete($filter_id);
} else {
$filter = $this->filter_repository->trash($filter_id);
}
return new \WP_REST_Response($this->prepare_item_for_response($filter, $request), 200);
}

View File

@ -411,9 +411,11 @@ class REST_Items_Controller extends REST_Controller {
$item_id = $request['item_id'];
$permanently = $request['permanently'];
$args = [$item_id, $permanently];
$item = $this->items_repository->delete($args);
if($permanently == true) {
$item = $this->items_repository->delete($item_id);
} else {
$item = $this->items_repository->trash($item_id);
}
$prepared_item = $this->prepare_item_for_response($item, $request);

View File

@ -390,7 +390,7 @@ class REST_Metadata_Controller extends REST_Controller {
public function delete_item( $request ) {
$metadatum_id = $request['metadatum_id'];
$metadatum_trashed = $this->metadatum_repository->delete($metadatum_id);
$metadatum_trashed = $this->metadatum_repository->trash($metadatum_id);
$prepared = $this->prepare_item_for_response($metadatum_trashed, $request);

View File

@ -167,33 +167,24 @@ class REST_Taxonomies_Controller extends REST_Controller {
$taxonomy_id = $request['taxonomy_id'];
$permanently = $request['permanently'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
if(!empty($taxonomy)) {
$taxonomy_name = $taxonomy->get_db_identifier();
$args = [ $taxonomy_id, $taxonomy_name, $permanently ];
$deleted = $this->taxonomy_repository->delete( $args );
if($deleted instanceof \WP_Error) {
return new \WP_REST_Response( $deleted->get_error_message(), 400 );
} elseif(!$deleted){
return new \WP_REST_Response( [
'error_message' => __('Failed to delete', 'tainacan'),
'deleted' => $deleted
], 400 );
} elseif (!$deleted){
return new \WP_REST_Response($deleted, 400);
}
return new \WP_REST_Response($this->prepare_item_for_response($deleted, $request), 200);
if($permanently == true){
$deleted = $this->taxonomy_repository->delete($taxonomy_id);
} else {
$deleted = $this->taxonomy_repository->trash($taxonomy_id);
}
return new \WP_REST_Response([
'error_message' => __('A taxonomy with the ID "'. $taxonomy_id .'" was not found.', 'tainacan'),
'taxonomy' => $this->prepare_item_for_response($taxonomy, $request)
], 400);
if ( $deleted instanceof \WP_Error ) {
return new \WP_REST_Response( $deleted->get_error_message(), 400 );
} elseif ( ! $deleted ) {
return new \WP_REST_Response( [
'error_message' => __( 'Failed to delete', 'tainacan' ),
'deleted' => $deleted
], 400 );
} elseif ( ! $deleted ) {
return new \WP_REST_Response( $deleted, 400 );
}
return new \WP_REST_Response( $this->prepare_item_for_response( $deleted, $request ), 200 );
}
/**
@ -216,6 +207,7 @@ class REST_Taxonomies_Controller extends REST_Controller {
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
* @throws \Exception
*/
public function get_items( $request ) {
$args = $this->prepare_filters($request);

View File

@ -150,7 +150,8 @@ class REST_Terms_Controller extends REST_Controller {
$term_id = $request['term_id'];
$taxonomy_id = $request['taxonomy_id'];
$taxonomy_name = $this->taxonomy_repository->fetch( $taxonomy_id )->get_db_identifier();
$taxonomy = $this->taxonomy_repository->fetch( $taxonomy_id );
$taxonomy_name = $taxonomy->get_db_identifier();
if(!$taxonomy_name){
return new \WP_REST_Response([
@ -158,9 +159,9 @@ class REST_Terms_Controller extends REST_Controller {
]);
}
$args = [$term_id, $taxonomy_name];
$delete_args = ['term_id' => $term_id, 'taxonomy' => $taxonomy_name];
$is_deleted = $this->terms_repository->delete($args);
$is_deleted = $this->terms_repository->delete($delete_args);
return new \WP_REST_Response($is_deleted, 200);
}

View File

@ -279,22 +279,27 @@ class Collections extends Repository {
}
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
* @param $collection_id
*
* @return mixed|Collection
*/
public function delete( $args ) {
if ( ! empty( $args[1] ) && $args[1] == true ) {
$deleted = new Entities\Collection( wp_delete_post( $args[0], $args[1] ) );
public function delete( $collection_id ) {
$deleted = new Entities\Collection( wp_delete_post( $collection_id, true ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, [], false, true );
}
return $deleted;
if ( $deleted ) {
do_action( 'tainacan-deleted', $deleted, [], false, true );
}
$trashed = new Entities\Collection( wp_trash_post( $args[0] ) );
return $deleted;
}
/**
* @param $collection_id
*
* @return mixed|Collection
*/
public function trash( $collection_id ) {
$trashed = new Entities\Collection( wp_trash_post( $collection_id ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, [], false, false, true );

View File

@ -187,28 +187,31 @@ class Filters extends Repository {
return new Entities\Filter($metadatum->WP_Post);
}*/
/**
* @param array $args
* @return Entities\Filter
*
*/
public function delete($args){
if(!empty($args[1]) && $args[1] == true){
/**
* @param $filter_id
*
* @return Entities\Filter
*/
public function delete($filter_id){
$deleted = new Entities\Filter( wp_delete_post( $filter_id, true ) );
$deleted = new Entities\Filter(wp_delete_post($args[0], $args[1]));
if($deleted){
do_action('tainacan-deleted', $deleted, [], false, true);
}
return $deleted;
if ( $deleted ) {
do_action( 'tainacan-deleted', $deleted, [], false, true );
}
return $deleted;
}
$trashed = new Entities\Filter(wp_trash_post($args[0]));
/**
* @param $filter_id
*
* @return mixed|Entities\Filter
*/
public function trash($filter_id){
$trashed = new Entities\Filter(wp_trash_post($filter_id));
if($trashed){
do_action('tainacan-trashed', $trashed, [], false, false, true);
do_action('tainacan-trashed', $trashed, [], false, false, true);
}
return $trashed;

View File

@ -23,7 +23,13 @@ class Item_Metadata extends Repository {
return self::$instance;
}
public function insert($item_metadata) {
/**
* @param Entities\Entity $item_metadata
*
* @return Entities\Entity|Entities\Item_Metadata_Entity
* @throws \Exception
*/
public function insert($item_metadata) {
if ( ! $item_metadata->get_validated() ) {
throw new \Exception( 'Entities must be validated before you can save them' );
@ -114,13 +120,25 @@ class Item_Metadata extends Repository {
}
/**
* @param $item_metadata
* @param $item_metadata_id
*
* @return mixed|void
*/
public function delete($item_metadata){}
public function delete($item_metadata_id){}
public function save_core_metadatum_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
/**
* @param $item_metadata_id
*
* @return mixed|void
*/
public function trash($item_metadata_id){}
/**
* @param Entities\Item_Metadata_Entity $item_metadata
*
* @throws \Exception
*/
public function save_core_metadatum_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
$metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object();
if ($metadata_type->get_core()) {
@ -139,6 +157,11 @@ class Item_Metadata extends Repository {
}
}
/**
* @param $item_metadata
*
* @throws \Exception
*/
public function save_terms_metadatum_value($item_metadata) {
$metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object();
if ($metadata_type->get_primitive_type() == 'term') {

View File

@ -286,23 +286,27 @@ class Items extends Repository {
}
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
* @param $item_id
*
* @return mixed|Entities\Item
* @return mixed|Item
*/
public function delete( $args ) {
if ( ! empty( $args[1] ) && $args[1] == true ) {
public function delete( $item_id ) {
$deleted = new Entities\Item( wp_delete_post( $item_id, true ) );
$deleted = new Entities\Item( wp_delete_post( $args[0], $args[1] ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, false, true );
}
return $deleted;
if ( $deleted ) {
do_action( 'tainacan-deleted', $deleted, false, true );
}
$trashed = new Entities\Item( wp_trash_post( $args[0] ) );
return $deleted;
}
/**
* @param $item_id
*
* @return mixed|Item
*/
public function trash($item_id){
$trashed = new Entities\Item( wp_trash_post( $item_id ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, [], false, false, true );

View File

@ -208,9 +208,8 @@ class Logs extends Repository {
}
}
public function delete( $object ) {
}
public function delete( $object ){}
public function trash( $object ){}
public function update( $object, $new_values = null ) {
return $this->insert($object);
@ -282,7 +281,7 @@ class Logs extends Repository {
* @param bool $is_delete
* @param bool $is_trash
*
* @return Entities\Log new created log
* @return Entities\Log|bool new created log
*/
public function insert_log( $new_value, $diffs = [], $is_update = false, $is_delete = false, $is_trash = false ) {
$msn = null;
@ -323,7 +322,6 @@ class Logs extends Repository {
// was created
$msn = $this->prepare_event_message($new_value, $name, $class_name, 'created');
$description = $msn;
} elseif( $is_trash ) {
// was trashed
$msn = $this->prepare_event_message($new_value, $name, $class_name, 'trashed');
@ -368,17 +366,14 @@ class Logs extends Repository {
private function prepare_event_message($object, $name, $class_name, $action_message){
if ( $object instanceof Entities\Metadatum || $object instanceof Entities\Item || $object instanceof Entities\Filter) {
$collection = $object->get_collection();
$parent = $collection;
if ( $collection ) {
$parent = $collection->get_name();
if ( ! $parent ) {
$parent = $collection->get_status();
}
$parent = '(parent '. $collection->get_name() .')';
} else {
$parent = '(on repository level)';
}
$description = sprintf( __( "The \"%s\" %s has been %s (parent %s).", 'tainacan' ), $name, strtolower( $class_name ), $action_message, $parent );
$description = sprintf( __( "The \"%s\" %s has been %s %s.", 'tainacan' ), $name, strtolower( $class_name ), $action_message, $parent );
} else {
$description = sprintf( __( "The \"%s\" %s has been %s.", 'tainacan' ), $name, strtolower( $class_name ), $action_message );
}

View File

@ -538,16 +538,38 @@ class Metadata extends Repository {
return $this->insert($object);
}
public function delete($metadatum_id){
$this->delete_taxonomy_metadatum($metadatum_id);
/**
* @param $metadatum_id
*
* @return mixed|void
* @throws \Exception
*/
public function delete($metadatum_id){
$deleted = new Entities\Metadatum( wp_delete_post( $metadatum_id, true ) );
$deleted = new Entities\Metadatum( wp_trash_post( $metadatum_id ) );
if($deleted) {
if ( $deleted ) {
do_action( 'tainacan-deleted', $deleted, [], false, true );
}
return $deleted;
}
/**
* @param $metadatum_id
*
* @return mixed|Entities\Metadatum
* @throws \Exception
*/
public function trash($metadatum_id){
$this->delete_taxonomy_metadatum($metadatum_id);
$trashed = new Entities\Metadatum( wp_trash_post( $metadatum_id ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, [], false, false, true );
}
return $trashed;
}
/**
@ -629,8 +651,6 @@ class Metadata extends Repository {
wp_cache_flush();
}
/**
@ -764,6 +784,7 @@ class Metadata extends Repository {
* @param Entities\Collection $collection
*
* @return \Tainacan\Entities\Metadatum The Core Title Metadatum
* @throws \Exception
*/
public function get_core_title_metadatum( Entities\Collection $collection ) {
@ -789,6 +810,7 @@ class Metadata extends Repository {
* @param Entities\Collection $collection
*
* @return \Tainacan\Entities\Metadatum The Core Description Metadatum
* @throws \Exception
*/
public function get_core_description_metadatum( Entities\Collection $collection ) {
@ -1019,7 +1041,6 @@ class Metadata extends Repository {
}
}
//return $results;
return $this->unique_multidimensional_array($results, 'mvalue');
}

View File

@ -86,7 +86,7 @@ abstract class Repository {
*
* @param \Tainacan\Entities\Entity $obj
*
* @return \Tainacan\Entities\Entity
* @return \Tainacan\Entities\Entity | bool
* @throws \Exception
*/
public function insert( $obj ) {
@ -586,6 +586,13 @@ abstract class Repository {
*/
public abstract function delete( $object );
/**
* @param $object
*
* @return mixed
*/
public abstract function trash( $object );
/**
* @param $args
*

View File

@ -183,49 +183,51 @@ class Taxonomies extends Repository {
return $this->insert($object);
}
public function delete($args){
$taxonomy_id = $args[0];
$taxonomy_name = $args[1];
$permanently = $args[2];
public function delete($taxonomy_id){
$taxonomy_name = $this->fetch($taxonomy_id)->get_db_identifier();
if($permanently == true){
/* TODO: Investigate the cause of taxonomies aren't been registered
*
* This cause a 'invalid taxonomy' exception when try to delete permanently a taxonomy
*
* This condition is a temporary solution
*/
if(taxonomy_exists($taxonomy_name)) {
$unregistered = unregister_taxonomy( $taxonomy_name );
/* TODO: Investigate the cause of taxonomies aren't been registered
*
* This cause a 'invalid taxonomy' exception when try to delete permanently a taxonomy
*
* This condition is a temporary solution
*/
if ( taxonomy_exists( $taxonomy_name ) ) {
$unregistered = unregister_taxonomy( $taxonomy_name );
if ( $unregistered instanceof \WP_Error ) {
return $unregistered;
}
if ( $unregistered instanceof \WP_Error ) {
return $unregistered;
}
$deleted = new Entities\Taxonomy(wp_delete_post($taxonomy_id, true));
if(!$deleted){
return $deleted;
}
do_action('tainacan-deleted', $deleted, [], false, true);
return $deleted;
}
$deleted = new Entities\Taxonomy( wp_delete_post( $taxonomy_id, true ) );
if ( ! $deleted ) {
return $deleted;
}
do_action( 'tainacan-deleted', $deleted, [], false, true );
return $deleted;
}
/**
* @param $taxonomy_id
*
* @return mixed|Entities\Taxonomy
*/
public function trash($taxonomy_id){
$trashed = new Entities\Taxonomy(wp_trash_post($taxonomy_id));
if(!$trashed){
return $trashed;
if(!$trashed){
return $trashed;
}
do_action('tainacan-trashed', $trashed, [], false, false, true );
return $trashed;
return $trashed;
}
public function added_collection($taxonomy_id, $collection) {
$id = $taxonomy_id;
if (!empty($id) && is_numeric($id)) {

View File

@ -234,11 +234,16 @@ class Terms extends Repository {
return $this->insert($object);
}
public function delete($args){
$deleted = wp_delete_term($args[0], $args[1]);
/**
* @param Array $delete_args has ['term_id', 'taxonomy']
*
* @return bool|int|mixed|\WP_Error
*/
public function delete($delete_args){
$deleted = wp_delete_term($delete_args['term_id'], $delete_args['taxonomy']);
if($deleted) {
$deleted_term_tainacan = new Entities\Term($args[0], $args[1]);
$deleted_term_tainacan = new Entities\Term($delete_args['term_id'], $delete_args['taxonomy']);
do_action( 'tainacan-deleted', $deleted_term_tainacan, [], false, true );
}
@ -246,5 +251,12 @@ class Terms extends Repository {
return $deleted;
}
/**
* @param $term_id
*
* @return mixed|void
*/
public function trash($term_id){}
public function register_post_type() { }
}

View File

@ -194,7 +194,7 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
$this->assertContains($collection->get_id(), $checkTax2->get_collections_ids(), 'Collection must be added to taxonomy when metadatum is updated');
$this->assertNotContains($collection->get_id(), $checkTax->get_collections_ids(), 'Collection must be removed from taxonomy when metadatum is updated');
$metadatum = $Tainacan_Metadata->delete($metadatum->get_id());
$metadatum = $Tainacan_Metadata->trash($metadatum->get_id());
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());