wp/wp-includes/sitemaps/class-wp-sitemaps-index.php
changeset 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
       
     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 class WP_Sitemaps_Index {
       
    19 	/**
       
    20 	 * The main registry of supported sitemaps.
       
    21 	 *
       
    22 	 * @since 5.5.0
       
    23 	 * @var WP_Sitemaps_Registry
       
    24 	 */
       
    25 	protected $registry;
       
    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 
       
    36 	/**
       
    37 	 * WP_Sitemaps_Index constructor.
       
    38 	 *
       
    39 	 * @since 5.5.0
       
    40 	 *
       
    41 	 * @param WP_Sitemaps_Registry $registry Sitemap provider registry.
       
    42 	 */
       
    43 	public function __construct( WP_Sitemaps_Registry $registry ) {
       
    44 		$this->registry = $registry;
       
    45 	}
       
    46 
       
    47 	/**
       
    48 	 * Gets a sitemap list for the index.
       
    49 	 *
       
    50 	 * @since 5.5.0
       
    51 	 *
       
    52 	 * @return array[] Array of all sitemaps.
       
    53 	 */
       
    54 	public function get_sitemap_list() {
       
    55 		$sitemaps = array();
       
    56 
       
    57 		$providers = $this->registry->get_providers();
       
    58 		/* @var WP_Sitemaps_Provider $provider */
       
    59 		foreach ( $providers as $name => $provider ) {
       
    60 			$sitemap_entries = $provider->get_sitemap_entries();
       
    61 
       
    62 			// Prevent issues with array_push and empty arrays on PHP < 7.3.
       
    63 			if ( ! $sitemap_entries ) {
       
    64 				continue;
       
    65 			}
       
    66 
       
    67 			// Using array_push is more efficient than array_merge in a loop.
       
    68 			array_push( $sitemaps, ...$sitemap_entries );
       
    69 			if ( count( $sitemaps ) >= $this->max_sitemaps ) {
       
    70 				break;
       
    71 			}
       
    72 		}
       
    73 
       
    74 		return array_slice( $sitemaps, 0, $this->max_sitemaps, true );
       
    75 	}
       
    76 
       
    77 	/**
       
    78 	 * Builds the URL for the sitemap index.
       
    79 	 *
       
    80 	 * @since 5.5.0
       
    81 	 *
       
    82 	 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
       
    83 	 *
       
    84 	 * @return string The sitemap index URL.
       
    85 	 */
       
    86 	public function get_index_url() {
       
    87 		global $wp_rewrite;
       
    88 
       
    89 		if ( ! $wp_rewrite->using_permalinks() ) {
       
    90 			return home_url( '/?sitemap=index' );
       
    91 		}
       
    92 
       
    93 		return home_url( '/wp-sitemap.xml' );
       
    94 	}
       
    95 }