diff --git a/wp-includes/sitemaps.php b/wp-includes/sitemaps.php index 8678993dbd..9600540156 100644 --- a/wp-includes/sitemaps.php +++ b/wp-includes/sitemaps.php @@ -17,26 +17,13 @@ * * @global WP_Sitemaps $wp_sitemaps Global Core Sitemaps instance. * - * @return WP_Sitemaps|null Sitemaps instance, or null if sitemaps are disabled. + * @return WP_Sitemaps Sitemaps instance. */ function wp_sitemaps_get_server() { global $wp_sitemaps; $is_enabled = (bool) get_option( 'blog_public' ); - /** - * Filters whether XML Sitemaps are enabled or not. - * - * @since 5.5.0 - * - * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults to true for public sites. - */ - $is_enabled = (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled ); - - if ( ! $is_enabled ) { - return null; - } - // If there isn't a global instance, set and bootstrap the sitemaps system. if ( empty( $wp_sitemaps ) ) { $wp_sitemaps = new WP_Sitemaps(); diff --git a/wp-includes/sitemaps/class-wp-sitemaps.php b/wp-includes/sitemaps/class-wp-sitemaps.php index 918220317b..828e7931dc 100644 --- a/wp-includes/sitemaps/class-wp-sitemaps.php +++ b/wp-includes/sitemaps/class-wp-sitemaps.php @@ -56,19 +56,54 @@ class WP_Sitemaps { /** * Initiates all sitemap functionality. * + * If sitemaps are disabled, only the rewrite rules will be registered + * by this method, in order to properly send 404s. + * * @since 5.5.0 */ public function init() { // These will all fire on the init hook. $this->register_rewrites(); + + add_action( 'template_redirect', array( $this, 'render_sitemaps' ) ); + + if ( ! $this->sitemaps_enabled() ) { + return; + } + $this->register_sitemaps(); // Add additional action callbacks. - add_action( 'template_redirect', array( $this, 'render_sitemaps' ) ); add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 ); add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 ); } + /** + * Determines whether sitemaps are enabled or not. + * + * @since 5.5.0 + * + * @return bool Whether sitemaps are enabled. + */ + public function sitemaps_enabled() { + $is_enabled = (bool) get_option( 'blog_public' ); + + /** + * Filters whether XML Sitemaps are enabled or not. + * + * When XML Sitemaps are disabled via this filter, rewrite rules are still + * in place to ensure a 404 is returned. + * + * @see WP_Sitemaps::register_rewrites() + * + * @since 5.5.0 + * + * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults + * to true for public sites. + */ + return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled ); + } + /** * Registers and sets up the functionality for all supported sitemaps. * @@ -155,6 +190,12 @@ class WP_Sitemaps { return; } + if ( ! $this->sitemaps_enabled() ) { + $wp_query->set_404(); + status_header( 404 ); + return; + } + // Render stylesheet if this is stylesheet route. if ( $stylesheet_type ) { $stylesheet = new WP_Sitemaps_Stylesheet(); @@ -186,6 +227,7 @@ class WP_Sitemaps { // Force a 404 and bail early if no URLs are present. if ( empty( $url_list ) ) { $wp_query->set_404(); + status_header( 404 ); return; } diff --git a/wp-includes/version.php b/wp-includes/version.php index be325d64da..9892f5d23b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-beta2-48522'; +$wp_version = '5.5-beta2-48523'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.