Make WordPress Core


Ignore:
Timestamp:
01/24/2023 04:52:42 PM (2 years ago)
Author:
hellofromTonya
Message:

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:

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

Props ntsekouras, ryelle, arrasel403, hellofromTonya, ironprogrammer, mukesh27, robinwpdeveloper.
Fixes #57501.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

    r53665 r55132  
    8282     * @since 5.8.0
    8383     * @since 6.0.0 Added 'slug' to request.
     84     * @since 6.2.0 Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.
    8485     *
    8586     * @param WP_REST_Request $request Full details about the request.
     
    9495        require ABSPATH . WPINC . '/version.php';
    9596
    96         $query_args = array(
    97             'locale'     => get_user_locale(),
    98             'wp-version' => $wp_version,
    99         );
    100 
    101         $category_id = $request['category'];
    102         $keyword_id  = $request['keyword'];
    103         $search_term = $request['search'];
    104         $slug        = $request['slug'];
    105 
    106         if ( $category_id ) {
    107             $query_args['pattern-categories'] = $category_id;
    108         }
    109 
    110         if ( $keyword_id ) {
    111             $query_args['pattern-keywords'] = $keyword_id;
    112         }
    113 
    114         if ( $search_term ) {
    115             $query_args['search'] = $search_term;
    116         }
    117 
    118         if ( $slug ) {
    119             $query_args['slug'] = $slug;
    120         }
     97        $valid_query_args = array(
     98            'offset'   => true,
     99            'order'    => true,
     100            'orderby'  => true,
     101            'page'     => true,
     102            'per_page' => true,
     103            'search'   => true,
     104            'slug'     => true,
     105        );
     106        $query_args = array_intersect_key( $request->get_params(), $valid_query_args );
     107
     108        $query_args['locale']             = get_user_locale();
     109        $query_args['wp-version']         = $wp_version; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- it's defined in `version.php` above.
     110        $query_args['pattern-categories'] = isset( $request['category'] ) ? $request['category'] : false;
     111        $query_args['pattern-keywords']   = isset( $request['keyword'] ) ? $request['keyword'] : false;
     112
     113        $query_args = array_filter( $query_args );
    121114
    122115        $transient_key = $this->get_transient_key( $query_args );
     
    304297     *
    305298     * @since 5.8.0
     299     * @since 6.2.0 Added 'per_page', 'page', 'offset', 'order', and 'orderby' to request.
    306300     *
    307301     * @return array Collection parameters.
     
    310304        $query_params = parent::get_collection_params();
    311305
    312         // Pagination is not supported.
    313         unset( $query_params['page'] );
    314         unset( $query_params['per_page'] );
    315 
     306        $query_params['per_page']['default'] = 100;
    316307        $query_params['search']['minLength'] = 1;
    317308        $query_params['context']['default']  = 'view';
     
    332323            'description' => __( 'Limit results to those matching a pattern (slug).' ),
    333324            'type'        => 'array',
     325        );
     326
     327        $query_params['offset'] = array(
     328            'description' => __( 'Offset the result set by a specific number of items.' ),
     329            'type'        => 'integer',
     330        );
     331
     332        $query_params['order'] = array(
     333            'description' => __( 'Order sort attribute ascending or descending.' ),
     334            'type'        => 'string',
     335            'default'     => 'desc',
     336            'enum'        => array( 'asc', 'desc' ),
     337        );
     338
     339        $query_params['orderby'] = array(
     340            'description' => __( 'Sort collection by post attribute.' ),
     341            'type'        => 'string',
     342            'default'     => 'date',
     343            'enum'        => array(
     344                'author',
     345                'date',
     346                'id',
     347                'include',
     348                'modified',
     349                'parent',
     350                'relevance',
     351                'slug',
     352                'include_slugs',
     353                'title',
     354                'favorite_count',
     355            ),
    334356        );
    335357
Note: See TracChangeset for help on using the changeset viewer.