Make WordPress Core

Ticket #29189: 29189.2.patch

File 29189.2.patch, 5.3 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 7c42574..4f3dfdf 100644
    class WP_Comment_Query { 
    257257         * Execute the query
    258258         *
    259259         * @since 3.1.0
     260         * @since 4.1.0 Introduced 'comment_id__in', 'comment_id__not_in',
     261         *              'post_id__in', and 'post_id__not_in' to $query_vars.
    260262         *
    261263         * @param string|array $query_vars
    262264         * @return int|array
    class WP_Comment_Query { 
    268270                        'author_email' => '',
    269271                        'fields' => '',
    270272                        'ID' => '',
     273                        'comment_id__in' => '',
     274                        'comment_id__not_in' => '',
    271275                        'karma' => '',
    272276                        'number' => '',
    273277                        'offset' => '',
    class WP_Comment_Query { 
    276280                        'parent' => '',
    277281                        'post_ID' => '',
    278282                        'post_id' => 0,
     283                        'post_id__in' => '',
     284                        'post_id__not_in' => '',
    279285                        'post_author' => '',
    280286                        'post_name' => '',
    281287                        'post_parent' => '',
    class WP_Comment_Query { 
    408414                        $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
    409415                }
    410416
     417                // Parse comment IDs for an IN clause.
     418                if ( ! empty( $this->query_vars['comment_id__in'] ) ) {
     419                        $where .= ' AND comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment_id__in'] ) ) . ' )';
     420                }
     421
     422                // Parse comment IDs for a NOT IN clause.
     423                if ( ! empty( $this->query_vars['comment_id__not_in'] ) ) {
     424                        $where .= ' AND comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment_id__not_in'] ) ) . ' )';
     425                }
     426
     427                // Parse comment post IDs for an IN clause.
     428                if ( ! empty( $this->query_vars['post_id__in'] ) ) {
     429                        $where .= ' AND comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_id__in'] ) ) . ' )';
     430                }
     431
     432                // Parse comment post IDs for a NOT IN clause.
     433                if ( ! empty( $this->query_vars['post_id__not_in'] ) ) {
     434                        $where .= ' AND comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_id__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'] );
    413439                }
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 51140a7..4dea39b 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    198198                $this->assertCount( 3, $comment_ids );
    199199                $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
    200200        }
     201
     202        /**
     203         * @ticket 29189
     204         */
     205        function test_fields_comment_id__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_id__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_id__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_id__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_id__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_id__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_id__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_id__not_in' => array( $p1, $p2 ),
     269                ) );
     270
     271                $this->assertEqualSets( array( $c3 ), $comment_ids );
     272        }
    201273}