Make WordPress Core

Changeset 29045


Ignore:
Timestamp:
07/09/2014 06:15:42 PM (11 years ago)
Author:
nacin
Message:

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

props mordauk.
fixes #28434.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r28922 r29045  
    246246        $defaults = array(
    247247            'author_email' => '',
     248            'fields' => '',
    248249            'ID' => '',
    249250            'karma' => '',
     
    369370            $fields = 'COUNT(*)';
    370371        } else {
    371             $fields = '*';
    372         }
     372            switch ( strtolower( $this->query_vars['fields'] ) ) {
     373                case 'ids':
     374                    $fields = "$wpdb->comments.comment_ID";
     375                    break;
     376                default:
     377                    $fields = "*";
     378                    break;
     379            }
     380        }
     381
    373382        $join = '';
    374383        $where = $approved;
     
    461470            return $wpdb->get_var( $query );
    462471        }
     472
     473        if ( 'ids' == $this->query_vars['fields'] ) {
     474            $this->comments = $wpdb->get_col( $query );
     475            return array_map( 'intval', $this->comments );
     476        }
     477
    463478        $results = $wpdb->get_results( $query );
    464479        /**
  • trunk/tests/phpunit/tests/comment/query.php

    r27258 r29045  
    181181
    182182    }
     183
     184    /**
     185     * @ticket 28434
     186     */
     187    function test_fields_ids_query() {
     188        $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     189        $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     190        $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     191
     192        // Ensure we are dealing with integers, and not objects.
     193        $this->assertInternalType( 'integer', $comment_1 );
     194        $this->assertInternalType( 'integer', $comment_2 );
     195        $this->assertInternalType( 'integer', $comment_3 );
     196
     197        $comment_ids = get_comments( array( 'fields' => 'ids' ) );
     198        $this->assertCount( 3, $comment_ids );
     199        $this->assertEquals( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
     200    }
    183201}
Note: See TracChangeset for help on using the changeset viewer.