Make WordPress Core

Ticket #53362: 53362.diff

File 53362.diff, 6.4 KB (added by SergeyBiryukov, 3 years ago)
  • src/wp-includes/canonical.php

     
    190190                }
    191191
    192192                // Strip off non-existing <!--nextpage--> links from single posts or pages.
    193                 if ( get_query_var( 'page' ) ) {
     193                if ( '' !== get_query_var( 'page' ) ) {
    194194                        $post_id = 0;
    195195
    196196                        if ( $wp_query->queried_object instanceof WP_Post ) {
  • src/wp-includes/class-wp.php

     
    696696
    697697                        // If posts were found, check for paged content.
    698698                } 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'] : '';
    700701
    701702                        if ( is_singular() ) {
    702703                                $post = isset( $wp_query->post ) ? $wp_query->post : null;
     
    708709
    709710                                // Check for paged content that exceeds the max number of pages.
    710711                                $next = '<!--nextpage-->';
    711                                 if ( $post && ! empty( $this->query_vars['page'] ) ) {
     712                                if ( $post && '' !== $page_query_var ) {
     713                                        $page = (int) trim( $page_query_var, '/' );
     714
    712715                                        // 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 );
    716718                                        } else {
    717719                                                $content_found = false;
    718720                                        }
     
    720722                        }
    721723
    722724                        // 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 ) {
    724726                                $content_found = false;
    725727                        }
    726728
  • tests/phpunit/tests/canonical/paged.php

     
    66 */
    77class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase {
    88
    9         public function test_redirect_canonical_with_nextpage_pagination() {
     9        public static $post_id;
     10
     11        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    1012                $para = 'This is a paragraph.
    1113                        This is a paragraph.
    1214                        This is a paragraph.';
    1315                $next = '<!--nextpage-->';
    1416
    15                 $post_id = self::factory()->post->create(
     17                self::$post_id = self::factory()->post->create(
    1618                        array(
    1719                                'post_status'  => 'publish',
    1820                                'post_content' => "{$para}{$next}{$para}{$next}{$para}",
    1921                        )
    2022                );
     23        }
    2124
    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 );
    2330
    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 );
    2832        }
    2933
     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        }
    3051}
  • tests/phpunit/tests/canonical/pageOnFront.php

     
    1515                        'page_for_posts',
    1616                        self::factory()->post->create(
    1717                                array(
    18                                         'post_title' => 'blog-page',
     18                                        'post_title' => 'posts-page',
    1919                                        'post_type'  => 'page',
    2020                                )
    2121                        )
     
    5151                 */
    5252                return array(
    5353                        // 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 ),
    5860
    5961                        // 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 ),
    6163                        // 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 ),
    6466                        // 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 ),
    6769                        // 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 ),
    6971                );
    7072        }
    7173}