Changeset 55194
- Timestamp:
- 02/02/2023 07:36:29 PM (23 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-template-utils.php
r55086 r55194 1326 1326 } 1327 1327 1328 $matches = array(); 1329 1328 1330 $template_hierarchy = array( $slug ); 1329 1330 1331 // Most default templates don't have `$template_prefix` assigned. 1331 if ( $template_prefix) {1332 if ( ! empty( $template_prefix ) ) { 1332 1333 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. 1334 1335 if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) { 1335 1336 $template_hierarchy[] = $template_prefix; … … 1338 1339 $template_hierarchy[] = $type; 1339 1340 } 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 } 1342 1368 // Handle `archive` template. 1343 1369 if ( … … 1354 1380 $template_hierarchy[] = 'single'; 1355 1381 } 1356 1357 1382 // Handle `singular` template. 1358 1383 if ( … … 1363 1388 $template_hierarchy[] = 'singular'; 1364 1389 } 1365 1366 1390 $template_hierarchy[] = 'index'; 1367 1368 1391 return $template_hierarchy; 1369 1392 } -
trunk/tests/phpunit/tests/block-templates/getTemplateHierarchy.php
r54269 r55194 8 8 */ 9 9 class Tests_Block_Templates_GetTemplate_Hierarchy extends WP_Block_Templates_UnitTestCase { 10 11 public function set_up() { 12 parent::set_up(); 13 register_post_type( 14 'custom_book', 15 array( 16 'public' => true, 17 'show_in_rest' => true, 18 ) 19 ); 20 register_taxonomy( 'book_type', 'custom_book' ); 21 register_taxonomy( 'books', 'custom_book' ); 22 } 23 24 public function tear_down() { 25 unregister_post_type( 'custom_book' ); 26 unregister_taxonomy( 'book_type' ); 27 unregister_taxonomy( 'books' ); 28 parent::tear_down(); 29 } 10 30 11 31 /** … … 84 104 'expected' => array( 'category-fruits', 'category', 'archive', 'index' ), 85 105 ), 106 'single word categories no prefix' => array( 107 'args' => array( 'category-fruits', false ), 108 'expected' => array( 'category-fruits', 'category', 'archive', 'index' ), 109 ), 86 110 'multi word categories' => array( 87 111 'args' => array( 'category-fruits-yellow', false, 'category' ), 88 112 'expected' => array( 'category-fruits-yellow', 'category', 'archive', 'index' ), 89 113 ), 114 'multi word categories no prefix' => array( 115 'args' => array( 'category-fruits-yellow', false ), 116 'expected' => array( 'category-fruits-yellow', 'category', 'archive', 'index' ), 117 ), 90 118 'single word taxonomy and term' => array( 91 119 'args' => array( 'taxonomy-books-action', false, 'taxonomy-books' ), 120 'expected' => array( 'taxonomy-books-action', 'taxonomy-books', 'taxonomy', 'archive', 'index' ), 121 ), 122 'single word taxonomy and term no prefix' => array( 123 'args' => array( 'taxonomy-books-action', false ), 92 124 'expected' => array( 'taxonomy-books-action', 'taxonomy-books', 'taxonomy', 'archive', 'index' ), 93 125 ), … … 120 152 'expected' => array( 'author-rigas', 'author', 'archive', 'index' ), 121 153 ), 154 'multiple word taxonomy no prefix' => array( 155 'args' => array( 'taxonomy-book_type-adventure', false ), 156 'expected' => array( 'taxonomy-book_type-adventure', 'taxonomy-book_type', 'taxonomy', 'archive', 'index' ), 157 ), 158 'single post type no prefix' => array( 159 'args' => array( 'single-custom_book', false ), 160 'expected' => array( 161 'single-custom_book', 162 'single', 163 'singular', 164 'index', 165 ), 166 ), 167 'single post and post type no prefix' => array( 168 'args' => array( 'single-custom_book-book-1', false ), 169 'expected' => array( 170 'single-custom_book-book-1', 171 'single-custom_book', 172 'single', 173 'singular', 174 'index', 175 ), 176 ), 177 'page no prefix' => array( 178 'args' => array( 'page-hi', false ), 179 'expected' => array( 180 'page-hi', 181 'page', 182 'singular', 183 'index', 184 ), 185 ), 186 'post type archive no prefix' => array( 187 'args' => array( 'archive-book', false ), 188 'expected' => array( 189 'archive-book', 190 'archive', 191 'index', 192 ), 193 ), 122 194 ); 123 195 }
Note: See TracChangeset
for help on using the changeset viewer.