Editor: Adds pagination and ordering support to `WP_REST_Pattern_Directory_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:
* [https://github.com/WordPress/gutenberg/pull/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: http://core.svn.wordpress.org/trunk@54665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2023-01-24 16:54:11 +00:00
parent 28f3369220
commit 16b9fdfc34
2 changed files with 49 additions and 27 deletions

View File

@ -81,6 +81,7 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
*
* @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.
@ -93,31 +94,23 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
*/
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 );
@ -303,16 +296,14 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
* 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';
@ -333,6 +324,37 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
'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.
*

View File

@ -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.