Adds support to terms pagination

This commit is contained in:
weryques 2018-06-29 12:55:12 -03:00
parent 1964576d36
commit afe2f7974c
3 changed files with 28 additions and 2 deletions

View File

@ -78,6 +78,7 @@ class REST_Controller extends \WP_REST_Controller {
'metacompare' => 'meta_compare', 'metacompare' => 'meta_compare',
'hideempty' => 'hide_empty', 'hideempty' => 'hide_empty',
'perpage' => 'posts_per_page', 'perpage' => 'posts_per_page',
'number' => 'number',
'paged' => 'paged', 'paged' => 'paged',
'postin' => 'post__in', 'postin' => 'post__in',
'relation' => 'relation', 'relation' => 'relation',

View File

@ -192,7 +192,11 @@ class REST_Collections_Controller extends REST_Controller {
$total_items = wp_count_posts( $item->get_db_identifier(), 'readable' ); $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']['trash'] = $total_items->trash;
$item_arr['total_items']['publish'] = $total_items->publish; $item_arr['total_items']['publish'] = $total_items->publish;
$item_arr['total_items']['draft'] = $total_items->draft; $item_arr['total_items']['draft'] = $total_items->draft;

View File

@ -284,6 +284,7 @@ class REST_Terms_Controller extends REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id); $taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$args = $this->prepare_filters($request); $args = $this->prepare_filters($request);
$prepared_args = $args;
$terms = $this->terms_repository->fetch($args, $taxonomy); $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 )); 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;
} }
/** /**