WordPress.org

Make WordPress Core

Ticket #11248: 11248.2.patch

File 11248.2.patch, 1.9 KB (added by tyxla, 5 years ago)

In redirect_canonical() redirect comment pages that are larger than the maximum comment page to the post itself.

  • src/wp-includes/canonical.php

     
    319319                        }
    320320
    321321                        if ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) {
    322                                 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
    323                                 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
     322                                if ( $wp_query->post ) {
     323                                        $total_comment_pages = get_comment_pages_count( get_comments( 'post_id=' . $wp_query->post->ID ) );
     324                                        if ( get_query_var('cpage') <= $total_comment_pages ) {
     325                                                $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
     326                                                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
     327                                        }
     328                                }
    324329                        }
    325330
    326331                        $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
  • tests/phpunit/tests/canonical.php

     
    102102
    103103                        // Comments
    104104                        array( '/2008/03/03/comment-test/?cpage=2', '/2008/03/03/comment-test/comment-page-2/' ),
     105                        array( '/2008/03/03/comment-test/comment-page-4/', '/2008/03/03/comment-test/' ),
    105106
    106107                        // Attachments
    107108                        array( '/?attachment_id=611', '/2008/06/10/post-format-test-gallery/canola2/' ),