From aca1923c0b6ee087f1093d22ae1583f71180696c Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 11 Jun 2026 08:12:52 +0200 Subject: [PATCH 1/5] add DOING_SITEMAP --- src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php index 10a1ef1e7fcd5..232a8c59831ad 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php @@ -123,6 +123,10 @@ public function get_sitemap_index_stylesheet_url() { * @param array $sitemaps Array of sitemap URLs. */ public function render_index( $sitemaps ) { + if ( ! defined( 'DOING_SITEMAP' ) ) { + define( 'DOING_SITEMAP', true ); + } + header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); @@ -187,6 +191,10 @@ public function get_sitemap_index_xml( $sitemaps ) { * @param array $url_list Array of URLs for a sitemap. */ public function render_sitemap( $url_list ) { + if ( ! defined( 'DOING_SITEMAP' ) ) { + define( 'DOING_SITEMAP', true ); + } + header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); From 5437c66b1378e30ca596e29a60dd1cb54abc52b7 Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 11 Jun 2026 08:14:16 +0200 Subject: [PATCH 2/5] add DOING_SITEMAP --- src/wp-includes/load.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 27c58b57dd671..33e6c7676f5c4 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -1789,6 +1789,24 @@ function wp_doing_cron() { return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON ); } +/** + * Determines whether the current request is a WordPress sitemap request. + * + * @since 7.1.0 + * + * @return bool True if it's a WordPress sitemap request, false otherwise. + */ +function wp_doing_sitemap() { + /** + * Filters whether the current request is a WordPress sitemap request. + * + * @since 7.1.0 + * + * @param bool $wp_doing_sitemap Whether the current request is a WordPress sitemap request. + */ + return apply_filters( 'wp_doing_sitemap', defined( 'DOING_SITEMAP' ) && DOING_SITEMAP ); +} + /** * Checks whether the given variable is a WordPress Error. * From 72d7aa155076c9c57a15475619ea3dfc265ff6b3 Mon Sep 17 00:00:00 2001 From: Brian Date: Mon, 15 Jun 2026 19:29:05 +0200 Subject: [PATCH 3/5] Apply suggestion from @westonruter Co-authored-by: Weston Ruter --- src/wp-includes/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 33e6c7676f5c4..f888bbb43a49e 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -1796,7 +1796,7 @@ function wp_doing_cron() { * * @return bool True if it's a WordPress sitemap request, false otherwise. */ -function wp_doing_sitemap() { +function wp_doing_sitemap(): bool { /** * Filters whether the current request is a WordPress sitemap request. * From e3eeb69b1361b13651d53379832b7c0553eadffb Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 16 Jun 2026 14:34:13 +0000 Subject: [PATCH 4/5] replace DOING_SITEMAP --- src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php | 8 -------- src/wp-includes/sitemaps/class-wp-sitemaps.php | 4 ++++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php index 232a8c59831ad..10a1ef1e7fcd5 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php @@ -123,10 +123,6 @@ public function get_sitemap_index_stylesheet_url() { * @param array $sitemaps Array of sitemap URLs. */ public function render_index( $sitemaps ) { - if ( ! defined( 'DOING_SITEMAP' ) ) { - define( 'DOING_SITEMAP', true ); - } - header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); @@ -191,10 +187,6 @@ public function get_sitemap_index_xml( $sitemaps ) { * @param array $url_list Array of URLs for a sitemap. */ public function render_sitemap( $url_list ) { - if ( ! defined( 'DOING_SITEMAP' ) ) { - define( 'DOING_SITEMAP', true ); - } - header( 'Content-Type: application/xml; charset=UTF-8' ); $this->check_for_simple_xml_availability(); diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps.php b/src/wp-includes/sitemaps/class-wp-sitemaps.php index d4bbe38c86b11..87183b8bffe61 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps.php @@ -179,6 +179,10 @@ public function render_sitemaps() { return; } + if ( ! defined( 'DOING_SITEMAP' ) ) { + define( 'DOING_SITEMAP', true ); + } + // Render stylesheet if this is stylesheet route. if ( $stylesheet_type ) { $stylesheet = new WP_Sitemaps_Stylesheet(); From 6e00d8d8c9c7f4260fdbd6c707ae42bcaa91a515 Mon Sep 17 00:00:00 2001 From: Nimesh Date: Wed, 17 Jun 2026 16:34:01 +0530 Subject: [PATCH 5/5] Tests: Add unit tests for wp_doing_sitemap() and the DOING_SITEMAP constant. Adds coverage for the wp_doing_sitemap() function introduced for #56954, including the default false result, the DOING_SITEMAP constant path, and the wp_doing_sitemap filter overriding both the default and the constant. Also adds integration tests asserting that WP_Sitemaps::render_sitemaps() defines the DOING_SITEMAP constant while rendering a sitemap, and that the constant is not defined when sitemaps are disabled. See #56954. --- tests/phpunit/tests/load/wpDoingSitemap.php | 67 +++++++++++++++++++++ tests/phpunit/tests/sitemaps/sitemaps.php | 48 +++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/phpunit/tests/load/wpDoingSitemap.php diff --git a/tests/phpunit/tests/load/wpDoingSitemap.php b/tests/phpunit/tests/load/wpDoingSitemap.php new file mode 100644 index 0000000000000..a1ce10496575c --- /dev/null +++ b/tests/phpunit/tests/load/wpDoingSitemap.php @@ -0,0 +1,67 @@ +assertFalse( wp_doing_sitemap() ); + } + + /** + * The 'wp_doing_sitemap' filter should be able to force a true result. + * + * @ticket 56954 + */ + public function test_filter_can_force_true() { + add_filter( 'wp_doing_sitemap', '__return_true' ); + + $this->assertTrue( wp_doing_sitemap() ); + } + + /** + * The DOING_SITEMAP constant should make the function return true. + * + * Runs in a separate process because the constant cannot be undefined + * once it has been set. + * + * @ticket 56954 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_constant_defined_returns_true() { + $this->assertFalse( wp_doing_sitemap(), 'wp_doing_sitemap() should be false before the constant is defined.' ); + + define( 'DOING_SITEMAP', true ); + + $this->assertTrue( wp_doing_sitemap(), 'wp_doing_sitemap() should be true once DOING_SITEMAP is defined.' ); + } + + /** + * The 'wp_doing_sitemap' filter should be able to override the constant. + * + * @ticket 56954 + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_filter_overrides_constant() { + define( 'DOING_SITEMAP', true ); + + add_filter( 'wp_doing_sitemap', '__return_false' ); + + $this->assertFalse( wp_doing_sitemap() ); + } +} diff --git a/tests/phpunit/tests/sitemaps/sitemaps.php b/tests/phpunit/tests/sitemaps/sitemaps.php index 85f9965245842..e24c2d9bc7282 100644 --- a/tests/phpunit/tests/sitemaps/sitemaps.php +++ b/tests/phpunit/tests/sitemaps/sitemaps.php @@ -493,4 +493,52 @@ public function test_empty_url_list_should_return_404() { $this->assertTrue( is_404() ); } + + /** + * Ensures rendering a sitemap defines the DOING_SITEMAP constant. + * + * @ticket 56954 + * + * @covers WP_Sitemaps::render_sitemaps + * @covers ::wp_doing_sitemap + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_render_sitemaps_defines_doing_sitemap() { + $this->assertFalse( wp_doing_sitemap(), 'wp_doing_sitemap() should be false before a sitemap is rendered.' ); + + wp_register_sitemap_provider( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) ); + + $this->go_to( home_url( '/?sitemap=foo' ) ); + + wp_sitemaps_get_server()->render_sitemaps(); + + $this->assertTrue( defined( 'DOING_SITEMAP' ), 'The DOING_SITEMAP constant should be defined.' ); + $this->assertTrue( wp_doing_sitemap(), 'wp_doing_sitemap() should be true while rendering a sitemap.' ); + } + + /** + * Ensures the DOING_SITEMAP constant is not defined when sitemaps are disabled. + * + * The constant is only defined once the request has passed the preliminary + * checks, so a disabled sitemap request should never reach that point. + * + * @ticket 56954 + * + * @covers WP_Sitemaps::render_sitemaps + * + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function test_render_sitemaps_does_not_define_doing_sitemap_when_disabled() { + add_filter( 'wp_sitemaps_enabled', '__return_false' ); + + $this->go_to( home_url( '/?sitemap=index' ) ); + + wp_sitemaps_get_server()->render_sitemaps(); + + $this->assertFalse( defined( 'DOING_SITEMAP' ), 'The DOING_SITEMAP constant should not be defined when sitemaps are disabled.' ); + $this->assertFalse( wp_doing_sitemap(), 'wp_doing_sitemap() should be false when sitemaps are disabled.' ); + } }