From 15612ef35adb15f4dea6e5a1539bb6218d76929d Mon Sep 17 00:00:00 2001 From: weryques Date: Thu, 18 Jan 2018 11:38:31 -0200 Subject: [PATCH] Fetch a Term --- ...class-tainacan-rest-filters-controller.php | 10 ++++++ .../class-tainacan-rest-terms-controller.php | 31 +++++++++++++++++++ tests/test-api-filters.php | 2 +- tests/test-api-terms.php | 14 ++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index 0df6aa2d9..56bf27ebc 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -258,6 +258,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { return $this->filter_repository->can_read($this->filter); } + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ public function get_item( $request ) { $filter_id = $request['filter_id']; @@ -266,6 +271,11 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { return new WP_REST_Response($filter->__toArray(), 200); } + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ public function get_item_permissions_check( $request ) { $filter = $this->filter_repository->fetch($request['filter_id']); return $this->filter_repository->can_read($filter); diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 1a6fa0d2c..e7edfc1bd 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -50,6 +50,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { 'methods' => WP_REST_Server::EDITABLE, 'callback' => array($this, 'update_item'), 'permission_callback' => array($this, 'update_item_permissions_check') + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_item'), + 'permission_callback' => array($this, 'get_item_permissions_check') ) ) ); @@ -251,6 +256,32 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); return $this->taxonomy_repository->can_read($taxonomy); } + + /** + * @param WP_REST_Request $request + * + * @return WP_Error|WP_REST_Response + */ + public function get_item( $request ) { + $term_id = $request['term_id']; + $tax_id = $request['taxonomy_id']; + + $taxonomy = $this->taxonomy_repository->fetch($tax_id); + + $term = $this->terms_repository->fetch($term_id, $taxonomy); + + return new WP_REST_Response($term->__toArray(), 200); + } + + /** + * @param WP_REST_Request $request + * + * @return bool|WP_Error + */ + public function get_item_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-filters.php b/tests/test-api-filters.php index ca5996797..8028a660e 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -235,7 +235,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals($filter2->get_name(), $first_filter['name']); $this->assertEquals($filter->get_name(), $second_filter['name']); - #### FETCH ONE FILTER #### + #### FETCH A FILTER #### $request = new \WP_REST_Request( 'GET', $this->namespace . '/filters/' . $filter->get_id() diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php index 1abedf0e7..bdc1f89dd 100644 --- a/tests/test-api-terms.php +++ b/tests/test-api-terms.php @@ -135,7 +135,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { true ); - $this->tainacan_entity_factory->create_entity( + $term2 = $this->tainacan_entity_factory->create_entity( 'term', array( 'taxonomy' => $taxonomy->get_db_identifier(), @@ -164,6 +164,18 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { $this->assertEquals('Trap', $termB['name']); $this->assertEquals('Rock', $termA['name']); + + #### FETCH A TERM #### + + $request = new \WP_REST_Request( + 'GET', $this->namespace . '/terms/' . $term2 . '/taxonomy/' . $taxonomy->get_id() + ); + + $response = $this->server->dispatch($request); + + $data = $response->get_data(); + + $this->assertEquals('Trap', $data['name']); } }