Make WordPress Core

Changeset 30402


Ignore:
Timestamp:
11/20/2014 01:51:38 AM (9 years ago)
Author:
boonebgorges
Message:

Return an empty array from get_approved_comments() when $post_id is empty.

This behavior was broken when moving the internals to WP_Comment_Query in
[30098]. As a result, get_approved_comments( 0 ) was fetching *all* approved
comments, causing performance issues.

Props dd32.
Fixes #30412.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r30281 r30402  
    137137 *                             argument is true.
    138138 */
    139 function get_approved_comments( $post_id = 0, $args = array() ) {
     139function get_approved_comments( $post_id, $args = array() ) {
     140    if ( ! $post_id ) {
     141        return array();
     142    }
     143
    140144    $defaults = array(
    141145        'status'  => 1,
  • trunk/tests/phpunit/tests/comment.php

    r30098 r30402  
    3131        $this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck( $found, 'comment_ID' ) );
    3232    }
     33
     34    /**
     35     * @ticket 30412
     36     */
     37    public function test_get_approved_comments_with_post_id_0_should_return_empty_array() {
     38        $p = $this->factory->post->create();
     39        $ca1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1' ) );
     40
     41        $found = get_approved_comments( 0 );
     42
     43        $this->assertSame( array(), $found );
     44    }
    3345}
  • trunk/tests/phpunit/tests/comment/query.php

    r30266 r30402  
    2929
    3030        $this->assertEqualSets( array( $c1, $c2, $c3, $c4, $c5 ), $found );
     31    }
     32
     33    public function test_query_post_id_0() {
     34        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     35
     36        $q = new WP_Comment_Query();
     37        $found = $q->query( array(
     38            'post_id' => 0,
     39            'fields' => 'ids',
     40        ) );
     41
     42        $this->assertEqualSets( array( $c1 ), $found );
    3143    }
    3244
Note: See TracChangeset for help on using the changeset viewer.