Make WordPress Core

Changeset 57944


Ignore:
Timestamp:
04/08/2024 04:45:22 PM (3 years ago)
Author:
jorgefilipecosta
Message:

Honor template_hierarchy filters when creating a template in the Site Editor.

Currently, in blocks themes it's possible to use the ${type}_template_hierarchy filter to alter the template hierarchy. However, those filters are not taken into consideration by the Choose a pattern popup screen that appears when creating a new template in the Site Editor, causing a mismatch between the editor and the frontend.

Props aljullu, mukesh27, ntsekouras, jorgefilipecosta, gziolo.
Fixes #60846.

Location:
trunk
Files:
2 edited

Legend:

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

    r57928 r57944  
    13981398function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
    13991399        if ( 'index' === $slug ) {
    1400                 return array( 'index' );
     1400                /** This filter is documented in wp-includes/template.php */
     1401                return apply_filters( 'index_template_hierarchy', array( 'index' ) );
    14011402        }
    14021403        if ( $is_custom ) {
    1403                 return array( 'page', 'singular', 'index' );
     1404                /** This filter is documented in wp-includes/template.php */
     1405                return apply_filters( 'page_template_hierarchy', array( 'page', 'singular', 'index' ) );
    14041406        }
    14051407        if ( 'front-page' === $slug ) {
    1406                 return array( 'front-page', 'home', 'index' );
     1408                /** This filter is documented in wp-includes/template.php */
     1409                return apply_filters( 'frontpage_template_hierarchy', array( 'front-page', 'home', 'index' ) );
    14071410        }
    14081411
    … …  
    14701473        }
    14711474        $template_hierarchy[] = 'index';
     1475
     1476        $template_type = '';
     1477        if ( ! empty( $template_prefix ) ) {
     1478                list( $template_type ) = explode( '-', $template_prefix );
     1479        } else {
     1480                list( $template_type ) = explode( '-', $slug );
     1481        }
     1482        $valid_template_types = array( '404', 'archive', 'attachment', 'author', 'category', 'date', 'embed', 'frontpage', 'home', 'index', 'page', 'paged', 'privacypolicy', 'search', 'single', 'singular', 'tag', 'taxonomy' );
     1483        if ( in_array( $template_type, $valid_template_types ) ) {
     1484                /** This filter is documented in wp-includes/template.php */
     1485                return apply_filters( "{$template_type}_template_hierarchy", $template_hierarchy );
     1486        }
    14721487        return $template_hierarchy;
    14731488}
  • trunk/tests/phpunit/tests/block-templates/getTemplateHierarchy.php

    r55194 r57944  
    4242
    4343        /**
     44         * @ticket 60846
     45         */
     46        public function test_get_template_hierarchy_with_hooks() {
     47                add_filter(
     48                        'date_template_hierarchy',
     49                        function ( $templates ) {
     50                                return array_merge( array( 'date-custom' ), $templates );
     51                        }
     52                );
     53                $expected = array( 'date-custom', 'date', 'archive', 'index' );
     54                $this->assertSame( $expected, get_template_hierarchy( 'date' ) );
     55        }
     56
     57        /**
    4458         * Data provider.
    4559         *
Note: See TracChangeset for help on using the changeset viewer.