Make WordPress Core

Ticket #28434: 28434.diff

File 28434.diff, 2.7 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/comment.php

     
    245245
    246246                $defaults = array(
    247247                        'author_email' => '',
     248                        'fields' => '',
    248249                        'ID' => '',
    249250                        'karma' => '',
    250251                        'number' => '',
     
    368369                if ( $this->query_vars['count'] ) {
    369370                        $fields = 'COUNT(*)';
    370371                } else {
    371                         $fields = '*';
     372                        switch ( strtolower( $this->query_vars['fields'] ) ) {
     373                                case 'ids':
     374                                        $fields = "$wpdb->comments.comment_ID";
     375                                        break;
     376                                case 'id=>parent':
     377                                        $fields = "$wpdb->comments.comment_ID, $wpdb->comments.comment_post_ID";
     378                                        break;
     379                                default:
     380                                        $fields = "*";
     381                                        break;
     382                        }
    372383                }
     384
    373385                $join = '';
    374386                $where = $approved;
    375387
     
    460472                if ( $this->query_vars['count'] ) {
    461473                        return $wpdb->get_var( $query );
    462474                }
     475
     476                if ( 'ids' == $this->query_vars['fields'] ) {
     477                        $this->comments = $wpdb->get_col( $query );
     478                        return array_map( 'intval', $this->comments );
     479                }
     480
     481                if ( 'id=>parent' == $this->query_vars['fields'] ) {
     482                        $this->comments = $wpdb->get_results( $query );
     483
     484                        $r = array();
     485                        foreach ( $this->comments as $comments ) {
     486                                $r[ (int) $comments->comment_ID ] = (int) $comments->comment_post_ID;
     487                        }
     488                        return $r;
     489                }
     490
     491
    463492                $results = $wpdb->get_results( $query );
    464493                /**
    465494                 * Filter the comment query results.
  • tests/phpunit/tests/comment/query.php

     
    180180                $this->assertEquals( $users[1], $comments[2]->user_id );
    181181
    182182        }
     183
     184        /**
     185         * @ticket 28434
     186         */
     187        function test_fields_query() {
     188
     189                $comment_id1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     190                $comment_id2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     191                $comment_id3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     192
     193                $comments1 = get_comments( array( 'fields' => 'ids' ) );
     194                $this->assertEquals( 3, count( $comments1 ) );
     195                $this->assertEquals( array(
     196                        $comment_id1,
     197                        $comment_id2,
     198                        $comment_id3
     199                ), $comments1 );
     200
     201                $comments2 = get_comments( array( 'fields' => 'id=>parent' ) );
     202                $this->assertEquals( 3, count( $comments2 ) );
     203                $this->assertEquals( array(
     204                        $comment_id1 => $this->post_id,
     205                        $comment_id2 => $this->post_id,
     206                        $comment_id3 => $this->post_id
     207                ), $comments2 );
     208        }
    183209}