From afe2f7974ceded51b7f131f4a8f1401b61c9c043 Mon Sep 17 00:00:00 2001 From: weryques Date: Fri, 29 Jun 2018 12:55:12 -0300 Subject: [PATCH] Adds support to terms pagination --- src/api/class-tainacan-rest-controller.php | 1 + ...s-tainacan-rest-collections-controller.php | 6 ++++- .../class-tainacan-rest-terms-controller.php | 23 ++++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index 17e824159..ac18597ca 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -78,6 +78,7 @@ class REST_Controller extends \WP_REST_Controller { 'metacompare' => 'meta_compare', 'hideempty' => 'hide_empty', 'perpage' => 'posts_per_page', + 'number' => 'number', 'paged' => 'paged', 'postin' => 'post__in', 'relation' => 'relation', diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index 8d3b4785d..01ca91ca8 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -192,7 +192,11 @@ class REST_Collections_Controller extends REST_Controller { $total_items = wp_count_posts( $item->get_db_identifier(), 'readable' ); - if (isset($total_items->publish)) { + if (isset($total_items->publish) || + isset($total_items->private) || + isset($total_items->trash) || + isset($total_items->draft)) { + $item_arr['total_items']['trash'] = $total_items->trash; $item_arr['total_items']['publish'] = $total_items->publish; $item_arr['total_items']['draft'] = $total_items->draft; diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 1bcab8a0d..37b2bdd17 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -284,6 +284,7 @@ class REST_Terms_Controller extends REST_Controller { $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); $args = $this->prepare_filters($request); + $prepared_args = $args; $terms = $this->terms_repository->fetch($args, $taxonomy); @@ -292,7 +293,27 @@ class REST_Terms_Controller extends REST_Controller { array_push($response, $this->prepare_item_for_response( $term, $request )); } - return new \WP_REST_Response($response, 200); + $response = new \WP_REST_Response($response, 200); + + if(isset($args['number'], $args['offset'])){ + unset( $args['number'], $args['offset'] ); + $total_terms = wp_count_terms( $this->taxonomy->get_db_identifier(), $args ); + + if ( ! $total_terms ) { + $total_terms = 0; + } + + $per_page = (int) $prepared_args['number']; + $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); + + $response->header( 'X-WP-Total', (int) $total_terms ); + + $max_pages = ceil( $total_terms / $per_page ); + + $response->header( 'X-WP-TotalPages', (int) $max_pages ); + } + + return $response; } /**