From 1f40d422c23a2e3a35140c4b3856721b7eac70db Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 30 Sep 2024 18:16:18 +0000 Subject: [PATCH] Canonical: Revert redirect when front page's paginated states not found. r59091 introduced a backward compatibility (BC) break for a static homepage that includes a shortcode's or block's with paginated content that uses the `'paged'` query var, e.g. bbPress. In this use case, attempting to navigate the shortcode / block's pagination causes a canonical redirect, rather than navigating to the next page of content within that shortcode or block. Follow-up to [59091]. Props davidbinda, jjj. See #50163, #meta5184. git-svn-id: https://develop.svn.wordpress.org/trunk@59133 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 9 +++-- tests/phpunit/tests/canonical/paged.php | 45 ------------------------- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 11a0b9e6f81ed..ce88c102cfade 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -747,15 +747,14 @@ public function handle_404() { $content_found = true; if ( is_singular() ) { - $post = isset( $wp_query->post ) ? $wp_query->post : null; - $next = ''; - $page_qv = is_front_page() ? 'paged' : 'page'; + $post = isset( $wp_query->post ) ? $wp_query->post : null; + $next = ''; // Check for paged content that exceeds the max number of pages. - if ( $post && ! empty( $this->query_vars[ $page_qv ] ) ) { + if ( $post && ! empty( $this->query_vars['page'] ) ) { // Check if content is actually intended to be paged. if ( str_contains( $post->post_content, $next ) ) { - $page = trim( $this->query_vars[ $page_qv ], '/' ); + $page = trim( $this->query_vars['page'], '/' ); $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 0929dde057e1b..d2440ef18bf48 100644 --- a/tests/phpunit/tests/canonical/paged.php +++ b/tests/phpunit/tests/canonical/paged.php @@ -26,49 +26,4 @@ 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.' ); - } }