WordPress.org

Make WordPress Core

Ticket #19623: 19623.03.patch

File 19623.03.patch, 6.7 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 0d44376..9b102d4 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['unapproved_user_id'] = $user_ID;
     1124        } else if ( ! empty( $comment_author ) ) {
     1125                $comment_args['unapproved_author'] = wp_specialchars_decode( $comment_author, ENT_QUOTES );
     1126                $comment_args['unapproved_author_email'] = $comment_author_email;
    11221127        }
    11231128
     1129        $comments = get_comments( $comment_args );
     1130
    11241131        /**
    11251132         * Filter the comments array.
    11261133         *
  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index 516c1eb..6ec3413 100644
    class WP_Comment_Query { 
    303303                        return $cache;
    304304                }
    305305
     306                // Assemble clauses related to 'comment_approved'
     307                $approved_clauses = array();
    306308                $status = $this->query_vars['status'];
    307309                if ( 'hold' == $status ) {
    308                         $approved = "comment_approved = '0'";
     310                        $approved_clauses[] = "comment_approved = '0'";
    309311                } elseif ( 'approve' == $status ) {
    310                         $approved = "comment_approved = '1'";
     312                        $approved_clauses[] = "comment_approved = '1'";
    311313                } elseif ( ! empty( $status ) && 'all' != $status ) {
    312                         $approved = $wpdb->prepare( "comment_approved = %s", $status );
     314                        $approved_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
    313315                } else {
    314                         $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     316                        $approved_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
    315317                }
     318
     319                if ( ! empty( $this->query_vars['unapproved_user_id'] ) ) {
     320                        $approved_clauses[] = $wpdb->prepare( "user_id = %d AND comment_approved = '0'", $this->query_vars['unapproved_user_id'] );
     321                }
     322
     323                if ( ! empty( $this->query_vars['unapproved_author'] ) && ! empty( $this->query_vars['unapproved_author_email'] ) ) {
     324                        $approved_clauses[] = $wpdb->prepare( "comment_author = %s AND comment_author_email = %s AND comment_approved = '0'", $this->query_vars['unapproved_author'], $this->query_vars['unapproved_author_email'] );
     325                }
     326
     327                // Collapse comment_approved clauses into a single OR-separated clause
     328                if ( 1 === count( $approved_clauses ) ) {
     329                        $approved = $approved_clauses[0];
     330                } else {
     331                        $approved = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
     332                }
     333
    316334                $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
    317335
    318336                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 51140a7..d471842 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        public function test_get_comments_with_status_all() {
     203                $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     204                $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     205                $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     206                $comments_approved_1 = get_comments( array( 'status' => 'all' ) );
     207
     208                $comment_ids = get_comments( array( 'fields' => 'ids' ) );
     209                $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
     210        }
     211
     212        public function test_get_comments_with_unapproved_user_id() {
     213                $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     214                $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) );
     215                $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '0' ) );
     216
     217                // With unapproved user id
     218                $comment_ids_1 = get_comments( array(
     219                        'fields' => 'ids',
     220                        'unapproved_user_id' => 1,
     221                        'status' => 'approve',
     222                ) );
     223
     224                $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids_1 );
     225
     226                // Without unapproved user id
     227                $comment_ids_2 = get_comments( array(
     228                        'fields' => 'ids',
     229                        'status' => 'approve',
     230                ) );
     231
     232                $this->assertEqualSets( array( $comment_1, $comment_2 ), $comment_ids_2 );
     233        }
     234
     235        public function test_get_comments_with_unapproved_author_and_unapproved_author_email() {
     236                $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) );
     237                $comment_2 = $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' ) );
     238                $comment_3 = $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' ) );
     239
     240                // With unapproved author/email
     241                $comment_ids_1 = get_comments( array(
     242                        'fields' => 'ids',
     243                        'unapproved_author' => 'foo',
     244                        'unapproved_author_email' => 'foo@example.com',
     245                        'status' => 'approve',
     246                ) );
     247
     248                $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids_1 );
     249
     250                // Without unapproved author/email
     251                $comment_ids_2 = get_comments( array(
     252                        'fields' => 'ids',
     253                        'status' => 'approve',
     254                ) );
     255
     256                $this->assertEqualSets( array( $comment_1, $comment_2 ), $comment_ids_2 );
     257        }
    201258}