Comment/post author in/not_in for WP_Comment_Query.

Props nofearinc, chriscct7.
Fixes #29885.

git-svn-id: https://develop.svn.wordpress.org/trunk@29935 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-17 01:57:18 +00:00
parent 64bc8466db
commit aee4647da0
2 changed files with 127 additions and 3 deletions

View File

@ -257,7 +257,8 @@ class WP_Comment_Query {
* Execute the query
*
* @since 3.1.0
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in',
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
* 'post_author__not_in', 'author__in', 'author__not_in',
* 'post__in', and 'post__not_in' to $query_vars.
*
* @param string|array $query_vars
@ -268,6 +269,8 @@ class WP_Comment_Query {
$defaults = array(
'author_email' => '',
'author__in' => '',
'author__not_in' => '',
'fields' => '',
'ID' => '',
'comment__in' => '',
@ -278,6 +281,8 @@ class WP_Comment_Query {
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_author__in' => '',
'post_author__not_in' => '',
'post_ID' => '',
'post_id' => 0,
'post__in' => '',
@ -467,13 +472,42 @@ class WP_Comment_Query {
);
}
// If any post-related query vars are passed, join the posts table.
$join_posts_table = false;
$plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
$post_fields = array_filter( $plucked );
if ( ! empty( $post_fields ) ) {
$join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
foreach( $post_fields as $field_name => $field_value )
$join_posts_table = true;
foreach ( $post_fields as $field_name => $field_value ) {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
}
}
// Comment author IDs for an IN clause.
if ( ! empty( $this->query_vars['author__in'] ) ) {
$where .= ' AND user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
}
// Comment author IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['author__not_in'] ) ) {
$where .= ' AND user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
}
// Post author IDs for an IN clause.
if ( ! empty( $this->query_vars['post_author__in'] ) ) {
$join_posts_table = true;
$where .= ' AND post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
}
// Post author IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
$join_posts_table = true;
$where .= ' AND post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
}
if ( $join_posts_table ) {
$join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
}
if ( ! empty( $this->meta_query->queries ) ) {

View File

@ -270,4 +270,94 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEqualSets( array( $c3 ), $comment_ids );
}
/**
* @ticket 29885
*/
function test_fields_post_author__in() {
$author_id1 = 105;
$author_id2 = 106;
$p1 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
$p2 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
$p3 = $this->factory->post->create( array( 'post_author' => $author_id2 ) );
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, '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_author__in' => array( $author_id1 ),
) );
$this->assertEqualSets( array( $c1, $c2 ), $comment_ids );
}
/**
* @ticket 29885
*/
function test_fields_post_author__not_in() {
$author_id1 = 111;
$author_id2 = 112;
$p1 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
$p2 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
$p3 = $this->factory->post->create( array( 'post_author' => $author_id2 ) );
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, '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_author__not_in' => array( $author_id1 ),
) );
$this->assertEqualSets( array( $c3 ), $comment_ids );
}
/**
* @ticket 29885
*/
function test_fields_author__in() {
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ) );
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 2, 'comment_approved' => '1' ) );
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 3, 'comment_approved' => '1' ) );
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $p4, 'user_id' => 4, 'comment_approved' => '1' ) );
$comment_ids = get_comments( array(
'fields' => 'ids',
'author__in' => array( 1, 3 ),
) );
$this->assertEqualSets( array( $c1, $c3 ), $comment_ids );
}
/**
* @ticket 29885
*/
function test_fields_author__not_in() {
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ) );
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 2, 'comment_approved' => '1' ) );
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 3, 'comment_approved' => '1' ) );
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $p4, 'user_id' => 4, 'comment_approved' => '1' ) );
$comment_ids = get_comments( array(
'fields' => 'ids',
'author__not_in' => array( 1, 2 ),
) );
$this->assertEqualSets( array( $c3, $c4 ), $comment_ids );
}
}