WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/27/2020 01:28:24 AM (14 months ago)
Author:
peterwilsoncc
Message:

Sitemaps: Prevent incorrect redirection of paged sitemap requests.

Update redirect_canonical() to account for custom pagination and URL format used by sitemaps in order to follow standard practices.

Introduce the function get_sitemap_url() to simplify getting the index and provider URLs as needed.

Props jonathanstegall, pbiron, GamerZ, salvoaranzulla, peterwilsoncc.
Fixes #50910.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/canonical/sitemaps.php

    r48166 r48872  
    4141    }
    4242
     43    /**
     44     * Ensure sitemaps redirects work as expected with pretty permalinks.
     45     *
     46     * @dataProvider data_sitemaps_canonical_pretty_redirects
     47     * @ticket 50910
     48     */
     49    public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected ) {
     50        $this->set_permalink_structure( '/%postname%/' );
     51        $this->assertCanonical( $test_url, $expected, 50910 );
     52    }
     53
     54    /**
     55     * Data provider for test_sitemaps_canonical_pretty_redirects.
     56     *
     57     * @return array[] {
     58     *     Data to test with.
     59     *
     60     *     @type string $0 The test URL.
     61     *     @type string $1 The expected canonical URL.
     62     * }
     63     */
     64    public function data_sitemaps_canonical_pretty_redirects() {
     65        return array(
     66            // Ugly/incorrect versions redirect correctly.
     67            array( '/?sitemap=index', '/wp-sitemap.xml' ),
     68            array( '/wp-sitemap.xml/', '/wp-sitemap.xml' ),
     69            array( '/?sitemap=posts&sitemap-subtype=post', '/wp-sitemap-posts-post-1.xml' ),
     70            array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/wp-sitemap-posts-post-2.xml' ),
     71            array( '/?sitemap=taxonomies&sitemap-subtype=category', '/wp-sitemap-taxonomies-category-1.xml' ),
     72            array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/wp-sitemap-taxonomies-category-2.xml' ),
     73
     74            // Pretty versions don't redirect incorrectly.
     75            array( '/wp-sitemap.xml', '/wp-sitemap.xml' ),
     76            array( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' ),
     77            array( '/wp-sitemap-posts-post-2.xml', '/wp-sitemap-posts-post-2.xml' ),
     78            array( '/wp-sitemap-taxonomies-category-1.xml', '/wp-sitemap-taxonomies-category-1.xml' ),
     79            array( '/wp-sitemap-taxonomies-category-2.xml', '/wp-sitemap-taxonomies-category-2.xml' ),
     80        );
     81    }
     82
     83    /**
     84     * Ensure sitemaps redirects work as expected with ugly permalinks.
     85     *
     86     * @dataProvider data_sitemaps_canonical_ugly_redirects
     87     * @ticket 50910
     88     */
     89    public function test_sitemaps_canonical_ugly_redirects( $test_url, $expected ) {
     90        $this->set_permalink_structure( '' );
     91        $this->assertCanonical( $test_url, $expected, 50910 );
     92    }
     93
     94    /**
     95     * Data provider for test_sitemaps_canonical_ugly_redirects.
     96     *
     97     * @return array[] {
     98     *     Data to test with.
     99     *
     100     *     @type string $0 The test URL.
     101     *     @type string $1 The expected canonical URL.
     102     * }
     103     */
     104    public function data_sitemaps_canonical_ugly_redirects() {
     105        return array(
     106            // Ugly permalinks remain ugly.
     107            array( '/?sitemap=index', '/?sitemap=index' ),
     108            array( '/?sitemap=posts&sitemap-subtype=post', '/?sitemap=posts&sitemap-subtype=post' ),
     109            array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/?sitemap=posts&sitemap-subtype=post&paged=2' ),
     110            array( '/?sitemap=taxonomies&sitemap-subtype=category', '/?sitemap=taxonomies&sitemap-subtype=category' ),
     111            array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/?sitemap=taxonomies&sitemap-subtype=category&paged=2' ),
     112        );
     113    }
    43114}
Note: See TracChangeset for help on using the changeset viewer.