Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.73% |
8 / 11 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| REST_Base_Controller | |
80.00% |
8 / 10 |
|
33.33% |
1 / 3 |
7.39 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_per_page_argument | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| get_offset_argument | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack CRM REST API base controller class. |
| 4 | * |
| 5 | * @package automattic/jetpack-crm |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\CRM\REST_API\V4; |
| 9 | |
| 10 | use WP_REST_Controller; |
| 11 | use WP_REST_Request; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit( 0 ); |
| 14 | |
| 15 | /** |
| 16 | * Abstract base controller class. |
| 17 | * |
| 18 | * @since 6.1.0 |
| 19 | */ |
| 20 | abstract class REST_Base_Controller extends WP_REST_Controller { |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @since 6.1.0 |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | $this->namespace = 'jetpack-crm/v4'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the per page argument. |
| 33 | * |
| 34 | * We limit results to a maximum of 100 based on the default behaviour of WP Core REST API. |
| 35 | * You can read more about default behaviour in the official "REST API > Pagination" documentation. |
| 36 | * |
| 37 | * @link https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/#pagination-parameters |
| 38 | * |
| 39 | * @since 6.2.0 |
| 40 | * |
| 41 | * @param WP_REST_Request $request The request object. |
| 42 | * @return int Return the per page argument. |
| 43 | */ |
| 44 | protected function get_per_page_argument( WP_REST_Request $request ): ?int { |
| 45 | $per_page = (int) $request->has_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 10; |
| 46 | |
| 47 | if ( $per_page > 100 ) { |
| 48 | return 100; |
| 49 | } |
| 50 | |
| 51 | return $per_page; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get request offset. |
| 56 | * |
| 57 | * We cannot combine "page" and "offset" since they mean the same thing; they're just |
| 58 | * calculated and requested differently. |
| 59 | * You can check the official WordPress REST documentation for more information about |
| 60 | * how they're typically handled. |
| 61 | * |
| 62 | * @link https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/#pagination-parameters |
| 63 | * |
| 64 | * @since 6.2.0 |
| 65 | * |
| 66 | * @param WP_REST_Request $request The request object. |
| 67 | * @return int Return the offset argument. |
| 68 | */ |
| 69 | protected function get_offset_argument( WP_REST_Request $request ): int { |
| 70 | if ( $request->has_param( 'offset' ) ) { |
| 71 | return $request->get_param( 'offset' ); |
| 72 | } |
| 73 | |
| 74 | if ( $request->has_param( 'page' ) ) { |
| 75 | // We have to reduce the page number by 1 when calculating the offset because otherwise we |
| 76 | // would always display "the next page". |
| 77 | // |
| 78 | // E.g.: We want to display the second page with 10 results per page. |
| 79 | // [Request] Page: 2, Per page: 10. |
| 80 | // ["Translated"] We want to show post 11-20. |
| 81 | // [Offset] (2 - 1) * 10 = 10 |
| 82 | // The outcome of the calculated offset means that we will return results after the first 10 entries. |
| 83 | return ( $request->get_param( 'page' ) - 1 ) * $this->get_per_page_argument( $request ); |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | } |