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
30 changes: 28 additions & 2 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,32 @@ function wp_should_load_block_editor_scripts_and_styles() {
return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
}

/**
* Determines whether the current request is known not to be a front-end page load.
*
* This is used by {@see wp_should_load_separate_core_block_assets()} and
* {@see wp_should_load_block_assets_on_demand()}, which can be called very early (such as at `init` or
* `wp_default_styles`) before the main query has been run. Conditional query tags like {@see is_feed()} cannot return
* meaningful results until then, so the feed check is only performed once the main query is known.
*
* @since 7.2.0
* @access private
*
* @return bool Whether the request is for the admin, a feed, or a REST API endpoint.
*/
function _wp_is_non_frontend_request_for_block_assets() {
if ( is_admin() ) {
return true;
}

// The query is only parsed and run by the time the 'wp' action fires.
if ( did_action( 'wp' ) && is_feed() ) {
return true;
}

return wp_is_rest_endpoint();
}

/**
* Checks whether separate styles should be loaded for core blocks.
*
Expand All @@ -3032,7 +3058,7 @@ function wp_should_load_block_editor_scripts_and_styles() {
* @return bool Whether separate core block assets will be loaded.
*/
function wp_should_load_separate_core_block_assets() {
if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
if ( _wp_is_non_frontend_request_for_block_assets() ) {
return false;
}

Expand Down Expand Up @@ -3067,7 +3093,7 @@ function wp_should_load_separate_core_block_assets() {
* @return bool Whether to load block assets only when they are rendered.
*/
function wp_should_load_block_assets_on_demand() {
if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
if ( _wp_is_non_frontend_request_for_block_assets() ) {
return false;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/tests/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
* @expectedDeprecated get_themes
* @expectedDeprecated get_current_theme
*/
public function test_switch_theme() {

Check warning on line 446 in tests/phpunit/tests/theme.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 / MySQL 8.4 (test reporting enabled)

Slow PHPUnit test

Tests_Theme::test_switch_theme took 1.339129s
$themes = get_themes();

$this->assertNotEmpty( $themes );
Expand Down Expand Up @@ -1013,6 +1013,44 @@
$this->assertTrue( wp_should_load_block_assets_on_demand() );
}

/**
* Tests that the feed check does not apply before the main query has been run.
*
* @ticket 63693
*
* @covers ::wp_should_load_separate_core_block_assets
* @covers ::wp_should_load_block_assets_on_demand
* @covers ::_wp_is_non_frontend_request_for_block_assets
*/
public function test_block_assets_functions_ignore_feed_before_query_is_run() {
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
$GLOBALS['wp_query']->is_feed = true;

$this->assertTrue( wp_should_load_separate_core_block_assets(), 'Feed check should be skipped before the wp action.' );
$this->assertTrue( wp_should_load_block_assets_on_demand(), 'Feed check should be skipped before the wp action.' );

do_action( 'wp' );

$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Feeds should not load separate assets.' );
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Feeds should not load assets on demand.' );
}

/**
* Tests that a REST endpoint request disables loading block assets separately and on demand.
*
* @ticket 63693
*
* @covers ::wp_should_load_separate_core_block_assets
* @covers ::wp_should_load_block_assets_on_demand
*/
public function test_block_assets_functions_return_false_for_rest_endpoint() {
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
add_filter( 'wp_is_rest_endpoint', '__return_true' );

$this->assertFalse( wp_should_load_separate_core_block_assets() );
$this->assertFalse( wp_should_load_block_assets_on_demand() );
}

/**
* Tests that a theme in the custom test data theme directory is recognized.
*
Expand Down
Loading