Make WordPress Core


Ignore:
Timestamp:
11/25/2021 10:57:19 AM (4 years ago)
Author:
youknowriad
Message:

Themes: Update the base folders for templates and template parts in block themes.

Block Themes should now use the following folders:

  • templates instead of block-templates
  • parts instead of block-template-parts

Existing themes and folders will continue to work without impact.

Props bernhard-reiter.
Fixes #54493.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r52110 r52247  
    1919if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
    2020    define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
     21}
     22
     23/**
     24 * For backward compatibility reasons,
     25 * block themes might be using block-templates or block-template-parts,
     26 * this function ensures we fallback to these folders properly.
     27 *
     28 * @since 5.9.0
     29 *
     30 * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root.
     31 *
     32 * @return array Folder names used by block themes.
     33 */
     34function get_block_theme_folders( $theme_stylesheet = null ) {
     35    $theme_name = null === $theme_stylesheet ? get_stylesheet() : $theme_stylesheet;
     36    $root_dir   = get_theme_root( $theme_name );
     37    $theme_dir  = "$root_dir/$theme_name";
     38
     39    if ( is_readable( $theme_dir . '/block-templates/index.html' ) ) {
     40        return array(
     41            'wp_template'      => 'block-templates',
     42            'wp_template_part' => 'block-template-parts',
     43        );
     44    }
     45
     46    return array(
     47        'wp_template'      => 'templates',
     48        'wp_template_part' => 'parts',
     49    );
    2150}
    2251
     
    225254    }
    226255
    227     $template_base_paths = array(
    228         'wp_template'      => 'block-templates',
    229         'wp_template_part' => 'block-template-parts',
    230     );
    231     $themes              = array(
     256    $themes = array(
    232257        get_stylesheet() => get_stylesheet_directory(),
    233258        get_template()   => get_template_directory(),
    234259    );
    235260    foreach ( $themes as $theme_slug => $theme_dir ) {
    236         $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
     261        $template_base_paths = get_block_theme_folders( $theme_slug );
     262        $file_path           = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html';
    237263        if ( file_exists( $file_path ) ) {
    238264            $new_template_item = array(
     
    273299    }
    274300
    275     $template_base_paths = array(
    276         'wp_template'      => 'block-templates',
    277         'wp_template_part' => 'block-template-parts',
    278     );
    279     $themes              = array(
     301    $themes         = array(
    280302        get_stylesheet() => get_stylesheet_directory(),
    281303        get_template()   => get_template_directory(),
    282304    );
    283 
    284305    $template_files = array();
    285306    foreach ( $themes as $theme_slug => $theme_dir ) {
     307        $template_base_paths  = get_block_theme_folders( $theme_slug );
    286308        $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
    287309        foreach ( $theme_template_files as $template_file ) {
Note: See TracChangeset for help on using the changeset viewer.