Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/wp-includes/sitemaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,27 @@ function wp_sitemaps_get_max_urls( $object_type ) {
* Retrieves the full URL for a sitemap.
*
* @since 5.5.1
* @since 7.1.0 Added $format parameter.
*
* @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.
* @param string $format The format for the sitemap index. Accepts 'xml', 'html'. Default 'xml'.
* @return string|false The sitemap URL or false if the sitemap doesn't exist.
*/
function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) {
function get_sitemap_url( $name, $subtype_name = '', $page = 1, $format = 'xml' ) {
$sitemaps = wp_sitemaps_get_server();

if ( ! $sitemaps ) {
return false;
}

if ( ! in_array( $format, array( 'xml', 'html' ), true ) ) {
$format = 'xml';
}

if ( 'index' === $name ) {
return $sitemaps->index->get_index_url();
return $sitemaps->index->get_index_url( $format );
}

$provider = $sitemaps->registry->get_provider( $name );
Expand All @@ -125,5 +131,5 @@ function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) {
$page = 1;
}

return $provider->get_sitemap_url( $subtype_name, $page );
return $provider->get_sitemap_url( $subtype_name, $page, $format );
}
22 changes: 17 additions & 5 deletions src/wp-includes/sitemaps/class-wp-sitemaps-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,22 @@ public function __construct( WP_Sitemaps_Registry $registry ) {
* Gets a sitemap list for the index.
*
* @since 5.5.0
* @since 7.1.0 Added $format parameter.
*
* @param string $format The format for the sitemap index. Accepts 'xml', 'html'.
* @return array[] Array of all sitemaps.
*/
public function get_sitemap_list() {
public function get_sitemap_list( $format ) {
$sitemaps = array();

if ( ! in_array( $format, array( 'xml', 'html' ), true ) ) {
$format = 'xml';
}

$providers = $this->registry->get_providers();
/* @var WP_Sitemaps_Provider $provider */
foreach ( $providers as $name => $provider ) {
$sitemap_entries = $provider->get_sitemap_entries();
$sitemap_entries = $provider->get_sitemap_entries( $format );

// Prevent issues with array_push and empty arrays on PHP < 7.3.
if ( ! $sitemap_entries ) {
Expand All @@ -79,18 +85,24 @@ public function get_sitemap_list() {
* Builds the URL for the sitemap index.
*
* @since 5.5.0
* @since 7.1.0 Added $format parameter.
*
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
*
* @param string $format The format for the sitemap index. Accepts 'xml', 'html'.
* @return string The sitemap index URL.
*/
public function get_index_url() {
public function get_index_url( $format ) {
global $wp_rewrite;

if ( ! in_array( $format, array( 'xml', 'html' ), true ) ) {
$format = 'xml';
}

if ( ! $wp_rewrite->using_permalinks() ) {
return home_url( '/?sitemap=index' );
return home_url( '/?sitemap=index&sitemap-format=' . $format );
}

return home_url( '/wp-sitemap.xml' );
return home_url( "/wp-sitemap.{$format}" );
}
}
33 changes: 25 additions & 8 deletions src/wp-includes/sitemaps/class-wp-sitemaps-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,40 @@ public function get_sitemap_type_data() {
* The returned data is used to populate the sitemap entries of the index.
*
* @since 5.5.0
* @since 7.1.0 Added $format parameter.
*
* @param string $format The format for the sitemap index. Accepts 'xml', 'html'.
* @return array[] Array of sitemap entries.
*/
public function get_sitemap_entries() {
public function get_sitemap_entries( $format ) {
$sitemaps = array();

if ( ! in_array( $format, array( 'xml', 'html' ), true ) ) {
$format = 'xml';
}

$sitemap_types = $this->get_sitemap_type_data();

foreach ( $sitemap_types as $type ) {
for ( $page = 1; $page <= $type['pages']; $page++ ) {
$sitemap_entry = array(
'loc' => $this->get_sitemap_url( $type['name'], $page ),
'loc' => $this->get_sitemap_url( $type['name'], $page, $format ),
);

/**
* Filters the sitemap entry for the sitemap index.
*
* @since 5.5.0
* @since 7.1.0 Added $format parameter.
*
* @param array $sitemap_entry Sitemap entry for the post.
* @param string $object_type Object empty name.
* @param string $object_subtype Object subtype name.
* Empty string if the object type does not support subtypes.
* @param int $page Page number of results.
* @param string $format The format for the sitemap index. Accepts 'xml', 'html'.
*/
$sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page );
$sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page, $format );

$sitemaps[] = $sitemap_entry;
}
Expand All @@ -138,14 +146,16 @@ public function get_sitemap_entries() {
* Gets the URL of a sitemap entry.
*
* @since 5.5.0
* @since 7.1.0 Added $format parameter.
*
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
*
* @param string $name The name of the sitemap.
* @param int $page The page of the sitemap.
* @param string $name The name of the sitemap.
* @param int $page The page of the sitemap.
* @param string $format The format for the sitemap index. Accepts 'xml', 'html'.
* @return string The composed URL for a sitemap entry.
*/
public function get_sitemap_url( $name, $page ) {
public function get_sitemap_url( $name, $page, $format ) {
global $wp_rewrite;

// Accounts for cases where name is not included, ex: sitemaps-users-1.xml.
Expand All @@ -157,12 +167,19 @@ public function get_sitemap_url( $name, $page ) {
)
);

if ( ! in_array( $format, array( 'xml', 'html' ), true ) ) {
$format = 'xml';
}

$basename = sprintf(
'/wp-sitemap-%1$s.xml',
implode( '-', $params )
'/wp-sitemap-%1$s.%2$s',
implode( '-', $params ),
$format
);

if ( ! $wp_rewrite->using_permalinks() ) {
$params['sitemap-format'] = $format;

$basename = '/?' . http_build_query( $params, '', '&' );
}

Expand Down
Loading
Loading