Make WordPress Core

Ticket #20902: 20902.2.diff

File 20902.2.diff, 10.9 KB (added by akrocks, 7 weeks ago)
  • src/wp-includes/canonical.php

    diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
    index 6faeab2c27..42277040f0 100644
    a b function redirect_canonical( $requested_url = null, $do_redirect = true ) {  
    261261                        if ( $redirect_url ) {
    262262                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    263263                        }
     264                } elseif ( is_page() && ! empty( $_GET['pagename'] ) && ! get_query_var( 'paged' ) && ! $redirect_url ) {
     265                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     266                        $redirect_obj = get_post( $wp_query->get_queried_object_id() );
     267
     268                        if ( $redirect_url ) {
     269                                // Feeds and comment pages are appended to this path further down.
     270                                $redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
     271                                $redirect['query'] = remove_query_arg( 'pagename', $redirect['query'] );
     272                        }
    264273                } elseif ( is_page() && ! is_feed() && ! $redirect_url
    265274                        && 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' )
    266275                ) {
    … … function redirect_canonical( $requested_url = null, $do_redirect = true ) {  
    274283                        if ( $redirect_url ) {
    275284                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    276285                        }
     286                } elseif ( is_home() && ! empty( $_GET['pagename'] ) && ! is_feed() && ! $redirect_url
     287                        && 'page' === get_option( 'show_on_front' ) && $wp_query->get_queried_object_id() === (int) get_option( 'page_for_posts' )
     288                ) {
     289                        $redirect_url = get_permalink( get_option( 'page_for_posts' ) );
     290                        $redirect_obj = get_post( get_option( 'page_for_posts' ) );
     291
     292                        if ( $redirect_url ) {
     293                                // Paging is appended to this path further down.
     294                                $redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
     295                                $redirect['query'] = remove_query_arg( 'pagename', $redirect['query'] );
     296                        }
    277297                } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
    278298                        $m = get_query_var( 'm' );
    279299
    … … function redirect_canonical( $requested_url = null, $do_redirect = true ) {  
    317337                                        $redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
    318338                                }
    319339                        }
    320                 } elseif ( is_author() && ! empty( $_GET['author'] )
    321                         && is_string( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] )
     340                } elseif ( is_author()
     341                        && ( ( ! empty( $_GET['author'] ) && is_string( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) )
     342                                || ( ! empty( $_GET['author_name'] ) && is_string( $_GET['author_name'] ) ) )
    322343                ) {
    323344                        $author = get_userdata( get_query_var( 'author' ) );
    324345
    … … function redirect_canonical( $requested_url = null, $do_redirect = true ) {  
    329350                                $redirect_obj = $author;
    330351
    331352                                if ( $redirect_url ) {
    332                                         $redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
     353                                        // Paging and feeds are appended to this path further down.
     354                                        $redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
     355                                        $redirect['query'] = remove_query_arg( array( 'author', 'author_name' ), $redirect['query'] );
    333356                                }
    334357                        }
    335358                } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories).
    … … function redirect_canonical( $requested_url = null, $do_redirect = true ) {  
    367390
    368391                                                $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) );
    369392
     393                                                /*
     394                                                 * _post_format_request() adds a `post_type` query var to every post format request.
     395                                                 * It is not part of the requested URL, so it does not come from the rewrite rules.
     396                                                 */
     397                                                if ( 'post_format' === $obj->taxonomy ) {
     398                                                        $rewrite_vars = array_diff( $rewrite_vars, array( 'post_type' ) );
     399                                                }
     400
    370401                                                // Check to see if all the query vars are coming from the rewrite, none are set via $_GET.
    371402                                                if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) {
    372403                                                        // Remove all of the per-tax query vars.
  • tests/phpunit/includes/testcase-canonical.php

    diff --git a/tests/phpunit/includes/testcase-canonical.php b/tests/phpunit/includes/testcase-canonical.php
    index 33dbb3d9f4..f0f2785d2f 100644
    a b abstract class WP_Canonical_UnitTestCase extends WP_UnitTestCase {  
    5454                // Already created by install defaults:
    5555                // $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
    5656
    57                 self::$post_ids[] = $factory->post->create(
     57                $audio_post_id = $factory->post->create(
    5858                        array(
    5959                                'import_id'  => 587,
    6060                                'post_title' => 'post-format-test-audio',
    … … abstract class WP_Canonical_UnitTestCase extends WP_UnitTestCase {  
    6262                        )
    6363                );
    6464
     65                set_post_format( $audio_post_id, 'audio' );
     66
     67                self::$post_ids[] = $audio_post_id;
     68
    6569                $gallery_post_id = $factory->post->create(
    6670                        array(
    6771                                'post_title' => 'post-format-test-gallery',
  • tests/phpunit/tests/canonical.php

    diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php
    index 886b093129..b0e0d76311 100644
    a b class Tests_Canonical extends WP_Canonical_UnitTestCase {  
    148148                        // Taxonomies with extra query vars.
    149149                        array( '/category/cat-a/page/1/?test=one%20two', '/category/cat-a/?test=one%20two', 18086 ), // Extra query vars should stay encoded.
    150150
     151                        // Post formats.
     152                        array( '/?post_format=audio', '/type/audio/', 20902 ),
     153                        array( '/?post_format=audio&test=one', '/type/audio/?test=one', 20902 ), // Extra query vars should be kept.
     154                        array( '/type/audio/', '/type/audio/', 20902 ), // No redirect.
     155                        array( '/?post_format=video', '/?post_format=video', 20902 ), // A format without posts is a 404: no redirect.
     156
    151157                        // Categories with dates.
    152158                        array(
    153159                                '/2008/04/?cat=1',
    … … class Tests_Canonical extends WP_Canonical_UnitTestCase {  
    177183                        // Pages.
    178184                        array( '/child-page-1/', '/parent-page/child-page-1/' ),
    179185                        array( '/?page_id=144', '/parent-page/child-page-1/' ),
     186                        array( '/?pagename=sample-page', '/sample-page/', 20902 ),
     187                        array( '/?pagename=sample-page&test=one', '/sample-page/?test=one', 20902 ), // Extra query vars should be kept.
     188                        array( '/?pagename=parent/child1/grandchild', '/parent/child1/grandchild/', 20902 ), // Hierarchical page path.
     189                        array( '/?pagename=does-not-exist', '/?pagename=does-not-exist', 20902 ), // A page that does not exist is a 404: no redirect.
     190                        array( '/?pagename=sample-page&feed=rss2', '/sample-page/feed/', 20902 ),
     191                        array( '/?pagename=sample-page&paged=2', '/page/2/?pagename=sample-page', 20902 ), // Paging does not apply to a page: unchanged, the page name is not dropped.
    180192                        array( '/abo', '/about/' ),
    181193                        array( '/parent/child1/grandchild/', '/parent/child1/grandchild/' ),
    182194                        array( '/parent/child2/grandchild/', '/parent/child2/grandchild/' ),
    … … class Tests_Canonical extends WP_Canonical_UnitTestCase {  
    238250
    239251                        // Authors.
    240252                        array( '/?author=%d', '/author/canonical-author/' ),
     253                        array( '/?author_name=canonical-author', '/author/canonical-author/', 20902 ),
     254                        array( '/?author_name=canonical-author&test=one', '/author/canonical-author/?test=one', 20902 ), // Extra query vars should be kept.
     255                        array( '/?author_name=does-not-exist', '/?author_name=does-not-exist', 20902 ), // No such author: no redirect.
     256                        // Paging and feeds should be added to the author URL, not replace it.
     257                        array( '/?author_name=canonical-author&paged=1', '/author/canonical-author/', 20902 ),
     258                        array( '/?author_name=canonical-author&paged=2', '/author/canonical-author/page/2/', 20902 ),
     259                        array( '/?author_name=canonical-author&feed=rss2', '/author/canonical-author/feed/', 20902 ),
     260                        array( '/?author=%d&paged=2', '/author/canonical-author/page/2/', 20902 ),
    241261                        // array( '/?author=%d&year=2008', '/2008/?author=3'),
    242262                        // array( '/author/canonical-author/?year=2008', '/2008/?author=3'), // Either or, see previous testcase.
    243263                        array( '/author/canonical-author/?author[1]=hello', '/author/canonical-author/?author[1]=hello', 60059 ),
    … … class Tests_Canonical extends WP_Canonical_UnitTestCase {  
    267287                );
    268288        }
    269289
     290        /**
     291         * A `?author_name=` request for a user without published posts should not be redirected,
     292         * the same as a `?author=` request for that user.
     293         *
     294         * @ticket 20902
     295         *
     296         * @dataProvider data_author_query_vars_without_published_posts
     297         *
     298         * @param string $query_var The author query var to test, either `author` or `author_name`.
     299         */
     300        public function test_author_query_var_without_published_posts_is_not_redirected( $query_var ) {
     301                $user_id = self::factory()->user->create( array( 'user_login' => 'author-without-posts' ) );
     302
     303                $test_url = ( 'author' === $query_var ) ? "/?author={$user_id}" : '/?author_name=author-without-posts';
     304
     305                $this->assertCanonical( $test_url, $test_url, 20902 );
     306        }
     307
     308        /**
     309         * Data provider.
     310         *
     311         * @return array[]
     312         */
     313        public function data_author_query_vars_without_published_posts() {
     314                return array(
     315                        'author'      => array( 'author' ),
     316                        'author_name' => array( 'author_name' ),
     317                );
     318        }
     319
     320        /**
     321         * Paging and feeds of a `?post_format=` request should be added to the post format URL.
     322         *
     323         * @ticket 20902
     324         */
     325        public function test_post_format_query_var_with_paging_and_feed() {
     326                // Together with the audio post from the fixtures, this gives two pages of audio posts.
     327                $post_ids = self::factory()->post->create_many( 5, array( 'post_date' => '2008-07-01 00:00:00' ) );
     328
     329                foreach ( $post_ids as $post_id ) {
     330                        set_post_format( $post_id, 'audio' );
     331                }
     332
     333                $this->assertCanonical( '/?post_format=audio&paged=1', '/type/audio/', 20902 );
     334                $this->assertCanonical( '/?post_format=audio&paged=2', '/type/audio/page/2/', 20902 );
     335                $this->assertCanonical( '/?post_format=audio&paged=2&test=one', '/type/audio/page/2/?test=one', 20902 );
     336                $this->assertCanonical( '/?post_format=audio&feed=rss2', '/type/audio/feed/', 20902 );
     337        }
     338
    270339        /**
    271340         * @ticket 16557
    272341         */
  • tests/phpunit/tests/canonical/pageOnFront.php

    diff --git a/tests/phpunit/tests/canonical/pageOnFront.php b/tests/phpunit/tests/canonical/pageOnFront.php
    index e8833dd660..81c0495cfb 100644
    a b class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase {  
    6666                        array( '/blog-page/?page=2', '/blog-page/', 45337 ),
    6767                        // The posts page supports regular pagination.
    6868                        array( '/blog-page/?paged=2', '/blog-page/page/2/', 20385 ),
     69
     70                        // The `pagename` query var of the front page should redirect to the front of the site.
     71                        array( '/?pagename=front-page', '/', 20902 ),
     72                        array( '/?pagename=front-page&test=one', '/?test=one', 20902 ),
     73                        // The front page supports the <!--nextpage--> pagination.
     74                        array( '/?pagename=front-page&page=2', '/page/2/', 20902 ),
     75                        // The `pagename` query var of the posts page should redirect to the pretty permalink.
     76                        array( '/?pagename=blog-page', '/blog-page/', 20902 ),
     77                        array( '/?pagename=blog-page&test=one', '/blog-page/?test=one', 20902 ),
     78                        // The posts page supports regular pagination.
     79                        array( '/?pagename=blog-page&paged=2', '/blog-page/page/2/', 20902 ),
    6980                );
    7081        }
    7182}