Make WordPress Core


Ignore:
Timestamp:
10/26/2016 09:36:29 PM (8 years ago)
Author:
rachelbaker
Message:

REST API: Remove experimental filter wrapper parameter from the Posts Controller class.

Hiding WP_Query params under the filter key (instead of allowing them to be top-level params) was one of our biggest complaints from users of v1 of our REST API. This walks back the re-introduction of the filter param during Beta 15, which introduced an "inconsistent mess" and "exposing WP_Query through filter has and will continue to be difficult to support." See https://github.com/WP-API/WP-API/issues/2799.

Props websupporter, rachelbaker.
Fixes #38378.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r38911 r38968  
    6868            'context',
    6969            'exclude',
    70             'filter',
    7170            'include',
    7271            'offset',
     
    9897        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    9998        $request->set_query_params( array(
    100             'filter' => array( 'year' => 2008 ),
    101         ) );
    102         $response = $this->server->dispatch( $request );
    103         $this->assertEquals( array(), $response->get_data() );
     99            'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
     100        ) );
     101        $response = $this->server->dispatch( $request );
     102
     103        $this->assertEmpty( $response->get_data() );
    104104        $this->assertEquals( 200, $response->get_status() );
    105105    }
     
    310310        // Permit stickies
    311311        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    312         $request->set_param( 'filter', array( 'ignore_sticky_posts' => false ) );
     312        $request->set_param( 'ignore_sticky_posts', false );
    313313        $response = $this->server->dispatch( $request );
    314314        $data = $response->get_data();
     
    577577    }
    578578
    579     public function test_get_items_private_filter_query_var() {
     579    public function test_get_items_private_status_query_var() {
    580580        // Private query vars inaccessible to unauthorized users
    581581        wp_set_current_user( 0 );
    582582        $draft_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
    583583        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    584         $request->set_param( 'filter', array( 'post_status' => 'draft' ) );
    585         $response = $this->server->dispatch( $request );
    586         $data = $response->get_data();
    587         $this->assertCount( 1, $data );
    588         $this->assertEquals( $this->post_id, $data[0]['id'] );
     584        $request->set_param( 'status', 'draft' );
     585        $response = $this->server->dispatch( $request );
     586        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     587
    589588        // But they are accessible to authorized users
    590589        wp_set_current_user( $this->editor_id );
     
    600599        $response = $this->server->dispatch( $request );
    601600        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    602     }
    603 
    604     public function test_get_items_invalid_posts_per_page_ignored() {
    605         // This test ensures that filter[posts_per_page] is ignored, and that -1
    606         // cannot be used to sidestep per_page's valid range to retrieve all posts
    607         for ( $i = 0; $i < 20; $i++ ) {
    608             $this->factory->post->create( array( 'post_status' => 'publish' ) );
    609         }
    610         $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    611         $request->set_query_params( array( 'filter' => array( 'posts_per_page' => -1 ) ) );
    612         $response = $this->server->dispatch( $request );
    613         $this->assertCount( 10, $response->get_data() );
    614601    }
    615602
Note: See TracChangeset for help on using the changeset viewer.