WP_Comment_Query: comment__in, comment__not_in, post__in, post__not_in.

Props nofearinc, mordauk, boonebgorges

Fixes #25386

git-svn-id: https://develop.svn.wordpress.org/trunk@29808 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-02 01:40:56 +00:00
parent c313878af2
commit dd9e9501c1
2 changed files with 98 additions and 0 deletions

View File

@ -257,6 +257,8 @@ class WP_Comment_Query {
* Execute the query
*
* @since 3.1.0
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in',
* 'post__in', and 'post__not_in' to $query_vars.
*
* @param string|array $query_vars
* @return int|array
@ -268,6 +270,8 @@ class WP_Comment_Query {
'author_email' => '',
'fields' => '',
'ID' => '',
'comment__in' => '',
'comment__not_in' => '',
'karma' => '',
'number' => '',
'offset' => '',
@ -276,6 +280,8 @@ class WP_Comment_Query {
'parent' => '',
'post_ID' => '',
'post_id' => 0,
'post__in' => '',
'post__not_in' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '',
@ -408,6 +414,26 @@ class WP_Comment_Query {
$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
}
// Parse comment IDs for an IN clause.
if ( ! empty( $this->query_vars['comment__in'] ) ) {
$where .= ' AND comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
}
// Parse comment IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
$where .= ' AND comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
}
// Parse comment post IDs for an IN clause.
if ( ! empty( $this->query_vars['post__in'] ) ) {
$where .= ' AND comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
}
// Parse comment post IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['post__not_in'] ) ) {
$where .= ' AND comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
}
if ( '' !== $this->query_vars['author_email'] ) {
$where .= $wpdb->prepare( ' AND comment_author_email = %s', $this->query_vars['author_email'] );
}

View File

@ -198,4 +198,76 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertCount( 3, $comment_ids );
$this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
}
/**
* @ticket 29189
*/
function test_fields_comment__in() {
$comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
$comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
$comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
$comment_ids = get_comments( array(
'fields' => 'ids',
'comment__in' => array( $comment_1, $comment_3 ),
) );
$this->assertEqualSets( array( $comment_1, $comment_3 ), $comment_ids );
}
/**
* @ticket 29189
*/
function test_fields_comment__not_in() {
$comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
$comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
$comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
$comment_ids = get_comments( array(
'fields' => 'ids',
'comment__not_in' => array( $comment_2, $comment_3 ),
) );
$this->assertEqualSets( array( $comment_1 ), $comment_ids );
}
/**
* @ticket 29189
*/
function test_fields_post__in() {
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 7, 'comment_approved' => '1' ) );
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ) );
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ) );
$comment_ids = get_comments( array(
'fields' => 'ids',
'post__in' => array( $p1, $p2 ),
) );
$this->assertEqualSets( array( $c1, $c2 ), $comment_ids );
}
/**
* @ticket 29189
*/
function test_fields_post__not_in() {
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 7, 'comment_approved' => '1' ) );
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ) );
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ) );
$comment_ids = get_comments( array(
'fields' => 'ids',
'post__not_in' => array( $p1, $p2 ),
) );
$this->assertEqualSets( array( $c3 ), $comment_ids );
}
}