Skip to content

Commit

Permalink
Themes: Introduces block-based template parts for Classic themes.
Browse files Browse the repository at this point in the history
Allows Classic / Hybrid themes to use block-based template parts without using complete block-based templates.

* Exposes the Site Editor's template parts UI
* Adds Appearance > "Template Parts" menu
* Enabled within the theme via adding a theme support for `'block-template-parts'`
{{{#!php
add_theme_support( 'block-template-parts' );
}}}

This is a backport from Gutenberg.[WordPress/gutenberg#42729 See WordPress/gutenberg PR 42729].

Follow-up to [52330], [52069], [52178].

Props mamaduka, fabiankaegy, poena, scruffian, manfcarlo, bernhard-reiter, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54176
  • Loading branch information
hellofromTonya authored and hellofromTonya committed Sep 15, 2022
1 parent 809b2ef commit ee330bf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
10 changes: 9 additions & 1 deletion wp-admin/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
Expand Down
29 changes: 22 additions & 7 deletions wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
);
}

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.' ) );
}

$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.
Expand Down Expand Up @@ -64,14 +69,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,
);

/**
* 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;
}

// 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 );
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
7 changes: 7 additions & 0 deletions wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-54175';
$wp_version = '6.1-alpha-54176';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit ee330bf

Please sign in to comment.