Make WordPress Core

Changeset 30098


Ignore:
Timestamp:
10/29/2014 09:57:07 PM (9 years ago)
Author:
boonebgorges
Message:

Use WP_Comment_Query to query comments in get_approved_comments().

Props dancameron.
See #12668.

Location:
trunk
Files:
3 edited

Legend:

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

    r30096 r30098  
    129129 *
    130130 * @since 2.0.0
    131  * @uses $wpdb
    132  *
    133  * @param int $post_id The ID of the post
    134  * @return array $comments The approved comments
    135  */
    136 function get_approved_comments($post_id) {
    137     global $wpdb;
    138     return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
     131 *
     132 * @param  int   $post_id  The ID of the post.
     133 * @param  array $args     Optional. WP_Comment_Query args.
     134 * @return array $comments The approved comments.
     135 */
     136function get_approved_comments( $post_id = 0, $args = array() ) {
     137    $defaults = array(
     138        'status'  => 1,
     139        'post_id' => $post_id,
     140        'order'   => 'ASC',
     141    );
     142    $r = wp_parse_args( $args, $defaults );
     143
     144    $query = new WP_Comment_Query;
     145    return $query->query( $r );
    139146}
    140147
  • trunk/tests/phpunit/tests/comment.php

    r25002 r30098  
    1515        $this->assertEquals( 0, $result );
    1616    }
     17
     18    public function test_get_approved_comments() {
     19        $p = $this->factory->post->create();
     20        $ca1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1' ) );
     21        $ca2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1' ) );
     22        $ca3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '0' ) );
     23        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     24        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     25        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     26        $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     27
     28        $found = get_approved_comments( $p );
     29
     30        // all comments types will be returned
     31        $this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck( $found, 'comment_ID' ) );
     32    }
    1733}
  • trunk/tests/phpunit/tests/comment/query.php

    r30096 r30098  
    962962        ) );
    963963
    964         $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
     964        $this->assertEqualSets( array( $c1, $c2, $c3, $c4, $c5 ), $found );
    965965    }
    966966
Note: See TracChangeset for help on using the changeset viewer.