Make WordPress Core


Ignore:
Timestamp:
08/28/2018 12:22:48 PM (6 years ago)
Author:
flixos90
Message:

REST API: Support pagination, order, search and other common query parameters for revisions.

The original REST API revisions controller relied on wp_get_post_revisions(), getting all revisions of a post without any possibility to restrict the result. This changeset replaces that function call with a proper WP_Query setup, replicating how wp_get_post_revisions() works while offering parameters to alter the default behavior.

Props adamsilverstein, birgire, flixos90.
Fixes #40510.

File:
1 edited

Legend:

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

    r43571 r43584  
    206206        }
    207207
    208         $revisions = wp_get_post_revisions( $request['parent'] );
     208        // Ensure a search string is set in case the orderby is set to 'relevance'.
     209        if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
     210            return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
     211        }
     212
     213        // Ensure an include parameter is set in case the orderby is set to 'include'.
     214        if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
     215            return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) );
     216        }
     217
     218        if ( wp_revisions_enabled( $parent ) ) {
     219            $registered = $this->get_collection_params();
     220            $args       = array(
     221                'post_parent'      => $parent->ID,
     222                'post_type'        => 'revision',
     223                'post_status'      => 'inherit',
     224                'posts_per_page'   => -1,
     225                'orderby'          => 'date ID',
     226                'order'            => 'DESC',
     227                'suppress_filters' => true,
     228            );
     229
     230            $parameter_mappings = array(
     231                'exclude'  => 'post__not_in',
     232                'include'  => 'post__in',
     233                'offset'   => 'offset',
     234                'order'    => 'order',
     235                'orderby'  => 'orderby',
     236                'page'     => 'paged',
     237                'per_page' => 'posts_per_page',
     238                'search'   => 's',
     239            );
     240
     241            foreach ( $parameter_mappings as $api_param => $wp_param ) {
     242                if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
     243                    $args[ $wp_param ] = $request[ $api_param ];
     244                }
     245            }
     246
     247            // For backward-compatibility, 'date' needs to resolve to 'date ID'.
     248            if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
     249                $args['orderby'] = 'date ID';
     250            }
     251
     252            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
     253            $args       = apply_filters( 'rest_revision_query', $args, $request );
     254            $query_args = $this->prepare_items_query( $args, $request );
     255
     256            $revisions_query = new WP_Query();
     257            $revisions       = $revisions_query->query( $query_args );
     258            $offset          = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
     259            $page            = (int) $query_args['paged'];
     260            $total_revisions = $revisions_query->found_posts;
     261
     262            if ( $total_revisions < 1 ) {
     263                // Out-of-bounds, run the query again without LIMIT for total count.
     264                unset( $query_args['paged'], $query_args['offset'] );
     265
     266                $count_query = new WP_Query();
     267                $count_query->query( $query_args );
     268
     269                $total_revisions = $count_query->found_posts;
     270            }
     271
     272            if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
     273                $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
     274            } else {
     275                $max_pages = $total_revisions > 0 ? 1 : 0;
     276            }
     277
     278            if ( $total_revisions > 0 ) {
     279                if ( $offset >= $total_revisions ) {
     280                    return new WP_Error( 'rest_revision_invalid_offset_number', __( 'The offset number requested is larger than or equal to the number of available revisions.' ), array( 'status' => 400 ) );
     281                } elseif ( ! $offset && $page > $max_pages ) {
     282                    return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
     283                }
     284            }
     285        } else {
     286            $revisions       = array();
     287            $total_revisions = 0;
     288            $max_pages       = 0;
     289            $page            = (int) $request['page'];
     290        }
    209291
    210292        $response = array();
     
    213295            $response[] = $this->prepare_response_for_collection( $data );
    214296        }
    215         return rest_ensure_response( $response );
     297
     298        $response = rest_ensure_response( $response );
     299
     300        $response->header( 'X-WP-Total', (int) $total_revisions );
     301        $response->header( 'X-WP-TotalPages', (int) $max_pages );
     302
     303        $request_params = $request->get_query_params();
     304        $base           = add_query_arg( $request_params, rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
     305
     306        if ( $page > 1 ) {
     307            $prev_page = $page - 1;
     308
     309            if ( $prev_page > $max_pages ) {
     310                $prev_page = $max_pages;
     311            }
     312
     313            $prev_link = add_query_arg( 'page', $prev_page, $base );
     314            $response->link_header( 'prev', $prev_link );
     315        }
     316        if ( $max_pages > $page ) {
     317            $next_page = $page + 1;
     318            $next_link = add_query_arg( 'page', $next_page, $base );
     319
     320            $response->link_header( 'next', $next_link );
     321        }
     322
     323        return $response;
    216324    }
    217325
     
    329437        );
    330438        return $response;
     439    }
     440
     441    /**
     442     * Determines the allowed query_vars for a get_items() response and prepares
     443     * them for WP_Query.
     444     *
     445     * @since 5.0.0
     446     *
     447     * @param array           $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
     448     * @param WP_REST_Request $request       Optional. Full details about the request.
     449     * @return array Items query arguments.
     450     */
     451    protected function prepare_items_query( $prepared_args = array(), $request = null ) {
     452        $query_args = array();
     453
     454        foreach ( $prepared_args as $key => $value ) {
     455            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
     456            $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
     457        }
     458
     459        // Map to proper WP_Query orderby param.
     460        if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
     461            $orderby_mappings = array(
     462                'id'            => 'ID',
     463                'include'       => 'post__in',
     464                'slug'          => 'post_name',
     465                'include_slugs' => 'post_name__in',
     466            );
     467
     468            if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
     469                $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
     470            }
     471        }
     472
     473        return $query_args;
    331474    }
    332475
     
    551694     */
    552695    public function get_collection_params() {
    553         return array(
    554             'context' => $this->get_context_param( array( 'default' => 'view' ) ),
    555         );
     696        $query_params = parent::get_collection_params();
     697
     698        $query_params['context']['default'] = 'view';
     699
     700        unset( $query_params['per_page']['default'] );
     701
     702        $query_params['exclude'] = array(
     703            'description' => __( 'Ensure result set excludes specific IDs.' ),
     704            'type'        => 'array',
     705            'items'       => array(
     706                'type' => 'integer',
     707            ),
     708            'default'     => array(),
     709        );
     710
     711        $query_params['include'] = array(
     712            'description' => __( 'Limit result set to specific IDs.' ),
     713            'type'        => 'array',
     714            'items'       => array(
     715                'type' => 'integer',
     716            ),
     717            'default'     => array(),
     718        );
     719
     720        $query_params['offset'] = array(
     721            'description' => __( 'Offset the result set by a specific number of items.' ),
     722            'type'        => 'integer',
     723        );
     724
     725        $query_params['order'] = array(
     726            'description' => __( 'Order sort attribute ascending or descending.' ),
     727            'type'        => 'string',
     728            'default'     => 'desc',
     729            'enum'        => array( 'asc', 'desc' ),
     730        );
     731
     732        $query_params['orderby'] = array(
     733            'description' => __( 'Sort collection by object attribute.' ),
     734            'type'        => 'string',
     735            'default'     => 'date',
     736            'enum'        => array(
     737                'date',
     738                'id',
     739                'include',
     740                'relevance',
     741                'slug',
     742                'include_slugs',
     743                'title',
     744            ),
     745        );
     746
     747        return $query_params;
    556748    }
    557749
Note: See TracChangeset for help on using the changeset viewer.