diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b12e762..5a333535 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ + +## [3.7.1-beta.3] (2025-10-22) + +### Added +* Snippets REST API now supports pagination via page and per_page query parameters. + + ## [3.7.1-beta.2] (2025-10-22) ### Added diff --git a/package-lock.json b/package-lock.json index a6ff81b7..fe96a6f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "code-snippets", - "version": "3.7.1-beta.2", + "version": "3.7.1-beta.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "code-snippets", - "version": "3.7.1-beta.2", + "version": "3.7.1-beta.3", "license": "GPL-2.0-or-later", "dependencies": { "@codemirror/fold": "^0.19.4", diff --git a/package.json b/package.json index 63237fe4..8d664089 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "code-snippets", "description": "Manage code snippets running on a WordPress-powered site through a graphical interface.", "homepage": "https://codesnippets.pro", - "version": "3.7.1-beta.2", + "version": "3.7.1-beta.3", "main": "src/dist/edit.js", "directories": { "test": "tests" diff --git a/src/code-snippets.php b/src/code-snippets.php index abcf2270..1db3c0bc 100644 --- a/src/code-snippets.php +++ b/src/code-snippets.php @@ -8,11 +8,11 @@ * License: GPL-2.0-or-later * License URI: license.txt * Text Domain: code-snippets - * Version: 3.7.1-beta.2 + * Version: 3.7.1-beta.3 * Requires PHP: 7.4 * Requires at least: 5.0 * - * @version 3.7.1-beta.2 + * @version 3.7.1-beta.3 * @package Code_Snippets * @author Shea Bunge * @copyright 2012-2024 Code Snippets Pro @@ -37,7 +37,7 @@ * * @const string */ - define( 'CODE_SNIPPETS_VERSION', '3.7.1-beta.2' ); + define( 'CODE_SNIPPETS_VERSION', '3.7.1-beta.3' ); /** * The full path to the main file of this plugin. 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; } /** diff --git a/src/readme.txt b/src/readme.txt index b5b0bf43..167f9bf3 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -105,6 +105,14 @@ You can report security bugs found in the source code of this plugin through the == Changelog == + += 3.7.1-beta.3 (2025-10-22) = + +__Added__ + +* Snippets REST API now supports pagination via page and per_page query parameters. + + = 3.7.1-beta.2 (2025-10-22) = __Added__