Sitemaps: Correctly enforce maximum number of sitemaps in index.

Before this change, the limit of 50k entries was enforced for the number of providers, not the amount of sitemaps all providers add to the index in total.

Props pbiron, swissspidy.
Fixes #50666.

git-svn-id: https://develop.svn.wordpress.org/trunk@48532 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2020-07-21 13:55:45 +00:00
parent fd9f7232e5
commit 38cade3b07
5 changed files with 98 additions and 18 deletions
@@ -24,6 +24,15 @@ class WP_Sitemaps_Index {
*/
protected $registry;
/**
* Maximum number of sitemaps to include in an index.
*
* @sincee 5.5.0
*
* @var int Maximum number of sitemaps.
*/
private $max_sitemaps = 50000;
/**
* WP_Sitemaps_Index constructor.
*
@@ -47,7 +56,7 @@ class WP_Sitemaps_Index {
$providers = $this->registry->get_sitemaps();
/* @var WP_Sitemaps_Provider $provider */
foreach ( $providers as $provider ) {
foreach ( $providers as $name => $provider ) {
$sitemap_entries = $provider->get_sitemap_entries();
// Prevent issues with array_push and empty arrays on PHP < 7.3.
@@ -57,9 +66,12 @@ class WP_Sitemaps_Index {
// Using array_push is more efficient than array_merge in a loop.
array_push( $sitemaps, ...$sitemap_entries );
if ( count( $sitemaps ) >= $this->max_sitemaps ) {
break;
}
}
return $sitemaps;
return array_slice( $sitemaps, 0, $this->max_sitemaps, true );
}
/**
@@ -24,15 +24,6 @@ class WP_Sitemaps_Registry {
*/
private $sitemaps = array();
/**
* Maximum number of sitemaps to include in an index.
*
* @sincee 5.5.0
*
* @var int Maximum number of sitemaps.
*/
private $max_sitemaps = 50000;
/**
* Adds a sitemap with route to the registry.
*
@@ -69,19 +60,13 @@ class WP_Sitemaps_Registry {
}
/**
* Lists all registered sitemaps.
* Returns all registered sitemap providers.
*
* @since 5.5.0
*
* @return WP_Sitemaps_Provider[] Array of sitemap providers.
*/
public function get_sitemaps() {
$total_sitemaps = count( $this->sitemaps );
if ( $total_sitemaps > $this->max_sitemaps ) {
return array_slice( $this->sitemaps, 0, $this->max_sitemaps, true );
}
return $this->sitemaps;
}
}