WordPress/wp-includes/sitemaps/class-wp-sitemaps-registry.php
Pascal Birchler 609dd1d14f Sitemaps: Add XML sitemaps functionality to WordPress.
While web crawlers are able to discover pages from links within the site and from other sites, XML sitemaps supplement this approach by allowing crawlers to quickly and comprehensively identify all URLs included in the sitemap and learn other signals about those URLs using the associated metadata.

See https://make.wordpress.org/core/2020/06/10/merge-announcement-extensible-core-sitemaps/ for more details.

This feature exposes the sitemap index via `/wp-sitemap.xml` and exposes a variety of new filters and hooks for developers to modify the behavior. Users can disable sitemaps completely by turning off search engine visibility in WordPress admin.

This change also introduces a new `esc_xml()` function to escape strings for output in XML, as well as XML support to `wp_kses_normalize_entities()`.

Props Adrian McShane, afragen, adamsilverstein, casiepa, flixos90, garrett-eclipse, joemcgill, kburgoine, kraftbj, milana_cap, pacifika, pbiron, pfefferle, Ruxandra Gradina, swissspidy, szepeviktor, tangrufus, tweetythierry.
Fixes #50117.
See #3670. See #19998.

Built from https://develop.svn.wordpress.org/trunk@48072


git-svn-id: http://core.svn.wordpress.org/trunk@47839 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-06-17 15:24:07 +00:00

88 lines
1.7 KiB
PHP

<?php
/**
* Sitemaps: WP_Sitemaps_Registry class
*
* Handles registering sitemaps.
*
* @package WordPress
* @subpackage Sitemaps
* @since 5.5.0
*/
/**
* Class WP_Sitemaps_Registry.
*
* @since 5.5.0
*/
class WP_Sitemaps_Registry {
/**
* Registered sitemaps.
*
* @since 5.5.0
*
* @var array Array of registered sitemaps.
*/
private $sitemaps = array();
/**
* Maximum number of sitemaps to include in an index.
*
* @sincee 5.5.0
*
* @var int Maximum number of sitemaps.
*/
private $max_sitemaps = 50000;
/**
* Adds a sitemap with route to the registry.
*
* @since 5.5.0
*
* @param string $name Name of the sitemap.
* @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider.
* @return bool True if the sitemap was added, false if it is already registered.
*/
public function add_sitemap( $name, WP_Sitemaps_Provider $provider ) {
if ( isset( $this->sitemaps[ $name ] ) ) {
return false;
}
$this->sitemaps[ $name ] = $provider;
return true;
}
/**
* Returns a single registered sitemaps provider.
*
* @since 5.5.0
*
* @param string $name Sitemap provider name.
* @return WP_Sitemaps_Provider|null Sitemaps provider if it exists, null otherwise.
*/
public function get_sitemap( $name ) {
if ( ! isset( $this->sitemaps[ $name ] ) ) {
return null;
}
return $this->sitemaps[ $name ];
}
/**
* Lists all registered sitemaps.
*
* @since 5.5.0
*
* @return array List of sitemaps.
*/
public function get_sitemaps() {
$total_sitemaps = count( $this->sitemaps );
if ( $total_sitemaps > $this->max_sitemaps ) {
return array_slice( $this->sitemaps, 0, $this->max_sitemaps, true );
}
return $this->sitemaps;
}
}