Make WordPress Core


Ignore:
Timestamp:
08/27/2020 01:28:24 AM (5 years 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/sitemaps/functions.php

    r48541 r48872  
    5959        }
    6060    }
     61
     62    /**
     63     * Test get_sitemap_url() with ugly permalinks.
     64     *
     65     * @dataProvider ugly_permalinks_provider
     66     */
     67    public function test_get_sitemap_url_ugly_permalinks( $name, $subtype_name, $page, $expected ) {
     68        $actual = get_sitemap_url( $name, $subtype_name, $page );
     69
     70        $this->assertSame( $expected, $actual );
     71    }
     72
     73    /**
     74     * Test get_sitemap_url() with pretty permalinks.
     75     *
     76     * @dataProvider pretty_permalinks_provider
     77     */
     78    public function test_get_sitemap_url_pretty_permalinks( $name, $subtype_name, $page, $expected ) {
     79        $this->set_permalink_structure( '/%postname%/' );
     80
     81        $actual = get_sitemap_url( $name, $subtype_name, $page );
     82
     83        $this->assertSame( $expected, $actual );
     84    }
     85
     86    /**
     87     * Data provider for test_get_sitemap_url_ugly_permalinks.
     88     *
     89     * @return array[] {
     90     *     Data to test with.
     91     *
     92     *     @type string       $0 Sitemap name.
     93     *     @type string       $1 Sitemap subtype name.
     94     *     @type int          $3 Sitemap page.
     95     *     @type string|false $4 Sitemap URL.
     96     * }
     97     */
     98    function ugly_permalinks_provider() {
     99        return array(
     100            array( 'posts', 'post', 1, home_url( '/?sitemap=posts&sitemap-subtype=post&paged=1' ) ),
     101            array( 'posts', 'post', 0, home_url( '/?sitemap=posts&sitemap-subtype=post&paged=1' ) ),
     102            array( 'posts', 'page', 1, home_url( '/?sitemap=posts&sitemap-subtype=page&paged=1' ) ),
     103            array( 'posts', 'page', 5, home_url( '/?sitemap=posts&sitemap-subtype=page&paged=5' ) ),
     104            // post_type doesn't exist.
     105            array( 'posts', 'foo', 5, false ),
     106            array( 'taxonomies', 'category', 1, home_url( '/?sitemap=taxonomies&sitemap-subtype=category&paged=1' ) ),
     107            array( 'taxonomies', 'post_tag', 1, home_url( '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1' ) ),
     108            array( 'taxonomies', 'post_tag', -1, home_url( '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1' ) ),
     109            // negative paged, gets converted to it's absolute value.
     110            array( 'users', '', 4, home_url( '/?sitemap=users&paged=4' ) ),
     111            // users provider doesn't allow subtypes.
     112            array( 'users', 'foo', 4, false ),
     113            // provider doesn't exist.
     114            array( 'foo', '', 4, false ),
     115        );
     116    }
     117
     118    /**
     119     * Data provider for test_get_sitemap_url_pretty_permalinks
     120     *
     121     * @return array[] {
     122     *     Data to test with.
     123     *
     124     *     @type string       $0 Sitemap name.
     125     *     @type string       $1 Sitemap subtype name.
     126     *     @type int          $3 Sitemap page.
     127     *     @type string|false $4 Sitemap URL.
     128     * }
     129     */
     130    function pretty_permalinks_provider() {
     131        return array(
     132            array( 'posts', 'post', 1, home_url( '/wp-sitemap-posts-post-1.xml' ) ),
     133            array( 'posts', 'post', 0, home_url( '/wp-sitemap-posts-post-1.xml' ) ),
     134            array( 'posts', 'page', 1, home_url( '/wp-sitemap-posts-page-1.xml' ) ),
     135            array( 'posts', 'page', 5, home_url( '/wp-sitemap-posts-page-5.xml' ) ),
     136            // post_type doesn't exist.
     137            array( 'posts', 'foo', 5, false ),
     138            array( 'taxonomies', 'category', 1, home_url( '/wp-sitemap-taxonomies-category-1.xml' ) ),
     139            array( 'taxonomies', 'post_tag', 1, home_url( '/wp-sitemap-taxonomies-post_tag-1.xml' ) ),
     140            // negative paged, gets converted to it's absolute value.
     141            array( 'taxonomies', 'post_tag', -1, home_url( '/wp-sitemap-taxonomies-post_tag-1.xml' ) ),
     142            array( 'users', '', 4, home_url( '/wp-sitemap-users-4.xml' ) ),
     143            // users provider doesn't allow subtypes.
     144            array( 'users', 'foo', 4, false ),
     145            // provider doesn't exist.
     146            array( 'foo', '', 4, false ),
     147        );
     148    }
    61149}
Note: See TracChangeset for help on using the changeset viewer.