Make WordPress Core

Changeset 59091


Ignore:
Timestamp:
09/25/2024 07:35:20 PM (3 months ago)
Author:
hellofromTonya
Message:

Canonical: Redirect when front page's paginated states not found.

Perform a canonical redirect for an invalid pagination request of a static front page.

When a site has a static front page assigned and that page has a <!--nextpage--> within its content, previously accessing non-existing pages (e.g. example.com/page/3/) did not redirect or return a 404 or 301. This changeset resolves that issue by performing a canonical redirect.

Unit tests are also included for this specific use case and to ensure the fix does not affect a blog listing home page.

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

Props dd32, audrasjb, chaion07, hellofromTonya, joemcgill, lukecarbis, Mte90, mukesh27, peterwilsoncc, rajinsharwar, SergeyBiryukov.
Fixes #50163.
See meta#5184.

Location:
trunk
Files:
2 edited

Legend:

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

    r56395 r59091  
    748748
    749749            if ( is_singular() ) {
    750                 $post = isset( $wp_query->post ) ? $wp_query->post : null;
    751                 $next = '<!--nextpage-->';
     750                $post    = isset( $wp_query->post ) ? $wp_query->post : null;
     751                $next    = '<!--nextpage-->';
     752                $page_qv = is_front_page() ? 'paged' : 'page';
    752753
    753754                // Check for paged content that exceeds the max number of pages.
    754                 if ( $post && ! empty( $this->query_vars['page'] ) ) {
     755                if ( $post && ! empty( $this->query_vars[ $page_qv ] ) ) {
    755756                    // Check if content is actually intended to be paged.
    756757                    if ( str_contains( $post->post_content, $next ) ) {
    757                         $page          = trim( $this->query_vars['page'], '/' );
     758                        $page          = trim( $this->query_vars[ $page_qv ], '/' );
    758759                        $content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
    759760                    } else {
  • trunk/tests/phpunit/tests/canonical/paged.php

    r56547 r59091  
    2727        $this->assertCanonical( $link . '4/', $link );
    2828    }
     29
     30    /**
     31     * Ensures canonical redirects are performed for sites with a static front page.
     32     *
     33     * @ticket 50163
     34     */
     35    public function test_redirect_missing_front_page_pagination_canonical() {
     36        update_option( 'show_on_front', 'page' );
     37
     38        $page_id = self::factory()->post->create(
     39            array(
     40                'post_title'   => 'front-page-1',
     41                'post_type'    => 'page',
     42                'post_content' => "Front Page 1\n<!--nextpage-->\nPage 2",
     43            )
     44        );
     45
     46        update_option( 'page_on_front', $page_id );
     47
     48        $link = parse_url( get_permalink( $page_id ), PHP_URL_PATH );
     49
     50        // Valid page numbers should not redirect.
     51        $this->assertCanonical( $link, $link, 'The home page is not expected to redirect.' );
     52        $this->assertCanonical( $link . 'page/2/', $link . 'page/2/', 'Page 2 exists and is not expected to redirect.' );
     53
     54        // Invalid page numbers should redirect to the front page.
     55        $this->assertCanonical( $link . 'page/3/', $link, 'Page 3 does not exist and is expected to redirect to the home page.' );
     56    }
     57
     58    /**
     59     * Ensures that canonical redirects are not performed for sites with a blog listing home page.
     60     *
     61     * @ticket 50163
     62     */
     63    public function test_redirect_missing_front_page_pagination_does_not_affect_posts_canonical() {
     64        self::factory()->post->create_many( 3 );
     65        update_option( 'posts_per_page', 2 );
     66
     67        // Valid page numbers should not redirect.
     68        $this->assertCanonical( '/', '/', 'Page one of the blog archive should not redirect.' );
     69        $this->assertCanonical( '/page/2/', '/page/2/', 'Page 2 of the blog archive exists and is not expected to redirect.' );
     70
     71        // Neither should invalid page numbers.
     72        $this->assertCanonical( '/page/3/', '/page/3/', 'Page 3 of the blog archive is not populated but is not expected to redirect.' );
     73    }
    2974}
Note: See TracChangeset for help on using the changeset viewer.