From 7d9a297eb42f6777e6f92b915164db51729fc7a0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 14 Oct 2024 08:49:19 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@59228 git-svn-id: http://core.svn.wordpress.org/trunk@58620 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-rewrite.php | 7 +++++-- wp-includes/sitemaps/class-wp-sitemaps.php | 2 +- wp-includes/version.php | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/wp-includes/class-wp-rewrite.php b/wp-includes/class-wp-rewrite.php index 8bcf8fc962..0d081ada91 100644 --- a/wp-includes/class-wp-rewrite.php +++ b/wp-includes/class-wp-rewrite.php @@ -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 ); } /** diff --git a/wp-includes/sitemaps/class-wp-sitemaps.php b/wp-includes/sitemaps/class-wp-sitemaps.php index d897fa8f2a..a0fd5be624 100644 --- a/wp-includes/sitemaps/class-wp-sitemaps.php +++ b/wp-includes/sitemaps/class-wp-sitemaps.php @@ -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. diff --git a/wp-includes/version.php b/wp-includes/version.php index 49cad58eb5..e895dbef1f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-beta2-59227'; +$wp_version = '6.7-beta2-59228'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.