Make WordPress Core


Ignore:
Timestamp:
08/27/2020 01:28:24 AM (5 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/sitemaps.php

    r48574 r48872  
    8888    return apply_filters( 'wp_sitemaps_max_urls', 2000, $object_type );
    8989}
     90
     91/**
     92 * Retrieves the full URL for a sitemap.
     93 *
     94 * @since 5.5.1
     95 *
     96 * @param string $name         The sitemap name.
     97 * @param string $subtype_name The sitemap subtype name.  Default empty string.
     98 * @param int    $page         The page of the sitemap.  Default 1.
     99 * @return string|false The sitemap URL or false if the sitemap doesn't exist.
     100 */
     101function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) {
     102    $sitemaps = wp_sitemaps_get_server();
     103    if ( ! $sitemaps ) {
     104        return false;
     105    }
     106
     107    if ( 'index' === $name ) {
     108        return $sitemaps->index->get_index_url();
     109    }
     110
     111    $provider = $sitemaps->registry->get_provider( $name );
     112    if ( ! $provider ) {
     113        return false;
     114    }
     115
     116    if ( $subtype_name && ! in_array( $subtype_name, array_keys( $provider->get_object_subtypes() ), true ) ) {
     117        return false;
     118    }
     119
     120    $page = absint( $page );
     121    if ( 0 >= $page ) {
     122        $page = 1;
     123    }
     124    return $provider->get_sitemap_url( $subtype_name, $page );
     125}
Note: See TracChangeset for help on using the changeset viewer.