Make WordPress Core

Changeset 48133


Ignore:
Timestamp:
06/23/2020 05:22:39 AM (6 years ago)
Author:
whyisjake
Message:

Comments: Ensure the proper comment count and pages for unapproved comments.

Previiously, unapproved comments can alter the comment count, returning incorrect page numbers.

Fixes #8973.

Props GregMulhauser, dd32, ryan, mrmist, hakre, solarissmoke, billerickson, ericlewis, SergeyBiryukov, chriscct7, dossy, lukecavanagh, renggo888, jdorner, matjack1, pento, audrasjb, imath, davidbaumwald, whyisjake.

Location:
trunk
Files:
3 edited

Legend:

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

    r48104 r48133  
    13311331 *
    13321332 * @since 1.5.0
     1333 * @since 5.5.0 Removed the need to use the $user_ID global.
    13331334 *
    13341335 * @global WP_Query   $wp_query         WordPress Query object.
    … …  
    13381339 * @global WP_Comment $comment          Global comment object.
    13391340 * @global string     $user_login
    1340  * @global int        $user_ID
    13411341 * @global string     $user_identity
    13421342 * @global bool       $overridden_cpage
    … …  
    13481348 */
    13491349function comments_template( $file = '/comments.php', $separate_comments = false ) {
    1350         global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
     1350        global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
    13511351
    13521352        if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
    … …  
    13971397        }
    13981398
    1399         if ( $user_ID ) {
    1400                 $comment_args['include_unapproved'] = array( $user_ID );
    1401         } else {
    1402                 $unapproved_email = wp_get_unapproved_comment_author_email();
    1403 
    1404                 if ( $unapproved_email ) {
    1405                         $comment_args['include_unapproved'] = array( $unapproved_email );
    1406                 }
     1399        $include_unapproved = wp_get_include_unapproved_comments_argument();
     1400        if ( $include_unapproved ) {
     1401                $comment_args['include_unapproved'] = $include_unapproved;
    14071402        }
    14081403
  • trunk/src/wp-includes/comment.php

    r48121 r48133  
    11331133                );
    11341134
     1135                $include_unapproved = wp_get_include_unapproved_comments_argument();
     1136                if ( $include_unapproved ) {
     1137                        $comment_args['include_unapproved'] = $include_unapproved;
     1138                }
     1139
     1140                /**
     1141                 * Filters the arguments used to query comments in get_page_of_comment().
     1142                 *
     1143                 * @since 5.5.0
     1144                 *
     1145                 * @see WP_Comment_Query::__construct()
     1146                 *
     1147                 * @param array $comment_args {
     1148                 *     Array of WP_Comment_Query arguments.
     1149                 *
     1150                 *     @type string $type               Limit paginated comments to those matching a given type. Accepts 'comment',
     1151                 *                                      'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
     1152                 *                                      Default is 'all'.
     1153                 *     @type int    $post_id            ID of the post.
     1154                 *     @type string $fields             Comment fields to return.
     1155                 *     @type bool   $count              Whether to return a comment count (true) or array of
     1156                 *                                      comment objects (false)
     1157                 *     @type string $status             Comment status.
     1158                 *     @type int    $parent             Parent ID of comment to retrieve children of.
     1159                 *     @type array  $date_query         Date query clauses to limit comments by. See WP_Date_Query.
     1160                 *     @type array  $include_unapproved Array of IDs or email addresses whose unapproved comments
     1161                 *                                      will be included in paginated comments.
     1162                 * }
     1163                 */
     1164                $comment_args        = apply_filters( 'get_page_of_comment_query_args', $comment_args );
    11351165                $comment_query       = new WP_Comment_Query();
    11361166                $older_comment_count = $comment_query->query( $comment_args );
    … …  
    18951925
    18961926        return $commenter_email;
     1927}
     1928
     1929/**
     1930 * Get include unapproved comments query argument.
     1931 *
     1932 * Used to include unapproved comments of currrent commenters to
     1933 * keep them informed their comments were successfully saved.
     1934 *
     1935 * @since 5.5.0
     1936 *
     1937 * @return array The unapproved comments query argument.
     1938 */
     1939function wp_get_include_unapproved_comments_argument() {
     1940        $user_id            = get_current_user_id();
     1941        $include_unapproved = array();
     1942
     1943        if ( $user_id ) {
     1944                $include_unapproved = array( $user_id );
     1945        } else {
     1946                $unapproved_email = wp_get_unapproved_comment_author_email();
     1947
     1948                if ( $unapproved_email ) {
     1949                        $include_unapproved = array( $unapproved_email );
     1950                }
     1951        }
     1952
     1953        return $include_unapproved;
    18971954}
    18981955
  • trunk/tests/phpunit/tests/comment/getPageOfComment.php

    r47122 r48133  
    440440                $this->assertEquals( 2, get_page_of_comment( $c3 ) );
    441441        }
     442
     443        /**
     444         * @ticket 8973
     445         */
     446        public function test_page_number_when_unapproved_comments_are_included_for_current_commenter() {
     447                $post         = self::factory()->post->create();
     448                $comment_args = array(
     449                        'comment_post_ID'      => $post,
     450                        'comment_approved'     => 0,
     451                        'comment_author_email' => 'foo@bar.test',
     452                        'comment_author'       => 'Foo',
     453                        'comment_author_url'   => 'https://bar.test',
     454                );
     455
     456                for ( $i = 1; $i < 4; $i++ ) {
     457                        self::factory()->comment->create(
     458                                array_merge(
     459                                        $comment_args,
     460                                        array(
     461                                                'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', time() - ( $i * 1000 ) ),
     462                                        )
     463                                )
     464                        );
     465                }
     466
     467                $new_unapproved = self::factory()->comment->create(
     468                        $comment_args
     469                );
     470
     471                add_filter( 'wp_get_current_commenter', array( $this, 'get_current_commenter' ) );
     472
     473                $page     = get_page_of_comment( $new_unapproved, array( 'per_page' => 3 ) );
     474                $comments = get_comments(
     475                        array(
     476                                'number'             => 3,
     477                                'paged'              => $page,
     478                                'post_id'            => $post,
     479                                'status'             => 'approve',
     480                                'include_unapproved' => array( 'foo@bar.test' ),
     481                                'orderby'            => 'comment_date_gmt',
     482                                'order'              => 'ASC',
     483                        )
     484                );
     485
     486                remove_filter( 'wp_get_current_commenter', array( $this, 'get_current_commenter' ) );
     487
     488                $this->assertContains( $new_unapproved, wp_list_pluck( $comments, 'comment_ID' ) );
     489        }
     490
     491        /**
     492         * @ticket 8973
     493         */
     494        public function test_page_number_when_unapproved_comments_are_included_for_current_user() {
     495                $current_user = get_current_user_id();
     496                $post         = self::factory()->post->create();
     497                $user         = self::factory()->user->create_and_get();
     498                $comment_args = array(
     499                        'comment_post_ID'      => $post,
     500                        'comment_approved'     => 0,
     501                        'comment_author_email' => $user->user_email,
     502                        'comment_author'       => $user->display_name,
     503                        'comment_author_url'   => $user->user_url,
     504                        'user_id'              => $user->ID,
     505                );
     506
     507                for ( $i = 1; $i < 4; $i++ ) {
     508                        self::factory()->comment->create(
     509                                array_merge(
     510                                        $comment_args,
     511                                        array(
     512                                                'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', time() - ( $i * 1000 ) ),
     513                                        )
     514                                )
     515                        );
     516                }
     517
     518                $new_unapproved = self::factory()->comment->create(
     519                        $comment_args
     520                );
     521
     522                wp_set_current_user( $user->ID );
     523
     524                $page     = get_page_of_comment( $new_unapproved, array( 'per_page' => 3 ) );
     525                $comments = get_comments(
     526                        array(
     527                                'number'             => 3,
     528                                'paged'              => $page,
     529                                'post_id'            => $post,
     530                                'status'             => 'approve',
     531                                'include_unapproved' => array( $user->ID ),
     532                                'orderby'            => 'comment_date_gmt',
     533                                'order'              => 'ASC',
     534                        )
     535                );
     536
     537                $this->assertContains( $new_unapproved, wp_list_pluck( $comments, 'comment_ID' ) );
     538
     539                wp_set_current_user( $current_user );
     540        }
     541
     542        public function get_current_commenter() {
     543                return array(
     544                        'comment_author_email' => 'foo@bar.test',
     545                        'comment_author'       => 'Foo',
     546                        'comment_author_url'   => 'https://bar.test',
     547                );
     548        }
    442549}
Note: See TracChangeset for help on using the changeset viewer.