Make WordPress Core

Changeset 54176


Ignore:
Timestamp:
09/15/2022 12:18:30 PM (2 years ago)
Author:
hellofromTonya
Message:

Themes: Introduces block-based template parts for Classic themes.

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'
    add_theme_support( 'block-template-parts' );
    

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

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

Props mamaduka, fabiankaegy, poena, scruffian, manfcarlo, bernhard-reiter, hellofromTonya.
See #56467.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/menu.php

    r54165 r54176  
    215215}
    216216
     217if ( ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ) ) {
     218    $submenu['themes.php'][6] = array(
     219        __( 'Template Parts' ),
     220        'edit_theme_options',
     221        'site-editor.php?postType=wp_template_part',
     222    );
     223}
     224
    217225$customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' );
    218226
     
    220228// is using 'customize_register' to add a setting.
    221229if ( ! wp_is_block_theme() || has_action( 'customize_register' ) ) {
    222     $position = wp_is_block_theme() ? 7 : 6;
     230    $position = ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) ? 7 : 6;
    223231
    224232    $submenu['themes.php'][ $position ] = array( __( 'Customize' ), 'customize', esc_url( $customize_url ), '', 'hide-if-no-customize' );
  • trunk/src/wp-admin/site-editor.php

    r53784 r54176  
    2020}
    2121
    22 if ( ! wp_is_block_theme() ) {
     22if ( ! ( current_theme_supports( 'block-template-parts' ) || wp_is_block_theme() ) ) {
    2323    wp_die( __( 'The theme you are currently using is not compatible with Full Site Editing.' ) );
     24}
     25
     26$is_template_part_editor = isset( $_GET['postType'] ) && 'wp_template_part' === sanitize_key( $_GET['postType'] );
     27if ( ! wp_is_block_theme() && ! $is_template_part_editor ) {
     28    wp_die( __( 'The theme you are currently using is not compatible with the Site Editor.' ) );
    2429}
    2530
     
    6570$block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
    6671$custom_settings      = array(
    67     'siteUrl'                  => site_url(),
    68     'postsPerPage'             => get_option( 'posts_per_page' ),
    69     'styles'                   => get_block_editor_theme_styles(),
    70     'defaultTemplateTypes'     => $indexed_template_types,
    71     'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
    72     '__unstableHomeTemplate'   => $home_template,
     72    'siteUrl'                   => site_url(),
     73    'postsPerPage'              => get_option( 'posts_per_page' ),
     74    'styles'                    => get_block_editor_theme_styles(),
     75    'defaultTemplateTypes'      => $indexed_template_types,
     76    'defaultTemplatePartAreas'  => get_allowed_block_template_part_areas(),
     77    'supportsLayout'            => WP_Theme_JSON_Resolver::theme_has_support(),
     78    'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ),
     79    '__unstableHomeTemplate'    => $home_template,
    7380);
     81
     82/**
     83 * Home template resolution is not needed when block template parts are supported.
     84 * Set the value to `true` to satisfy the editor initialization guard clause.
     85 */
     86if ( $custom_settings['supportsTemplatePartsMode'] ) {
     87    $custom_settings['__unstableHomeTemplate'] = true;
     88}
    7489
    7590// Add additional back-compat patterns registered by `current_screen` et al.
  • trunk/src/wp-includes/functions.php

    r54157 r54176  
    52625262
    52635263    $menu_name = __( 'Widgets' );
    5264     if ( wp_is_block_theme() ) {
     5264    if ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) {
    52655265        $submenu['themes.php'][] = array( $menu_name, 'edit_theme_options', 'widgets.php' );
    52665266    } else {
  • trunk/src/wp-includes/theme.php

    r54174 r54176  
    38453845    );
    38463846    register_theme_feature(
     3847        'block-template-parts',
     3848        array(
     3849            'description'  => __( 'Whether a theme uses block-based template parts.' ),
     3850            'show_in_rest' => true,
     3851        )
     3852    );
     3853    register_theme_feature(
    38473854        'custom-background',
    38483855        array(
  • trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r54162 r54176  
    389389        $this->assertArrayHasKey( 'automatic-feed-links', $theme_supports );
    390390        $this->assertArrayHasKey( 'block-templates', $theme_supports );
     391        $this->assertArrayHasKey( 'block-template-parts', $theme_supports, "Theme supports should have 'block-template-parts' key" );
    391392        $this->assertArrayHasKey( 'custom-header', $theme_supports );
    392393        $this->assertArrayHasKey( 'custom-background', $theme_supports );
     
    408409        $this->assertArrayHasKey( 'title-tag', $theme_supports );
    409410        $this->assertArrayHasKey( 'wp-block-styles', $theme_supports );
    410         $this->assertCount( 22, $theme_supports );
     411        $this->assertCount( 23, $theme_supports, 'There should be 23 theme supports' );
    411412    }
    412413
Note: See TracChangeset for help on using the changeset viewer.