From b672d77e873f78f37f89e8549adbff2c1d95f03b Mon Sep 17 00:00:00 2001 From: whyisjake Date: Tue, 8 Jun 2021 22:51:01 +0000 Subject: [PATCH] Permalinks: Limit pagination for posts with comments. Additionally, redirect pages back to the source page if comments don't exist. Props devrekli, carike, sumanm, mukesh27, chaion07, audrasjb, whyisjake, SergeyBiryukov. Fixes #50233. Built from https://develop.svn.wordpress.org/trunk@51118 git-svn-id: http://core.svn.wordpress.org/trunk@50727 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/canonical.php | 12 ++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php index 184fb756e8..0eba17d60d 100644 --- a/wp-includes/canonical.php +++ b/wp-includes/canonical.php @@ -27,6 +27,7 @@ * or query in an attempt to figure the correct page to go to. * * @since 2.3.0 + * @since 5.8.0 Checks comment page count to limit pagination. * * @global WP_Rewrite $wp_rewrite WordPress rewrite component. * @global bool $is_IIS @@ -504,8 +505,15 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { && ( 'newest' === $default_comments_page && $cpage > 0 || 'newest' !== $default_comments_page && $cpage > 1 ) ) { - $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); - $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); + // Checks comment page count to limit pagination. + $per_page = get_option( 'comments_per_page' ); + + // Get the total number of pages for comments. + $comments_total_pages = ceil( ( $wp_query->post->comment_count ) / $per_page ); + if ( $cpage <= $comments_total_pages ) { + $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); + $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); + } $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 666012fd72..d3b8ebf284 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-51117'; +$wp_version = '5.8-alpha-51118'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.