Make WordPress Core

Changeset 53498


Ignore:
Timestamp:
06/14/2022 12:40:29 PM (21 months ago)
Author:
spacedmonkey
Message:

REST API: Avoid duplicated query in post collections.

Avoid duplicated query when retrieving empty posts collections by adding a check if the page is more than 1.

Props furi3r, gdetassigny, TimothyBlynJacobs, spacedmonkey.
Fixes #55677.

Location:
trunk
Files:
3 edited

Legend:

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

    r53482 r53498  
    389389        $total_posts = $posts_query->found_posts;
    390390
    391         if ( $total_posts < 1 ) {
     391        if ( $total_posts < 1 && $page > 1 ) {
    392392            // Out-of-bounds, run the query again without LIMIT for total count.
    393393            unset( $query_args['paged'] );
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r52389 r53498  
    3030    private $test_file2;
    3131
     32    /**
     33     * @var array The recorded posts query clauses.
     34     */
     35    protected $posts_clauses;
     36
    3237    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    3338        self::$superadmin_id  = $factory->user->create(
     
    8691        $this->test_file2 = get_temp_dir() . 'codeispoetry.png';
    8792        copy( $orig_file2, $this->test_file2 );
     93
     94        add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
     95        add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
     96    }
     97
     98    public function wpSetUpBeforeRequest( $result ) {
     99        $this->posts_clauses = array();
     100        return $result;
     101    }
     102
     103    public function save_posts_clauses( $clauses ) {
     104        $this->posts_clauses[] = $clauses;
     105        return $clauses;
    88106    }
    89107
     
    618636        $this->assertCount( 1, $data );
    619637        $this->assertSame( $id2, $data[0]['id'] );
     638    }
     639
     640    /**
     641     * @ticket 55677
     642     */
     643    public function test_get_items_avoid_duplicated_count_query_if_no_items() {
     644        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     645        $request->set_param( 'media_type', 'video' );
     646
     647        $response = rest_get_server()->dispatch( $request );
     648
     649        $this->assertCount( 1, $this->posts_clauses );
     650
     651        $headers = $response->get_headers();
     652
     653        $this->assertSame( 0, $headers['X-WP-Total'] );
     654        $this->assertSame( 0, $headers['X-WP-TotalPages'] );
     655    }
     656
     657    /**
     658     * @ticket 55677
     659     */
     660    public function test_get_items_with_empty_page_runs_count_query_after() {
     661        $this->factory->attachment->create_object(
     662            $this->test_file,
     663            0,
     664            array(
     665                'post_date'      => '2022-06-12T00:00:00Z',
     666                'post_mime_type' => 'image/jpeg',
     667                'post_excerpt'   => 'A sample caption',
     668            )
     669        );
     670
     671        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     672        $request->set_param( 'media_type', 'image' );
     673        $request->set_param( 'page', 2 );
     674
     675        $response = rest_get_server()->dispatch( $request );
     676
     677        $this->assertCount( 2, $this->posts_clauses );
     678
     679        $this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 );
    620680    }
    621681
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r52389 r53498  
    13861386        $this->assertCount( 0, $response->get_data() );
    13871387
    1388         // FIXME Since this request returns zero posts, the query is executed twice.
    1389         $this->assertCount( 2, $this->posts_clauses );
     1388        $this->assertCount( 1, $this->posts_clauses );
    13901389        $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 );
    13911390
     
    14181417        $this->assertCount( 0, $response->get_data() );
    14191418
    1420         // FIXME Since this request returns zero posts, the query is executed twice.
    1421         $this->assertCount( 2, $this->posts_clauses );
     1419        $this->assertCount( 1, $this->posts_clauses );
    14221420        $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 );
    14231421
     
    14371435        $this->assertCount( 0, $response->get_data() );
    14381436
    1439         // FIXME Since this request returns zero posts, the query is executed twice.
    1440         $this->assertCount( 2, $this->posts_clauses );
     1437        $this->assertCount( 1, $this->posts_clauses );
    14411438        $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 );
    14421439
Note: See TracChangeset for help on using the changeset viewer.