Refactor trash and delete methods in repository #272

This commit is contained in:
Leo Germani 2019-06-27 22:53:14 -03:00
parent a4ad04c072
commit d2b469397f
18 changed files with 180 additions and 267 deletions

View File

@ -357,13 +357,20 @@ class REST_Collections_Controller extends REST_Controller {
* @return string|\WP_Error|\WP_REST_Response
*/
public function delete_item( $request ) {
$collection_id = $request['collection_id'];
$permanently = $request['permanently'];
$collection = $this->collections_repository->fetch($request['collection_id']);
if(! $collection instanceof Entities\Collection) {
return new \WP_REST_Response([
'error_message' => __('Collection with this ID was not found', 'tainacan' ),
'collection_id' => $collection_id
], 400);
}
if($permanently == true) {
$collection = $this->collections_repository->delete($collection_id);
$collection = $this->collections_repository->delete($collection);
} else {
$collection = $this->collections_repository->trash($collection_id);
$collection = $this->collections_repository->trash($collection);
}
$prepared_collection = $this->prepare_item_for_response($collection, $request);

View File

@ -223,13 +223,21 @@ class REST_Filters_Controller extends REST_Controller {
* @return \WP_Error|\WP_REST_Response
*/
public function delete_item( $request ) {
$filter_id = $request['filter_id'];
$permanently = $request['permanently'];
$filter = $this->filter_repository->fetch($request['filter_id']);
if (! $filter instanceof Entities\Filter) {
return new \WP_REST_Response([
'error_message' => __('A filter with this ID was not found', 'tainacan' ),
'filter_id' => $filter_id
], 400);
}
if($permanently == true) {
$filter = $this->filter_repository->delete($filter_id);
$filter = $this->filter_repository->delete($filter);
} else {
$filter = $this->filter_repository->trash($filter_id);
$filter = $this->filter_repository->trash($filter);
}
return new \WP_REST_Response($this->prepare_item_for_response($filter, $request), 200);
@ -421,7 +429,14 @@ class REST_Filters_Controller extends REST_Controller {
$filter_id = $request['filter_id'];
$filter = $this->filter_repository->fetch($filter_id);
if(! $filter instanceof Entities\Filter) {
return new \WP_REST_Response([
'error_message' => __('A filter with this ID was not found', 'tainacan' ),
'filter_id' => $filter_id
], 400);
}
return new \WP_REST_Response($this->prepare_item_for_response($filter, $request), 200);
}

View File

@ -237,7 +237,14 @@ class REST_Items_Controller extends REST_Controller {
$item_id = $request['item_id'];
$item = $this->items_repository->fetch($item_id);
if (! $item instanceof Entities\Item) {
return new \WP_REST_Response([
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
}
$response = $this->prepare_item_for_response($item, $request);
return new \WP_REST_Response(apply_filters('tainacan-rest-response', $response, $request), 200);
@ -513,11 +520,20 @@ class REST_Items_Controller extends REST_Controller {
public function delete_item( $request ) {
$item_id = $request['item_id'];
$permanently = $request['permanently'];
$item = $this->items_repository->fetch($request['item_id']);
if (! $item instanceof Entities\Item) {
return new \WP_REST_Response([
'error_message' => __('An item with this ID was not found', 'tainacan' ),
'item_id' => $item_id
], 400);
}
if($permanently == true) {
$item = $this->items_repository->delete($item_id);
$item = $this->items_repository->delete($item);
} else {
$item = $this->items_repository->trash($item_id);
$item = $this->items_repository->trash($item);
}
$prepared_item = $this->prepare_item_for_response($item, $request);

View File

@ -137,6 +137,13 @@ class REST_Metadata_Controller extends REST_Controller {
}
$result = $this->metadatum_repository->fetch($metadatum_id, 'OBJECT');
if (! $result instanceof Entities\Metadatum) {
return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'item_id' => $item_id
], 400);
}
return new \WP_REST_Response($this->prepare_item_for_response($result, $request), 200);
}
@ -385,8 +392,17 @@ class REST_Metadata_Controller extends REST_Controller {
*/
public function delete_item( $request ) {
$metadatum_id = $request['metadatum_id'];
$metadatum_trashed = $this->metadatum_repository->trash($metadatum_id);
$metadatum = $this->metadatum_repository->fetch($metadatum_id);
if (! $metadatum instanceof Entities\Metadatum) {
return new \WP_REST_Response([
'error_message' => __('Metadata with this ID was not found', 'tainacan'),
'item_id' => $item_id
], 400);
}
$metadatum_trashed = $this->metadatum_repository->trash($metadatum);
$prepared = $this->prepare_item_for_response($metadatum_trashed, $request);

View File

@ -182,7 +182,14 @@ class REST_Taxonomies_Controller extends REST_Controller {
$taxonomy_id = $request['taxonomy_id'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
if (! $taxonomy instanceof Entities\Taxonomy) {
return new \WP_REST_Response([
'error_message' => __('A taxonomy with this ID was not found', 'tainacan' ),
'taxonomy_id' => $taxonomy_id
], 400);
}
$taxonomy_prepared = $this->prepare_item_for_response($taxonomy, $request);
return new \WP_REST_Response($taxonomy_prepared, 200);
@ -218,11 +225,20 @@ class REST_Taxonomies_Controller extends REST_Controller {
public function delete_item( $request ) {
$taxonomy_id = $request['taxonomy_id'];
$permanently = $request['permanently'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
if (! $taxonomy instanceof Entities\Taxonomy) {
return new \WP_REST_Response([
'error_message' => __('A taxonomy with this ID was not found', 'tainacan' ),
'taxonomy_id' => $taxonomy_id
], 400);
}
if($permanently == true){
$deleted = $this->taxonomy_repository->delete($taxonomy_id);
$deleted = $this->taxonomy_repository->delete($taxonomy);
} else {
$deleted = $this->taxonomy_repository->trash($taxonomy_id);
$deleted = $this->taxonomy_repository->trash($taxonomy);
}
if ( $deleted instanceof \WP_Error ) {

View File

@ -149,19 +149,20 @@ class REST_Terms_Controller extends REST_Controller {
public function delete_item( $request ) {
$term_id = $request['term_id'];
$taxonomy_id = $request['taxonomy_id'];
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$taxonomy = $this->taxonomy_repository->fetch( $taxonomy_id );
$taxonomy_name = $taxonomy->get_db_identifier();
if(!$taxonomy_name){
$term = $this->terms_repository->fetch($term_id, $taxonomy);
if ( ! $term instanceof Entities\Term ) {
return new \WP_REST_Response([
'error_message' => 'The ID of taxonomy may be incorrect.'
]);
'error_message' => __('A term with this ID was not found', 'tainacan' ),
'taxonomy_id' => $taxonomy_id,
'term_id' => $term_id
], 400);
}
$delete_args = ['term_id' => $term_id, 'taxonomy' => $taxonomy_name];
$is_deleted = $this->terms_repository->delete($delete_args);
$is_deleted = $this->terms_repository->delete($term);
return new \WP_REST_Response($is_deleted, 200);
}
@ -375,6 +376,14 @@ class REST_Terms_Controller extends REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($tax_id);
$term = $this->terms_repository->fetch($term_id, $taxonomy);
if ( ! $term instanceof Entities\Term ) {
return new \WP_REST_Response([
'error_message' => __('A term with this ID was not found', 'tainacan' ),
'taxonomy_id' => $tax_id,
'term_id' => $term_id
], 400);
}
return new \WP_REST_Response($this->prepare_item_for_response($term, $request), 200);
}

View File

@ -294,40 +294,6 @@ class Collections extends Repository {
return $this->insert( $object );
}
/**
* @param $collection_id
*
* @return mixed|Collection
*/
public function delete( $collection_id ) {
$deleted = new Entities\Collection( wp_delete_post( $collection_id, true ) );
if ( $deleted && $this->use_logs) {
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
}
return $deleted;
}
/**
* @param $collection_id
*
* @return mixed|Collection
*/
public function trash( $collection_id ) {
$trashed = new Entities\Collection( wp_trash_post( $collection_id ) );
if ( $trashed && $this->use_logs) {
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
}
/**
* fetch collection based on ID or WP_Query args
*

View File

@ -24,7 +24,7 @@ class Filters extends Repository {
protected function __construct() {
parent::__construct();
add_action( 'tainacan-deleted', array( &$this, 'hook_delete_when_metadata_deleted' ) );
add_action( 'tainacan-deleted-tainacan-metadatum', array( &$this, 'hook_delete_when_metadata_deleted' ), 10, 2 );
}
public function get_map() {
@ -188,40 +188,6 @@ class Filters extends Repository {
return new Entities\Filter($metadatum->WP_Post);
}*/
/**
* @param $filter_id
*
* @return Entities\Filter
*/
public function delete( $filter_id ) {
$deleted = new Entities\Filter( wp_delete_post( $filter_id, true ) );
if ( $deleted && $this->use_logs) {
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
}
return $deleted;
}
/**
* @param $filter_id
*
* @return mixed|Entities\Filter
*/
public function trash( $filter_id ) {
$trashed = new Entities\Filter( wp_trash_post( $filter_id ) );
if ( $trashed && $this->use_logs) {
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
}
public function update( $object, $new_values = null ) {
return $this->insert( $object );
}
@ -542,14 +508,12 @@ class Filters extends Repository {
return $result;
}
public function hook_delete_when_metadata_deleted($entity) {
public function hook_delete_when_metadata_deleted($filter, $permanent) {
if ( $entity instanceof Entities\Metadatum ) {
$metadatum_id = $entity->get_id();
var_dump($metadatum_id);
$filters = $this->fetch(['metadatum_id' => $metadatum_id], 'OBJECT');
if ( $filter instanceof Entities\Metadatum && $permanent ) {
$metadatum_id = $filter->get_id();
$filters = $this->fetch(['metadatum_id' => $metadatum_id, 'post_status' => 'any'], 'OBJECT');
foreach ($filters as $filter) {
var_dump($filter->get_id());
$this->delete($filter);
}
}

View File

@ -128,22 +128,6 @@ class Item_Metadata extends Repository {
}
/**
* @param $item_metadata_id
*
* @return mixed|void
*/
public function delete( $item_metadata_id ) {
}
/**
* @param $item_metadata_id
*
* @return mixed|void
*/
public function trash( $item_metadata_id ) {
}
/**
* @param Entities\Item_Metadata_Entity $item_metadata
*

View File

@ -323,39 +323,6 @@ class Items extends Repository {
return $this->insert( $object );
}
/**
* @param $item_id
*
* @return mixed|Item
*/
public function delete( $item_id ) {
$deleted = new Entities\Item( wp_delete_post( $item_id, true ) );
if ( $deleted && $this->use_logs) {
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
}
return $deleted;
}
/**
* @param $item_id
*
* @return mixed|Item
*/
public function trash( $item_id ) {
$trashed = new Entities\Item( wp_trash_post( $item_id ) );
if ( $trashed && $this->use_logs) {
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
}
/**
* allow wp query filter post by array of titles

View File

@ -215,12 +215,6 @@ class Logs extends Repository {
}
}
public function delete( $object ) {
}
public function trash( $object ) {
}
public function update( $object, $new_values = null ) {
return $this->insert( $object );
}

View File

@ -550,45 +550,6 @@ class Metadata extends Repository {
return $this->insert( $object );
}
/**
* @param $metadatum_id
*
* @return mixed|void
* @throws \Exception
*/
public function delete( $metadatum_id ) {
$deleted = new Entities\Metadatum( wp_delete_post( $metadatum_id, true ) );
var_dump($deleted);
var_dump($deleted->get_id());
if ( $deleted && $this->use_logs) {
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
}
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 && $this->use_logs) {
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
}
/**
* fetch all registered metadatum type classes
*
@ -1390,9 +1351,16 @@ class Metadata extends Repository {
}
}
/**
* @inheritDoc
*/
public function delete( Entities\Entity $entity, $permanent = true ) {
$this->delete_taxonomy_metadatum($entity);
return parent::delete($entity, $permanent);
}
private function delete_taxonomy_metadatum( $metadatum_id ) {
$metadatum = $this->fetch( $metadatum_id );
private function delete_taxonomy_metadatum( $metadatum ) {
$metadata_type = $metadatum->get_metadata_type_object();
if ( $metadata_type->get_primitive_type() == 'term' ) {

View File

@ -595,20 +595,45 @@ abstract class Repository {
return false;
}
/**
* Shortcut to delete($entity, false)
*
* @param Entities\Entity $entity
*
* @return mixed|Entity @see https://developer.wordpress.org/reference/functions/wp_delete_post/
*/
public function trash( Entities\Entity $entity ) {
return $this->delete( $entity, false );
}
/**
* @param $object
* @param Entities\Entity $entity
* @param bool $permanent If false, sendo to trash, if true, permanently delete. Default true
*
* @return mixed
* @return mixed|Entity @see https://developer.wordpress.org/reference/functions/wp_delete_post/
*/
public abstract function delete( $object );
public function delete( Entities\Entity $entity, $permanent = true ) {
if ($permanent === true) {
$return = wp_delete_post( $entity->get_id(), $permanent );
} elseif ($permanent === false) {
$return = wp_trash_post( $entity->get_id() );
}
/**
* @param $object
*
* @return mixed
*/
public abstract function trash( $object );
if ( $return instanceof \WP_Post && $this->use_logs) {
$this->logs_repository->insert_log( $entity, [], false, false, true );
do_action( 'tainacan-deleted', $entity, $permanent );
var_dump('tainacan-deleted-' . $entity->get_post_type());
do_action( 'tainacan-deleted-' . $entity->get_post_type(), $entity, $permanent );
$return = $this->get_entity_by_post($return);
}
return $return;
}
/**
* @param $args

View File

@ -245,8 +245,8 @@ class Taxonomies extends Repository {
return $this->insert( $object );
}
public function delete( $taxonomy_id ) {
$taxonomy_name = $this->fetch( $taxonomy_id )->get_db_identifier();
public function delete( Entities\Entity $taxonomy, $permanent = true ) {
$taxonomy_name = $taxonomy->get_db_identifier();
/* TODO: Investigate the cause of taxonomies aren't been registered
*
@ -254,48 +254,16 @@ class Taxonomies extends Repository {
*
* This condition is a temporary solution
*/
if ( taxonomy_exists( $taxonomy_name ) ) {
if ( taxonomy_exists( $taxonomy_name ) && $permanent ) {
$unregistered = unregister_taxonomy( $taxonomy_name );
if ( $unregistered instanceof \WP_Error ) {
return $unregistered;
}
}
$deleted = new Entities\Taxonomy( wp_delete_post( $taxonomy_id, true ) );
if ( ! $deleted ) {
return $deleted;
}
if($this->use_logs){
$this->logs_repository->insert_log( $deleted, [], false, true );
}
do_action( 'tainacan-deleted', $deleted );
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($this->use_logs){
$this->logs_repository->insert_log( $trashed, [], false, false, true );
}
do_action( 'tainacan-trashed', $trashed );
return $trashed;
return parent::delete($taxonomy, $permanent);
}
public function added_collection( $taxonomy_id, $collection_id ) {

View File

@ -177,7 +177,7 @@ class Terms extends Repository {
do_action( 'tainacan-insert', $term, $diffs, $is_update );
do_action( 'tainacan-insert-Term', $term );
do_action( 'tainacan-insert-term', $term );
return new Entities\Term( $term_saved['term_id'], $term->get_taxonomy() );
}
@ -260,24 +260,22 @@ class Terms extends Repository {
}
/**
* @param Array $delete_args has ['term_id', 'taxonomy']
* @param Entities\Term $term
* @param bool $permanent this parameter is not used by Terms repository
*
* @return bool|int|mixed|\WP_Error
*/
public function delete( $delete_args ) {
$deleted = wp_delete_term( $delete_args['term_id'], $delete_args['taxonomy'] );
public function delete( Entities\Entity $term, $permanent = true ) {
$deleted = $term;
$return = wp_delete_term( $term->get_id(), $term->get_taxonomy() );
if ( $deleted ) {
$deleted_term_tainacan = new Entities\Term( $delete_args['term_id'], $delete_args['taxonomy'] );
if($this->use_logs){
$this->logs_repository->insert_log( $deleted_term_tainacan, [], false, true );
}
do_action( 'tainacan-deleted', $deleted_term_tainacan );
if ( $deleted && $this->use_logs ) {
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
do_action( 'tainacan-deleted-term', $deleted );
}
return $deleted;
return $return;
}
/**
@ -341,13 +339,6 @@ class Terms extends Repository {
}
/**
* @param $term_id
*
* @return mixed|void
*/
public function trash( $term_id ) {
}
public function register_post_type() {
}

View File

@ -80,8 +80,15 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
}
public function test_delete_or_trash_item_from_a_collection(){
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Agile',
'description' => 'Agile methods',
'status' => 'publish'
),
true
);
$item1 = $this->tainacan_entity_factory->create_entity(
'item',
array(

View File

@ -213,7 +213,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->trash($metadatum->get_id());
$metadatum = $Tainacan_Metadata->trash($metadatum);
$checkTax2 = $Tainacan_Taxonomies->fetch($tax2->get_id());

View File

@ -406,7 +406,7 @@ class Items extends TAINACAN_UnitTestCase {
$items = \Tainacan\Repositories\Items::get_instance();
$items->delete($item_id);
$items->delete($item);
$fetch_item = $items->fetch($item_id);