diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php index 5a63de3481..6a74ac2d43 100644 --- a/wp-includes/canonical.php +++ b/wp-includes/canonical.php @@ -410,8 +410,11 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); } - // Paging and feeds. - if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { + if ( get_query_var( 'sitemap' ) ) { + $redirect_url = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) ); + $redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] ); + } elseif ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { + // Paging and feeds. $paged = get_query_var( 'paged' ); $feed = get_query_var( 'feed' ); $cpage = get_query_var( 'cpage' ); @@ -508,11 +511,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; } - // Remove trailing slash for sitemaps requests. - if ( ! empty( get_query_var( 'sitemap' ) ) ) { - $redirect['path'] = untrailingslashit( $redirect['path'] ); - } - $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; } diff --git a/wp-includes/sitemaps.php b/wp-includes/sitemaps.php index 62752dada9..1dfcd1d69c 100644 --- a/wp-includes/sitemaps.php +++ b/wp-includes/sitemaps.php @@ -87,3 +87,39 @@ function wp_sitemaps_get_max_urls( $object_type ) { */ return apply_filters( 'wp_sitemaps_max_urls', 2000, $object_type ); } + +/** + * Retrieves the full URL for a sitemap. + * + * @since 5.5.1 + * + * @param string $name The sitemap name. + * @param string $subtype_name The sitemap subtype name. Default empty string. + * @param int $page The page of the sitemap. Default 1. + * @return string|false The sitemap URL or false if the sitemap doesn't exist. + */ +function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) { + $sitemaps = wp_sitemaps_get_server(); + if ( ! $sitemaps ) { + return false; + } + + if ( 'index' === $name ) { + return $sitemaps->index->get_index_url(); + } + + $provider = $sitemaps->registry->get_provider( $name ); + if ( ! $provider ) { + return false; + } + + if ( $subtype_name && ! in_array( $subtype_name, array_keys( $provider->get_object_subtypes() ), true ) ) { + return false; + } + + $page = absint( $page ); + if ( 0 >= $page ) { + $page = 1; + } + return $provider->get_sitemap_url( $subtype_name, $page ); +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 5c4ffc7cb5..79b4d0c1b6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5.1-alpha-48871'; +$wp_version = '5.5.1-alpha-48873'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.