1<?php
2/**
3 * Sitemaps: WP_Sitemaps_Index class.
4 *
5 * Generates the sitemap index.
6 *
7 * @package WordPress
8 * @subpackage Sitemaps
9 * @since 5.5.0
10 */
11
12/**
13 * Class WP_Sitemaps_Index.
14 * Builds the sitemap index page that lists the links to all of the sitemaps.
15 *
16 * @since 5.5.0
17 */
18#[AllowDynamicProperties]
19class WP_Sitemaps_Index {
20 /**
21 * The main registry of supported sitemaps.
22 *
23 * @since 5.5.0
24 * @var WP_Sitemaps_Registry
25 */
26 protected $registry;
27
28 /**
29 * Maximum number of sitemaps to include in an index.
30 *
31 * @since 5.5.0
32 *
33 * @var int Maximum number of sitemaps.
34 */
35 private $max_sitemaps = 50000;
36
37 /**
38 * WP_Sitemaps_Index constructor.
39 *
40 * @since 5.5.0
41 *
42 * @param WP_Sitemaps_Registry $registry Sitemap provider registry.
43 */
44 public function __construct( WP_Sitemaps_Registry $registry ) {
45 $this->registry = $registry;
46 }
47
48 /**
49 * Gets a sitemap list for the index.
50 *
51 * @since 5.5.0
52 *
53 * @return array[] Array of all sitemaps.
54 */
55 public function get_sitemap_list() {
56 $sitemaps = array();
57
58 $providers = $this->registry->get_providers();
59 /* @var WP_Sitemaps_Provider $provider */
60 foreach ( $providers as $name => $provider ) {
61 $sitemap_entries = $provider->get_sitemap_entries();
62
63 // Prevent issues with array_push and empty arrays on PHP < 7.3.
64 if ( ! $sitemap_entries ) {
65 continue;
66 }
67
68 // Using array_push is more efficient than array_merge in a loop.
69 array_push( $sitemaps, ...$sitemap_entries );
70 if ( count( $sitemaps ) >= $this->max_sitemaps ) {
71 break;
72 }
73 }
74
75 return array_slice( $sitemaps, 0, $this->max_sitemaps, true );
76 }
77
78 /**
79 * Builds the URL for the sitemap index.
80 *
81 * @since 5.5.0
82 *
83 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
84 *
85 * @return string The sitemap index URL.
86 */
87 public function get_index_url() {
88 global $wp_rewrite;
89
90 if ( ! $wp_rewrite->using_permalinks() ) {
91 return home_url( '/?sitemap=index' );
92 }
93
94 return home_url( '/wp-sitemap.xml' );
95 }
96}
97