From 1f2791419119ba86b1bb983706ce0989447b7a3a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 21 Jul 2020 13:57:05 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@48532 git-svn-id: http://core.svn.wordpress.org/trunk@48294 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../sitemaps/class-wp-sitemaps-index.php | 16 ++++++++++++++-- .../sitemaps/class-wp-sitemaps-registry.php | 17 +---------------- wp-includes/version.php | 2 +- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/wp-includes/sitemaps/class-wp-sitemaps-index.php b/wp-includes/sitemaps/class-wp-sitemaps-index.php index 4ec1b07d92..91ceea0ae0 100644 --- a/wp-includes/sitemaps/class-wp-sitemaps-index.php +++ b/wp-includes/sitemaps/class-wp-sitemaps-index.php @@ -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 ); } /** diff --git a/wp-includes/sitemaps/class-wp-sitemaps-registry.php b/wp-includes/sitemaps/class-wp-sitemaps-registry.php index c232764924..c95f04c77f 100644 --- a/wp-includes/sitemaps/class-wp-sitemaps-registry.php +++ b/wp-includes/sitemaps/class-wp-sitemaps-registry.php @@ -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; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 201b43246a..cf6f6d8f37 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-beta2-48531'; +$wp_version = '5.5-beta2-48532'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.