From a8eeca14238a570e8db5ff94fde705350a34978e Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 13 Sep 2022 12:49:54 +0400 Subject: [PATCH 1/6] Feature: Block-based template parts for Classic themes --- src/wp-admin/menu.php | 10 +++++++++- src/wp-admin/site-editor.php | 24 +++++++++++++++++------- src/wp-includes/functions.php | 2 +- src/wp-includes/theme.php | 7 +++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index f6a0c207334c6..05da5f213a9e1 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -214,12 +214,20 @@ ); } +if ( ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ) ) { + $submenu['themes.php'][6] = array( + __( 'Template Parts' ), + 'edit_theme_options', + 'site-editor.php?postType=wp_template_part', + ); +} + $customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // Hide Customize link on block themes unless a plugin or theme // is using 'customize_register' to add a setting. if ( ! wp_is_block_theme() || has_action( 'customize_register' ) ) { - $position = wp_is_block_theme() ? 7 : 6; + $position = ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) ? 7 : 6; $submenu['themes.php'][ $position ] = array( __( 'Customize' ), 'customize', esc_url( $customize_url ), '', 'hide-if-no-customize' ); } diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index 365c6d701eaf1..0f0305a8d1493 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -19,7 +19,7 @@ ); } -if ( ! wp_is_block_theme() ) { +if ( ! ( current_theme_supports( 'block-template-parts' ) || wp_is_block_theme() ) ) { wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) ); } @@ -64,14 +64,24 @@ static function( $classes ) { $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); $custom_settings = array( - 'siteUrl' => site_url(), - 'postsPerPage' => get_option( 'posts_per_page' ), - 'styles' => get_block_editor_theme_styles(), - 'defaultTemplateTypes' => $indexed_template_types, - 'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), - '__unstableHomeTemplate' => $home_template, + 'siteUrl' => site_url(), + 'postsPerPage' => get_option( 'posts_per_page' ), + 'styles' => get_block_editor_theme_styles(), + 'defaultTemplateTypes' => $indexed_template_types, + 'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(), + 'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(), + 'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ), + '__unstableHomeTemplate' => $home_template, ); +/** + * We don't need home template resolution when block template parts are supported. + * Set the value to true to satisfy the editor initialization guard clause. + */ +if ( $custom_settings['supportsTemplatePartsMode'] ) { + $custom_settings['__unstableHomeTemplate'] = true; +} + // Add additional back-compat patterns registered by `current_screen` et al. $custom_settings['__experimentalAdditionalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true ); $custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true ); diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 9690e6128b14c..74f421f862c99 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -5261,7 +5261,7 @@ function wp_widgets_add_menu() { } $menu_name = __( 'Widgets' ); - if ( wp_is_block_theme() ) { + if ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) { $submenu['themes.php'][] = array( $menu_name, 'edit_theme_options', 'widgets.php' ); } else { $submenu['themes.php'][7] = array( $menu_name, 'edit_theme_options', 'widgets.php' ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 18afee3abd54d..f532d11428caf 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -3843,6 +3843,13 @@ function create_initial_theme_features() { 'show_in_rest' => true, ) ); + register_theme_feature( + 'block-template-parts', + array( + 'description' => __( 'Whether a theme uses block-based template parts.' ), + 'show_in_rest' => true, + ) + ); register_theme_feature( 'custom-background', array( From c21592ef79aa2f7a7eeec48eae8482da528301bf Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 13 Sep 2022 13:36:04 +0400 Subject: [PATCH 2/6] Update unit tests --- tests/phpunit/tests/rest-api/rest-themes-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index 8055e95116067..89cd511fe88f5 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -388,6 +388,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'align-wide', $theme_supports ); $this->assertArrayHasKey( 'automatic-feed-links', $theme_supports ); $this->assertArrayHasKey( 'block-templates', $theme_supports ); + $this->assertArrayHasKey( 'block-template-parts', $theme_supports ); $this->assertArrayHasKey( 'custom-header', $theme_supports ); $this->assertArrayHasKey( 'custom-background', $theme_supports ); $this->assertArrayHasKey( 'custom-logo', $theme_supports ); From cfa8c586c5e5e7ea0df5ccb35c193c268d056f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Wed, 14 Sep 2022 21:03:15 +0200 Subject: [PATCH 3/6] fix prevent unwanted access of site-editor.php non block themes that support block based template parts should only be allowed to access the site-editor.php file if the query parameter to access the template part editor is set correctly --- src/wp-admin/site-editor.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index 0f0305a8d1493..e97470814eaf6 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -23,6 +23,11 @@ wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) ); } +$is_template_part_editor = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] ); +if ( ! wp_is_block_theme() && ! $is_template_part_editor ) { + wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) ); +} + /** * Do a server-side redirection if missing `postType` and `postId` * query args when visiting Site Editor. From 60cadbd2104c63457f105fb7bfed78b285b0fccf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 15 Sep 2022 12:06:07 +0200 Subject: [PATCH 4/6] Bump theme_supports count to 23 in test --- tests/phpunit/tests/rest-api/rest-themes-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index 89cd511fe88f5..a3ce236c258a2 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -408,7 +408,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'responsive-embeds', $theme_supports ); $this->assertArrayHasKey( 'title-tag', $theme_supports ); $this->assertArrayHasKey( 'wp-block-styles', $theme_supports ); - $this->assertCount( 22, $theme_supports ); + $this->assertCount( 23, $theme_supports ); } /** From f09fb1f7cf4ba13531e3199c7bed56bd39b65de8 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Thu, 15 Sep 2022 06:32:30 -0500 Subject: [PATCH 5/6] Updates for Core coding standards --- src/wp-admin/site-editor.php | 4 ++-- tests/phpunit/tests/rest-api/rest-themes-controller.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index e97470814eaf6..64ed4b9c27cad 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -80,8 +80,8 @@ static function( $classes ) { ); /** - * We don't need home template resolution when block template parts are supported. - * Set the value to true to satisfy the editor initialization guard clause. + * Home template resolution is not needed when block template parts are supported. + * Set the value to `true` to satisfy the editor initialization guard clause. */ if ( $custom_settings['supportsTemplatePartsMode'] ) { $custom_settings['__unstableHomeTemplate'] = true; diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index a3ce236c258a2..4fc51d1c12009 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -388,7 +388,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'align-wide', $theme_supports ); $this->assertArrayHasKey( 'automatic-feed-links', $theme_supports ); $this->assertArrayHasKey( 'block-templates', $theme_supports ); - $this->assertArrayHasKey( 'block-template-parts', $theme_supports ); + $this->assertArrayHasKey( 'block-template-parts', $theme_supports, "Theme supports should have 'block-template-parts' key" ); $this->assertArrayHasKey( 'custom-header', $theme_supports ); $this->assertArrayHasKey( 'custom-background', $theme_supports ); $this->assertArrayHasKey( 'custom-logo', $theme_supports ); @@ -408,7 +408,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'responsive-embeds', $theme_supports ); $this->assertArrayHasKey( 'title-tag', $theme_supports ); $this->assertArrayHasKey( 'wp-block-styles', $theme_supports ); - $this->assertCount( 23, $theme_supports ); + $this->assertCount( 23, $theme_supports, 'There should be 23 theme supports' ); } /** From c3702aef39795aa2054fc1e4856239e01e410d73 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Thu, 15 Sep 2022 06:39:21 -0500 Subject: [PATCH 6/6] Removes extra space for phpcs happiness --- tests/phpunit/tests/rest-api/rest-themes-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index 4fc51d1c12009..0492f191fe8a4 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -388,7 +388,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'align-wide', $theme_supports ); $this->assertArrayHasKey( 'automatic-feed-links', $theme_supports ); $this->assertArrayHasKey( 'block-templates', $theme_supports ); - $this->assertArrayHasKey( 'block-template-parts', $theme_supports, "Theme supports should have 'block-template-parts' key" ); + $this->assertArrayHasKey( 'block-template-parts', $theme_supports, "Theme supports should have 'block-template-parts' key" ); $this->assertArrayHasKey( 'custom-header', $theme_supports ); $this->assertArrayHasKey( 'custom-background', $theme_supports ); $this->assertArrayHasKey( 'custom-logo', $theme_supports );