Skip to content

Commit

Permalink
Editor: Adds pagination and ordering support to `WP_REST_Pattern_Dire…
Browse files Browse the repository at this point in the history
…ctory_Controller`.

Adds pagination and ordering support to `WP_REST_Pattern_Directory_Controller` by allow listing `'per_page'`, `'page'`, `'offset'`, `'order'`, and `'orderby'` query parameters. This change enables pagination and ordering features in the pattern directory explorer by using the same sort as wordpress.org/patterns.

Reference:
* [WordPress/gutenberg#45293 Gutenberg PR 45293]

Follow-up to [55098], [51206], [51021].

Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.
Built from https://develop.svn.wordpress.org/trunk@55132


git-svn-id: https://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
hellofromtonya committed Jan 24, 2023
1 parent ddf125b commit 5a262b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function get_items_permissions_check( $request ) {
*
* @since 5.8.0
* @since 6.0.0 Added 'slug' to request.
* @since 6.2.0 Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
Expand All @@ -93,31 +94,23 @@ public function get_items( $request ) {
*/
require ABSPATH . WPINC . '/version.php';

$query_args = array(
'locale' => get_user_locale(),
'wp-version' => $wp_version,
$valid_query_args = array(
'offset' => true,
'order' => true,
'orderby' => true,
'page' => true,
'per_page' => true,
'search' => true,
'slug' => true,
);
$query_args = array_intersect_key( $request->get_params(), $valid_query_args );

$category_id = $request['category'];
$keyword_id = $request['keyword'];
$search_term = $request['search'];
$slug = $request['slug'];
$query_args['locale'] = get_user_locale();
$query_args['wp-version'] = $wp_version; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- it's defined in `version.php` above.
$query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false;
$query_args['pattern-keywords'] = isset( $request['keyword'] ) ? $request['keyword'] : false;

if ( $category_id ) {
$query_args['pattern-categories'] = $category_id;
}

if ( $keyword_id ) {
$query_args['pattern-keywords'] = $keyword_id;
}

if ( $search_term ) {
$query_args['search'] = $search_term;
}

if ( $slug ) {
$query_args['slug'] = $slug;
}
$query_args = array_filter( $query_args );

$transient_key = $this->get_transient_key( $query_args );

Expand Down Expand Up @@ -303,16 +296,14 @@ public function get_item_schema() {
* Retrieves the search parameters for the block pattern's collection.
*
* @since 5.8.0
* @since 6.2.0 Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.
*
* @return array Collection parameters.
*/
public function get_collection_params() {
$query_params = parent::get_collection_params();

// Pagination is not supported.
unset( $query_params['page'] );
unset( $query_params['per_page'] );

$query_params['per_page']['default'] = 100;
$query_params['search']['minLength'] = 1;
$query_params['context']['default'] = 'view';

Expand All @@ -333,6 +324,37 @@ public function get_collection_params() {
'type' => 'array',
);

$query_params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of items.' ),
'type' => 'integer',
);

$query_params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
);

$query_params['orderby'] = array(
'description' => __( 'Sort collection by post attribute.' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'author',
'date',
'id',
'include',
'modified',
'parent',
'relevance',
'slug',
'include_slugs',
'title',
'favorite_count',
),
);

/**
* Filter collection parameters for the block pattern directory controller.
*
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55131';
$wp_version = '6.2-alpha-55132';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 5a262b7

Please sign in to comment.