WC_API_Server::add_pagination_headers() public WC 2.1
Send pagination headers for resources
{} It's a method of the class: WC_API_Server{}
Hooks from the method
Return
Null. Nothing.
Usage
$WC_API_Server = new WC_API_Server(); $WC_API_Server->add_pagination_headers( $query );
$query *(WP_Query | WP_User_Query) (required)* |
---|
Changelog
Since 2.1 | Introduced. |
Code of WC_API_Server::add_pagination_headers() WC API Server::add pagination headers WC 5.0.0
public function add_pagination_headers( $query ) {
// WP_User_Query
if ( is_a( $query, 'WP_User_Query' ) ) {
$page = $query->page;
$single = count( $query->get_results() ) == 1;
$total = $query->get_total();
$total_pages = $query->total_pages;
// WP_Query
} else {
$page = $query->get( 'paged' );
$single = $query->is_single();
$total = $query->found_posts;
$total_pages = $query->max_num_pages;
}
if ( ! $page ) {
$page = 1;
}
$next_page = absint( $page ) + 1;
if ( ! $single ) {
// first/prev
if ( $page > 1 ) {
$this->link_header( 'first', $this->get_paginated_url( 1 ) );
$this->link_header( 'prev', $this->get_paginated_url( $page -1 ) );
}
// next
if ( $next_page <= $total_pages ) {
$this->link_header( 'next', $this->get_paginated_url( $next_page ) );
}
// last
if ( $page != $total_pages ) {
$this->link_header( 'last', $this->get_paginated_url( $total_pages ) );
}
}
$this->header( 'X-WC-Total', $total );
$this->header( 'X-WC-TotalPages', $total_pages );
do_action( 'woocommerce_api_pagination_headers', $this, $query );
}