From de14ea86bfe7b34cf99276ae356da43cec088c5c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 4 May 2020 10:40:06 +0000 Subject: [PATCH] Canonical: Redirect paged requests for a static page assigned as the "Posts page". This avoids displaying duplicate content of the home page under different URLs with appended page numbers. This change only affects the `` pagination (`page` query variable) and not the regular multiple posts pagination (`paged` query variable). The posts page does not support the `` pagination, so requests for invalid page numbers should be redirected to the page permalink, applying the logic previously implemented for single posts or pages. Follow-up to [34492], [47727]. Props jeremyfelt, sachit.tandukar, SergeyBiryukov. Fixes #45337. See #40773, #28081, #11694. git-svn-id: https://develop.svn.wordpress.org/trunk@47760 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/canonical.php | 24 ++++++++++++------- src/wp-includes/class-wp.php | 21 +++++++++------- tests/phpunit/tests/canonical/pageOnFront.php | 5 ++++ tests/phpunit/tests/canonical/paged.php | 11 +++++---- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index b97db55709..12f7e5e467 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -186,6 +186,22 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { ); } + // Strip off non-existing links from single posts or pages. + if ( get_query_var( 'page' ) ) { + $post_id = 0; + + if ( $wp_query->queried_object instanceof WP_Post ) { + $post_id = $wp_query->queried_object->ID; + } elseif ( $wp_query->post ) { + $post_id = $wp_query->post->ID; + } + + $redirect_url = get_permalink( $post_id ); + + $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); + $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); + } + if ( ! $redirect_url ) { $redirect_url = redirect_guess_404_permalink(); @@ -197,14 +213,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { ); } } - - // Strip off non-existing page links from single posts or pages. - if ( get_query_var( 'page' ) && $wp_query->post ) { - $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); - $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); - - $redirect_url = get_permalink( $wp_query->post->ID ); - } } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101. diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 980777bb67..ba1f15d033 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -670,15 +670,15 @@ class WP { } elseif ( $wp_query->posts ) { $content_found = true; - $post = isset( $wp_query->post ) ? $wp_query->post : null; - - // Only set X-Pingback for single posts that allow pings. - if ( is_singular() && $post && pings_open( $post ) && ! headers_sent() ) { - header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) ); - } - - // Check for paged content that exceeds the max number of pages. if ( is_singular() ) { + $post = isset( $wp_query->post ) ? $wp_query->post : null; + + // Only set X-Pingback for single posts that allow pings. + if ( $post && pings_open( $post ) && ! headers_sent() ) { + header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) ); + } + + // Check for paged content that exceeds the max number of pages. $next = ''; if ( $post && ! empty( $this->query_vars['page'] ) ) { // Check if content is actually intended to be paged. @@ -691,6 +691,11 @@ class WP { } } + // The posts page does not support the pagination. + if ( $wp_query->is_posts_page && ! empty( $this->query_vars['page'] ) ) { + $content_found = false; + } + if ( $content_found ) { $set_404 = false; } diff --git a/tests/phpunit/tests/canonical/pageOnFront.php b/tests/phpunit/tests/canonical/pageOnFront.php index 43a8275903..659cbcdaa9 100644 --- a/tests/phpunit/tests/canonical/pageOnFront.php +++ b/tests/phpunit/tests/canonical/pageOnFront.php @@ -58,8 +58,13 @@ class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase { // The page designated as the front page should redirect to the front of the site. array( '/front-page/', '/' ), + // The front page supports the pagination. array( '/front-page/2/', '/page/2/' ), array( '/front-page/?page=2', '/page/2/' ), + // The posts page does not support the pagination. + array( '/blog-page/2/', '/blog-page/' ), + array( '/blog-page/?page=2', '/blog-page/' ), + // The posts page supports regular pagination. array( '/blog-page/?paged=2', '/blog-page/page/2/' ), ); } diff --git a/tests/phpunit/tests/canonical/paged.php b/tests/phpunit/tests/canonical/paged.php index 6a85aff677..27aef3622a 100644 --- a/tests/phpunit/tests/canonical/paged.php +++ b/tests/phpunit/tests/canonical/paged.php @@ -6,7 +6,7 @@ */ class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase { - function test_nextpage() { + function test_redirect_canonical_with_nextpage_pagination() { $para = 'This is a paragraph. This is a paragraph. This is a paragraph.'; @@ -19,9 +19,12 @@ class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase { ) ); - $link = parse_url( get_permalink( $post_id ), PHP_URL_PATH ); - $paged = $link . '4/'; + $link = parse_url( get_permalink( $post_id ), PHP_URL_PATH ); - $this->assertCanonical( $paged, $link ); + // Existing page should be displayed as is. + $this->assertCanonical( $link . '3/', $link . '3/' ); + // Non-existing page should redirect to the permalink. + $this->assertCanonical( $link . '4/', $link ); } + }