Make WordPress Core


Ignore:
Timestamp:
02/02/2023 07:36:29 PM (4 years ago)
Author:
jorgefilipecosta
Message:

Editor: Remove need for template prefix in get_template_hierarchy.

This commit removes the need to pass a template prefix in get_template_hierarchy.
This is required because, in some block editor usages, the template prefix is not known.

Props youknowriad, davidbaumwald, jorgefilipecosta.
Fixes #57614.

File:
1 edited

Legend:

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

    r55086 r55194  
    13261326        }
    13271327
     1328        $matches = array();
     1329
    13281330        $template_hierarchy = array( $slug );
    1329 
    13301331        // Most default templates don't have `$template_prefix` assigned.
    1331         if ( $template_prefix ) {
     1332        if ( ! empty( $template_prefix ) ) {
    13321333                list( $type ) = explode( '-', $template_prefix );
    1333                 // These checks are needed because the `$slug` above is always added.
     1334                // We need these checks because we always add the `$slug` above.
    13341335                if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
    13351336                        $template_hierarchy[] = $template_prefix;
    … …  
    13381339                        $template_hierarchy[] = $type;
    13391340                }
    1340         }
    1341 
     1341        } else if ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) {
     1342                $template_hierarchy[] = $matches[1];
     1343        } else if ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) {
     1344                $type           = $matches[1];
     1345                $slug_remaining = $matches[2];
     1346
     1347                $items = 'single' === $type ? get_post_types() : get_taxonomies();
     1348                foreach ( $items as $item ) {
     1349                        if ( ! str_starts_with( $slug_remaining, $item ) ) {
     1350                                        continue;
     1351                        }
     1352
     1353                        // If $slug_remaining is equal to $post_type or $taxonomy we have
     1354                        // the single-$post_type template or the taxonomy-$taxonomy template.
     1355                        if ( $slug_remaining === $item ) {
     1356                                $template_hierarchy[] = $type;
     1357                                break;
     1358                        }
     1359
     1360                        // If $slug_remaining is single-$post_type-$slug template.
     1361                        if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) {
     1362                                $template_hierarchy[] = "$type-$item";
     1363                                $template_hierarchy[] = $type;
     1364                                break;
     1365                        }
     1366                }
     1367        }
    13421368        // Handle `archive` template.
    13431369        if (
    … …  
    13541380                $template_hierarchy[] = 'single';
    13551381        }
    1356 
    13571382        // Handle `singular` template.
    13581383        if (
    … …  
    13631388                $template_hierarchy[] = 'singular';
    13641389        }
    1365 
    13661390        $template_hierarchy[] = 'index';
    1367 
    13681391        return $template_hierarchy;
    13691392}
Note: See TracChangeset for help on using the changeset viewer.