Make WordPress Core

Ticket #50666: 50666.diff

File 50666.diff, 4.9 KB (added by swissspidy, 4 years ago)
  • src/wp-includes/sitemaps/class-wp-sitemaps-index.php

    diff --git src/wp-includes/sitemaps/class-wp-sitemaps-index.php src/wp-includes/sitemaps/class-wp-sitemaps-index.php
    index 4ec1b07d92..4c93d18031 100644
    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 { 
    5968                        array_push( $sitemaps, ...$sitemap_entries );
    6069                }
    6170
    62                 return $sitemaps;
     71                return array_slice( $sitemaps, 0, $this->max_sitemaps, true );
    6372        }
    6473
    6574        /**
  • src/wp-includes/sitemaps/class-wp-sitemaps-registry.php

    diff --git src/wp-includes/sitemaps/class-wp-sitemaps-registry.php src/wp-includes/sitemaps/class-wp-sitemaps-registry.php
    index c232764924..c95f04c77f 100644
    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 tests/phpunit/includes/bootstrap.php tests/phpunit/includes/bootstrap.php
    index 817aea3bbe..8c88fc45e2 100644
    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 tests/phpunit/includes/class-wp-sitemaps-large-test-provider.php tests/phpunit/includes/class-wp-sitemaps-large-test-provider.php
    new file mode 100644
    index 0000000000..c2fbb11192
    - +  
     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         * WP_Sitemaps_Large_Test_Provider constructor.
     11         *
     12         * @param string $object_type Optional. Object type name to use. Default 'test'.
     13         */
     14        public function __construct( $object_type = 'test' ) {
     15                $this->object_type = $object_type;
     16        }
     17
     18        /**
     19         * Gets a URL list for a sitemap.
     20         *
     21         * @param int    $page_num       Page of results.
     22         * @param string $object_subtype Optional. Object subtype name. Default empty.
     23         * @return array List of URLs for a sitemap.
     24         */
     25        public function get_url_list( $page_num, $object_subtype = '' ) {
     26                return array_fill(0, 1000000, array( 'loc' => home_url( '/' ) ) );
     27        }
     28
     29        /**
     30         * Lists sitemap pages exposed by this provider.
     31         *
     32         * The returned data is used to populate the sitemap entries of the index.
     33         *
     34         * @return array[] Array of sitemap entries.
     35         */
     36        public function get_sitemap_entries() {
     37                return array_fill(0, 1000000, array( 'loc' => home_url( '/' ) ) );
     38        }
     39
     40        /**
     41         * Query for determining the number of pages.
     42         *
     43         * @param string $object_subtype Optional. Object subtype. Default empty.
     44         * @return int Total number of pages.
     45         */
     46        public function get_max_num_pages( $object_subtype = '' ) {
     47                return 1000000;
     48        }
     49}
  • tests/phpunit/tests/sitemaps/sitemaps-index.php

    diff --git tests/phpunit/tests/sitemaps/sitemaps-index.php tests/phpunit/tests/sitemaps/sitemaps-index.php
    index 5df604af8a..88b9294509 100644
    class Test_WP_Sitemaps_Index extends WP_UnitTestCase { 
    2020                $this->assertCount( 24, $sitemap_index->get_sitemap_list() );
    2121        }
    2222
     23        /**
     24         * @ticket 50666
     25         */
     26        public function test_get_sitemap_list_limit() {
     27                $registry = new WP_Sitemaps_Registry();
     28                $registry->add_sitemap( 'foo', new WP_Sitemaps_Large_Test_Provider() );
     29                $sitemap_index = new WP_Sitemaps_Index( $registry );
     30                $this->assertCount( 50000, $sitemap_index->get_sitemap_list() );
     31        }
     32
    2333        public function test_get_sitemap_list_no_entries() {
    2434                $registry = new WP_Sitemaps_Registry();
    2535