Ticket #34073: 34073.diff
| File 34073.diff, 10.6 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/comment-template.php
diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php index bad4a14..4fb5c58 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 } 716 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 } 695 734 696 if ( empty($args['per_page']) ) { 697 $args['per_page'] = 0; 698 $args['page'] = 0; 735 $default_comments_page = get_option( 'default_comments_page' ); 736 if ( 737 ( 'newest' === $default_comments_page && $cpage == $total_pages ) || 738 ( 'oldest' === $default_comments_page && $cpage == 1 ) 739 ) { 740 $cpage = ''; 741 } 699 742 } 700 743 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 ); 744 if ( $cpage ) { 745 if ( $wp_rewrite->using_permalinks() ) { 746 if ( $cpage ) { 747 $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage; 748 } 704 749 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 ); 750 $link = user_trailingslashit( $link, 'comment' ); 751 } elseif ( $cpage ) { 752 $link = add_query_arg( 'cpage', $cpage, $link ); 753 } 754 755 } 756 757 if ( $wp_rewrite->using_permalinks() ) { 758 $link = user_trailingslashit( $link, 'comment' ); 711 759 } 712 760 713 761 $link = $link . '#comment-' . $comment->comment_ID; 762 714 763 /** 715 764 * Filter the returned single comment permalink. 716 765 * 717 766 * @since 2.8.0 767 * @since 4.4.0 Added the `$cpage` parameter. 718 768 * 719 769 * @see get_page_of_comment() 720 770 * 721 771 * @param string $link The comment permalink with '#comment-$id' appended. 722 772 * @param WP_Comment $comment The current comment object. 723 773 * @param array $args An array of arguments to override the defaults. 774 * @param int $cpage The calculated 'cpage' value. 724 775 */ 725 return apply_filters( 'get_comment_link', $link, $comment, $args );776 return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage ); 726 777 } 727 778 728 779 /** … … function wp_list_comments( $args = array(), $comments = null ) { 1921 1972 } 1922 1973 1923 1974 // 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; 1975 if ( $wp_query->max_num_comment_pages ) { 1976 $default_comments_page = get_option( 'default_comments_page' ); 1977 $cpage = get_query_var( 'cpage' ); 1978 if ( 'newest' === $default_comments_page ) { 1979 $r['cpage'] = ( $cpage == $wp_query->max_num_comment_pages ) ? '' : $cpage; 1980 } else { 1981 $r['cpage'] = ( $cpage == 1 ) ? '' : $cpage; 1982 } 1983 1984 $r['page'] = 0; 1985 $r['per_page'] = 0; 1926 1986 } 1927 1987 } 1928 1988 -
tests/phpunit/tests/comment/commentsTemplate.php
diff --git tests/phpunit/tests/comment/commentsTemplate.php tests/phpunit/tests/comment/commentsTemplate.php index f86f365..77f6a32 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_post_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_post_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_post_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_post_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_post_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_post_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_post_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_post_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_post_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_post_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 // This is the main post page, so we don't expect any cpage param. 445 foreach ( $matches[1] as $m ) { 446 $this->assertNotContains( 'cpage', $m ); 447 } 448 449 $link_p2 = add_query_arg( array( 450 'cpage' => 2, 451 'comments_per_page' => 2, 452 ), get_permalink( $p ) ); 453 454 $this->go_to( $link_p2 ); 455 456 $found_p2 = get_echo( 'comments_template' ); 457 458 // Find the comment permalinks. 459 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches ); 460 461 // They should all be on page 2. 462 foreach ( $matches[1] as $m ) { 463 $this->assertContains( 'cpage=2', $m ); 464 } 465 466 // p1 is the last page (neat!). 467 $link_p1 = add_query_arg( array( 468 'cpage' => 1, 469 'comments_per_page' => 2, 470 ), get_permalink( $p ) ); 471 472 $this->go_to( $link_p1 ); 473 474 $found_p1 = get_echo( 'comments_template' ); 475 476 // Find the comment permalinks. 477 preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches ); 478 479 // They should all be on page 2. 480 foreach ( $matches[1] as $m ) { 481 $this->assertContains( 'cpage=1', $m ); 482 } 483 } 328 484 }