From 33069c3c6b6001a25a455ac6536c9ba8613b99d2 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 26 Oct 2023 18:42:46 +0000 Subject: [PATCH] Themes: Fix block theme supports being added too early, leading to Customizer live preview bugs in 6.4. The Customizer live preview broke because of [56635], however the root cause for the bug was a lower-level problem that had been present since WordPress 5.8: The block theme specific functions `_add_default_theme_supports()` and `wp_enable_block_templates()` were being hooked into the `setup_theme` action, which fires too early to initialize theme features. Because of that, theme functionality would be initialized before the current theme setup being completed. In the case of the Customizer, that includes overriding which theme is the current theme entirely, thus leading to an inconsistent experience. This changeset fixes the bug by moving those two callbacks to the `after_setup_theme` action, which is the appropriate action to initialize theme features. Props karl94, hellofromTonya, joemcgill, flixos90. Fixes #59732. See #18298, #53397, #54597. git-svn-id: https://develop.svn.wordpress.org/trunk@57009 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 4 ++-- src/wp-includes/theme.php | 4 ++-- tests/phpunit/tests/block-template.php | 1 + tests/phpunit/tests/theme.php | 6 ++++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index fa756427d1..9cb447181a 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -521,7 +521,7 @@ add_action( 'init', 'wp_sitemaps_get_server' ); */ // Theme. add_action( 'setup_theme', 'create_initial_theme_features', 0 ); -add_action( 'setup_theme', '_add_default_theme_supports', 1 ); +add_action( 'after_setup_theme', '_add_default_theme_supports', 1 ); add_action( 'wp_loaded', '_custom_header_background_just_in_time' ); add_action( 'wp_head', '_custom_logo_header_styles' ); add_action( 'plugins_loaded', '_wp_customize_include' ); @@ -718,7 +718,7 @@ add_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug', add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' ); add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_template_skip_link' ); add_action( 'wp_footer', 'the_block_template_skip_link' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_block_template_skip_link(). -add_action( 'setup_theme', 'wp_enable_block_templates' ); +add_action( 'after_setup_theme', 'wp_enable_block_templates', 1 ); add_action( 'wp_loaded', '_add_template_loader_filters' ); // wp_navigation post type. diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index b5fba76159..6315cc2ab5 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -4335,9 +4335,9 @@ function wp_theme_get_element_class_name( $element ) { } /** - * Adds default theme supports for block themes when the 'setup_theme' action fires. + * Adds default theme supports for block themes when the 'after_setup_theme' action fires. * - * See {@see 'setup_theme'}. + * See {@see 'after_setup_theme'}. * * @since 5.9.0 * @access private diff --git a/tests/phpunit/tests/block-template.php b/tests/phpunit/tests/block-template.php index 7b4cbf3322..d7ffad7f90 100644 --- a/tests/phpunit/tests/block-template.php +++ b/tests/phpunit/tests/block-template.php @@ -15,6 +15,7 @@ class Tests_Block_Template extends WP_UnitTestCase { parent::set_up(); switch_theme( 'block-theme' ); do_action( 'setup_theme' ); + do_action( 'after_setup_theme' ); } public function tear_down() { diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index 77cad7156b..7260d6af57 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -746,6 +746,7 @@ class Tests_Theme extends WP_UnitTestCase { * * @ticket 54597 * @ticket 54731 + * @ticket 59732 * * @dataProvider data_block_theme_has_default_support * @@ -774,7 +775,7 @@ class Tests_Theme extends WP_UnitTestCase { "Could not remove support for $support_data_str." ); - do_action( 'setup_theme' ); + do_action( 'after_setup_theme' ); $this->assertTrue( current_theme_supports( ...$support_data ), @@ -858,6 +859,7 @@ class Tests_Theme extends WP_UnitTestCase { * Tests that block themes load separate core block assets by default. * * @ticket 54597 + * @ticket 59732 * * @covers ::_add_default_theme_supports * @covers ::wp_should_load_separate_core_block_assets @@ -872,7 +874,7 @@ class Tests_Theme extends WP_UnitTestCase { 'Could not disable loading separate core block assets.' ); - do_action( 'setup_theme' ); + do_action( 'after_setup_theme' ); $this->assertTrue( wp_should_load_separate_core_block_assets(),