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.

git-svn-id: https://develop.svn.wordpress.org/trunk@59228 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2024-10-14 08:47:34 +00:00
parent 1dcbe1096e
commit f9bd807866
3 changed files with 22 additions and 4 deletions

View File

@ -1287,6 +1287,9 @@ class WP_Rewrite {
// 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 @@ class WP_Rewrite {
// 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 );
}
/**

View File

@ -75,7 +75,6 @@ class WP_Sitemaps {
$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 @@ class WP_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.

View File

@ -51,6 +51,17 @@ class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase {
$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 @@ class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase {
* @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' ),