diff --git a/src/wp-includes/class-wp-rewrite.php b/src/wp-includes/class-wp-rewrite.php index 8bcf8fc962246..0d081ada91cf3 100644 --- a/src/wp-includes/class-wp-rewrite.php +++ b/src/wp-includes/class-wp-rewrite.php @@ -1287,6 +1287,9 @@ public function rewrite_rules() { // favicon.ico -- only if installed at the root. $favicon_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'favicon\.ico$' => $this->index . '?favicon=1' ) : array(); + // sitemap.xml -- only if installed at the root. + $sitemap_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'sitemap\.xml' => $this->index . '??sitemap=index' ) : array(); + // Old feed and service files. $deprecated_files = array( '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old', @@ -1449,9 +1452,9 @@ public function rewrite_rules() { // Put them together. if ( $this->use_verbose_page_rules ) { - $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 ); + $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 ); } else { - $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 ); + $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 ); } /** diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps.php b/src/wp-includes/sitemaps/class-wp-sitemaps.php index d897fa8f2a291..a0fd5be6242e8 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps.php @@ -75,7 +75,6 @@ public function init() { $this->register_sitemaps(); // Add additional action callbacks. - add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 ); add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 ); } @@ -223,6 +222,7 @@ public function render_sitemaps() { * Redirects a URL to the wp-sitemap.xml * * @since 5.5.0 + * @deprecated 6.7.0 Deprecated in favor of {@see WP_Rewrite::rewrite_rules()} * * @param bool $bypass Pass-through of the pre_handle_404 filter value. * @param WP_Query $query The WP_Query object. diff --git a/tests/phpunit/tests/canonical/sitemaps.php b/tests/phpunit/tests/canonical/sitemaps.php index 4041ab93f68c0..97fa6c4ee8937 100644 --- a/tests/phpunit/tests/canonical/sitemaps.php +++ b/tests/phpunit/tests/canonical/sitemaps.php @@ -51,6 +51,17 @@ public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected ) $this->assertCanonical( $test_url, $expected, 50910 ); } + /** + * Ensure sitemaps redirects work as expected with a more custom rewrite structure. + * + * @dataProvider data_sitemaps_canonical_pretty_redirects + * @ticket 61931 + */ + public function test_sitemaps_canonical_custom_pretty_redirects( $test_url, $expected ) { + $this->set_permalink_structure( '/%category%/%year%/%monthnum%/%postname%/' ); + $this->assertCanonical( $test_url, $expected, 61931 ); + } + /** * Data provider for test_sitemaps_canonical_pretty_redirects. * @@ -61,8 +72,12 @@ public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected ) * @type string $1 The expected canonical URL. * } */ - public function data_sitemaps_canonical_pretty_redirects() { + public static function data_sitemaps_canonical_pretty_redirects() { return array( + // sitemap.xml special case. + array( '/sitemap.xml', '/wp-sitemap.xml' ), + array( '/sitemap.xml/', '/wp-sitemap.xml' ), + // Ugly/incorrect versions redirect correctly. array( '/?sitemap=index', '/wp-sitemap.xml' ), array( '/wp-sitemap.xml/', '/wp-sitemap.xml' ),