Ticket #53362: 53362.diff
| File 53362.diff, 6.4 KB (added by , 3 years ago) |
|---|
-
src/wp-includes/canonical.php
190 190 } 191 191 192 192 // Strip off non-existing <!--nextpage--> links from single posts or pages. 193 if ( get_query_var( 'page' ) ) {193 if ( '' !== get_query_var( 'page' ) ) { 194 194 $post_id = 0; 195 195 196 196 if ( $wp_query->queried_object instanceof WP_Post ) { -
src/wp-includes/class-wp.php
696 696 697 697 // If posts were found, check for paged content. 698 698 } elseif ( $wp_query->posts ) { 699 $content_found = true; 699 $content_found = true; 700 $page_query_var = isset( $this->query_vars['page'] ) ? $this->query_vars['page'] : ''; 700 701 701 702 if ( is_singular() ) { 702 703 $post = isset( $wp_query->post ) ? $wp_query->post : null; … … 708 709 709 710 // Check for paged content that exceeds the max number of pages. 710 711 $next = '<!--nextpage-->'; 711 if ( $post && ! empty( $this->query_vars['page'] ) ) { 712 if ( $post && '' !== $page_query_var ) { 713 $page = (int) trim( $page_query_var, '/' ); 714 712 715 // Check if content is actually intended to be paged. 713 if ( false !== strpos( $post->post_content, $next ) ) { 714 $page = trim( $this->query_vars['page'], '/' ); 715 $content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 ); 716 if ( $page > 0 && false !== strpos( $post->post_content, $next ) ) { 717 $content_found = $page <= ( substr_count( $post->post_content, $next ) + 1 ); 716 718 } else { 717 719 $content_found = false; 718 720 } … … 720 722 } 721 723 722 724 // The posts page does not support the <!--nextpage--> pagination. 723 if ( $wp_query->is_posts_page && ! empty( $this->query_vars['page'] )) {725 if ( $wp_query->is_posts_page && '' !== $page_query_var ) { 724 726 $content_found = false; 725 727 } 726 728 -
tests/phpunit/tests/canonical/paged.php
6 6 */ 7 7 class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase { 8 8 9 public function test_redirect_canonical_with_nextpage_pagination() { 9 public static $post_id; 10 11 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 10 12 $para = 'This is a paragraph. 11 13 This is a paragraph. 12 14 This is a paragraph.'; 13 15 $next = '<!--nextpage-->'; 14 16 15 $post_id = self::factory()->post->create(17 self::$post_id = self::factory()->post->create( 16 18 array( 17 19 'post_status' => 'publish', 18 20 'post_content' => "{$para}{$next}{$para}{$next}{$para}", 19 21 ) 20 22 ); 23 } 21 24 22 $link = parse_url( get_permalink( $post_id ), PHP_URL_PATH ); 25 /** 26 * @dataProvider data_redirect_canonical_with_nextpage_pagination 27 */ 28 public function test_redirect_canonical_with_nextpage_pagination( $page, $expected_page, $ticket = 0 ) { 29 $link = parse_url( get_permalink( self::$post_id ), PHP_URL_PATH ); 23 30 24 // Existing page should be displayed as is. 25 $this->assertCanonical( $link . '3/', $link . '3/' ); 26 // Non-existing page should redirect to the permalink. 27 $this->assertCanonical( $link . '4/', $link ); 31 $this->assertCanonical( $link . $page, $link . $expected_page, $ticket ); 28 32 } 29 33 34 /** 35 * Data provider for test_redirect_canonical_with_nextpage_pagination(). 36 * 37 * @return array[] Test parameters { 38 * @type string $page Page number to test, with trailing slash. 39 * @type string $expected_page Expected page number to be redirected to. 40 * @type int $ticket The ticket the test refers to. Can be skipped if unknown. 41 * } 42 */ 43 public function data_redirect_canonical_with_nextpage_pagination() { 44 return array( 45 'page 0' => array( '0/', '', 53362 ), 46 'page 1' => array( '1/', '', 53362 ), 47 'existing page' => array( '3/', '3/', 45337 ), 48 'non-existing page' => array( '4/', '', 45337 ), 49 ); 50 } 30 51 } -
tests/phpunit/tests/canonical/pageOnFront.php
15 15 'page_for_posts', 16 16 self::factory()->post->create( 17 17 array( 18 'post_title' => ' blog-page',18 'post_title' => 'posts-page', 19 19 'post_type' => 'page', 20 20 ) 21 21 ) … … 51 51 */ 52 52 return array( 53 53 // Check against an odd redirect. 54 array( '/page/2/', '/page/2/', 20385 ), 55 array( '/?page=2', '/page/2/', 35344 ), 56 array( '/page/1/', '/', 35344 ), 57 array( '/?page=1', '/', 35344 ), 54 'home, page 2, pretty link' => array( '/page/2/', '/page/2/', 20385 ), 55 'home, page 2, plain link' => array( '/?page=2', '/page/2/', 35344 ), 56 'home, page 1, pretty link' => array( '/page/1/', '/', 53362 ), 57 'home, page 1, plain link' => array( '/?page=1', '/', 53362 ), 58 'home, page 0, pretty link' => array( '/page/0/', '/', 53362 ), 59 'home, page 0, plain link' => array( '/?page=0', '/', 53362 ), 58 60 59 61 // The page designated as the front page should redirect to the front of the site. 60 array( '/front-page/', '/', 20385 ),62 'front page' => array( '/front-page/', '/', 20385 ), 61 63 // The front page supports the <!--nextpage--> pagination. 62 array( '/front-page/2/', '/page/2/', 35344 ),63 array( '/front-page/?page=2', '/page/2/', 35344 ),64 'front, page 2, pretty link' => array( '/front-page/2/', '/page/2/', 35344 ), 65 'front, page 2, plain link' => array( '/front-page/?page=2', '/page/2/', 35344 ), 64 66 // The posts page does not support the <!--nextpage--> pagination. 65 array( '/blog-page/2/', '/blog-page/', 45337 ),66 array( '/blog-page/?page=2', '/blog-page/', 45337 ),67 'posts, page 2, pretty link' => array( '/posts-page/2/', '/posts-page/', 45337 ), 68 'posts, page 2, plain link' => array( '/posts-page/?page=2', '/posts-page/', 45337 ), 67 69 // The posts page supports regular pagination. 68 array( '/blog-page/?paged=2', '/blog-page/page/2/', 20385 ),70 'posts, previous page, plain' => array( '/posts-page/?paged=2', '/posts-page/page/2/', 20385 ), 69 71 ); 70 72 } 71 73 }