| @@ -11,8 +11,9 @@ | ||
| 11 | 11 | * @package Code_Snippets |
| 12 | 12 | * |
| 13 | 13 | * @property Cloud_Snippet[] $snippets List of snippet items for the current page. |
| 14 | 14 | * @property int $page Page of data that this data belongs to. |
| 15 | + * @property int $per_page Number of items requested per page. | |
| 15 | 16 | * @property int $total_pages Total number of available pages of items. |
| 16 | 17 | * @property int $total_snippets Total number of available snippet items. |
| 17 | 18 | * @property array<int, int> $cloud_id_rev An array of all cloud snippet IDs and their revision numbers. |
| 18 | 19 | * @property array<string, array> $available_filters An array of available filters that can be applied to the collection. |
| @@ -28,8 +29,9 @@ | ||
| 28 | 29 | 'snippets' => [], |
| 29 | 30 | 'total_snippets' => 0, |
| 30 | 31 | 'total_pages' => 0, |
| 31 | 32 | 'page' => 0, |
| 33 | + 'per_page' => 0, | |
| 32 | 34 | 'cloud_id_rev' => [], |
| 33 | 35 | 'available_filters' => [], |
| 34 | 36 | ]; |
| 35 | 37 | |
| @@ -55,8 +57,9 @@ | ||
| 55 | 57 | */ |
| 56 | 58 | protected function prepare_field( $value, string $field ) { |
| 57 | 59 | switch ( $field ) { |
| 58 | 60 | case 'page': |
| 61 | + case 'per_page': | |
| 59 | 62 | case 'total_pages': |
| 60 | 63 | case 'total_snippets': |
| 61 | 64 | return absint( $value ); |
| 62 | 65 | |
| @@ -102,8 +105,12 @@ | ||
| 102 | 105 | |
| 103 | 106 | if ( isset( $meta['page'] ) && is_numeric( $meta['page'] ) ) { |
| 104 | 107 | $this->page = max( 0, (int) $meta['page'] - 1 ); |
| 105 | 108 | } |
| 109 | + | |
| 110 | + if ( isset( $meta['per_page'] ) && is_numeric( $meta['per_page'] ) ) { | |
| 111 | + $this->per_page = $meta['per_page']; | |
| 112 | + } | |
| 106 | 113 | } |
| 107 | 114 | |
| 108 | 115 | /** |
| 109 | 116 | * Normalize payloads returned by the cloud API into the shape expected by this class. |
| @@ -109,12 +116,17 @@ | ||
| 109 | 116 | * Normalize payloads returned by the cloud API into the shape expected by this class. |
| 110 | 117 | * |
| 111 | 118 | * @param array|null $response Response data as returned from API. |
| 112 | 119 | * @param int|null $page_number Page number requested, if applicible. |
| 120 | + * @param int|null $per_page Page size requested, if applicible. | |
| 113 | 121 | * |
| 114 | 122 | * @return Cloud_Snippets Constructed cloud snippets object from response data. |
| 115 | 123 | */ |
| 116 | - public static function unpack_api_response( ?array $response, ?int $page_number = null ): ?Cloud_Snippets { | |
| 124 | + public static function unpack_api_response( | |
| 125 | + ?array $response, | |
| 126 | + ?int $page_number = null, | |
| 127 | + ?int $per_page = null | |
| 128 | + ): ?Cloud_Snippets { | |
| 117 | 129 | if ( ! $response ) { |
| 118 | 130 | return null; |
| 119 | 131 | } |
| 120 | 132 | |
| @@ -136,8 +148,15 @@ | ||
| 136 | 148 | } |
| 137 | 149 | |
| 138 | 150 | if ( ! is_null( $page_number ) ) { |
| 139 | 151 | $result->page = $page_number; |
| 152 | + } | |
| 153 | + | |
| 154 | + // The requested size, not the reported one: callers compare it against a | |
| 155 | + // later request to decide whether cached data still covers what is asked | |
| 156 | + // for, and older cloud builds omit `per_page` from the meta block. | |
| 157 | + if ( ! is_null( $per_page ) ) { | |
| 158 | + $result->per_page = $per_page; | |
| 140 | 159 | } |
| 141 | 160 | |
| 142 | 161 | return $result; |
| 143 | 162 | } |