WordPress.org

Make WordPress Core

Changeset 29935


Ignore:
Timestamp:
10/17/2014 01:57:18 AM (5 years ago)
Author:
boonebgorges
Message:

Comment/post author in/not_in for WP_Comment_Query.

Props nofearinc, chriscct7.
Fixes #29885.

Location:
trunk
Files:
2 edited

Legend:

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

    r29808 r29935  
    258258     *
    259259     * @since 3.1.0
    260      * @since 4.1.0 Introduced 'comment__in', 'comment__not_in',
     260     * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
     261     *              'post_author__not_in', 'author__in', 'author__not_in',
    261262     *              'post__in', and 'post__not_in' to $query_vars.
    262263     *
     
    269270        $defaults = array(
    270271            'author_email' => '',
     272            'author__in' => '',
     273            'author__not_in' => '',
    271274            'fields' => '',
    272275            'ID' => '',
     
    279282            'order' => 'DESC',
    280283            'parent' => '',
     284            'post_author__in' => '',
     285            'post_author__not_in' => '',
    281286            'post_ID' => '',
    282287            'post_id' => 0,
     
    468473        }
    469474
     475        // If any post-related query vars are passed, join the posts table.
     476        $join_posts_table = false;
    470477        $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
    471478        $post_fields = array_filter( $plucked );
    472479
    473480        if ( ! empty( $post_fields ) ) {
     481            $join_posts_table = true;
     482            foreach ( $post_fields as $field_name => $field_value ) {
     483                $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
     484            }
     485        }
     486
     487        // Comment author IDs for an IN clause.
     488        if ( ! empty( $this->query_vars['author__in'] ) ) {
     489            $where .= ' AND user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
     490        }
     491
     492        // Comment author IDs for a NOT IN clause.
     493        if ( ! empty( $this->query_vars['author__not_in'] ) ) {
     494            $where .= ' AND user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
     495        }
     496
     497        // Post author IDs for an IN clause.
     498        if ( ! empty( $this->query_vars['post_author__in'] ) ) {
     499            $join_posts_table = true;
     500            $where .= ' AND post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
     501        }
     502
     503        // Post author IDs for a NOT IN clause.
     504        if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
     505            $join_posts_table = true;
     506            $where .= ' AND post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
     507        }
     508
     509        if ( $join_posts_table ) {
    474510            $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
    475             foreach( $post_fields as $field_name => $field_value )
    476                 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
    477511        }
    478512
  • trunk/tests/phpunit/tests/comment/query.php

    r29808 r29935  
    271271        $this->assertEqualSets( array( $c3 ), $comment_ids );
    272272    }
     273
     274    /**
     275     * @ticket 29885
     276     */
     277    function test_fields_post_author__in() {
     278        $author_id1 = 105;
     279        $author_id2 = 106;
     280
     281        $p1 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
     282        $p2 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
     283        $p3 = $this->factory->post->create( array( 'post_author' => $author_id2 ) );
     284
     285        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ) );
     286        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ) );
     287        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ) );
     288
     289        $comment_ids = get_comments( array(
     290            'fields' => 'ids',
     291            'post_author__in' => array( $author_id1 ),
     292        ) );
     293
     294        $this->assertEqualSets( array( $c1, $c2 ), $comment_ids );
     295    }
     296
     297    /**
     298     * @ticket 29885
     299     */
     300    function test_fields_post_author__not_in() {
     301        $author_id1 = 111;
     302        $author_id2 = 112;
     303
     304        $p1 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
     305        $p2 = $this->factory->post->create( array( 'post_author' => $author_id1 ) );
     306        $p3 = $this->factory->post->create( array( 'post_author' => $author_id2 ) );
     307
     308        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ) );
     309        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 1, 'comment_approved' => '1' ) );
     310        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p3, 'user_id' => 1, 'comment_approved' => '1' ) );
     311
     312        $comment_ids = get_comments( array(
     313            'fields' => 'ids',
     314            'post_author__not_in' => array( $author_id1 ),
     315        ) );
     316
     317        $this->assertEqualSets( array( $c3 ), $comment_ids );
     318    }
     319
     320        /**
     321         * @ticket 29885
     322         */
     323    function test_fields_author__in() {
     324        $p1 = $this->factory->post->create();
     325        $p2 = $this->factory->post->create();
     326        $p3 = $this->factory->post->create();
     327        $p4 = $this->factory->post->create();
     328
     329        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ) );
     330        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 2, 'comment_approved' => '1' ) );
     331        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 3, 'comment_approved' => '1' ) );
     332        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $p4, 'user_id' => 4, 'comment_approved' => '1' ) );
     333
     334        $comment_ids = get_comments( array(
     335            'fields' => 'ids',
     336            'author__in' => array( 1, 3 ),
     337        ) );
     338
     339        $this->assertEqualSets( array( $c1, $c3 ), $comment_ids );
     340    }
     341
     342        /**
     343         * @ticket 29885
     344         */
     345    function test_fields_author__not_in() {
     346        $p1 = $this->factory->post->create();
     347        $p2 = $this->factory->post->create();
     348        $p3 = $this->factory->post->create();
     349        $p4 = $this->factory->post->create();
     350
     351        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 1, 'comment_approved' => '1' ) );
     352        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p1, 'user_id' => 2, 'comment_approved' => '1' ) );
     353        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p2, 'user_id' => 3, 'comment_approved' => '1' ) );
     354        $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $p4, 'user_id' => 4, 'comment_approved' => '1' ) );
     355
     356        $comment_ids = get_comments( array(
     357            'fields' => 'ids',
     358            'author__not_in' => array( 1, 2 ),
     359        ) );
     360
     361        $this->assertEqualSets( array( $c3, $c4 ), $comment_ids );
     362    }
    273363}
Note: See TracChangeset for help on using the changeset viewer.