Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/wp-includes/sitemaps/class-wp-sitemaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function register_sitemaps() {
* Registers sitemap rewrite tags and routing rules.
*
* @since 5.5.0
* @since 7.3.0 Provider names may contain digits and underscores.
*/
public function register_rewrites() {
// Add rewrite tags.
Expand All @@ -137,12 +138,12 @@ public function register_rewrites() {

// Register routes for providers.
add_rewrite_rule(
'^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
'^wp-sitemap-([a-z\d_-]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&sitemap-subtype=$matches[2]&paged=$matches[3]',
'top'
);
add_rewrite_rule(
'^wp-sitemap-([a-z]+?)-(\d+?)\.xml$',
'^wp-sitemap-([a-z\d_-]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&paged=$matches[2]',
'top'
);
Expand All @@ -152,6 +153,7 @@ public function register_rewrites() {
* Renders sitemap templates based on rewrite rules.
*
* @since 5.5.0
* @since 7.3.0 Resolves providers whose names contain dashes.
*/
public function render_sitemaps() {
/*
Expand Down Expand Up @@ -192,6 +194,25 @@ public function render_sitemaps() {

$provider = $this->registry->get_provider( $sitemap );

if ( $object_subtype && ( ! $provider || ! array_key_exists( $object_subtype, $provider->get_object_subtypes() ) ) ) {
$route = $sitemap . '-' . $object_subtype;

if ( $this->registry->get_provider( $route ) ) {
$provider = $this->registry->get_provider( $route );
$object_subtype = '';
} else {
foreach ( $this->registry->get_providers() as $name => $candidate ) {
if ( str_starts_with( $route, $name . '-' )
&& array_key_exists( substr( $route, strlen( $name ) + 1 ), $candidate->get_object_subtypes() )
) {
$provider = $candidate;
$object_subtype = substr( $route, strlen( $name ) + 1 );
break;
}
}
}
}

// Force a 404 and bail early if the requested provider is not registered.
if ( ! $provider ) {
$this->send_404();
Expand Down
Loading