diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
index 184fb756e8..fbe7c3e471 100644
a
|
b
|
|
27 | 27 | * or query in an attempt to figure the correct page to go to. |
28 | 28 | * |
29 | 29 | * @since 2.3.0 |
| 30 | * @since 5.8.0 Checks comment page count to limit pagination. |
30 | 31 | * |
31 | 32 | * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
32 | 33 | * @global bool $is_IIS |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
504 | 505 | && ( 'newest' === $default_comments_page && $cpage > 0 |
505 | 506 | || 'newest' !== $default_comments_page && $cpage > 1 ) |
506 | 507 | ) { |
507 | | $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); |
508 | | $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); |
| 508 | // Checks comment page count to limit pagination. |
| 509 | $per_page = get_option( 'comments_per_page' ); |
| 510 | // Get the total number of pages for comments. |
| 511 | $comments_total_pages = ceil( ( $wp_query->post->comment_count ) / $per_page ); |
| 512 | if ( $cpage <= $comments_total_pages ) { |
| 513 | $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); |
| 514 | $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); |
| 515 | } |
509 | 516 | |
510 | 517 | $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
511 | 518 | } |
diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php
index b8a2c30768..bd504884a4 100644
a
|
b
|
function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader |
2934 | 2934 | * |
2935 | 2935 | * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
2936 | 2936 | * |
| 2937 | * @since 5.9.0 |
| 2938 | * |
| 2939 | * @global WP_Query $wp_query WordPress query object. |
| 2940 | * |
2937 | 2941 | * @param int $pagenum Optional. Page number. Default 1. |
2938 | 2942 | * @param int $max_page Optional. The maximum number of comment pages. Default 0. |
2939 | 2943 | * @return string The comments page number link URL. |
2940 | 2944 | */ |
2941 | 2945 | function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { |
2942 | | global $wp_rewrite; |
| 2946 | global $wp_rewrite, $wp_query; |
2943 | 2947 | |
2944 | 2948 | $pagenum = (int) $pagenum; |
2945 | 2949 | |
… |
… |
function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { |
2950 | 2954 | if ( $wp_rewrite->using_permalinks() ) { |
2951 | 2955 | $result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' ); |
2952 | 2956 | } else { |
2953 | | $result = add_query_arg( 'cpage', $pagenum, $result ); |
| 2957 | $cpage = get_query_var( 'cpage' ); |
| 2958 | |
| 2959 | // Checks comment page count to limit pagination. |
| 2960 | $per_page = get_option( 'comments_per_page' ); |
| 2961 | |
| 2962 | // Get the total number of pages for comments. |
| 2963 | $comments_total_pages = ceil( ( $wp_query->post->comment_count ) / $per_page ); |
| 2964 | |
| 2965 | if ( $cpage <= $comments_total_pages ) { |
| 2966 | $result = add_query_arg( 'cpage', $pagenum, $result ); |
| 2967 | } |
2954 | 2968 | } |
2955 | 2969 | } |
2956 | 2970 | } elseif ( $pagenum > 1 ) { |
diff --git a/tests/phpunit/tests/link/wpGetCanonicalUrl.php b/tests/phpunit/tests/link/wpGetCanonicalUrl.php
index 5b7eddaada..3311ecb46e 100644
a
|
b
|
class Tests_Link_wpGetCanonicalUrl extends WP_UnitTestCase { |
151 | 151 | $this->assertSame( $this->canonical_url_filter(), $canonical_url ); |
152 | 152 | } |
153 | 153 | |
| 154 | /** |
| 155 | * Limit pagination for comments. |
| 156 | * |
| 157 | * @ticket 50233 |
| 158 | */ |
| 159 | public function test_comments_limit_paged_with_plain_permalink_structure() { |
| 160 | $cpage = 5; |
| 161 | |
| 162 | $link = add_query_arg( |
| 163 | array( |
| 164 | 'cpage' => $cpage, |
| 165 | 'foo' => 'bar', |
| 166 | ), |
| 167 | get_permalink( self::$post_id ) |
| 168 | ); |
| 169 | |
| 170 | $this->go_to( $link ); |
| 171 | $expected = get_permalink( self::$post_id ) . '#comments'; |
| 172 | $this->assertSame( $expected, wp_get_canonical_url( self::$post_id ) ); |
| 173 | } |
| 174 | |
154 | 175 | /** |
155 | 176 | * Filter callback for testing of filter usage. |
156 | 177 | * |