WordPress.org

Make WordPress Core

Changeset 48532


Ignore:
Timestamp:
07/21/2020 01:55:45 PM (6 months ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/sitemaps/class-wp-sitemaps-index.php

    r48470 r48532  
    2626
    2727    /**
     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
     36    /**
    2837     * WP_Sitemaps_Index constructor.
    2938     *
     
    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
     
    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
  • trunk/src/wp-includes/sitemaps/class-wp-sitemaps-registry.php

    r48098 r48532  
    2424     */
    2525    private $sitemaps = array();
    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;
    3526
    3627    /**
     
    7061
    7162    /**
    72      * Lists all registered sitemaps.
     63     * Returns all registered sitemap providers.
    7364     *
    7465     * @since 5.5.0
     
    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    }
  • trunk/tests/phpunit/includes/bootstrap.php

    r48115 r48532  
    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/**
  • trunk/tests/phpunit/tests/sitemaps/sitemaps-index.php

    r48072 r48532  
    1919        $sitemap_index = new WP_Sitemaps_Index( $registry );
    2020        $this->assertCount( 24, $sitemap_index->get_sitemap_list() );
     21    }
     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() );
    2144    }
    2245
Note: See TracChangeset for help on using the changeset viewer.