ArrayUtils.php
2 years ago
CartController.php
2 years ago
CheckoutTrait.php
2 years ago
DraftOrderTrait.php
2 years ago
JsonWebToken.php
2 years ago
LocalPickupUtils.php
2 years ago
NoticeHandler.php
2 years ago
OrderAuthorizationTrait.php
2 years ago
OrderController.php
2 years ago
Pagination.php
2 years ago
ProductItemTrait.php
2 years ago
ProductQuery.php
2 years ago
ProductQueryFilters.php
2 years ago
QuantityLimits.php
2 years ago
RateLimits.php
2 years ago
ValidationUtils.php
2 years ago
Pagination.php
72 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Utilities; |
| 3 | |
| 4 | /** |
| 5 | * Pagination class. |
| 6 | */ |
| 7 | class Pagination { |
| 8 | |
| 9 | /** |
| 10 | * Add pagination headers to a response object. |
| 11 | * |
| 12 | * @param \WP_REST_Response $response Reference to the response object. |
| 13 | * @param \WP_REST_Request $request The request object. |
| 14 | * @param int $total_items Total items found. |
| 15 | * @param int $total_pages Total pages found. |
| 16 | * @return \WP_REST_Response |
| 17 | */ |
| 18 | public function add_headers( $response, $request, $total_items, $total_pages ) { |
| 19 | $response->header( 'X-WP-Total', $total_items ); |
| 20 | $response->header( 'X-WP-TotalPages', $total_pages ); |
| 21 | |
| 22 | $current_page = $this->get_current_page( $request ); |
| 23 | $link_base = $this->get_link_base( $request ); |
| 24 | |
| 25 | if ( $current_page > 1 ) { |
| 26 | $previous_page = $current_page - 1; |
| 27 | if ( $previous_page > $total_pages ) { |
| 28 | $previous_page = $total_pages; |
| 29 | } |
| 30 | $this->add_page_link( $response, 'prev', $previous_page, $link_base ); |
| 31 | } |
| 32 | |
| 33 | if ( $total_pages > $current_page ) { |
| 34 | $this->add_page_link( $response, 'next', ( $current_page + 1 ), $link_base ); |
| 35 | } |
| 36 | |
| 37 | return $response; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get current page. |
| 42 | * |
| 43 | * @param \WP_REST_Request $request The request object. |
| 44 | * @return int Get the page from the request object. |
| 45 | */ |
| 46 | protected function get_current_page( $request ) { |
| 47 | return (int) $request->get_param( 'page' ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get base for links from the request object. |
| 52 | * |
| 53 | * @param \WP_REST_Request $request The request object. |
| 54 | * @return string |
| 55 | */ |
| 56 | protected function get_link_base( $request ) { |
| 57 | return esc_url( add_query_arg( $request->get_query_params(), rest_url( $request->get_route() ) ) ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add a page link. |
| 62 | * |
| 63 | * @param \WP_REST_Response $response Reference to the response object. |
| 64 | * @param string $name Page link name. e.g. prev. |
| 65 | * @param int $page Page number. |
| 66 | * @param string $link_base Base URL. |
| 67 | */ |
| 68 | protected function add_page_link( &$response, $name, $page, $link_base ) { |
| 69 | $response->link_header( $name, add_query_arg( 'page', $page, $link_base ) ); |
| 70 | } |
| 71 | } |
| 72 |