Make WordPress Core

Changeset 51118

Timestamp:
06/08/2021 10:49:27 PM (3 years ago)
Author:
whyisjake
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r50132 r51118  
    2828 *
    2929 * @since 2.3.0
     30
    3031 *
    3132 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
     
    505506                    || 'newest' !== $default_comments_page && $cpage > 1 )
    506507            ) {
    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
     511                // Get the total number of pages for comments.
     512                $comments_total_pages = ceil( ( $wp_query->post->comment_count ) / $per_page );
     513                if ( $cpage <= $comments_total_pages ) {
     514                    $addl_path  = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' );
     515                    $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' );
     516                }
    509517
    510518                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
  • trunk/tests/phpunit/tests/link/wpGetCanonicalUrl.php

    r50452 r51118  
    153153
    154154    /**
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
    155176     * Filter callback for testing of filter usage.
    156177     *
Note: See TracChangeset for help on using the changeset viewer.