From af596bcc5b37b259982b3e7af4a8c5ab41b8e27e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 30 Apr 2020 12:03:11 +0000 Subject: [PATCH] Canonical: Redirect paged requests for non-paginated posts to the post permalink. This avoids displaying duplicate content of the same post under different URLs and ensures the canonical URL is correct. Previously, requests for invalid page numbers were only redirected to the post permalink if the post was actually paginated using the `` marker. Follow-up to [34492]. Props jeremyfelt, prografika, sachit.tandukar, subrataemfluence, hronak, ekatherine, henry.wright, chesio, dd32, SergeyBiryukov. Fixes #40773. See #45337, #28081, #11694. git-svn-id: https://develop.svn.wordpress.org/trunk@47727 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/canonical.php | 3 +-- src/wp-includes/class-wp.php | 11 ++++++++--- tests/phpunit/tests/canonical.php | 3 +++ tests/phpunit/tests/link/wpGetCanonicalURL.php | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 85ffa7d93c..fd6676724d 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -157,8 +157,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { } } - if ( get_query_var( 'page' ) && $wp_query->post && - false !== strpos( $wp_query->post->post_content, '' ) ) { + 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 ); diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 783a993cdc..e5dadcd232 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -678,9 +678,14 @@ class WP { // Check for paged content that exceeds the max number of pages. $next = ''; - if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) { - $page = trim( $this->query_vars['page'], '/' ); - $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 ); + if ( $p && ! empty( $this->query_vars['page'] ) ) { + // Check if content is actually intended to be paged. + if ( false !== strpos( $p->post_content, $next ) ) { + $page = trim( $this->query_vars['page'], '/' ); + $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 ); + } else { + $success = false; + } } } diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index 7160a46c83..1db5f71c72 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -175,6 +175,9 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase { array( '/2008/09/03/images-test/?page=3', '/2008/09/03/images-test/3/' ), array( '/2008/09/03/images-te?page=3', '/2008/09/03/images-test/3/' ), + array( '/2008/03/03/comment-test/3/', '/2008/03/03/comment-test/' ), + array( '/2008/03/03/comment-test/?page=3', '/2008/03/03/comment-test/' ), + // Comments. array( '/2008/03/03/comment-test/?cpage=2', '/2008/03/03/comment-test/comment-page-2/' ), diff --git a/tests/phpunit/tests/link/wpGetCanonicalURL.php b/tests/phpunit/tests/link/wpGetCanonicalURL.php index 6b4df35cbd..2dedd63c1a 100644 --- a/tests/phpunit/tests/link/wpGetCanonicalURL.php +++ b/tests/phpunit/tests/link/wpGetCanonicalURL.php @@ -11,7 +11,8 @@ class Tests_WpGetCanonicalURL extends WP_UnitTestCase { public static function wpSetUpBeforeClass( $factory ) { self::$post_id = $factory->post->create( array( - 'post_status' => 'publish', + 'post_content' => 'Page 1 Page 2 Page 3', + 'post_status' => 'publish', ) ); }