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 11:24:07 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Sitemaps: WP_Sitemaps_Index class.
|
|
|
|
*
|
|
|
|
* Generates the sitemap index.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Sitemaps
|
|
|
|
* @since 5.5.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WP_Sitemaps_Index.
|
|
|
|
* Builds the sitemap index page that lists the links to all of the sitemaps.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*/
|
|
|
|
class WP_Sitemaps_Index {
|
|
|
|
/**
|
|
|
|
* The main registry of supported sitemaps.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
* @var WP_Sitemaps_Registry
|
|
|
|
*/
|
|
|
|
protected $registry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WP_Sitemaps_Index constructor.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
|
|
|
* @param WP_Sitemaps_Registry $registry Sitemap provider registry.
|
|
|
|
*/
|
|
|
|
public function __construct( WP_Sitemaps_Registry $registry ) {
|
|
|
|
$this->registry = $registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a sitemap list for the index.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
|
|
|
* @return array List of all sitemaps.
|
|
|
|
*/
|
|
|
|
public function get_sitemap_list() {
|
|
|
|
$sitemaps = array();
|
|
|
|
|
|
|
|
$providers = $this->registry->get_sitemaps();
|
|
|
|
/* @var WP_Sitemaps_Provider $provider */
|
|
|
|
foreach ( $providers as $provider ) {
|
|
|
|
$sitemap_entries = $provider->get_sitemap_entries();
|
|
|
|
|
|
|
|
// Prevent issues with array_push and empty arrays on PHP < 7.3.
|
|
|
|
if ( ! $sitemap_entries ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using array_push is more efficient than array_merge in a loop.
|
|
|
|
array_push( $sitemaps, ...$sitemap_entries );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sitemaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the URL for the sitemap index.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
2020-06-19 13:56:09 -04:00
|
|
|
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
|
|
|
|
*
|
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 11:24:07 -04:00
|
|
|
* @return string The sitemap index url.
|
|
|
|
*/
|
|
|
|
public function get_index_url() {
|
|
|
|
global $wp_rewrite;
|
|
|
|
|
|
|
|
if ( ! $wp_rewrite->using_permalinks() ) {
|
|
|
|
return add_query_arg( 'sitemap', 'index', home_url( '/' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return home_url( '/wp-sitemap.xml' );
|
|
|
|
}
|
|
|
|
}
|