Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.' );
}
}