From 1f153b0d12ada5b7cf2661dcd795643df13a7ae2 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 21 Jul 2020 16:14:07 +0000 Subject: [PATCH] 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 --- .../sitemaps/class-wp-sitemaps-registry.php | 17 +++++++++++-- wp-includes/sitemaps/class-wp-sitemaps.php | 25 +++---------------- wp-includes/version.php | 2 +- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/wp-includes/sitemaps/class-wp-sitemaps-registry.php b/wp-includes/sitemaps/class-wp-sitemaps-registry.php index f4aec2f41a..5cb2e2fa45 100644 --- a/wp-includes/sitemaps/class-wp-sitemaps-registry.php +++ b/wp-includes/sitemaps/class-wp-sitemaps-registry.php @@ -25,19 +25,32 @@ class WP_Sitemaps_Registry { private $providers = array(); /** - * Adds a sitemap with route to the registry. + * Adds a new sitemap provider. * * @since 5.5.0 * * @param string $name Name of the sitemap 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 ) { if ( isset( $this->providers[ $name ] ) ) { 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; return true; diff --git a/wp-includes/sitemaps/class-wp-sitemaps.php b/wp-includes/sitemaps/class-wp-sitemaps.php index 01f1bf8b35..ff79d711f2 100644 --- a/wp-includes/sitemaps/class-wp-sitemaps.php +++ b/wp-includes/sitemaps/class-wp-sitemaps.php @@ -110,29 +110,12 @@ class WP_Sitemaps { * @since 5.5.0 */ public function register_sitemaps() { - /** - * Filters the list of registered sitemap providers. - * - * @since 5.5.0 - * - * @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(), - ) + $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 */ foreach ( $providers as $name => $provider ) { $this->registry->add_provider( $name, $provider ); diff --git a/wp-includes/version.php b/wp-includes/version.php index c0698543d6..dd0448b8f0 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @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.