Make WordPress Core

Changeset 59228


Ignore:
Timestamp:
10/14/2024 08:47:34 AM (16 months ago)
Author:
swissspidy
Message:

Sitemaps: Improve sitemap.xml redirects when using custom permalinks.

Changes the way redirects from sitemap.xml to wp-sitemap.xml happen, so that they also work when using a more complex custom rewrite structure.

Props gmariani405, swissspidy, euthelup, peterwilsoncc.
Fixes #61931.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-rewrite.php

    r56549 r59228  
    12881288        $favicon_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'favicon\.ico$' => $this->index . '?favicon=1' ) : array();
    12891289
     1290        // sitemap.xml -- only if installed at the root.
     1291        $sitemap_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'sitemap\.xml' => $this->index . '??sitemap=index' ) : array();
     1292
    12901293        // Old feed and service files.
    12911294        $deprecated_files = array(
     
    14501453        // Put them together.
    14511454        if ( $this->use_verbose_page_rules ) {
    1452             $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
     1455            $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
    14531456        } else {
    1454             $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );
     1457            $this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );
    14551458        }
    14561459
  • trunk/src/wp-includes/sitemaps/class-wp-sitemaps.php

    r55000 r59228  
    7676
    7777        // Add additional action callbacks.
    78         add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 );
    7978        add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 );
    8079    }
     
    224223     *
    225224     * @since 5.5.0
     225     * @deprecated 6.7.0 Deprecated in favor of {@see WP_Rewrite::rewrite_rules()}
    226226     *
    227227     * @param bool     $bypass Pass-through of the pre_handle_404 filter value.
  • trunk/tests/phpunit/tests/canonical/sitemaps.php

    r51568 r59228  
    5353
    5454    /**
     55     * Ensure sitemaps redirects work as expected with a more custom rewrite structure.
     56     *
     57     * @dataProvider data_sitemaps_canonical_pretty_redirects
     58     * @ticket 61931
     59     */
     60    public function test_sitemaps_canonical_custom_pretty_redirects( $test_url, $expected ) {
     61        $this->set_permalink_structure( '/%category%/%year%/%monthnum%/%postname%/' );
     62        $this->assertCanonical( $test_url, $expected, 61931 );
     63    }
     64
     65    /**
    5566     * Data provider for test_sitemaps_canonical_pretty_redirects.
    5667     *
     
    6273     * }
    6374     */
    64     public function data_sitemaps_canonical_pretty_redirects() {
     75    public static function data_sitemaps_canonical_pretty_redirects() {
    6576        return array(
     77            // sitemap.xml special case.
     78            array( '/sitemap.xml', '/wp-sitemap.xml' ),
     79            array( '/sitemap.xml/', '/wp-sitemap.xml' ),
     80
    6681            // Ugly/incorrect versions redirect correctly.
    6782            array( '/?sitemap=index', '/wp-sitemap.xml' ),
Note: See TracChangeset for help on using the changeset viewer.