Make WordPress Core

Ticket #50666: 50666.2.diff

File 50666.2.diff, 6.8 KB (added by pbiron, 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 { 
    2424         */
    2525        protected $registry;
    2626
     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
    2736        /**
    2837         * WP_Sitemaps_Index constructor.
    2938         *
    class WP_Sitemaps_Index { 
    4756
    4857                $providers = $this->registry->get_sitemaps();
    4958                /* @var WP_Sitemaps_Provider $provider */
    50                 foreach ( $providers as $provider ) {
     59                foreach ( $providers as $name => $provider ) {
    5160                        $sitemap_entries = $provider->get_sitemap_entries();
    5261
    5362                        // Prevent issues with array_push and empty arrays on PHP < 7.3.
    class WP_Sitemaps_Index { 
    5766
    5867                        // Using array_push is more efficient than array_merge in a loop.
    5968                        array_push( $sitemaps, ...$sitemap_entries );
     69                        if ( count( $sitemaps ) >= $this->max_sitemaps ) {
     70                                break;
     71                        }
    6072                }
    6173
    62                 return $sitemaps;
     74                return array_slice( $sitemaps, 0, $this->max_sitemaps, true );
    6375        }
    6476
    6577        /**
  • 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 { 
    2424         */
    2525        private $sitemaps = array();
    2626
    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 
    3627        /**
    3728         * Adds a sitemap with route to the registry.
    3829         *
    class WP_Sitemaps_Registry { 
    6960        }
    7061
    7162        /**
    72          * Lists all registered sitemaps.
     63         * Returns all registered sitemap providers.
    7364         *
    7465         * @since 5.5.0
    7566         *
    7667         * @return WP_Sitemaps_Provider[] Array of sitemap providers.
    7768         */
    7869        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 
    8570                return $this->sitemaps;
    8671        }
    8772}
  • 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'; 
    162162require __DIR__ . '/class-wp-fake-block-type.php';
    163163require __DIR__ . '/class-wp-sitemaps-test-provider.php';
    164164require __DIR__ . '/class-wp-sitemaps-empty-test-provider.php';
     165require __DIR__ . '/class-wp-sitemaps-large-test-provider.php';
    165166
    166167/**
    167168 * 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 */
     8class 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 { 
    2020                $this->assertCount( 24, $sitemap_index->get_sitemap_list() );
    2121        }
    2222
     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
    2346        public function test_get_sitemap_list_no_entries() {
    2447                $registry = new WP_Sitemaps_Registry();
    2548