diff --git src/wp-includes/sitemaps.php src/wp-includes/sitemaps.php
index 8678993dbd..9600540156 100644
|
|
|
17 | 17 | * |
18 | 18 | * @global WP_Sitemaps $wp_sitemaps Global Core Sitemaps instance. |
19 | 19 | * |
20 | | * @return WP_Sitemaps|null Sitemaps instance, or null if sitemaps are disabled. |
| 20 | * @return WP_Sitemaps Sitemaps instance. |
21 | 21 | */ |
22 | 22 | function wp_sitemaps_get_server() { |
23 | 23 | global $wp_sitemaps; |
24 | 24 | |
25 | 25 | $is_enabled = (bool) get_option( 'blog_public' ); |
26 | 26 | |
27 | | /** |
28 | | * Filters whether XML Sitemaps are enabled or not. |
29 | | * |
30 | | * @since 5.5.0 |
31 | | * |
32 | | * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults to true for public sites. |
33 | | */ |
34 | | $is_enabled = (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled ); |
35 | | |
36 | | if ( ! $is_enabled ) { |
37 | | return null; |
38 | | } |
39 | | |
40 | 27 | // If there isn't a global instance, set and bootstrap the sitemaps system. |
41 | 28 | if ( empty( $wp_sitemaps ) ) { |
42 | 29 | $wp_sitemaps = new WP_Sitemaps(); |
diff --git src/wp-includes/sitemaps/class-wp-sitemaps.php src/wp-includes/sitemaps/class-wp-sitemaps.php
index 918220317b..03b6d0c862 100644
|
|
class WP_Sitemaps { |
56 | 56 | /** |
57 | 57 | * Initiates all sitemap functionality. |
58 | 58 | * |
| 59 | * If sitemaps are disabled, only the rewrite rules will be registered |
| 60 | * by this method, in order to properly send 404s. |
| 61 | * |
59 | 62 | * @since 5.5.0 |
60 | 63 | */ |
61 | 64 | public function init() { |
62 | 65 | // These will all fire on the init hook. |
63 | 66 | $this->register_rewrites(); |
| 67 | |
| 68 | add_action( 'template_redirect', array( $this, 'render_sitemaps' ) ); |
| 69 | |
| 70 | if ( ! $this->sitemaps_enabled() ) { |
| 71 | return; |
| 72 | } |
| 73 | |
64 | 74 | $this->register_sitemaps(); |
65 | 75 | |
66 | 76 | // Add additional action callbacks. |
67 | | add_action( 'template_redirect', array( $this, 'render_sitemaps' ) ); |
68 | 77 | add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 ); |
69 | 78 | add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 ); |
70 | 79 | } |
71 | 80 | |
| 81 | /** |
| 82 | * Determines whether sitemaps are enabled or not. |
| 83 | * |
| 84 | * @since 5.5.0 |
| 85 | * |
| 86 | * @return bool Whether sitemaps are enabled. |
| 87 | */ |
| 88 | public function sitemaps_enabled() { |
| 89 | $is_enabled = (bool) get_option( 'blog_public' ); |
| 90 | |
| 91 | /** |
| 92 | * Filters whether XML Sitemaps are enabled or not. |
| 93 | * |
| 94 | * When XML Sitemaps are disabled via this filter, rewrite rules are still in place to ensure a 404 is returned. |
| 95 | * @see WP_Sitemaps->register_rewrites() |
| 96 | * |
| 97 | * @since 5.5.0 |
| 98 | * |
| 99 | * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults to true for public sites. |
| 100 | */ |
| 101 | return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled ); |
| 102 | } |
| 103 | |
72 | 104 | /** |
73 | 105 | * Registers and sets up the functionality for all supported sitemaps. |
74 | 106 | * |
… |
… |
class WP_Sitemaps { |
143 | 175 | * @global WP_Query $wp_query WordPress Query object. |
144 | 176 | */ |
145 | 177 | public function render_sitemaps() { |
| 178 | /* @var WP_Query $wp_query */ |
146 | 179 | global $wp_query; |
147 | 180 | |
148 | 181 | $sitemap = sanitize_text_field( get_query_var( 'sitemap' ) ); |
… |
… |
class WP_Sitemaps { |
155 | 188 | return; |
156 | 189 | } |
157 | 190 | |
| 191 | if ( ! $this->sitemaps_enabled() ) { |
| 192 | $wp_query->set_404(); |
| 193 | status_header( 404 ); |
| 194 | return; |
| 195 | } |
| 196 | |
158 | 197 | // Render stylesheet if this is stylesheet route. |
159 | 198 | if ( $stylesheet_type ) { |
160 | 199 | $stylesheet = new WP_Sitemaps_Stylesheet(); |
… |
… |
class WP_Sitemaps { |
186 | 225 | // Force a 404 and bail early if no URLs are present. |
187 | 226 | if ( empty( $url_list ) ) { |
188 | 227 | $wp_query->set_404(); |
| 228 | status_header( 404 ); |
189 | 229 | return; |
190 | 230 | } |
191 | 231 | |