Ticket #50666: 50666.2.diff
File 50666.2.diff, 6.8 KB (added by , 4 years ago) |
---|
-
src/wp-includes/sitemaps/class-wp-sitemaps-index.php
From 675423f79b3e1fb25b44efa73bd3805ab342035b Mon Sep 17 00:00:00 2001 From: Paul Biron <paul@sparrowhawkcomputing.com> Date: Mon, 20 Jul 2020 15:47:21 -0600 Subject: [PATCH] no message --- .../sitemaps/class-wp-sitemaps-index.php | 16 ++++- .../sitemaps/class-wp-sitemaps-registry.php | 17 +----- tests/phpunit/includes/bootstrap.php | 1 + .../class-wp-sitemaps-large-test-provider.php | 59 +++++++++++++++++++ .../phpunit/tests/sitemaps/sitemaps-index.php | 23 ++++++++ 5 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 tests/phpunit/includes/class-wp-sitemaps-large-test-provider.php diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-index.php b/src/wp-includes/sitemaps/class-wp-sitemaps-index.php index 4ec1b07d92..91ceea0ae0 100644
a b class WP_Sitemaps_Index { 24 24 */ 25 25 protected $registry; 26 26 27 /** 28 * Maximum number of sitemaps to include in an index. 29 * 30 * @sincee 5.5.0 31 * 32 * @var int Maximum number of sitemaps. 33 */ 34 private $max_sitemaps = 50000; 35 27 36 /** 28 37 * WP_Sitemaps_Index constructor. 29 38 * … … class WP_Sitemaps_Index { 47 56 48 57 $providers = $this->registry->get_sitemaps(); 49 58 /* @var WP_Sitemaps_Provider $provider */ 50 foreach ( $providers as $ provider ) {59 foreach ( $providers as $name => $provider ) { 51 60 $sitemap_entries = $provider->get_sitemap_entries(); 52 61 53 62 // Prevent issues with array_push and empty arrays on PHP < 7.3. … … class WP_Sitemaps_Index { 57 66 58 67 // Using array_push is more efficient than array_merge in a loop. 59 68 array_push( $sitemaps, ...$sitemap_entries ); 69 if ( count( $sitemaps ) >= $this->max_sitemaps ) { 70 break; 71 } 60 72 } 61 73 62 return $sitemaps;74 return array_slice( $sitemaps, 0, $this->max_sitemaps, true ); 63 75 } 64 76 65 77 /** -
src/wp-includes/sitemaps/class-wp-sitemaps-registry.php
diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-registry.php b/src/wp-includes/sitemaps/class-wp-sitemaps-registry.php index c232764924..c95f04c77f 100644
a b class WP_Sitemaps_Registry { 24 24 */ 25 25 private $sitemaps = array(); 26 26 27 /**28 * Maximum number of sitemaps to include in an index.29 *30 * @sincee 5.5.031 *32 * @var int Maximum number of sitemaps.33 */34 private $max_sitemaps = 50000;35 36 27 /** 37 28 * Adds a sitemap with route to the registry. 38 29 * … … class WP_Sitemaps_Registry { 69 60 } 70 61 71 62 /** 72 * Lists all registered sitemaps.63 * Returns all registered sitemap providers. 73 64 * 74 65 * @since 5.5.0 75 66 * 76 67 * @return WP_Sitemaps_Provider[] Array of sitemap providers. 77 68 */ 78 69 public function get_sitemaps() { 79 $total_sitemaps = count( $this->sitemaps );80 81 if ( $total_sitemaps > $this->max_sitemaps ) {82 return array_slice( $this->sitemaps, 0, $this->max_sitemaps, true );83 }84 85 70 return $this->sitemaps; 86 71 } 87 72 } -
tests/phpunit/includes/bootstrap.php
diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index 817aea3bbe..8c88fc45e2 100644
a b require __DIR__ . '/class-wp-rest-test-configurable-controller.php'; 162 162 require __DIR__ . '/class-wp-fake-block-type.php'; 163 163 require __DIR__ . '/class-wp-sitemaps-test-provider.php'; 164 164 require __DIR__ . '/class-wp-sitemaps-empty-test-provider.php'; 165 require __DIR__ . '/class-wp-sitemaps-large-test-provider.php'; 165 166 166 167 /** 167 168 * A class to handle additional command line arguments passed to the script. -
new file tests/phpunit/includes/class-wp-sitemaps-large-test-provider.php
diff --git a/tests/phpunit/includes/class-wp-sitemaps-large-test-provider.php b/tests/phpunit/includes/class-wp-sitemaps-large-test-provider.php new file mode 100644 index 0000000000..67563c654d
- + 1 <?php 2 3 /** 4 * Class WP_Sitemaps_Large_Test_Provider. 5 * 6 * Provides test data for additional registered providers. 7 */ 8 class WP_Sitemaps_Large_Test_Provider extends WP_Sitemaps_Provider { 9 /** 10 * Number of entries in the sitemap the provider produces. 11 * 12 * @var integer 13 */ 14 public $num_entries = 1; 15 16 /** 17 * WP_Sitemaps_Large_Test_Provider constructor. 18 * 19 * @param int $num_entries Optional. Number of entries in in the sitemap. 20 */ 21 public function __construct( $num_entries = 50001 ) { 22 $this->name = 'tests'; 23 $this->object_type = 'test'; 24 25 $this->num_entries = $num_entries; 26 } 27 28 /** 29 * Gets a URL list for a sitemap. 30 * 31 * @param int $page_num Page of results. 32 * @param string $object_subtype Optional. Object subtype name. Default empty. 33 * @return array List of URLs for a sitemap. 34 */ 35 public function get_url_list( $page_num, $object_subtype = '' ) { 36 return array_fill( 0, $this->num_entries, array( 'loc' => home_url( '/' ) ) ); 37 } 38 39 /** 40 * Lists sitemap pages exposed by this provider. 41 * 42 * The returned data is used to populate the sitemap entries of the index. 43 * 44 * @return array[] Array of sitemap entries. 45 */ 46 public function get_sitemap_entries() { 47 return array_fill( 0, $this->num_entries, array( 'loc' => home_url( '/' ) ) ); 48 } 49 50 /** 51 * Query for determining the number of pages. 52 * 53 * @param string $object_subtype Optional. Object subtype. Default empty. 54 * @return int Total number of pages. 55 */ 56 public function get_max_num_pages( $object_subtype = '' ) { 57 return $this->num_entries; 58 } 59 } -
tests/phpunit/tests/sitemaps/sitemaps-index.php
diff --git a/tests/phpunit/tests/sitemaps/sitemaps-index.php b/tests/phpunit/tests/sitemaps/sitemaps-index.php index 5df604af8a..56b92be569 100644
a b class Test_WP_Sitemaps_Index extends WP_UnitTestCase { 20 20 $this->assertCount( 24, $sitemap_index->get_sitemap_list() ); 21 21 } 22 22 23 /** 24 * Test that a sitemap index won't contain more than 50000 sitemaps. 25 * 26 * @ticket 50666 27 */ 28 public function test_get_sitemap_list_limit() { 29 $registry = new WP_Sitemaps_Registry(); 30 31 // add 3 providers, which combined produce more than the maximum 50000 sitemaps in the index. 32 $registry->add_sitemap( 'provider_1', new WP_Sitemaps_Large_Test_Provider( 25000 ) ); 33 $registry->add_sitemap( 'provider_2', new WP_Sitemaps_Large_Test_Provider( 25000 ) ); 34 $registry->add_sitemap( 'provider_3', new WP_Sitemaps_Large_Test_Provider( 25000 ) ); 35 36 $count = 0; 37 foreach ( $registry->get_sitemaps() as $provider ) { 38 $count += count( $provider->get_url_list( 1 ) ); 39 } 40 $this->assertGreaterThan( 50000, $count ); 41 42 $sitemap_index = new WP_Sitemaps_Index( $registry ); 43 $this->assertCount( 50000, $sitemap_index->get_sitemap_list() ); 44 } 45 23 46 public function test_get_sitemap_list_no_entries() { 24 47 $registry = new WP_Sitemaps_Registry(); 25 48