Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/wp-includes/load.php
Comment thread
masteradhoc marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -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(): bool {
/**
* 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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/sitemaps/class-wp-sitemaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
67 changes: 67 additions & 0 deletions tests/phpunit/tests/load/wpDoingSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* Tests for wp_doing_sitemap().
*
* @group load
* @group sitemaps
*
* @covers ::wp_doing_sitemap
*/
class Tests_Load_WpDoingSitemap extends WP_UnitTestCase {

/**
* The function should return false on a regular request.
*
* @ticket 56954
*/
public function test_should_return_false_by_default() {
$this->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() );
}
}
48 changes: 48 additions & 0 deletions tests/phpunit/tests/sitemaps/sitemaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
}
}
Loading