diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index f4bb7f6c60..01048e6a3f 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -92,7 +92,8 @@ class WP_Comment_Query { * * Sets up the comment query, based on the query vars passed. * - * @since 4.2.0 + * @since 4.2.0 + * @since 4.4.0 `$parent__in` and `$parent__not_in` were added. * @access public * * @param string|array $query { @@ -136,6 +137,8 @@ class WP_Comment_Query { * @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'. * Default: 'DESC'. * @type int $parent Parent ID of comment to retrieve children of. Default empty. + * @type array $parent__in Array of parent IDs of comments to retrieve children for. Default empty. + * @type array $parent__not_in Array of parent IDs of comments *not* to retrieve children for. Default empty. * @type array $post_author__in Array of author IDs to retrieve comments for. Default empty. * @type array $post_author__not_in Array of author IDs *not* to retrieve comments for. Default empty. * @type int $post_ID Currently unused. @@ -486,6 +489,16 @@ class WP_Comment_Query { $where[] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; } + // Parse comment parent IDs for an IN clause. + if ( ! empty( $this->query_vars['parent__in'] ) ) { + $where[] = 'comment_parent IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )'; + } + + // Parse comment parent IDs for a NOT IN clause. + if ( ! empty( $this->query_vars['parent__not_in'] ) ) { + $where[] = 'comment_parent NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )'; + } + // Parse comment post IDs for an IN clause. if ( ! empty( $this->query_vars['post__in'] ) ) { $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 420d8f2dad..7356541ef4 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -1737,4 +1737,104 @@ class Tests_Comment_Query extends WP_UnitTestCase { $q->query_vars['meta_key'] = 'foo'; $q->query_vars['meta_value'] = 'bar'; } + + /** + * @ticket 33882 + */ + public function test_parent__in() { + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); + $c2 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1', + 'comment_parent' => $c1, + ) ); + + $ids = new WP_Comment_Query( array( + 'comment_post_ID' => $this->post_id, + 'fields' => 'ids', + 'parent__in' => array( $c1 ) + ) ); + + $this->assertEqualSets( array( $c2 ), $ids->comments ); + } + + /** + * @ticket 33882 + */ + public function test_parent__in_commas() { + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); + $c2 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1' + ) ); + $c3 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1', + 'comment_parent' => $c1, + ) ); + $c4 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1', + 'comment_parent' => $c2, + ) ); + + $ids = new WP_Comment_Query( array( + 'comment_post_ID' => $this->post_id, + 'fields' => 'ids', + 'parent__in' => "$c1,$c2" + ) ); + + $this->assertEqualSets( array( $c3, $c4 ), $ids->comments ); + } + + /** + * @ticket 33882 + */ + public function test_parent__not_in() { + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); + + $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1', + 'comment_parent' => $c1, + ) ); + + $ids = new WP_Comment_Query( array( + 'comment_post_ID' => $this->post_id, + 'fields' => 'ids', + 'parent__not_in' => array( $c1 ) + ) ); + + $this->assertEqualSets( array( $c1 ), $ids->comments ); + } + + /** + * @ticket 33882 + */ + public function test_parent__not_in_commas() { + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); + $c2 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1' + ) ); + + $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1', + 'comment_parent' => $c1, + ) ); + $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1', + 'comment_parent' => $c2, + ) ); + + $ids = new WP_Comment_Query( array( + 'comment_post_ID' => $this->post_id, + 'fields' => 'ids', + 'parent__not_in' => "$c1,$c2" + ) ); + + $this->assertEqualSets( array( $c1, $c2 ), $ids->comments ); + } }