Allow comment query results to be limited to comments with comment_post_ID = 0.

Previously, this was not possible due to an overly broad `empty()` check.

Passing `null`, `false`, or `''` to 'post_id', or omitting 'post_id'
altogether, will continue to return comments regardless of `comment_post_ID`,
as before. Passing `0` or `'0'` will limit results to comments with no
associated post.

Props danielbachhuber.
Fixes #35090.

git-svn-id: https://develop.svn.wordpress.org/trunk@36381 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-01-22 20:19:49 +00:00
parent ad00ffffe4
commit dbac8968ed
2 changed files with 74 additions and 7 deletions

View File

@ -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.

View File

@ -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 );
}
/**