Skip to content

Commit 1eb7967

Browse files
authored
Merge pull request #269 from codesnippetspro/fix/rest-api-pagination
fix: rest-api-pagination
2 parents 30b6276 + ff72886 commit 1eb7967

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

src/css/settings.scss

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ body.js {
142142
}
143143

144144
#target_version {
145-
min-width: 200px;
145+
min-inline-size: 200px;
146146
margin-inline-start: 8px;
147147
}
148148

@@ -158,9 +158,9 @@ body.js {
158158

159159
// Warning box styling
160160
#version-switch-warning {
161-
margin-top: 20px !important;
161+
margin-block-start: 20px !important;
162162
padding: 12px 16px;
163-
border-left: 4px solid #dba617;
163+
border-inline-start: 4px solid #dba617;
164164
background: #fff8e5;
165165
border-radius: 4px;
166166

@@ -183,31 +183,31 @@ body.js {
183183
}
184184
}
185185

186-
.notice {
187-
&.notice {
188-
&-success {
189-
border-left-color: #00a32a;
190-
}
186+
.notice {
187+
&.notice {
188+
&-success {
189+
border-inline-start-color: #00a32a;
190+
}
191191

192-
&-error {
193-
border-left-color: #d63638;
194-
}
192+
&-error {
193+
border-inline-start-color: #d63638;
194+
}
195195

196-
&-warning {
197-
border-left-color: #dba617;
198-
}
196+
&-warning {
197+
border-inline-start-color: #dba617;
198+
}
199199

200-
&-info {
201-
border-left-color: #72aee6;
202-
}
203-
}
204-
}
200+
&-info {
201+
border-inline-start-color: #72aee6;
202+
}
203+
}
204+
}
205205
}
206206

207207
.version-switch-settings {
208208
.form-table {
209209
th {
210-
width: 180px;
210+
inline-size: 180px;
211211
}
212212
}
213213
}

src/php/rest-api/class-snippets-rest-controller.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public function register_routes() {
8080
[ 'network' ]
8181
);
8282

83+
// Allow standard collection parameters (page, per_page, etc.) on the collection route.
84+
$collection_args = array_merge( $network_args, $this->get_collection_params() );
85+
8386
register_rest_route(
8487
$this->namespace,
8588
$route,
@@ -88,7 +91,7 @@ public function register_routes() {
8891
'methods' => WP_REST_Server::READABLE,
8992
'callback' => [ $this, 'get_items' ],
9093
'permission_callback' => [ $this, 'get_items_permissions_check' ],
91-
'args' => $network_args,
94+
'args' => $collection_args,
9295
],
9396
[
9497
'methods' => WP_REST_Server::CREATABLE,
@@ -186,22 +189,44 @@ public function register_routes() {
186189
}
187190

188191
/**
189-
* Retrieves a collection of snippets.
192+
* Retrieves a collection of snippets, with pagination.
190193
*
191194
* @param WP_REST_Request $request Full details about the request.
192195
*
193196
* @return WP_REST_Response Response object on success.
194197
*/
195198
public function get_items( $request ): WP_REST_Response {
196-
$snippets = get_snippets();
199+
$network = $request->get_param( 'network' );
200+
$all_snippets = get_snippets( [], $network );
201+
202+
// Get collection params (page, per_page).
203+
$collection_params = $this->get_collection_params();
204+
$per_page_request = (int) $request->get_param( 'per_page' );
205+
$per_page = max( 1, $per_page_request ? $per_page_request : (int) $collection_params['per_page']['default'] );
206+
207+
$page_request = (int) $request->get_param( 'page' );
208+
$page = max( 1, $page_request ? $page_request : (int) $collection_params['page']['default'] );
209+
210+
// Count total items
211+
$total_items = count( $all_snippets );
212+
$total_pages = (int) ceil( $total_items / $per_page );
213+
214+
// Slice the full list to the requested page.
215+
$offset = ( $page - 1 ) * $per_page;
216+
$snippets = array_slice( $all_snippets, $offset, $per_page );
217+
197218
$snippets_data = [];
198219

199220
foreach ( $snippets as $snippet ) {
200221
$snippet_data = $this->prepare_item_for_response( $snippet, $request );
201222
$snippets_data[] = $this->prepare_response_for_collection( $snippet_data );
202223
}
203224

204-
return rest_ensure_response( $snippets_data );
225+
$response = rest_ensure_response( $snippets_data );
226+
$response->header( 'X-WP-Total', (string) $total_items );
227+
$response->header( 'X-WP-TotalPages', (string) $total_pages );
228+
229+
return $response;
205230
}
206231

207232
/**

0 commit comments

Comments
 (0)