WordPress.org

Make WordPress Core

Ticket #38985: 38985.3.diff

File 38985.3.diff, 4.4 KB (added by jnylen0, 4 years ago)

Reuse an existing error string; rename mappings to $orderby_mappings

  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index e0cdc79..872a660 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    148148                        return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
    149149                }
    150150
     151                // Ensure an include parameter is set in case the orderby is set to 'include'.
     152                if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
     153                        return new WP_Error( 'rest_orderby_include_missing_include', sprintf( __( 'Missing parameter(s): %s' ), 'include' ), array( 'status' => 400 ) );
     154                }
     155
    151156                // Retrieve the list of registered collection query parameters.
    152157                $registered = $this->get_collection_params();
    153158                $args = array();
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    836841                        $query_args['ignore_sticky_posts'] = true;
    837842                }
    838843
    839                 if ( 'include' === $query_args['orderby'] ) {
    840                         $query_args['orderby'] = 'post__in';
     844                // Map to proper WP_Query orderby param.
     845                if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
     846                        $orderby_mappings = array(
     847                                'id'      => 'ID',
     848                                'include' => 'post__in',
     849                                'slug'    => 'post_name',
     850                        );
     851
     852                        if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
     853                                $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
     854                        }
    841855                }
    842856
    843857                return $query_args;
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 8f8bc73..886ae3c 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    444444                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    445445        }
    446446
     447        public function test_get_items_with_orderby_include_without_include_param() {
     448                $this->factory->post->create( array( 'post_status' => 'publish' ) );
     449                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     450                $request->set_param( 'orderby', 'include' );
     451
     452                $response = $this->server->dispatch( $request );
     453
     454                $this->assertErrorResponse( 'rest_orderby_include_missing_include', $response, 400 );
     455        }
     456
     457        public function test_get_items_with_orderby_id() {
     458                $id1 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_date' => '2016-01-13 02:26:48' ) );
     459                $id2 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_date' => '2016-01-12 02:26:48' ) );
     460                $id3 = $this->factory->post->create( array( 'post_status' => 'publish', 'post_date' => '2016-01-11 02:26:48' ) );
     461
     462                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     463                $request->set_param( 'orderby', 'id' );
     464                $request->set_param( 'include', array( $id1, $id2, $id3 ) );
     465
     466                $response = $this->server->dispatch( $request );
     467                $data = $response->get_data();
     468
     469                // Default ORDER is DESC.
     470                $this->assertEquals( $id3, $data[0]['id'] );
     471                $this->assertEquals( $id2, $data[1]['id'] );
     472                $this->assertEquals( $id1, $data[2]['id'] );
     473        }
     474
     475        public function test_get_items_with_orderby_slug() {
     476                $id1 = $this->factory->post->create( array( 'post_title' => 'ABC', 'post_name' => 'xyz', 'post_status' => 'publish' ) );
     477                $id2 = $this->factory->post->create( array( 'post_title' => 'XYZ', 'post_name' => 'abc', 'post_status' => 'publish' ) );
     478
     479                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     480                $request->set_param( 'orderby', 'slug' );
     481                $request->set_param( 'include', array( $id1, $id2 ) );
     482
     483                $response = $this->server->dispatch( $request );
     484                $data = $response->get_data();
     485
     486                // Default ORDER is DESC.
     487                $this->assertEquals( 'xyz', $data[0]['slug'] );
     488                $this->assertEquals( 'abc', $data[1]['slug'] );
     489        }
     490
    447491        public function test_get_items_with_orderby_relevance() {
    448492                $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
    449493                $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );