From 7d616c5cca3aa528f88bd7a8fd06badc73db0d7f Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 29 Jan 2018 17:30:01 -0200 Subject: [PATCH] fixing terms api permission checks --- .../class-tainacan-rest-terms-controller.php | 20 ++++++++++++++----- .../class-tainacan-taxonomies.php | 7 ++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index a3248122c..1d0f61692 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -130,7 +130,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function create_item_permissions_check( $request ) { - return $this->terms_repository->can_edit($this->term); + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + if ($taxonomy instanceof Entities\Taxonomy) { + return $taxonomy->can_edit(); + } + return false; } /** @@ -163,8 +167,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function delete_item_permissions_check( $request ) { - $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); - return $this->terms_repository->can_delete($term); + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + if ($taxonomy instanceof Entities\Taxonomy) { + return $taxonomy->can_edit(); + } + return false; } /** @@ -209,8 +216,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function update_item_permissions_check( $request ) { - $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); - return $this->terms_repository->can_edit($term); + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + if ($taxonomy instanceof Entities\Taxonomy) { + return $taxonomy->can_edit(); + } + return false; } /** diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index ff6ca6fc4..77ebe1ed4 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -162,7 +162,12 @@ class Taxonomies extends Repository { // TODO: Pegar taxonomias registradas via código if( is_numeric($args) ){ - return new Entities\Taxonomy($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Taxonomy($existing_post); + } else { + return []; + } } elseif (is_array($args)) { $args = array_merge([