From 9e290835d6f006997d59ebaf063df113bb15a363 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Wed, 25 Sep 2024 19:35:20 +0000 Subject: [PATCH] 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 `` 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 --- src/wp-includes/class-wp.php | 9 ++--- tests/phpunit/tests/canonical/paged.php | 45 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index ce88c102cfade..11a0b9e6f81ed 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -747,14 +747,15 @@ public function handle_404() { $content_found = true; if ( is_singular() ) { - $post = isset( $wp_query->post ) ? $wp_query->post : null; - $next = ''; + $post = isset( $wp_query->post ) ? $wp_query->post : null; + $next = ''; + $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; diff --git a/tests/phpunit/tests/canonical/paged.php b/tests/phpunit/tests/canonical/paged.php index d2440ef18bf48..0929dde057e1b 100644 --- a/tests/phpunit/tests/canonical/paged.php +++ b/tests/phpunit/tests/canonical/paged.php @@ -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\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.' ); + } }