From bc4ffaea733545cc333ca8cc8cd616421b20faa5 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Wed, 5 Mar 2025 15:18:00 -0600 Subject: [PATCH 1/4] Revert "Editor: Set new default rendering mode for Pages." This reverts commit 83b90808e9b7e0d910c5237bda6b52d863702074. --- src/wp-includes/post.php | 5 ---- tests/phpunit/tests/option/networkOption.php | 28 +++++++------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index a9992071f0167..58504e140a139 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -69,11 +69,6 @@ function create_initial_post_types() { ) ); - // Enhance page editor for block themes by rendering template and content blocks. - if ( wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) { - add_post_type_support( 'page', 'editor', array( 'default-mode' => 'template-locked' ) ); - } - register_post_type( 'attachment', array( diff --git a/tests/phpunit/tests/option/networkOption.php b/tests/phpunit/tests/option/networkOption.php index 7771154ef86ca..a4247d4926cec 100644 --- a/tests/phpunit/tests/option/networkOption.php +++ b/tests/phpunit/tests/option/networkOption.php @@ -381,18 +381,14 @@ public function test_delete_network_option_does_not_use_network_notoptions_cache * @covers ::get_network_option */ public function test_get_network_option_does_not_use_single_site_notoptions_cache_for_networks() { - $network_notoptions_cache_before = wp_cache_get( '1:notoptions', 'site-options' ); - $single_site_notoptions_cache_before = wp_cache_get( 'notoptions', 'options' ); - get_network_option( 1, 'ticket_61730_notoption' ); - $network_notoptions_cache_after = wp_cache_get( '1:notoptions', 'site-options' ); - $single_site_notoptions_cache_after = wp_cache_get( 'notoptions', 'options' ); + $network_notoptions_cache = wp_cache_get( '1:notoptions', 'site-options' ); + $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' ); - $this->assertSame( $single_site_notoptions_cache_before, $single_site_notoptions_cache_after, 'Single site notoptions cache should not change for multisite installs.' ); - $this->assertNotSame( $network_notoptions_cache_before, $network_notoptions_cache_after, 'Multisite notoptions cache should change.' ); - $this->assertIsArray( $network_notoptions_cache_after, 'Multisite notoptions cache should be set.' ); - $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache_after, 'The option should be in the notoptions cache.' ); + $this->assertEmpty( $single_site_notoptions_cache, 'Single site notoptions cache should not be set for multisite installs.' ); + $this->assertIsArray( $network_notoptions_cache, 'Multisite notoptions cache should be set.' ); + $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache, 'The option should be in the notoptions cache.' ); } /** @@ -406,18 +402,14 @@ public function test_get_network_option_does_not_use_single_site_notoptions_cach * @covers ::delete_network_option */ public function test_delete_network_option_does_not_use_single_site_notoptions_cache_for_networks() { - $network_notoptions_cache_before = wp_cache_get( '1:notoptions', 'site-options' ); - $single_site_notoptions_cache_before = wp_cache_get( 'notoptions', 'options' ); - add_network_option( 1, 'ticket_61730_notoption', 'value' ); delete_network_option( 1, 'ticket_61730_notoption' ); - $network_notoptions_cache_after = wp_cache_get( '1:notoptions', 'site-options' ); - $single_site_notoptions_cache_after = wp_cache_get( 'notoptions', 'options' ); + $network_notoptions_cache = wp_cache_get( '1:notoptions', 'site-options' ); + $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' ); - $this->assertSame( $single_site_notoptions_cache_before, $single_site_notoptions_cache_after, 'Single site notoptions cache should not change for multisite installs.' ); - $this->assertNotSame( $network_notoptions_cache_before, $network_notoptions_cache_after, 'Multisite notoptions cache should change.' ); - $this->assertIsArray( $network_notoptions_cache_after, 'Multisite notoptions cache should be set.' ); - $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache_after, 'The option should be in the notoptions cache.' ); + $this->assertEmpty( $single_site_notoptions_cache, 'Single site notoptions cache should not be set for multisite installs.' ); + $this->assertIsArray( $network_notoptions_cache, 'Multisite notoptions cache should be set.' ); + $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache, 'The option should be in the notoptions cache.' ); } } From 05817c1ccef72132b6274ebfcacc3818cda14ad4 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Wed, 5 Mar 2025 15:43:25 -0600 Subject: [PATCH 2/4] Ensure theme is set up before adding post type support --- src/wp-includes/post.php | 5 ++++ tests/phpunit/tests/option/networkOption.php | 28 +++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 58504e140a139..3ab99d13ad5b4 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -69,6 +69,11 @@ function create_initial_post_types() { ) ); + // Enhance page editor for block themes by rendering template and content blocks. + if ( did_action( 'after_setup_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) { + add_post_type_support( 'page', 'editor', array( 'default-mode' => 'template-locked' ) ); + } + register_post_type( 'attachment', array( diff --git a/tests/phpunit/tests/option/networkOption.php b/tests/phpunit/tests/option/networkOption.php index a4247d4926cec..7771154ef86ca 100644 --- a/tests/phpunit/tests/option/networkOption.php +++ b/tests/phpunit/tests/option/networkOption.php @@ -381,14 +381,18 @@ public function test_delete_network_option_does_not_use_network_notoptions_cache * @covers ::get_network_option */ public function test_get_network_option_does_not_use_single_site_notoptions_cache_for_networks() { + $network_notoptions_cache_before = wp_cache_get( '1:notoptions', 'site-options' ); + $single_site_notoptions_cache_before = wp_cache_get( 'notoptions', 'options' ); + get_network_option( 1, 'ticket_61730_notoption' ); - $network_notoptions_cache = wp_cache_get( '1:notoptions', 'site-options' ); - $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' ); + $network_notoptions_cache_after = wp_cache_get( '1:notoptions', 'site-options' ); + $single_site_notoptions_cache_after = wp_cache_get( 'notoptions', 'options' ); - $this->assertEmpty( $single_site_notoptions_cache, 'Single site notoptions cache should not be set for multisite installs.' ); - $this->assertIsArray( $network_notoptions_cache, 'Multisite notoptions cache should be set.' ); - $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache, 'The option should be in the notoptions cache.' ); + $this->assertSame( $single_site_notoptions_cache_before, $single_site_notoptions_cache_after, 'Single site notoptions cache should not change for multisite installs.' ); + $this->assertNotSame( $network_notoptions_cache_before, $network_notoptions_cache_after, 'Multisite notoptions cache should change.' ); + $this->assertIsArray( $network_notoptions_cache_after, 'Multisite notoptions cache should be set.' ); + $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache_after, 'The option should be in the notoptions cache.' ); } /** @@ -402,14 +406,18 @@ public function test_get_network_option_does_not_use_single_site_notoptions_cach * @covers ::delete_network_option */ public function test_delete_network_option_does_not_use_single_site_notoptions_cache_for_networks() { + $network_notoptions_cache_before = wp_cache_get( '1:notoptions', 'site-options' ); + $single_site_notoptions_cache_before = wp_cache_get( 'notoptions', 'options' ); + add_network_option( 1, 'ticket_61730_notoption', 'value' ); delete_network_option( 1, 'ticket_61730_notoption' ); - $network_notoptions_cache = wp_cache_get( '1:notoptions', 'site-options' ); - $single_site_notoptions_cache = wp_cache_get( 'notoptions', 'options' ); + $network_notoptions_cache_after = wp_cache_get( '1:notoptions', 'site-options' ); + $single_site_notoptions_cache_after = wp_cache_get( 'notoptions', 'options' ); - $this->assertEmpty( $single_site_notoptions_cache, 'Single site notoptions cache should not be set for multisite installs.' ); - $this->assertIsArray( $network_notoptions_cache, 'Multisite notoptions cache should be set.' ); - $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache, 'The option should be in the notoptions cache.' ); + $this->assertSame( $single_site_notoptions_cache_before, $single_site_notoptions_cache_after, 'Single site notoptions cache should not change for multisite installs.' ); + $this->assertNotSame( $network_notoptions_cache_before, $network_notoptions_cache_after, 'Multisite notoptions cache should change.' ); + $this->assertIsArray( $network_notoptions_cache_after, 'Multisite notoptions cache should be set.' ); + $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache_after, 'The option should be in the notoptions cache.' ); } } From 399ad3458e99e3af7775041d7e96a162b6c06f2d Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Thu, 6 Mar 2025 21:32:51 -0600 Subject: [PATCH 3/4] Ensure WP_Theme::is_block_theme is not called before theme setup --- src/wp-includes/class-wp-theme.php | 5 +++++ src/wp-includes/post.php | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index ee74099766160..1bd1390351593 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -1576,6 +1576,11 @@ public function is_allowed( $check = 'both', $blog_id = null ) { * @return bool */ public function is_block_theme() { + if ( ! did_action( 'setup_theme' ) ) { + _doing_it_wrong( __METHOD__, __( 'This method should not be called before themes are setup.' ), '6.8.0' ); + return false; + } + if ( isset( $this->block_theme ) ) { return $this->block_theme; } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3ab99d13ad5b4..bd10b19e355cc 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -69,8 +69,17 @@ function create_initial_post_types() { ) ); - // Enhance page editor for block themes by rendering template and content blocks. - if ( did_action( 'after_setup_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) { + /* + * Enhance page editor for block themes by rendering template and content blocks. + * + * Theme support for block templates is added on the 'after_setup_theme' hook in + * wp_enable_block_templates(). So post type support will only be added on the second + * pass of this function on the 'init' hook. + + * Note: The order of these checks is important, a doing_it_wrong notice will be fired + * if the block theme check is done first. + */ + if ( current_theme_supports( 'block-templates' ) && wp_is_block_theme() ) { add_post_type_support( 'page', 'editor', array( 'default-mode' => 'template-locked' ) ); } From 493afc52a07770e647cc1f27349a77fcd87795f1 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Mon, 10 Mar 2025 15:09:45 -0500 Subject: [PATCH 4/4] Move logic to a standalone callback --- src/wp-includes/default-filters.php | 1 + src/wp-includes/post.php | 25 +++++++++++-------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 36f3c949dde77..2bc2e61e4a8b1 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -737,6 +737,7 @@ 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( 'after_setup_theme', 'wp_enable_block_templates', 1 ); +add_action( 'after_setup_theme', 'wp_set_editor_default_mode', 2 ); // Run after enabling block templates. add_action( 'wp_loaded', '_add_template_loader_filters' ); // wp_navigation post type. diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index bd10b19e355cc..4db40681bb6a5 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -69,20 +69,6 @@ function create_initial_post_types() { ) ); - /* - * Enhance page editor for block themes by rendering template and content blocks. - * - * Theme support for block templates is added on the 'after_setup_theme' hook in - * wp_enable_block_templates(). So post type support will only be added on the second - * pass of this function on the 'init' hook. - - * Note: The order of these checks is important, a doing_it_wrong notice will be fired - * if the block theme check is done first. - */ - if ( current_theme_supports( 'block-templates' ) && wp_is_block_theme() ) { - add_post_type_support( 'page', 'editor', array( 'default-mode' => 'template-locked' ) ); - } - register_post_type( 'attachment', array( @@ -8538,3 +8524,14 @@ function wp_create_initial_post_meta() { ) ); } + +/** + * Sets the default editor mode based on support for block templates. + * + * @since 6.8.0 + */ +function wp_set_editor_default_mode() { + if ( wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) { + add_post_type_support( 'page', 'editor', array( 'default-mode' => 'template-locked' ) ); + } +}