Commit b959c21d authored by Peter Wilson's avatar Peter Wilson
Browse files

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.
Merges [48872] to the 5.5 branch.
Fixes #50910.


Built from https://develop.svn.wordpress.org/branches/5.5@48873


git-svn-id: https://core.svn.wordpress.org/branches/5.5@48635 1a063a9b-81f0-0310-95a4-ce76da25c4cd
parent 12f915a5
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -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'];
		}

+36 −0
Original line number Diff line number Diff line
@@ -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 );
}
+1 −1
Original line number Diff line number Diff line
@@ -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.