Make WordPress Core

Changeset 47760


Ignore:
Timestamp:
05/04/2020 10:40:06 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Canonical: Redirect paged requests for a static page assigned as the "Posts page".

This avoids displaying duplicate content of the home page under different URLs with appended page numbers.

This change only affects the <!--nextpage--> pagination (page query variable) and not the regular multiple posts pagination (paged query variable).

The posts page does not support the <!--nextpage--> pagination, so requests for invalid page numbers should be redirected to the page permalink, applying the logic previously implemented for single posts or pages.

Follow-up to [34492], [47727].

Props jeremyfelt, sachit.tandukar, SergeyBiryukov.
Fixes #45337. See #40773, #28081, #11694.

Location:
trunk
Files:
4 edited

Legend:

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

    r47759 r47760  
    187187        }
    188188
     189        // Strip off non-existing <!--nextpage--> links from single posts or pages.
     190        if ( get_query_var( 'page' ) ) {
     191            $post_id = 0;
     192
     193            if ( $wp_query->queried_object instanceof WP_Post ) {
     194                $post_id = $wp_query->queried_object->ID;
     195            } elseif ( $wp_query->post ) {
     196                $post_id = $wp_query->post->ID;
     197            }
     198
     199            $redirect_url = get_permalink( $post_id );
     200
     201            $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
     202            $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
     203        }
     204
    189205        if ( ! $redirect_url ) {
    190206            $redirect_url = redirect_guess_404_permalink();
     
    197213                );
    198214            }
    199         }
    200 
    201         // Strip off non-existing page links from single posts or pages.
    202         if ( get_query_var( 'page' ) && $wp_query->post ) {
    203             $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
    204             $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    205 
    206             $redirect_url = get_permalink( $wp_query->post->ID );
    207215        }
    208216    } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
  • trunk/src/wp-includes/class-wp.php

    r47738 r47760  
    671671            $content_found = true;
    672672
    673             $post = isset( $wp_query->post ) ? $wp_query->post : null;
    674 
    675             // Only set X-Pingback for single posts that allow pings.
    676             if ( is_singular() && $post && pings_open( $post ) && ! headers_sent() ) {
    677                 header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
    678             }
    679 
    680             // Check for paged content that exceeds the max number of pages.
    681673            if ( is_singular() ) {
     674                $post = isset( $wp_query->post ) ? $wp_query->post : null;
     675
     676                // Only set X-Pingback for single posts that allow pings.
     677                if ( $post && pings_open( $post ) && ! headers_sent() ) {
     678                    header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
     679                }
     680
     681                // Check for paged content that exceeds the max number of pages.
    682682                $next = '<!--nextpage-->';
    683683                if ( $post && ! empty( $this->query_vars['page'] ) ) {
     
    692692            }
    693693
     694            // The posts page does not support the <!--nextpage--> pagination.
     695            if ( $wp_query->is_posts_page && ! empty( $this->query_vars['page'] ) ) {
     696                $content_found = false;
     697            }
     698
    694699            if ( $content_found ) {
    695700                $set_404 = false;
  • trunk/tests/phpunit/tests/canonical/pageOnFront.php

    r47122 r47760  
    5959            // The page designated as the front page should redirect to the front of the site.
    6060            array( '/front-page/', '/' ),
     61            // The front page supports the <!--nextpage--> pagination.
    6162            array( '/front-page/2/', '/page/2/' ),
    6263            array( '/front-page/?page=2', '/page/2/' ),
     64            // The posts page does not support the <!--nextpage--> pagination.
     65            array( '/blog-page/2/', '/blog-page/' ),
     66            array( '/blog-page/?page=2', '/blog-page/' ),
     67            // The posts page supports regular pagination.
    6368            array( '/blog-page/?paged=2', '/blog-page/page/2/' ),
    6469        );
  • trunk/tests/phpunit/tests/canonical/paged.php

    r46586 r47760  
    77class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase {
    88
    9     function test_nextpage() {
     9    function test_redirect_canonical_with_nextpage_pagination() {
    1010        $para = 'This is a paragraph.
    1111            This is a paragraph.
     
    2020        );
    2121
    22         $link  = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
    23         $paged = $link . '4/';
     22        $link = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
    2423
    25         $this->assertCanonical( $paged, $link );
     24        // Existing page should be displayed as is.
     25        $this->assertCanonical( $link . '3/', $link . '3/' );
     26        // Non-existing page should redirect to the permalink.
     27        $this->assertCanonical( $link . '4/', $link );
    2628    }
     29
    2730}
Note: See TracChangeset for help on using the changeset viewer.