From 840b7e4396ce64d2aefd07ee1f31a1eeabfd8e78 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 5 Mar 2021 15:26:01 -0300 Subject: [PATCH] feat: add endpoint to report of specific taxonomy #483 --- ...class-tainacan-rest-reports-controller.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php b/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php index 85045c495..840ab5229 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-reports-controller.php @@ -62,6 +62,15 @@ class REST_Reports_Controller extends REST_Controller { ), ) ); + register_rest_route($this->namespace, $this->rest_base . '/taxonomy/(?P[\d]+)', + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_taxonomy'), + 'permission_callback' => array($this, 'reports_permissions_check'), + ), + ) + ); } public function reports_permissions_check($request) { @@ -179,6 +188,39 @@ class REST_Reports_Controller extends REST_Controller { } return new \WP_REST_Response($response, 200); } + public function get_taxonomy($request) { + $response = array( + 'terms'=> array() + ); + $taxonomy_id = $request['taxonomy_id']; + $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); + $taxonomy_identifier = $taxonomy->get_db_identifier(); + $taxonomy_total_terms = wp_count_terms($taxonomy_identifier, array('hide_empty' => false) ); + $limit = 100; + $offset = 0; + + if ( !$taxonomy_total_terms) { + $taxonomy_total_terms = 0; + } + + while($offset < $taxonomy_total_terms) { + $terms = get_terms( array( + 'taxonomy' => $taxonomy->get_db_identifier(), + 'number' => $limit, + 'offset' => $offset, + 'hide_empty' => false, + ) ); + foreach ($terms as $term) { + $response['terms'][$term->term_id] = array( + 'id' => $term->term_id, + 'name' => $term->name, + 'count' => $term->count + ); + } + $offset+=$limit; + } + return new \WP_REST_Response($response, 200); + } public function get_stats_metadata($request) { $response = array(