diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 7eb5d23b6..046de7598 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -175,7 +175,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { $set_ = 'set_' . $key; $new_metadata->$set_( $value ); } catch (\Error $exception){ - //echo $exception->getMessage(); + // Do nothing } } diff --git a/src/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-metadata-controller.php index 90a376758..ec7e82e58 100644 --- a/src/api/endpoints/class-tainacan-rest-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -162,6 +162,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { } } + /** + * @param $request + * + * @return bool|WP_Error + */ public function create_item_permissions_check( $request ) { if(current_user_can('edit_posts')){ return true; @@ -170,6 +175,12 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { return false; } + /** + * @param mixed $item + * @param WP_REST_Request $request + * + * @return array|WP_Error|WP_REST_Response + */ public function prepare_item_for_response( $item, $request ) { $metadata_as = []; @@ -184,10 +195,14 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { } } - //var_dump($metadata_as); return $metadata_as; } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function get_items( $request ) { if(!empty($request['collection_id'])){ $collection_id = $request['collection_id']; @@ -212,6 +227,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { return new WP_REST_Response($prepared_item, 200); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function get_item_permissions_check( $request ) { if(current_user_can('read')){ return true; @@ -220,6 +240,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { return false; } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function get_items_permissions_check( $request ) { if(current_user_can('read')){ return true; @@ -228,10 +253,18 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { return false; } + /** + * @return array + */ public function get_collection_params() { return parent::get_collection_params(); // TODO: Change the autogenerated stub } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function delete_item( $request ) { if(!empty($request->get_body())){ $body = json_decode($request->get_body()); @@ -243,6 +276,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { } } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function delete_item_permissions_check( $request ) { if(current_user_can('delete_posts')){ return true; diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php index 5a72f6759..d63041164 100644 --- a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -7,6 +7,9 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { private $taxonomy; private $taxonomy_repository; + /** + * TAINACAN_REST_Taxonomies_Controller constructor. + */ public function __construct() { $this->namespace = 'tainacan/v2'; $this->rest_base = 'taxonomies'; @@ -50,6 +53,12 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { ); } + /** + * @param mixed $item + * @param WP_REST_Request $request + * + * @return array|WP_Error|WP_REST_Response + */ public function prepare_item_for_response( $item, $request ) { $taxonomies = []; @@ -64,6 +73,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { return $taxonomies; } + /** + * @param WP_REST_Request $request + * + * @return object|void|WP_Error + */ public function prepare_item_for_database( $request ) { foreach ($request as $key => $value){ $set_ = 'set_' . $key; @@ -71,6 +85,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { } } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function get_item( $request ) { $taxonomy_id = $request['taxonomy_id']; @@ -81,6 +100,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { return new WP_REST_Response($taxonomy_prepared, 200); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function get_item_permissions_check( $request ) { if(current_user_can('read')){ return true; @@ -90,9 +114,51 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { } public function delete_item( $request ) { - return parent::delete_item( $request ); // TODO: Change the autogenerated stub + $taxonomy_id = $request['taxonomy_id']; + + if(empty($request->get_body())){ + return new WP_REST_Response([ + 'error_message' => __('Body can not be empty.', 'tainacan'), + 'body' => $request->get_body() + ], 400); + } + + $is_permanently = json_decode($request->get_body(), true); + + $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); + + if(!empty($taxonomy)) { + $taxonomy_name = $taxonomy->get_db_identifier(); + + $args = [ $taxonomy_id, $taxonomy_name, $is_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' => __('Failure on deleted.', 'tainacan'), + 'deleted' => $deleted + ], 400 ); + } elseif (!$deleted){ + return new WP_REST_Response($deleted, 400); + } + + return new WP_REST_Response($deleted->__toArray(), 200); + } + + return new WP_REST_Response([ + 'error_message' => __('Taxonomy with this id ('. $taxonomy_id .') not found.', 'tainacan'), + 'taxonomy' => $taxonomy->__toArray() + ], 400); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function delete_item_permissions_check( $request ) { if (current_user_can('delete_posts')){ return true; @@ -101,6 +167,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { return false; } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function get_items( $request ) { $taxonomies = $this->taxonomy_repository->fetch([], 'OBJECT'); @@ -109,6 +180,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { return new WP_REST_Response($taxonomies_prepared, 200); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function get_items_permissions_check( $request ) { if (current_user_can('read')){ return true; @@ -117,6 +193,11 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { return false; } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function create_item( $request ) { $body = json_decode($request->get_body(), true); @@ -137,10 +218,16 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { } else { return new WP_REST_Response([ 'error_message' => __('Body can not be empty.', 'tainacan'), + 'body' => $body ], 400); } } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function create_item_permissions_check( $request ) { if(current_user_can('edit_posts')){ return true; diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 970d155e4..7ce547160 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -9,6 +9,9 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { private $taxonomy; private $taxonomy_repository; + /** + * TAINACAN_REST_Terms_Controller constructor. + */ public function __construct() { $this->namespace = 'tainacan/v2'; $this->rest_base = 'terms'; @@ -34,9 +37,14 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { } public function create_item( $request ) { - return parent::create_item( $request ); // TODO: Change the autogenerated stub + } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function create_item_permissions_check( $request ) { if(current_user_can('edit_posts')){ return true; diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index 9f48de755..467debaa4 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -41,11 +41,14 @@ class Entity { * @var boolean */ private $validated = false; - - /** - * Create an instance of Entity and get post data from database or create a new StdClass if $which is 0 - * @param integer|\WP_Post optional $which Entity ID or a WP_Post object for existing Entities. Leave empty to create a new Entity. - */ + + /** + * Create an instance of Entity and get post data from database or create a new StdClass if $which is 0 + * + * @param integer|\WP_Post optional $which Entity ID or a WP_Post object for existing Entities. Leave empty to create a new Entity. + * + * @throws \Exception + */ function __construct($which = 0) { if (is_numeric($which) && $which > 0) { $post = get_post($which); @@ -165,14 +168,14 @@ class Entity { if (is_array($prop_value)) { foreach ($prop_value as $val) { if (!$validation->validate($val)) { - // + $this->add_error('invalid', $message); $is_valid = false; } } } else { if (!$validation->validate($prop_value)) { - // + $this->add_error('invalid', $message); $is_valid = false; } @@ -253,8 +256,8 @@ class Entity { return $attributes; } + public function __toJSON(){ return json_encode($this->__toArray(), JSON_NUMERIC_CHECK); } - } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index b55d0631a..386e0474c 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -18,46 +18,46 @@ class Taxonomies extends Repository { 'name' => [ 'map' => 'post_title', 'title' => __('Name', 'tainacan'), - 'type' => 'string', - 'description'=> __('Name of the taxonomy', 'tainacan'), - 'on_error' => __('The taxonomy should be a text value and not empty', 'tainacan'), - 'validation' => v::stringType()->notEmpty(), + 'type' => 'string', + 'description' => __('Name of the taxonomy', 'tainacan'), + 'on_error' => __('The taxonomy should be a text value and not empty', 'tainacan'), + 'validation' => v::stringType()->notEmpty(), ], 'parent' => [ 'map' => 'parent', - 'title' => __('Parent', 'tainacan'), - 'type' => 'integer', - 'description'=> __('Parent taxonomy', 'tainacan'), + 'title' => __('Parent', 'tainacan'), + 'type' => 'integer', + 'description' => __('Parent taxonomy', 'tainacan'), 'validation' => '' ], 'description' => [ 'map' => 'post_content', - 'title' => __('Description', 'tainacan'), - 'type' => 'string', - 'description'=> __('The taxonomy description', 'tainacan'), + 'title' => __('Description', 'tainacan'), + 'type' => 'string', + 'description' => __('The taxonomy description', 'tainacan'), 'validation' => '' ], 'slug' => [ 'map' => 'post_name', - 'title' => __('Slug', 'tainacan'), - 'type' => 'string', - 'description'=> __('The taxonomy slug', 'tainacan'), + 'title' => __('Slug', 'tainacan'), + 'type' => 'string', + 'description' => __('The taxonomy slug', 'tainacan'), 'validation' => '' ], 'allow_insert' => [ 'map' => 'meta', - 'title' => __('Allow insert', 'tainacan'), - 'type' => 'string', - 'description'=> __('Allow/Deny the creation of new terms in the taxonomy', 'tainacan'), - 'on_error' => __('Allow insert is invalid, allowed values ( yes/no )', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no - 'default' => 'yes' + 'title' => __('Allow insert', 'tainacan'), + 'type' => 'string', + 'description' => __('Allow/Deny the creation of new terms in the taxonomy', 'tainacan'), + 'on_error' => __('Allow insert is invalid, allowed values ( yes/no )', 'tainacan'), + 'validation' => v::stringType()->in(['yes', 'no']), // yes or no + 'default' => 'yes' ], 'collections_ids' => [ 'map' => 'meta_multi', - 'title' => __('Collections', 'tainacan'), - 'type' => 'string', - 'description'=> __('The IDs of collection where the taxonomy is used', 'tainacan'), + 'title' => __('Collections', 'tainacan'), + 'type' => 'string', + 'description' => __('The IDs of collection where the taxonomy is used', 'tainacan'), 'validation' => '' ], ]); @@ -181,7 +181,33 @@ class Taxonomies extends Repository { } - public function delete($object){ + public function delete($args){ + $taxonomy_id = $args[0]; + $taxonomy_name = $args[1]; + $is_permanently = $args[2]['is_permanently']; + if($is_permanently === true){ + $unregistered = unregister_taxonomy($taxonomy_name); + + if($unregistered instanceof \WP_Error){ + return $unregistered; + } + + $deleted = wp_delete_post($taxonomy_id, true); + + if(!$deleted){ + return $deleted; + } + + return new Entities\Taxonomy($deleted); + } + + $trashed = wp_trash_post($taxonomy_id); + + if(!$trashed){ + return $trashed; + } + + return new Entities\Taxonomy($trashed); } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 47d9649ae..16c4586b5 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -63,8 +63,7 @@ class Terms extends Repository { ]); } - public function get_default_properties($map) - { + public function get_default_properties($map) { $defaults = parent::get_default_properties($map); //its uses the term_id and not id unset($defaults['id']); @@ -150,7 +149,7 @@ class Terms extends Repository { $tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) ); return $tainacan_term; - }else{ + } else{ return []; } }