Make WordPress Core

Changeset 27258


Ignore:
Timestamp:
02/25/2014 04:34:25 PM (12 years ago)
Author:
nacin
Message:

Allow user_id to be an array of IDs in WP_Comment_Query.

props mordauk.
fixes #27064.

Location:
trunk
Files:
2 edited

Legend:

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

    r27191 r27258  
    374374                if ( '' !== $parent )
    375375                        $where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
    376                 if ( '' !== $user_id )
     376
     377                if ( is_array( $user_id ) ) {
     378                        $where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $user_id ) ) . ')';
     379                } elseif ( '' !== $user_id ) {
    377380                        $where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
     381                }
     382
    378383                if ( '' !== $search )
    379384                        $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
  • trunk/tests/phpunit/tests/comment/query.php

    r25364 r27258  
    157157                $this->assertEquals( 10, count( get_comments( array( 'status' => 'spam' ) ) ) );
    158158        }
     159
     160        /**
     161         * @ticket 27064
     162         */
     163        function test_get_comments_by_user() {
     164                $users = $this->factory->user->create_many( 2 );
     165                $this->factory->comment->create( array( 'user_id' => $users[0], 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     166                $this->factory->comment->create( array( 'user_id' => $users[0], 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     167                $this->factory->comment->create( array( 'user_id' => $users[1], 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     168
     169                $comments = get_comments( array( 'user_id' => $users[0] ) );
     170
     171                $this->assertCount( 2, $comments );
     172                $this->assertEquals( $users[0], $comments[0]->user_id );
     173                $this->assertEquals( $users[0], $comments[1]->user_id );
     174
     175                $comments = get_comments( array( 'user_id' => $users ) );
     176
     177                $this->assertCount( 3, $comments );
     178                $this->assertEquals( $users[0], $comments[0]->user_id );
     179                $this->assertEquals( $users[0], $comments[1]->user_id );
     180                $this->assertEquals( $users[1], $comments[2]->user_id );
     181
     182        }
    159183}
Note: See TracChangeset for help on using the changeset viewer.