Sitemaps: Prevent incorrect redirection of paged sitemap requests.

Update `redirect_canonical()` to account for custom pagination and URL format used by sitemaps in order to follow standard practices.

Introduce the function `get_sitemap_url()` to simplify getting the index and provider URLs as needed.

Props jonathanstegall, pbiron, GamerZ, salvoaranzulla, peterwilsoncc.
Fixes #50910.


Built from https://develop.svn.wordpress.org/trunk@48872


git-svn-id: http://core.svn.wordpress.org/trunk@48634 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2020-08-27 01:30:04 +00:00
parent a861ce1b5f
commit 2db137784b
3 changed files with 42 additions and 8 deletions

View File

@ -410,8 +410,11 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
}
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.
if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
$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'];
}

View File

@ -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 );
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.6-alpha-48870';
$wp_version = '5.6-alpha-48872';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.