WordPress.org

Make WordPress Core

Changeset 29808


Ignore:
Timestamp:
10/02/2014 01:40:56 AM (5 years ago)
Author:
boonebgorges
Message:

WP_Comment_Query: commentin, commentnot_in, postin, postnot_in.

Props nofearinc, mordauk, boonebgorges

Fixes #25386

Location:
trunk
Files:
2 edited

Legend:

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

    r29788 r29808  
    258258     *
    259259     * @since 3.1.0
     260     * @since 4.1.0 Introduced 'comment__in', 'comment__not_in',
     261     *              'post__in', and 'post__not_in' to $query_vars.
    260262     *
    261263     * @param string|array $query_vars
     
    269271            'fields' => '',
    270272            'ID' => '',
     273            'comment__in' => '',
     274            'comment__not_in' => '',
    271275            'karma' => '',
    272276            'number' => '',
     
    277281            'post_ID' => '',
    278282            'post_id' => 0,
     283            'post__in' => '',
     284            'post__not_in' => '',
    279285            'post_author' => '',
    280286            'post_name' => '',
     
    409415        }
    410416
     417        // Parse comment IDs for an IN clause.
     418        if ( ! empty( $this->query_vars['comment__in'] ) ) {
     419            $where .= ' AND comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
     420        }
     421
     422        // Parse comment IDs for a NOT IN clause.
     423        if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
     424            $where .= ' AND comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
     425        }
     426
     427        // Parse comment post IDs for an IN clause.
     428        if ( ! empty( $this->query_vars['post__in'] ) ) {
     429            $where .= ' AND comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
     430        }
     431
     432        // Parse comment post IDs for a NOT IN clause.
     433        if ( ! empty( $this->query_vars['post__not_in'] ) ) {
     434            $where .= ' AND comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
     435        }
     436
    411437        if ( '' !== $this->query_vars['author_email'] ) {
    412438            $where .= $wpdb->prepare( ' AND comment_author_email = %s', $this->query_vars['author_email'] );
  • trunk/tests/phpunit/tests/comment/query.php

    r29134 r29808  
    199199        $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
    200200    }
     201
     202    /**
     203     * @ticket 29189
     204     */
     205    function test_fields_comment__in() {
     206        $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     207        $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     208        $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     209
     210        $comment_ids = get_comments( array(
     211            'fields' => 'ids',
     212            'comment__in' => array( $comment_1, $comment_3 ),
     213        ) );
     214
     215        $this->assertEqualSets( array( $comment_1, $comment_3 ), $comment_ids );
     216    }
     217
     218    /**
     219     * @ticket 29189
     220     */
     221    function test_fields_comment__not_in() {
     222        $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     223        $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     224        $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     225
     226        $comment_ids = get_comments( array(
     227            'fields' => 'ids',
     228            'comment__not_in' => array( $comment_2, $comment_3 ),
     229        ) );
     230
     231        $this->assertEqualSets( array( $comment_1 ), $comment_ids );
     232    }
     233
     234    /**
     235     * @ticket 29189
     236     */
     237    function test_fields_post__in() {
     238        $p1 = $this->factory->post->create();
     239        $p2 = $this->factory->post->create();
     240        $p3 = $this->factory->post->create();
     241
     242        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 7, 'comment_approved' => '1' ) );
     243        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ) );
     244        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ) );
     245
     246        $comment_ids = get_comments( array(
     247            'fields' => 'ids',
     248            'post__in' => array( $p1, $p2 ),
     249        ) );
     250
     251        $this->assertEqualSets( array( $c1, $c2 ), $comment_ids );
     252    }
     253
     254    /**
     255     * @ticket 29189
     256     */
     257    function test_fields_post__not_in() {
     258        $p1 = $this->factory->post->create();
     259        $p2 = $this->factory->post->create();
     260        $p3 = $this->factory->post->create();
     261
     262        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 7, 'comment_approved' => '1' ) );
     263        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ) );
     264        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ) );
     265
     266        $comment_ids = get_comments( array(
     267            'fields' => 'ids',
     268            'post__not_in' => array( $p1, $p2 ),
     269        ) );
     270
     271        $this->assertEqualSets( array( $c3 ), $comment_ids );
     272    }
    201273}
Note: See TracChangeset for help on using the changeset viewer.