From 2db137784b204b2ce8fdde7b13c0af02350c81f5 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 27 Aug 2020 01:30:04 +0000 Subject: [PATCH] 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 --- wp-includes/canonical.php | 12 +++++------- wp-includes/sitemaps.php | 36 ++++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) 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 483278d7af..3a122aba4f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.