Sitemaps: Replace `wp_sitemaps_register_providers` filter with more suitable `wp_sitemaps_add_provider` filter.

The previous filter failed the goal of allowing developers to filter all providers before they are registered, since it only filtered the built-in ones.

The more specific `wp_sitemaps_add_provider` filter enables exactly that, as it filters every sitemap provider right before it is added to the sitemaps registry.

Props pbiron, pfefferle, Chouby, swissspidy.
Fixes #50660.
Built from https://develop.svn.wordpress.org/trunk@48543


git-svn-id: http://core.svn.wordpress.org/trunk@48305 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2020-07-21 16:14:07 +00:00
parent b6b2f2ac3f
commit 1f153b0d12
3 changed files with 20 additions and 24 deletions

View File

@ -25,19 +25,32 @@ class WP_Sitemaps_Registry {
private $providers = array(); private $providers = array();
/** /**
* Adds a sitemap with route to the registry. * Adds a new sitemap provider.
* *
* @since 5.5.0 * @since 5.5.0
* *
* @param string $name Name of the sitemap provider. * @param string $name Name of the sitemap provider.
* @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider. * @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider.
* @return bool True if the provider was added, false if it is already registered. * @return bool Whether the provider was added successfully.
*/ */
public function add_provider( $name, WP_Sitemaps_Provider $provider ) { public function add_provider( $name, WP_Sitemaps_Provider $provider ) {
if ( isset( $this->providers[ $name ] ) ) { if ( isset( $this->providers[ $name ] ) ) {
return false; return false;
} }
/**
* Filters the sitemap provider before it is added.
*
* @since 5.5.0
*
* @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider.
* @param string $name Name of the sitemap provider.
*/
$provider = apply_filters( 'wp_sitemaps_add_provider', $provider, $name );
if ( ! $provider instanceof WP_Sitemaps_Provider ) {
return false;
}
$this->providers[ $name ] = $provider; $this->providers[ $name ] = $provider;
return true; return true;

View File

@ -110,29 +110,12 @@ class WP_Sitemaps {
* @since 5.5.0 * @since 5.5.0
*/ */
public function register_sitemaps() { public function register_sitemaps() {
/** $providers = array(
* Filters the list of registered sitemap providers. 'posts' => new WP_Sitemaps_Posts(),
* 'taxonomies' => new WP_Sitemaps_Taxonomies(),
* @since 5.5.0 'users' => new WP_Sitemaps_Users(),
*
* @param array $providers {
* Array of WP_Sitemaps_Provider objects keyed by their name.
*
* @type WP_Sitemaps_Posts $posts The WP_Sitemaps_Posts object.
* @type WP_Sitemaps_Taxonomies $taxonomies The WP_Sitemaps_Taxonomies object.
* @type WP_Sitemaps_Users $users The WP_Sitemaps_Users object.
* }
*/
$providers = apply_filters(
'wp_sitemaps_register_providers',
array(
'posts' => new WP_Sitemaps_Posts(),
'taxonomies' => new WP_Sitemaps_Taxonomies(),
'users' => new WP_Sitemaps_Users(),
)
); );
// Register each supported provider.
/* @var WP_Sitemaps_Provider $provider */ /* @var WP_Sitemaps_Provider $provider */
foreach ( $providers as $name => $provider ) { foreach ( $providers as $name => $provider ) {
$this->registry->add_provider( $name, $provider ); $this->registry->add_provider( $name, $provider );

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-beta2-48542'; $wp_version = '5.5-beta2-48543';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.