Ticket #34073: 34073.2.diff
File 34073.2.diff, 14.7 KB (added by , 9 years ago) |
---|
-
src/wp-includes/comment-functions.php
diff --git src/wp-includes/comment-functions.php src/wp-includes/comment-functions.php index 453b5a8..58c803e 100644
function get_page_of_comment( $comment_ID, $args = array() ) { 872 872 } 873 873 874 874 // Find this comment's top level parent if threading is enabled 875 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) 875 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) { 876 876 return get_page_of_comment( $comment->comment_parent, $args ); 877 } 877 878 878 879 $comment_args = array( 879 880 'type' => $args['type'], -
src/wp-includes/comment-template.php
diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php index bad4a14..72d08d7 100644
function comment_ID() { 659 659 * Retrieve the link to a given comment. 660 660 * 661 661 * @since 1.5.0 662 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. 662 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument. 663 663 * 664 664 * @see get_page_of_comment() 665 665 * … … function comment_ID() { 670 670 * @param array $args { 671 671 * An array of optional arguments to override the defaults. 672 672 * 673 * @type string $type Passed to {@see get_page_of_comment()}. 674 * @type int $page Current page of comments, for calculating comment pagination. 675 * @type int $per_page Per-page value for comment pagination. 676 * @type int $max_depth Passed to {@see get_page_of_comment()}. 673 * @type string $type Passed to {@see get_page_of_comment()}. 674 * @type int $page Current page of comments, for calculating comment pagination. 675 * @type int $per_page Per-page value for comment pagination. 676 * @type int $max_depth Passed to {@see get_page_of_comment()}. 677 * @type int|string $cpage Value to use for the comment's "comment-page" or "cpage" value. If provided, this 678 * value overrides any value calculated from `$page` and `$per_page`. 677 679 * } 678 680 * @return string The permalink to the given comment. 679 681 */ … … function get_comment_link( $comment = null, $args = array() ) { 687 689 $args = array( 'page' => $args ); 688 690 } 689 691 690 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' ); 692 $defaults = array( 693 'type' => 'all', 694 'page' => '', 695 'per_page' => '', 696 'max_depth' => '', 697 'cpage' => null, 698 ); 691 699 $args = wp_parse_args( $args, $defaults ); 692 700 693 if ( '' === $args['per_page'] ) 694 $args['per_page'] = get_option('comments_per_page'); 701 $link = get_permalink( $comment->comment_post_ID ); 702 703 // The 'cpage' param takes precedence. 704 if ( ! is_null( $args['cpage'] ) ) { 705 $cpage = $args['cpage']; 706 707 // No 'cpage' is provided, so we calculate one. 708 } else { 709 if ( '' === $args['per_page'] ) 710 $args['per_page'] = get_option('comments_per_page'); 711 712 if ( empty($args['per_page']) ) { 713 $args['per_page'] = 0; 714 $args['page'] = 0; 715 } 695 716 696 if ( empty($args['per_page']) ) { 697 $args['per_page'] = 0; 698 $args['page'] = 0; 717 $cpage = $args['page']; 718 719 if ( '' == $cpage ) { 720 if ( ! empty( $in_comment_loop ) ) { 721 $cpage = get_query_var( 'cpage' ); 722 } else { 723 $cpage = get_page_of_comment( $comment->comment_ID, $args ); 724 } 725 } 726 727 // Drop the 'page' var if we're on the default page. 728 $comment_post = get_post( $comment->comment_post_ID ); 729 if ( $args['per_page'] ) { 730 $total_pages = ceil( $comment_post->comment_count / $args['per_page'] ); 731 } else { 732 $total_pages = 1; 733 } 734 735 /* 736 * If the default page displays the oldest comments, the permalinks for comments on the default page 737 * do not need a 'cpage' query var. 738 */ 739 $default_comments_page = get_option( 'default_comments_page' ); 740 if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) { 741 $cpage = ''; 742 } 699 743 } 700 744 701 if ( $args['per_page'] ) { 702 if ( '' == $args['page'] ) 703 $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args ); 745 if ( $cpage ) { 746 if ( $wp_rewrite->using_permalinks() ) { 747 if ( $cpage ) { 748 $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage; 749 } 704 750 705 if ( $wp_rewrite->using_permalinks() ) 706 $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . $wp_rewrite->comments_pagination_base . '-' . $args['page'], 'comment' ); 707 else 708 $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) ); 709 } else { 710 $link = get_permalink( $comment->comment_post_ID ); 751 $link = user_trailingslashit( $link, 'comment' ); 752 } elseif ( $cpage ) { 753 $link = add_query_arg( 'cpage', $cpage, $link ); 754 } 755 756 } 757 758 if ( $wp_rewrite->using_permalinks() ) { 759 $link = user_trailingslashit( $link, 'comment' ); 711 760 } 712 761 713 762 $link = $link . '#comment-' . $comment->comment_ID; 763 714 764 /** 715 765 * Filter the returned single comment permalink. 716 766 * 717 767 * @since 2.8.0 768 * @since 4.4.0 Added the `$cpage` parameter. 718 769 * 719 770 * @see get_page_of_comment() 720 771 * 721 772 * @param string $link The comment permalink with '#comment-$id' appended. 722 773 * @param WP_Comment $comment The current comment object. 723 774 * @param array $args An array of arguments to override the defaults. 775 * @param int $cpage The calculated 'cpage' value. 724 776 */ 725 return apply_filters( 'get_comment_link', $link, $comment, $args );777 return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage ); 726 778 } 727 779 728 780 /** … … function wp_list_comments( $args = array(), $comments = null ) { 1921 1973 } 1922 1974 1923 1975 // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother. 1924 if ( 1 < $wp_query->max_num_comment_pages ) { 1925 $r['page'] = 1; 1976 if ( $wp_query->max_num_comment_pages ) { 1977 $default_comments_page = get_option( 'default_comments_page' ); 1978 $cpage = get_query_var( 'cpage' ); 1979 if ( 'newest' === $default_comments_page ) { 1980 $r['cpage'] = $cpage; 1981 } else { 1982 $r['cpage'] = ( $cpage == 1 ) ? '' : $cpage; 1983 } 1984 1985 $r['page'] = 0; 1986 $r['per_page'] = 0; 1926 1987 } 1927 1988 } 1928 1989 -
tests/phpunit/tests/comment/commentsTemplate.php
diff --git tests/phpunit/tests/comment/commentsTemplate.php tests/phpunit/tests/comment/commentsTemplate.php index f86f365..5319989 100644
class Tests_Comment_CommentsTemplate extends WP_UnitTestCase { 325 325 $found_cids = array_map( 'intval', $matches[1] ); 326 326 $this->assertSame( array( $comment_1, $comment_2 ), $found_cids ); 327 327 } 328 329 /** 330 * @ticket 34073 331 */ 332 public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_oldest() { 333 $now = time(); 334 $p = $this->factory->post->create(); 335 $comment_1 = $this->factory->comment->create( array( 336 'comment_post_ID' => $p, 337 'comment_content' => '1', 338 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), 339 ) ); 340 $comment_2 = $this->factory->comment->create( array( 341 'comment_post_ID' => $p, 342 'comment_content' => '2', 343 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), 344 ) ); 345 $comment_3 = $this->factory->comment->create( array( 346 'comment_post_ID' => $p, 347 'comment_content' => '3', 348 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), 349 ) ); 350 $comment_4 = $this->factory->comment->create( array( 351 'comment_post_ID' => $p, 352 'comment_content' => '4', 353 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), 354 ) ); 355 356 update_option( 'comment_order', 'desc' ); 357 update_option( 'default_comments_page', 'oldest' ); 358 359 $link_p1 = add_query_arg( array( 360 'comments_per_page' => 2, 361 ), get_permalink( $p ) ); 362 363 $this->go_to( $link_p1 ); 364 365 $found_p1 = get_echo( 'comments_template' ); 366 367 // Find the comment permalinks. 368 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches ); 369 370 // This is the main post page, so we don't expect any cpage param. 371 foreach ( $matches[1] as $m ) { 372 $this->assertNotContains( 'cpage', $m ); 373 } 374 375 $link_p2 = add_query_arg( array( 376 'cpage' => 2, 377 'comments_per_page' => 2, 378 ), get_permalink( $p ) ); 379 380 $this->go_to( $link_p2 ); 381 382 $found_p2 = get_echo( 'comments_template' ); 383 384 // Find the comment permalinks. 385 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches ); 386 387 // They should all be on page 2. 388 foreach ( $matches[1] as $m ) { 389 $this->assertContains( 'cpage=2', $m ); 390 } 391 } 392 393 /** 394 * @ticket 34073 395 */ 396 public function test_comment_permalinks_should_be_correct_when_using_default_display_callback_with_default_comment_page_newest() { 397 $now = time(); 398 $p = $this->factory->post->create(); 399 $comment_1 = $this->factory->comment->create( array( 400 'comment_post_ID' => $p, 401 'comment_content' => '1', 402 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), 403 ) ); 404 $comment_2 = $this->factory->comment->create( array( 405 'comment_post_ID' => $p, 406 'comment_content' => '2', 407 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), 408 ) ); 409 $comment_3 = $this->factory->comment->create( array( 410 'comment_post_ID' => $p, 411 'comment_content' => '3', 412 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), 413 ) ); 414 $comment_4 = $this->factory->comment->create( array( 415 'comment_post_ID' => $p, 416 'comment_content' => '4', 417 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), 418 ) ); 419 $comment_5 = $this->factory->comment->create( array( 420 'comment_post_ID' => $p, 421 'comment_content' => '4', 422 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ), 423 ) ); 424 $comment_6 = $this->factory->comment->create( array( 425 'comment_post_ID' => $p, 426 'comment_content' => '4', 427 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ), 428 ) ); 429 430 update_option( 'comment_order', 'desc' ); 431 update_option( 'default_comments_page', 'newest' ); 432 433 $link_p0 = add_query_arg( array( 434 'comments_per_page' => 2, 435 ), get_permalink( $p ) ); 436 437 $this->go_to( $link_p0 ); 438 439 $found_p0 = get_echo( 'comments_template' ); 440 441 // Find the comment permalinks. 442 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p0, $matches ); 443 444 foreach ( $matches[1] as $m ) { 445 $this->assertContains( 'cpage=3', $m ); 446 } 447 448 $link_p2 = add_query_arg( array( 449 'cpage' => 2, 450 'comments_per_page' => 2, 451 ), get_permalink( $p ) ); 452 453 $this->go_to( $link_p2 ); 454 455 $found_p2 = get_echo( 'comments_template' ); 456 457 // Find the comment permalinks. 458 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches ); 459 460 // They should all be on page 2. 461 foreach ( $matches[1] as $m ) { 462 $this->assertContains( 'cpage=2', $m ); 463 } 464 465 // p1 is the last page (neat!). 466 $link_p1 = add_query_arg( array( 467 'cpage' => 1, 468 'comments_per_page' => 2, 469 ), get_permalink( $p ) ); 470 471 $this->go_to( $link_p1 ); 472 473 $found_p1 = get_echo( 'comments_template' ); 474 475 // Find the comment permalinks. 476 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches ); 477 478 // They should all be on page 2. 479 foreach ( $matches[1] as $m ) { 480 $this->assertContains( 'cpage=1', $m ); 481 } 482 } 328 483 } -
new file tests/phpunit/tests/comment/getCommentLink.php
diff --git tests/phpunit/tests/comment/getCommentLink.php tests/phpunit/tests/comment/getCommentLink.php new file mode 100644 index 0000000..b42cf7e
- + 1 <?php 2 3 /** 4 * @group comment 5 */ 6 class Tests_Comment_GetCommentLink extends WP_UnitTestCase { 7 protected $p; 8 protected $comments = array(); 9 10 public function setUp() { 11 parent::setUp(); 12 13 $now = time(); 14 $this->p = $this->factory->post->create(); 15 $this->comments[] = $this->factory->comment->create( array( 16 'comment_post_ID' => $this->p, 17 'comment_content' => '1', 18 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ), 19 ) ); 20 $this->comments[] = $this->factory->comment->create( array( 21 'comment_post_ID' => $this->p, 22 'comment_content' => '2', 23 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ), 24 ) ); 25 $this->comments[] = $this->factory->comment->create( array( 26 'comment_post_ID' => $this->p, 27 'comment_content' => '3', 28 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ), 29 ) ); 30 $this->comments[] = $this->factory->comment->create( array( 31 'comment_post_ID' => $this->p, 32 'comment_content' => '4', 33 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 400 ), 34 ) ); 35 $this->comments[] = $this->factory->comment->create( array( 36 'comment_post_ID' => $this->p, 37 'comment_content' => '4', 38 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 500 ), 39 ) ); 40 $this->comments[] = $this->factory->comment->create( array( 41 'comment_post_ID' => $this->p, 42 'comment_content' => '4', 43 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 600 ), 44 ) ); 45 } 46 47 /** 48 * @ticket 34068 49 */ 50 public function test_default_comments_page_newest_default_page_should_have_cpage() { 51 update_option( 'default_comments_page', 'newest' ); 52 update_option( 'comments_per_page', 2 ); 53 54 $found = get_comment_link( $this->comments[1] ); 55 56 $this->assertContains( 'cpage=3', $found ); 57 } 58 59 /** 60 * @ticket 34068 61 */ 62 public function test_default_comments_page_newest_middle_page_should_have_cpage() { 63 update_option( 'default_comments_page', 'newest' ); 64 update_option( 'comments_per_page', 2 ); 65 66 $found = get_comment_link( $this->comments[3] ); 67 68 $this->assertContains( 'cpage=2', $found ); 69 } 70 71 /** 72 * @ticket 34068 73 */ 74 public function test_default_comments_page_newest_last_page_should_have_cpage() { 75 update_option( 'default_comments_page', 'newest' ); 76 update_option( 'comments_per_page', 2 ); 77 78 $found = get_comment_link( $this->comments[5] ); 79 80 $this->assertContains( 'cpage=1', $found ); 81 } 82 83 /** 84 * @ticket 34068 85 */ 86 public function test_default_comments_page_oldest_default_page_should_not_have_cpage() { 87 update_option( 'default_comments_page', 'oldest' ); 88 update_option( 'comments_per_page', 2 ); 89 90 $found = get_comment_link( $this->comments[5] ); 91 92 $this->assertNotContains( 'cpage', $found ); 93 } 94 95 /** 96 * @ticket 34068 97 */ 98 public function test_default_comments_page_oldest_middle_page_should_have_cpage() { 99 update_option( 'default_comments_page', 'oldest' ); 100 update_option( 'comments_per_page', 2 ); 101 102 $found = get_comment_link( $this->comments[3] ); 103 104 $this->assertContains( 'cpage=2', $found ); 105 } 106 107 /** 108 * @ticket 34068 109 */ 110 public function test_default_comments_page_oldest_last_page_should_have_cpage() { 111 update_option( 'default_comments_page', 'oldest' ); 112 update_option( 'comments_per_page', 2 ); 113 114 $found = get_comment_link( $this->comments[1] ); 115 116 $this->assertContains( 'cpage=3', $found ); 117 } 118 }