From f9e32aef2d37d77a5ed2002b521e3e5c233dfe0b Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 29 Oct 2014 21:57:07 +0000 Subject: [PATCH] Use `WP_Comment_Query` to query comments in `get_approved_comments()`. Props dancameron. See #12668. git-svn-id: https://develop.svn.wordpress.org/trunk@30098 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 19 +++++++++++++------ tests/phpunit/tests/comment.php | 16 ++++++++++++++++ tests/phpunit/tests/comment/query.php | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 0906040cba..106c1745e8 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -128,14 +128,21 @@ function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $ * Retrieve the approved comments for post $post_id. * * @since 2.0.0 - * @uses $wpdb * - * @param int $post_id The ID of the post - * @return array $comments The approved comments + * @param int $post_id The ID of the post. + * @param array $args Optional. WP_Comment_Query args. + * @return array $comments The approved comments. */ -function get_approved_comments($post_id) { - global $wpdb; - 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)); +function get_approved_comments( $post_id = 0, $args = array() ) { + $defaults = array( + 'status' => 1, + 'post_id' => $post_id, + 'order' => 'ASC', + ); + $r = wp_parse_args( $args, $defaults ); + + $query = new WP_Comment_Query; + return $query->query( $r ); } /** diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index ddb6212b9f..bd24396068 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -14,4 +14,20 @@ class Tests_Comment extends WP_UnitTestCase { $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1] ) ); $this->assertEquals( 0, $result ); } + + public function test_get_approved_comments() { + $p = $this->factory->post->create(); + $ca1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1' ) ); + $ca2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1' ) ); + $ca3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '0' ) ); + $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'pingback' ) ); + $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'trackback' ) ); + $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'mario' ) ); + $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => '1', 'comment_type' => 'luigi' ) ); + + $found = get_approved_comments( $p ); + + // all comments types will be returned + $this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), wp_list_pluck( $found, 'comment_ID' ) ); + } } diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index e6fcc65357..9d5f171b8c 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -961,7 +961,7 @@ class Tests_Comment_Query extends WP_UnitTestCase { 'fields' => 'ids', ) ); - $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found ); + $this->assertEqualSets( array( $c1, $c2, $c3, $c4, $c5 ), $found ); } public function test_orderby_default() {