WP_Comment_Query: Add fields => 'ids' query var.

props mordauk.
fixes #28434.


git-svn-id: https://develop.svn.wordpress.org/trunk@29045 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2014-07-09 18:15:42 +00:00
parent 3cda5caafd
commit adbe839132
2 changed files with 34 additions and 1 deletions

View File

@ -245,6 +245,7 @@ class WP_Comment_Query {
$defaults = array(
'author_email' => '',
'fields' => '',
'ID' => '',
'karma' => '',
'number' => '',
@ -368,8 +369,16 @@ class WP_Comment_Query {
if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)';
} else {
$fields = '*';
switch ( strtolower( $this->query_vars['fields'] ) ) {
case 'ids':
$fields = "$wpdb->comments.comment_ID";
break;
default:
$fields = "*";
break;
}
}
$join = '';
$where = $approved;
@ -460,6 +469,12 @@ class WP_Comment_Query {
if ( $this->query_vars['count'] ) {
return $wpdb->get_var( $query );
}
if ( 'ids' == $this->query_vars['fields'] ) {
$this->comments = $wpdb->get_col( $query );
return array_map( 'intval', $this->comments );
}
$results = $wpdb->get_results( $query );
/**
* Filter the comment query results.

View File

@ -180,4 +180,22 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEquals( $users[1], $comments[2]->user_id );
}
/**
* @ticket 28434
*/
function test_fields_ids_query() {
$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' ) );
// Ensure we are dealing with integers, and not objects.
$this->assertInternalType( 'integer', $comment_1 );
$this->assertInternalType( 'integer', $comment_2 );
$this->assertInternalType( 'integer', $comment_3 );
$comment_ids = get_comments( array( 'fields' => 'ids' ) );
$this->assertCount( 3, $comment_ids );
$this->assertEquals( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
}
}