| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for features present in Gutenberg. |
| 4 |
* This file should be removed when WordPress 6.2.0 becomes the lowest |
| 5 |
* supported version by this plugin. |
| 6 |
* |
| 7 |
* @package gutenberg |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Helper function to get the Template Hierarchy for a given slug. |
| 12 |
* We need to Handle special cases here like `front-page`, `singular` and `archive` templates. |
| 13 |
* |
| 14 |
* Noting that we always add `index` as the last fallback template. |
| 15 |
* |
| 16 |
* @param string $slug The template slug to be created. |
| 17 |
* @param boolean $is_custom Indicates if a template is custom or part of the template hierarchy. |
| 18 |
* @param string $template_prefix The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`. |
| 19 |
* |
| 20 |
* @return array<string> The template hierarchy. |
| 21 |
*/ |
| 22 |
function gutenberg_get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) { |
| 23 |
if ( 'index' === $slug ) { |
| 24 |
return array( 'index' ); |
| 25 |
} |
| 26 |
if ( $is_custom ) { |
| 27 |
return array( 'page', 'singular', 'index' ); |
| 28 |
} |
| 29 |
if ( 'front-page' === $slug ) { |
| 30 |
return array( 'front-page', 'home', 'index' ); |
| 31 |
} |
| 32 |
|
| 33 |
$matches = array(); |
| 34 |
|
| 35 |
$template_hierarchy = array( $slug ); |
| 36 |
// Most default templates don't have `$template_prefix` assigned. |
| 37 |
if ( ! empty( $template_prefix ) ) { |
| 38 |
list( $type ) = explode( '-', $template_prefix ); |
| 39 |
// We need these checks because we always add the `$slug` above. |
| 40 |
if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) { |
| 41 |
$template_hierarchy[] = $template_prefix; |
| 42 |
} |
| 43 |
if ( $slug !== $type ) { |
| 44 |
$template_hierarchy[] = $type; |
| 45 |
} |
| 46 |
} elseif ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) { |
| 47 |
$template_hierarchy[] = $matches[1]; |
| 48 |
} elseif ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) { |
| 49 |
$type = $matches[1]; |
| 50 |
$slug_remaining = $matches[2]; |
| 51 |
|
| 52 |
$items = 'single' === $type ? get_post_types() : get_taxonomies(); |
| 53 |
foreach ( $items as $item ) { |
| 54 |
if ( ! str_starts_with( $slug_remaining, $item ) ) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
// If $slug_remaining is equal to $post_type or $taxonomy we have |
| 59 |
// the single-$post_type template or the taxonomy-$taxonomy template. |
| 60 |
if ( $slug_remaining === $item ) { |
| 61 |
$template_hierarchy[] = $type; |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
// If $slug_remaining is single-$post_type-$slug template. |
| 66 |
if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) { |
| 67 |
$template_hierarchy[] = "$type-$item"; |
| 68 |
$template_hierarchy[] = $type; |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
// Handle `archive` template. |
| 74 |
if ( |
| 75 |
str_starts_with( $slug, 'author' ) || |
| 76 |
str_starts_with( $slug, 'taxonomy' ) || |
| 77 |
str_starts_with( $slug, 'category' ) || |
| 78 |
str_starts_with( $slug, 'tag' ) || |
| 79 |
'date' === $slug |
| 80 |
) { |
| 81 |
$template_hierarchy[] = 'archive'; |
| 82 |
} |
| 83 |
// Handle `single` template. |
| 84 |
if ( 'attachment' === $slug ) { |
| 85 |
$template_hierarchy[] = 'single'; |
| 86 |
} |
| 87 |
// Handle `singular` template. |
| 88 |
if ( |
| 89 |
str_starts_with( $slug, 'single' ) || |
| 90 |
str_starts_with( $slug, 'page' ) || |
| 91 |
'attachment' === $slug |
| 92 |
) { |
| 93 |
$template_hierarchy[] = 'singular'; |
| 94 |
} |
| 95 |
$template_hierarchy[] = 'index'; |
| 96 |
return $template_hierarchy; |
| 97 |
} |
| 98 |
|