From ab081cc992c04ab92e282ffbd82709cc399af30e Mon Sep 17 00:00:00 2001 From: weryques Date: Thu, 18 Jan 2018 10:45:31 -0200 Subject: [PATCH] Fetch all terms of a taxonomy --- .../class-tainacan-rest-terms-controller.php | 62 +++++++++++++++++++ tests/test-api-terms.php | 53 ++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 50df280d2..897611d4c 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -31,6 +31,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { 'methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'create_item'), 'permission_callback' => array($this, 'create_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check') ) ) ); @@ -196,6 +201,63 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); return $this->terms_repository->can_edit($term); } + + /** + * @param mixed $item + * @param WP_REST_Request $request + * + * @return array|mixed|WP_Error|WP_REST_Response + */ + public function prepare_item_for_response( $item, $request ) { + + if(is_array($item)){ + $prepared = []; + + foreach ($item as $term){ + $prepared[] = $term->__toArray(); + } + + return $prepared; + } + + return $item; + } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function get_items( $request ) { + $taxonomy_id = $request['taxonomy_id']; + + $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); + + if(!empty($taxonomy)){ + $args = json_decode($request->get_body(), true); + + $terms = $this->terms_repository->fetch($args, $taxonomy); + + $prepared_terms = $this->prepare_item_for_response($terms, $request); + + return new WP_REST_Response($prepared_terms, 200); + } + + return new WP_REST_Response([ + 'error_message' => "Taxonomy with this id ($taxonomy_id) not found.", + 'taxonomy_id' => $taxonomy_id + ], 400); + } + + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ + public function get_items_permissions_check( $request ) { + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + return $this->taxonomy_repository->can_read($taxonomy); + } } ?> \ No newline at end of file diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php index b434c6dc7..1abedf0e7 100644 --- a/tests/test-api-terms.php +++ b/tests/test-api-terms.php @@ -112,6 +112,59 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { $this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']); } + + public function test_fetch_terms(){ + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'Gender', + 'description' => 'Music types', + 'allow_insert' => 'yes', + 'status' => 'publish' + ), + true + ); + + $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Rock', + 'user' => get_current_user_id(), + ), + true + ); + + $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'name' => 'Trap', + 'user' => get_current_user_id(), + ), + true + ); + + $show_empty = json_encode([ + 'hide_empty' => false + ]); + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/terms/taxonomy/' . $taxonomy->get_id() + ); + + $request->set_body($show_empty); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $termA = $data[0]; + $termB = $data[1]; + + $this->assertEquals('Trap', $termB['name']); + $this->assertEquals('Rock', $termA['name']); + } } ?> \ No newline at end of file