diff --git a/src/php/rest-api/class-snippets-rest-controller.php b/src/php/rest-api/class-snippets-rest-controller.php index 2cfec385..e6c43e2b 100644 --- a/src/php/rest-api/class-snippets-rest-controller.php +++ b/src/php/rest-api/class-snippets-rest-controller.php @@ -200,21 +200,22 @@ public function get_items( $request ): WP_REST_Response { $all_snippets = get_snippets( [], $network ); $all_snippets = $this->get_network_items( $all_snippets, $network ); - // Get collection params (page, per_page). - $collection_params = $this->get_collection_params(); - $per_page_request = (int) $request->get_param( 'per_page' ); - $per_page = max( 1, $per_page_request ? $per_page_request : (int) $collection_params['per_page']['default'] ); - - $page_request = (int) $request->get_param( 'page' ); - $page = max( 1, $page_request ? $page_request : (int) $collection_params['page']['default'] ); - - // Count total items $total_items = count( $all_snippets ); - $total_pages = (int) ceil( $total_items / $per_page ); - - // Slice the full list to the requested page. - $offset = ( $page - 1 ) * $per_page; - $snippets = array_slice( $all_snippets, $offset, $per_page ); + $query_params = $request->get_query_params(); + + if ( isset( $query_params['per_page'] ) ) { + $collection_params = $this->get_collection_params(); + $per_page = max( 1, (int) $query_params['per_page'] ); + $page_request = (int) $request->get_param( 'page' ); + $page = max( 1, $page_request ? $page_request : (int) $collection_params['page']['default'] ); + $total_pages = (int) ceil( $total_items / $per_page ); + + $offset = ( $page - 1 ) * $per_page; + $snippets = array_slice( $all_snippets, $offset, $per_page ); + } else { + $snippets = $all_snippets; + $total_pages = 1; + } $snippets_data = [];