@@ -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