Skip to content

Commit

Permalink
Canonical: Redirect when front page's paginated states not found.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@59091 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 25, 2024
1 parent b275b38 commit 9e29083
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/wp-includes/class-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,14 +747,15 @@ public function handle_404() {
$content_found = true;

if ( is_singular() ) {
$post = isset( $wp_query->post ) ? $wp_query->post : null;
$next = '<!--nextpage-->';
$post = isset( $wp_query->post ) ? $wp_query->post : null;
$next = '<!--nextpage-->';
$page_qv = is_front_page() ? 'paged' : 'page';

// Check for paged content that exceeds the max number of pages.
if ( $post && ! empty( $this->query_vars['page'] ) ) {
if ( $post && ! empty( $this->query_vars[ $page_qv ] ) ) {
// Check if content is actually intended to be paged.
if ( str_contains( $post->post_content, $next ) ) {
$page = trim( $this->query_vars['page'], '/' );
$page = trim( $this->query_vars[ $page_qv ], '/' );
$content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
} else {
$content_found = false;
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/tests/canonical/paged.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,49 @@ public function test_redirect_canonical_with_nextpage_pagination() {
// Non-existing page should redirect to the permalink.
$this->assertCanonical( $link . '4/', $link );
}

/**
* Ensures canonical redirects are performed for sites with a static front page.
*
* @ticket 50163
*/
public function test_redirect_missing_front_page_pagination_canonical() {
update_option( 'show_on_front', 'page' );

$page_id = self::factory()->post->create(
array(
'post_title' => 'front-page-1',
'post_type' => 'page',
'post_content' => "Front Page 1\n<!--nextpage-->\nPage 2",
)
);

update_option( 'page_on_front', $page_id );

$link = parse_url( get_permalink( $page_id ), PHP_URL_PATH );

// Valid page numbers should not redirect.
$this->assertCanonical( $link, $link, 'The home page is not expected to redirect.' );
$this->assertCanonical( $link . 'page/2/', $link . 'page/2/', 'Page 2 exists and is not expected to redirect.' );

// Invalid page numbers should redirect to the front page.
$this->assertCanonical( $link . 'page/3/', $link, 'Page 3 does not exist and is expected to redirect to the home page.' );
}

/**
* Ensures that canonical redirects are not performed for sites with a blog listing home page.
*
* @ticket 50163
*/
public function test_redirect_missing_front_page_pagination_does_not_affect_posts_canonical() {
self::factory()->post->create_many( 3 );
update_option( 'posts_per_page', 2 );

// Valid page numbers should not redirect.
$this->assertCanonical( '/', '/', 'Page one of the blog archive should not redirect.' );
$this->assertCanonical( '/page/2/', '/page/2/', 'Page 2 of the blog archive exists and is not expected to redirect.' );

// Neither should invalid page numbers.
$this->assertCanonical( '/page/3/', '/page/3/', 'Page 3 of the blog archive is not populated but is not expected to redirect.' );
}
}

0 comments on commit 9e29083

Please sign in to comment.