|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pagination helper. |
|
4
|
|
|
* |
|
5
|
|
|
* Handles permission checks for endpoints. |
|
6
|
|
|
* |
|
7
|
|
|
* @package WooCommerce/RestApi |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4\Utilities; |
|
11
|
|
|
|
|
12
|
|
|
defined( 'ABSPATH' ) || exit; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Permissions class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Pagination { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Add pagination headers to a response object. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \WP_REST_Response $response Reference to the response object. |
|
23
|
|
|
* @param \WP_REST_Request $request The request object. |
|
24
|
|
|
* @param int $total_items Total items found. |
|
25
|
|
|
* @param int $total_pages Total pages found. |
|
26
|
|
|
* @return \WP_REST_Response |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function add_pagination_headers( $response, $request, $total_items, $total_pages ) { |
|
29
|
|
|
$response->header( 'X-WP-Total', $total_items ); |
|
30
|
|
|
$response->header( 'X-WP-TotalPages', $total_pages ); |
|
31
|
|
|
|
|
32
|
|
|
$current_page = self::get_current_page( $request ); |
|
33
|
|
|
$link_base = self::get_link_base( $request ); |
|
34
|
|
|
|
|
35
|
|
|
if ( $current_page > 1 ) { |
|
36
|
|
|
$previous_page = $current_page - 1; |
|
37
|
|
|
if ( $previous_page > $total_pages ) { |
|
38
|
|
|
$previous_page = $total_pages; |
|
39
|
|
|
} |
|
40
|
|
|
self::add_page_link( $response, 'prev', $previous_page, $link_base ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ( $total_pages > $current_page ) { |
|
44
|
|
|
self::add_page_link( $response, 'next', ( $current_page + 1 ), $link_base ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get current page. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \WP_REST_Request $request The request object. |
|
54
|
|
|
* @return int Get the page from the request object. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected static function get_current_page( $request ) { |
|
57
|
|
|
return (int) $request->get_param( 'page' ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get base for links from the request object. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \WP_REST_Request $request The request object. |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected static function get_link_base( $request ) { |
|
67
|
|
|
return add_query_arg( $request->get_query_params(), rest_url( $request->get_route() ) ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Add a page link. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \WP_REST_Response $response Reference to the response object. |
|
74
|
|
|
* @param string $name Page link name. e.g. prev. |
|
75
|
|
|
* @param int $page Page number. |
|
76
|
|
|
* @param string $link_base Base URL. |
|
77
|
|
|
*/ |
|
78
|
|
|
protected static function add_page_link( &$response, $name, $page, $link_base ) { |
|
79
|
|
|
$response->link_header( $name, add_query_arg( 'page', $page, $link_base ) ); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|