diff --git a/src/css/settings.scss b/src/css/settings.scss index 2ff6dc76..e2a040ae 100644 --- a/src/css/settings.scss +++ b/src/css/settings.scss @@ -142,7 +142,7 @@ body.js { } #target_version { - min-width: 200px; + min-inline-size: 200px; margin-inline-start: 8px; } @@ -158,9 +158,9 @@ body.js { // Warning box styling #version-switch-warning { - margin-top: 20px !important; + margin-block-start: 20px !important; padding: 12px 16px; - border-left: 4px solid #dba617; + border-inline-start: 4px solid #dba617; background: #fff8e5; border-radius: 4px; @@ -183,31 +183,31 @@ body.js { } } - .notice { - &.notice { - &-success { - border-left-color: #00a32a; - } + .notice { + &.notice { + &-success { + border-inline-start-color: #00a32a; + } - &-error { - border-left-color: #d63638; - } + &-error { + border-inline-start-color: #d63638; + } - &-warning { - border-left-color: #dba617; - } + &-warning { + border-inline-start-color: #dba617; + } - &-info { - border-left-color: #72aee6; - } - } - } + &-info { + border-inline-start-color: #72aee6; + } + } + } } .version-switch-settings { .form-table { th { - width: 180px; + inline-size: 180px; } } } diff --git a/src/php/rest-api/class-snippets-rest-controller.php b/src/php/rest-api/class-snippets-rest-controller.php index 18a1f827..6fe58ba9 100644 --- a/src/php/rest-api/class-snippets-rest-controller.php +++ b/src/php/rest-api/class-snippets-rest-controller.php @@ -80,6 +80,9 @@ public function register_routes() { [ 'network' ] ); + // Allow standard collection parameters (page, per_page, etc.) on the collection route. + $collection_args = array_merge( $network_args, $this->get_collection_params() ); + register_rest_route( $this->namespace, $route, @@ -88,7 +91,7 @@ public function register_routes() { 'methods' => WP_REST_Server::READABLE, 'callback' => [ $this, 'get_items' ], 'permission_callback' => [ $this, 'get_items_permissions_check' ], - 'args' => $network_args, + 'args' => $collection_args, ], [ 'methods' => WP_REST_Server::CREATABLE, @@ -186,14 +189,32 @@ public function register_routes() { } /** - * Retrieves a collection of snippets. + * Retrieves a collection of snippets, with pagination. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response Response object on success. */ public function get_items( $request ): WP_REST_Response { - $snippets = get_snippets(); + $network = $request->get_param( 'network' ); + $all_snippets = get_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 ); + $snippets_data = []; foreach ( $snippets as $snippet ) { @@ -201,7 +222,11 @@ public function get_items( $request ): WP_REST_Response { $snippets_data[] = $this->prepare_response_for_collection( $snippet_data ); } - return rest_ensure_response( $snippets_data ); + $response = rest_ensure_response( $snippets_data ); + $response->header( 'X-WP-Total', (string) $total_items ); + $response->header( 'X-WP-TotalPages', (string) $total_pages ); + + return $response; } /**