Make WordPress Core

Changeset 52247


Ignore:
Timestamp:
11/25/2021 10:57:19 AM (3 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.

Location:
trunk
Files:
6 added
2 deleted
3 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 ) {
  • trunk/src/wp-includes/theme.php

    r52110 r52247  
    40854085 */
    40864086function wp_is_block_template_theme() {
    4087     return is_readable( get_theme_file_path( '/block-templates/index.html' ) );
    4088 }
     4087    return is_readable( get_theme_file_path( '/block-templates/index.html' ) ) ||
     4088        is_readable( get_theme_file_path( '/templates/index.html' ) );
     4089}
  • trunk/tests/phpunit/tests/block-template.php

    r52246 r52247  
    3838        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page-home.php', $type, $templates );
    3939        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
    40         $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-home.html', $_wp_current_template_content );
     40        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-home.html', $_wp_current_template_content );
    4141    }
    4242
     
    5151        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/page.php', $type, $templates );
    5252        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
    53         $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page.html', $_wp_current_template_content );
     53        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page.html', $_wp_current_template_content );
    5454    }
    5555
     
    6262        $resolved_template_path = locate_block_template( get_stylesheet_directory() . '/index.php', $type, $templates );
    6363        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
    64         $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/index.html', $_wp_current_template_content );
     64        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/index.html', $_wp_current_template_content );
    6565    }
    6666
     
    129129        $resolved_template_path          = locate_block_template( $parent_theme_page_template_path, $type, $templates );
    130130        $this->assertEquals( self::$template_canvas_path, $resolved_template_path );
    131         $this->assertStringEqualsFile( get_stylesheet_directory() . '/block-templates/page-1.html', $_wp_current_template_content );
     131        $this->assertStringEqualsFile( get_stylesheet_directory() . '/templates/page-1.html', $_wp_current_template_content );
    132132    }
    133133
Note: See TracChangeset for help on using the changeset viewer.