Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/css/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ body.js {
}

#target_version {
min-width: 200px;
min-inline-size: 200px;
margin-inline-start: 8px;
}

Expand All @@ -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;

Expand All @@ -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;
}
}
}
33 changes: 29 additions & 4 deletions src/php/rest-api/class-snippets-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -186,22 +189,44 @@ 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 ) {
$snippet_data = $this->prepare_item_for_response( $snippet, $request );
$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;
}

/**
Expand Down