Make WordPress Core

Changeset 37655


Ignore:
Timestamp:
06/08/2016 04:00:18 AM (9 years ago)
Author:
boonebgorges
Message:

Comments: In wp_list_comments(), queries with custom pagination params should obey default comment_status logic.

When custom pagination parameters are passed to wp_list_comments(), a
secondary query must be performed to fetch the proper comments. See [36157].
This query should show comments of the same comment_status as the default
query initialized in comments_template(): show only comments that are
approved, or those that are unapproved but belong to the current user.

Props smerriman.
Fixes #37048.

Location:
trunk
Files:
2 edited

Legend:

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

    r37542 r37655  
    19611961            $current_per_page = get_query_var( 'comments_per_page' );
    19621962            if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
    1963 
    1964                 $comments = get_comments( array(
     1963                $comment_args = array(
    19651964                    'post_id' => get_the_ID(),
    19661965                    'orderby' => 'comment_date_gmt',
    19671966                    'order' => 'ASC',
    1968                     'status' => 'all',
    1969                 ) );
     1967                    'status' => 'approve',
     1968                );
     1969
     1970                if ( is_user_logged_in() ) {
     1971                    $comment_args['include_unapproved'] = get_current_user_id();
     1972                } else {
     1973                    $commenter = wp_get_current_commenter();
     1974                    if ( $commenter['comment_author_email'] ) {
     1975                        $comment_args['include_unapproved'] = $commenter['comment_author_email'];
     1976                    }
     1977                }
     1978
     1979                $comments = get_comments( $comment_args );
    19701980
    19711981                if ( 'all' != $r['type'] ) {
  • trunk/tests/phpunit/tests/comment/wpListComments.php

    r36276 r37655  
    147147        $this->assertSame( array( $comments[3] ), array_map( 'intval', $matches[1] ) );
    148148    }
     149
     150    /**
     151     * @ticket 37048
     152     */
     153    public function test_custom_pagination_should_not_result_in_unapproved_comments_being_shown() {
     154        $p = self::factory()->post->create();
     155
     156        $comments = array();
     157        $now = time();
     158        for ( $i = 0; $i <= 5; $i++ ) {
     159            $comments[] = self::factory()->comment->create( array(
     160                'comment_post_ID' => $p,
     161                'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
     162                'comment_author' => 'Commenter ' . $i,
     163            ) );
     164        }
     165
     166        // Only 2 and 5 are approved.
     167        wp_set_comment_status( $comments[0], '0' );
     168        wp_set_comment_status( $comments[1], '0' );
     169        wp_set_comment_status( $comments[3], '0' );
     170        wp_set_comment_status( $comments[4], '0' );
     171
     172        update_option( 'page_comments', true );
     173        update_option( 'comments_per_page', 2 );
     174
     175        $this->go_to( get_permalink( $p ) );
     176
     177        // comments_template() populates $wp_query->comments
     178        get_echo( 'comments_template' );
     179
     180        $found = wp_list_comments( array(
     181            'echo' => false,
     182            'per_page' => 1,
     183            'page' => 2,
     184        ) );
     185
     186        preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
     187        $this->assertSame( array( $comments[2] ), array_map( 'intval', $matches[1] ) );
     188    }
     189
     190    /**
     191     * @ticket 37048
     192     */
     193    public function test_custom_pagination_should_allow_ones_own_unapproved_comments() {
     194        $p = self::factory()->post->create();
     195        $u = self::factory()->user->create();
     196
     197        $comments = array();
     198        $now = time();
     199        for ( $i = 0; $i <= 5; $i++ ) {
     200            $comments[] = self::factory()->comment->create( array(
     201                'comment_post_ID' => $p,
     202                'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
     203                'comment_author' => 'Commenter ' . $i,
     204                'user_id' => $u,
     205            ) );
     206        }
     207
     208        // Only 2 and 5 are approved.
     209        wp_set_comment_status( $comments[0], '0' );
     210        wp_set_comment_status( $comments[1], '0' );
     211        wp_set_comment_status( $comments[3], '0' );
     212        wp_set_comment_status( $comments[4], '0' );
     213
     214        update_option( 'page_comments', true );
     215        update_option( 'comments_per_page', 2 );
     216
     217        wp_set_current_user( $u );
     218
     219        $this->go_to( get_permalink( $p ) );
     220
     221        // comments_template() populates $wp_query->comments
     222        get_echo( 'comments_template' );
     223
     224        $found = wp_list_comments( array(
     225            'echo' => false,
     226            'per_page' => 1,
     227            'page' => 2,
     228        ) );
     229
     230        preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
     231        $this->assertSame( array( $comments[4] ), array_map( 'intval', $matches[1] ) );
     232    }
    149233}
Note: See TracChangeset for help on using the changeset viewer.