diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index eb6f094a5e..71cd525833 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -214,7 +214,7 @@ class WP_Comment_Query { * Default empty. * @type int $post_ID Currently unused. * @type int $post_id Limit results to those affiliated with a given post ID. - * Default 0. + * Default null. * @type array $post__in Array of post IDs to include affiliated comments for. * Default empty. * @type array $post__not_in Array of post IDs to exclude affiliated comments for. @@ -276,7 +276,7 @@ class WP_Comment_Query { 'post_author__in' => '', 'post_author__not_in' => '', 'post_ID' => '', - 'post_id' => 0, + 'post_id' => null, 'post__in' => '', 'post__not_in' => '', 'post_author' => '', @@ -645,9 +645,8 @@ class WP_Comment_Query { $fields = "$wpdb->comments.comment_ID"; } - $post_id = absint( $this->query_vars['post_id'] ); - if ( ! empty( $post_id ) ) { - $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id ); + if ( strlen( $this->query_vars['post_id'] ) ) { + $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $this->query_vars['post_id'] ); } // Parse comment IDs for an IN clause. diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 6de468e10b..92dbcad458 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -36,8 +36,12 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertEqualSets( array( $c1, $c2, $c3, $c4, $c5 ), $found ); } - public function test_query_post_id_0() { + /** + * @ticket 35090 + */ + public function test_post_id_0_should_return_comments_with_no_parent() { $c1 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ) ); + $c2 = self::factory()->comment->create( array( 'comment_post_ID' => 0, 'comment_approved' => '1' ) ); $q = new WP_Comment_Query(); $found = $q->query( array( @@ -45,7 +49,71 @@ class Tests_Comment_Query extends WP_UnitTestCase { 'fields' => 'ids', ) ); - $this->assertEqualSets( array( $c1 ), $found ); + $this->assertEqualSets( array( $c2 ), $found ); + } + + /** + * @ticket 35090 + */ + public function test_post_id_string_0_should_return_comments_with_no_parent() { + $c1 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ) ); + $c2 = self::factory()->comment->create( array( 'comment_post_ID' => 0, 'comment_approved' => '1' ) ); + + $q = new WP_Comment_Query(); + $found = $q->query( array( + 'post_id' => '0', + 'fields' => 'ids', + ) ); + + $this->assertEqualSets( array( $c2 ), $found ); + } + + /** + * @ticket 35090 + */ + public function test_post_id_null_should_be_ignored() { + $c1 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ) ); + $c2 = self::factory()->comment->create( array( 'comment_post_ID' => 0, 'comment_approved' => '1' ) ); + + $q = new WP_Comment_Query(); + $found = $q->query( array( + 'post_id' => null, + 'fields' => 'ids', + ) ); + + $this->assertEqualSets( array( $c1, $c2 ), $found ); + } + + /** + * @ticket 35090 + */ + public function test_post_id_false_should_be_ignored() { + $c1 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ) ); + $c2 = self::factory()->comment->create( array( 'comment_post_ID' => 0, 'comment_approved' => '1' ) ); + + $q = new WP_Comment_Query(); + $found = $q->query( array( + 'post_id' => false, + 'fields' => 'ids', + ) ); + + $this->assertEqualSets( array( $c1, $c2 ), $found ); + } + + /** + * @ticket 35090 + */ + public function test_post_id_empty_string_should_be_ignored() { + $c1 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id, 'comment_approved' => '1' ) ); + $c2 = self::factory()->comment->create( array( 'comment_post_ID' => 0, 'comment_approved' => '1' ) ); + + $q = new WP_Comment_Query(); + $found = $q->query( array( + 'post_id' => '', + 'fields' => 'ids', + ) ); + + $this->assertEqualSets( array( $c1, $c2 ), $found ); } /**