WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/01/2015 05:12:39 AM (4 years ago)
Author:
boonebgorges
Message:

Ensure that comment permalinks reflect pagination.

After [34561], wp_list_comments() no longer passed all of a post's comments
to Walker_Comments. As a result, calls to get_comment_link() occurring
inside the comment loop had insufficient context to determine the proper
'cpage' value to use when generating comment permalinks. This, in turn, caused
comment permalinks to behave erratically.

The current changeset addresses the problem as follows:

  • get_comment_link() now accepts a 'cpage' parameter. When present, 'cpage' will be used to build the comment permalink - no automatic calculation will take place.
  • When called within the main loop, wp_list_comments() calculates the proper 'cpage' value for comments in the loop, and passes it down to get_comment_link().
  • cpage and comment-page-x query vars are generally required in comment permalinks (see #34068), but an exception is made when 'default_comment_page=oldest': the bare post permalink will always be the same as cpage=1, so cpage is excluded in this case.

Props peterwilsoncc for assiduous spreadsheeting.
Fixes #34073.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/commentsTemplate.php

    r34729 r34735  
    410410        $this->assertSame( array( $comment_1 ), $found_cids );
    411411    }
     412
     413    /**
     414     * @ticket 34073
     415     */
     416    public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_oldest() {
     417        $now = time();
     418        $p = $this->factory->post->create();
     419        $comment_1 = $this->factory->comment->create( array(
     420            'comment_post_ID' => $p,
     421            'comment_content' => '1',
     422            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
     423        ) );
     424        $comment_2 = $this->factory->comment->create( array(
     425            'comment_post_ID' => $p,
     426            'comment_content' => '2',
     427            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
     428        ) );
     429        $comment_3 = $this->factory->comment->create( array(
     430            'comment_post_ID' => $p,
     431            'comment_content' => '3',
     432            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
     433        ) );
     434        $comment_4 = $this->factory->comment->create( array(
     435            'comment_post_ID' => $p,
     436            'comment_content' => '4',
     437            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
     438        ) );
     439
     440        update_option( 'comment_order', 'desc' );
     441        update_option( 'default_comments_page', 'oldest' );
     442
     443        $link_p1 = add_query_arg( array(
     444            'comments_per_page' => 2,
     445        ), get_permalink( $p ) );
     446
     447        $this->go_to( $link_p1 );
     448
     449        $found_p1 = get_echo( 'comments_template' );
     450
     451        // Find the comment permalinks.
     452        preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
     453
     454        // This is the main post page, so we don't expect any cpage param.
     455        foreach ( $matches[1] as $m ) {
     456            $this->assertNotContains( 'cpage', $m );
     457        }
     458
     459        $link_p2 = add_query_arg( array(
     460            'cpage' => 2,
     461            'comments_per_page' => 2,
     462        ), get_permalink( $p ) );
     463
     464        $this->go_to( $link_p2 );
     465
     466        $found_p2 = get_echo( 'comments_template' );
     467
     468        // Find the comment permalinks.
     469        preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
     470
     471        // They should all be on page 2.
     472        foreach ( $matches[1] as $m ) {
     473            $this->assertContains( 'cpage=2', $m );
     474        }
     475    }
     476
     477    /**
     478     * @ticket 34073
     479     */
     480    public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_newest() {
     481        $now = time();
     482        $p = $this->factory->post->create();
     483        $comment_1 = $this->factory->comment->create( array(
     484            'comment_post_ID' => $p,
     485            'comment_content' => '1',
     486            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
     487        ) );
     488        $comment_2 = $this->factory->comment->create( array(
     489            'comment_post_ID' => $p,
     490            'comment_content' => '2',
     491            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
     492        ) );
     493        $comment_3 = $this->factory->comment->create( array(
     494            'comment_post_ID' => $p,
     495            'comment_content' => '3',
     496            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
     497        ) );
     498        $comment_4 = $this->factory->comment->create( array(
     499            'comment_post_ID' => $p,
     500            'comment_content' => '4',
     501            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ),
     502        ) );
     503        $comment_5 = $this->factory->comment->create( array(
     504            'comment_post_ID' => $p,
     505            'comment_content' => '4',
     506            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ),
     507        ) );
     508        $comment_6 = $this->factory->comment->create( array(
     509            'comment_post_ID' => $p,
     510            'comment_content' => '4',
     511            'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ),
     512        ) );
     513
     514        update_option( 'comment_order', 'desc' );
     515        update_option( 'default_comments_page', 'newest' );
     516
     517        $link_p0 = add_query_arg( array(
     518            'comments_per_page' => 2,
     519        ), get_permalink( $p ) );
     520
     521        $this->go_to( $link_p0 );
     522
     523        $found_p0 = get_echo( 'comments_template' );
     524
     525        // Find the comment permalinks.
     526        preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p0, $matches );
     527
     528        foreach ( $matches[1] as $m ) {
     529            $this->assertContains( 'cpage=3', $m );
     530        }
     531
     532        $link_p2 = add_query_arg( array(
     533            'cpage' => 2,
     534            'comments_per_page' => 2,
     535        ), get_permalink( $p ) );
     536
     537        $this->go_to( $link_p2 );
     538
     539        $found_p2 = get_echo( 'comments_template' );
     540
     541        // Find the comment permalinks.
     542        preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
     543
     544        // They should all be on page 2.
     545        foreach ( $matches[1] as $m ) {
     546            $this->assertContains( 'cpage=2', $m );
     547        }
     548
     549        // p1 is the last page (neat!).
     550        $link_p1 = add_query_arg( array(
     551            'cpage' => 1,
     552            'comments_per_page' => 2,
     553        ), get_permalink( $p ) );
     554
     555        $this->go_to( $link_p1 );
     556
     557        $found_p1 = get_echo( 'comments_template' );
     558
     559        // Find the comment permalinks.
     560        preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
     561
     562        // They should all be on page 2.
     563        foreach ( $matches[1] as $m ) {
     564            $this->assertContains( 'cpage=1', $m );
     565        }
     566    }
    412567}
Note: See TracChangeset for help on using the changeset viewer.