Make WordPress Core

Changeset 36041


Ignore:
Timestamp:
12/21/2015 03:10:45 AM (11 years ago)
Author:
boonebgorges
Message:

Respect approval status when determining comment page count in comments_template().

Since 4.4, when fetching the first page of comments and the 'newest' comments
are set to display first, comments_template() must perform arithmetic to
determine which comments to show. See #8071. This arithmetic requires the
total comment count for the current post, which is calculated with a separate
WP_Comment_Query. This secondary comment query did not properly account for
non-approved comment statuses; all unapproved comments should be part of the
comment count for admins, and individual users should have their own
unapproved comments included in the count. As a result, comments_template()
was, in some cases, being fooled into thinking that a post had fewer comments
available for pagination than it actually had, which resulted in empty pages
of comments.

We correct this problem by mirroring 'status' and 'include_unapproved' params
of the main comment query within the secondary query used to calculate pagination.

Merges [36040] to the 4.4 branch.

Fixes #35068.

Location:
branches/4.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/comment-template.php

    r35934 r36041  
    13141314                        // If fetching the first page of 'newest', we need a top-level comment count.
    13151315                        $top_level_query = new WP_Comment_Query();
    1316                         $top_level_count = $top_level_query->query( array(
     1316                        $top_level_args  = array(
    13171317                                'count'   => true,
    13181318                                'orderby' => false,
    13191319                                'post_id' => $post->ID,
    13201320                                'parent'  => 0,
    1321                         ) );
     1321                                'status'  => 'approve',
     1322                        );
     1323
     1324                        if ( isset( $comment_args['include_unapproved'] ) ) {
     1325                                $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
     1326                        }
     1327
     1328                        $top_level_count = $top_level_query->query( $top_level_args );
    13221329
    13231330                        $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
  • branches/4.4/tests/phpunit/tests/comment/commentsTemplate.php

    r35331 r36041  
    574574                }
    575575        }
     576
     577        /**
     578         * @ticket 35068
     579         */
     580        public function test_query_offset_should_not_include_unapproved_comments() {
     581                $now = time();
     582                $p = self::factory()->post->create();
     583                $comment_1 = self::factory()->comment->create( array(
     584                        'comment_post_ID' => $p,
     585                        'comment_content' => '1',
     586                        'comment_approved' => '0',
     587                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
     588                ) );
     589                $comment_2 = self::factory()->comment->create( array(
     590                        'comment_post_ID' => $p,
     591                        'comment_content' => '2',
     592                        'comment_approved' => '0',
     593                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
     594                ) );
     595                $comment_3 = self::factory()->comment->create( array(
     596                        'comment_post_ID' => $p,
     597                        'comment_content' => '3',
     598                        'comment_approved' => '0',
     599                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
     600                ) );
     601                $comment_4 = self::factory()->comment->create( array(
     602                        'comment_post_ID' => $p,
     603                        'comment_content' => '4',
     604                        'comment_approved' => '1',
     605                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
     606                ) );
     607
     608                update_option( 'comment_order', 'asc' );
     609                update_option( 'default_comments_page', 'newest' );
     610                update_option( 'page_comments', 1 );
     611                update_option( 'comments_per_page', 2 );
     612
     613                $this->go_to( get_permalink( $p ) );
     614                $found = get_echo( 'comments_template' );
     615
     616                // Find the found comments in the markup.
     617                preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
     618
     619                $found_cids = array_map( 'intval', $matches[1] );
     620                $this->assertSame( array( $comment_4 ), $found_cids );
     621        }
     622
     623        /**
     624         * @ticket 35068
     625         */
     626        public function test_query_offset_should_include_unapproved_comments() {
     627                $comment_author_email = 'foo@example.com';
     628
     629                $now = time();
     630                $p = self::factory()->post->create();
     631                $comment_1 = self::factory()->comment->create( array(
     632                        'comment_post_ID' => $p,
     633                        'comment_content' => '1',
     634                        'comment_approved' => '0',
     635                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
     636                ) );
     637                $comment_2 = self::factory()->comment->create( array(
     638                        'comment_post_ID' => $p,
     639                        'comment_content' => '2',
     640                        'comment_approved' => '0',
     641                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
     642                ) );
     643                $comment_3 = self::factory()->comment->create( array(
     644                        'comment_post_ID' => $p,
     645                        'comment_content' => '3',
     646                        'comment_approved' => '0',
     647                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
     648                        'comment_author_email' => $comment_author_email,
     649                ) );
     650                $comment_4 = self::factory()->comment->create( array(
     651                        'comment_post_ID' => $p,
     652                        'comment_content' => '4',
     653                        'comment_approved' => '0',
     654                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
     655                        'comment_author_email' => $comment_author_email,
     656                ) );
     657                $comment_5 = self::factory()->comment->create( array(
     658                        'comment_post_ID' => $p,
     659                        'comment_content' => '5',
     660                        'comment_approved' => '0',
     661                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
     662                        'comment_author_email' => $comment_author_email,
     663                ) );
     664                $comment_6 = self::factory()->comment->create( array(
     665                        'comment_post_ID' => $p,
     666                        'comment_content' => '6',
     667                        'comment_approved' => '1',
     668                        'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
     669                ) );
     670
     671                update_option( 'comment_order', 'asc' );
     672                update_option( 'default_comments_page', 'newest' );
     673                update_option( 'page_comments', 1 );
     674                update_option( 'comments_per_page', 2 );
     675
     676                add_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) );
     677                $this->go_to( get_permalink( $p ) );
     678                $found = get_echo( 'comments_template' );
     679                remove_filter( 'wp_get_current_commenter', array( $this, 'fake_current_commenter' ) );
     680
     681                // Find the found comments in the markup.
     682                preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
     683
     684                $found_cids = array_map( 'intval', $matches[1] );
     685                $this->assertSame( array( $comment_4, $comment_3 ), $found_cids );
     686        }
     687
     688        public function fake_current_commenter( $commenter ) {
     689                $commenter['comment_author_email'] = 'foo@example.com';
     690                return $commenter;
     691        }
    576692}
Note: See TracChangeset for help on using the changeset viewer.