From 5800fc758a255ff8ee75e0e19d497fbbcdbd0f91 Mon Sep 17 00:00:00 2001 From: Shreya Shrivastava Date: Tue, 20 Oct 2026 19:08:22 +0530 Subject: [PATCH] Script Loader: Don't check is_feed() before the main query is run in block asset functions. --- src/wp-includes/script-loader.php | 30 ++++++++++++++++++++++-- tests/phpunit/tests/theme.php | 38 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index fc3ebbd305168..e7d9b244f3832 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -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. * @@ -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; } @@ -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; } diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index b83739f42238a..5976da33a6586 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -1013,6 +1013,44 @@ public function test_block_theme_should_load_block_assets_on_demand_by_default_e $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. *