WordPress.org

Make WordPress Core

Ticket #19623: 19623.patch

File 19623.patch, 11.4 KB (added by boonebgorges, 6 years ago)
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index 41b1133..9bd0596 100644
    function comments_template( $file = '/comments.php', $separate_comments = false 
    11121112         */
    11131113        $comment_author_url = esc_url($commenter['comment_author_url']);
    11141114
    1115         /** @todo Use API instead of SELECTs. */
    1116         if ( $user_ID) {
    1117                 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
    1118         } else if ( empty($comment_author) ) {
    1119                 $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
    1120         } else {
    1121                 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
     1115        $comment_args = array(
     1116                'order'   => 'ASC',
     1117                'orderby' => 'comment_date_gmt',
     1118                'status'  => 'approve',
     1119                'post_id' => $post->ID,
     1120        );
     1121
     1122        if ( $user_ID ) {
     1123                $comment_args['include_unapproved'] = array( $user_ID );
     1124        } else if ( ! empty( $comment_author ) ) {
     1125                $comment_args['include_unapproved'] = array( $comment_author_email );
    11221126        }
    11231127
     1128        $comments = get_comments( $comment_args );
     1129
    11241130        /**
    11251131         * Filter the comments array.
    11261132         *
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index c84ecc2..98ecd4c 100644
    class WP_Comment_Query { 
    271271                        'author_email' => '',
    272272                        'author__in' => '',
    273273                        'author__not_in' => '',
     274                        'include_unapproved' => '',
    274275                        'fields' => '',
    275276                        'ID' => '',
    276277                        'comment__in' => '',
    class WP_Comment_Query { 
    333334                        return $cache;
    334335                }
    335336
     337                // Assemble clauses related to 'comment_approved'
     338                $approved_clauses = array();
    336339                $status = $this->query_vars['status'];
    337340                if ( 'hold' == $status ) {
    338                         $approved = "comment_approved = '0'";
     341                        $approved_clauses[] = "comment_approved = '0'";
    339342                } elseif ( 'approve' == $status ) {
    340                         $approved = "comment_approved = '1'";
     343                        $approved_clauses[] = "comment_approved = '1'";
    341344                } elseif ( ! empty( $status ) && 'all' != $status ) {
    342                         $approved = $wpdb->prepare( "comment_approved = %s", $status );
     345                        $approved_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
    343346                } else {
    344                         $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     347                        $approved_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
    345348                }
     349
     350                // User IDs or email addresses whose unapproved comments are included, regardless of $status.
     351                if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
     352                        $include_unapproved = $this->query_vars['include_unapproved'];
     353
     354                        // Accepts arrays or comma-separated strings.
     355                        if ( ! is_array( $include_unapproved ) ) {
     356                                $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
     357                        }
     358
     359                        $unapproved_ids = $unapproved_emails = array();
     360                        foreach ( $include_unapproved as $unapproved_identifier ) {
     361                                // Numeric values are assumed to be user ids.
     362                                if ( is_numeric( $unapproved_identifier ) ) {
     363                                        $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
     364
     365                                // Otherwise we match against email addresses.
     366                                } else {
     367                                        $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
     368                                }
     369                        }
     370                }
     371
     372                // Collapse comment_approved clauses into a single OR-separated clause
     373                if ( 1 === count( $approved_clauses ) ) {
     374                        $approved = $approved_clauses[0];
     375                } else {
     376                        $approved = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
     377                }
     378
    346379                $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
    347380
    348381                if ( ! empty( $this->query_vars['orderby'] ) ) {
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 9e7ba92..61a8586 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    360360
    361361                $this->assertEqualSets( array( $c3, $c4 ), $comment_ids );
    362362        }
     363
     364        /**
     365         * @ticket 19623
     366         */
     367        public function test_get_comments_with_status_all() {
     368                $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     369                $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     370                $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     371                $comments_approved_1 = get_comments( array( 'status' => 'all' ) );
     372
     373                $comment_ids = get_comments( array( 'fields' => 'ids' ) );
     374                $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
     375        }
     376
     377        /**
     378         * @ticket 19623
     379         */
     380        public function test_get_comments_with_include_unapproved_user_id() {
     381                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     382                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     383                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     384                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 6, 'comment_approved' => '0' ) );
     385
     386                $found = get_comments( array(
     387                        'fields' => 'ids',
     388                        'include_unapproved' => 1,
     389                        'status' => 'approve',
     390                ) );
     391
     392                $this->assertEqualSets( array( $c1, $c2, $c3 ), $found );
     393        }
     394
     395        /**
     396         * @ticket 19623
     397         */
     398        public function test_get_comments_with_include_unapproved_user_id_array() {
     399                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     400                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     401                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     402                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 6, 'comment_approved' => '0' ) );
     403                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 8, 'comment_approved' => '0' ) );
     404
     405                $found = get_comments( array(
     406                        'fields' => 'ids',
     407                        'include_unapproved' => array( 1, 8 ),
     408                        'status' => 'approve',
     409                ) );
     410
     411                $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     412        }
     413
     414        /**
     415         * @ticket 19623
     416         */
     417        public function test_get_comments_with_include_unapproved_user_id_comma_separated() {
     418                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     419                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     420                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     421                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 6, 'comment_approved' => '0' ) );
     422                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 8, 'comment_approved' => '0' ) );
     423
     424                $found = get_comments( array(
     425                        'fields' => 'ids',
     426                        'include_unapproved' => '1,8',
     427                        'status' => 'approve',
     428                ) );
     429
     430                $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     431        }
     432
     433        /**
     434         * @ticket 19623
     435         */
     436        public function test_get_comments_with_include_unapproved_author_email() {
     437                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     438                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     439                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     440                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     441
     442                $found = get_comments( array(
     443                        'fields' => 'ids',
     444                        'include_unapproved' => 'foo@example.com',
     445                        'status' => 'approve',
     446                ) );
     447
     448                $this->assertEqualSets( array( $c1, $c2, $c3 ), $found );
     449        }
     450
     451        /**
     452         * @ticket 19623
     453         */
     454        public function test_get_comments_with_include_unapproved_mixed_array() {
     455                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     456                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     457                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     458                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     459                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     460
     461                $found = get_comments( array(
     462                        'fields' => 'ids',
     463                        'include_unapproved' => array( 'foo@example.com', 4 ),
     464                        'status' => 'approve',
     465                ) );
     466
     467                $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     468        }
     469
     470        /**
     471         * @ticket 19623
     472         */
     473        public function test_get_comments_with_include_unapproved_mixed_comma_separated() {
     474                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     475                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '1', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     476                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'foo@example.com' ) );
     477                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 0, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     478                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 4, 'comment_approved' => '0', 'comment_author' => 'foo', 'comment_author_email' => 'bar@example.com' ) );
     479
     480                $found = get_comments( array(
     481                        'fields' => 'ids',
     482                        'include_unapproved' => 'foo@example.com, 4',
     483                        'status' => 'approve',
     484                ) );
     485
     486                $this->assertEqualSets( array( $c1, $c2, $c3, $c5 ), $found );
     487        }
    363488}