@@ -592,6 +592,15 @@ function _build_block_template_result_from_file( $template_file, $template_type
592592 $ template ->is_custom = true ;
593593 $ template ->modified = null ;
594594
595+ if ( 'wp_template ' === $ template_type ) {
596+ $ registered_template = WP_Block_Templates_Registry::get_instance ()->get_by_slug ( $ template_file ['slug ' ] );
597+ if ( $ registered_template ) {
598+ $ template ->plugin = $ registered_template ->plugin ;
599+ $ template ->title = empty ( $ template ->title ) || $ template ->title === $ template ->slug ? $ registered_template ->title : $ template ->title ;
600+ $ template ->description = empty ( $ template ->description ) ? $ registered_template ->description : $ template ->description ;
601+ }
602+ }
603+
595604 if ( 'wp_template ' === $ template_type && isset ( $ default_template_types [ $ template_file ['slug ' ] ] ) ) {
596605 $ template ->description = $ default_template_types [ $ template_file ['slug ' ] ]['description ' ];
597606 $ template ->title = $ default_template_types [ $ template_file ['slug ' ] ]['title ' ];
@@ -1014,6 +1023,19 @@ function _build_block_template_result_from_post( $post ) {
10141023 }
10151024 }
10161025
1026+ if ( 'wp_template ' === $ post ->post_type ) {
1027+ $ registered_template = WP_Block_Templates_Registry::get_instance ()->get_by_slug ( $ template ->slug );
1028+ if ( $ registered_template ) {
1029+ $ template ->plugin = $ registered_template ->plugin ;
1030+ $ template ->origin =
1031+ 'theme ' !== $ template ->origin && 'theme ' !== $ template ->source ?
1032+ 'plugin ' :
1033+ $ template ->origin ;
1034+ $ template ->title = empty ( $ template ->title ) || $ template ->title === $ template ->slug ? $ registered_template ->title : $ template ->title ;
1035+ $ template ->description = empty ( $ template ->description ) ? $ registered_template ->description : $ template ->description ;
1036+ }
1037+ }
1038+
10171039 $ hooked_blocks = get_hooked_blocks ();
10181040 if ( ! empty ( $ hooked_blocks ) || has_filter ( 'hooked_block_types ' ) ) {
10191041 $ before_block_visitor = make_before_block_visitor ( $ hooked_blocks , $ template , 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata ' );
@@ -1157,6 +1179,23 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
11571179 foreach ( $ template_files as $ template_file ) {
11581180 $ query_result [] = _build_block_template_result_from_file ( $ template_file , $ template_type );
11591181 }
1182+
1183+ if ( 'wp_template ' === $ template_type ) {
1184+ // Add templates registered in the template registry. Filtering out the ones which have a theme file.
1185+ $ registered_templates = WP_Block_Templates_Registry::get_instance ()->get_by_query ( $ query );
1186+ $ matching_registered_templates = array_filter (
1187+ $ registered_templates ,
1188+ function ( $ registered_template ) use ( $ template_files ) {
1189+ foreach ( $ template_files as $ template_file ) {
1190+ if ( $ template_file ['slug ' ] === $ registered_template ->slug ) {
1191+ return false ;
1192+ }
1193+ }
1194+ return true ;
1195+ }
1196+ );
1197+ $ query_result = array_merge ( $ query_result , $ matching_registered_templates );
1198+ }
11601199 }
11611200
11621201 /**
@@ -1287,18 +1326,17 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) {
12871326 }
12881327 list ( $ theme , $ slug ) = $ parts ;
12891328
1290- if ( get_stylesheet () ! == $ theme ) {
1291- /** This filter is documented in wp-includes/block-template-utils.php */
1292- return apply_filters ( ' get_block_file_template ' , null , $ id , $ template_type );
1293- }
1329+ if ( get_stylesheet () = == $ theme ) {
1330+ $ template_file = _get_block_template_file ( $ template_type , $ slug );
1331+ if ( null !== $ template_file ) {
1332+ $ block_template = _build_block_template_result_from_file ( $ template_file , $ template_type );
12941333
1295- $ template_file = _get_block_template_file ( $ template_type , $ slug );
1296- if ( null === $ template_file ) {
1297- /** This filter is documented in wp-includes/block-template-utils.php */
1298- return apply_filters ( 'get_block_file_template ' , null , $ id , $ template_type );
1334+ /** This filter is documented in wp-includes/block-template-utils.php */
1335+ return apply_filters ( 'get_block_file_template ' , $ block_template , $ id , $ template_type );
1336+ }
12991337 }
13001338
1301- $ block_template = _build_block_template_result_from_file ( $ template_file , $ template_type );
1339+ $ block_template = WP_Block_Templates_Registry:: get_instance ()-> get_by_slug ( $ slug );
13021340
13031341 /**
13041342 * Filters the block template object after it has been (potentially) fetched from the theme file.
@@ -1665,12 +1703,12 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
16651703 );
16661704 }
16671705
1668- $ content = get_comment_delimited_block_content (
1706+ $ content = get_comment_delimited_block_content (
16691707 'core/template-part ' ,
16701708 $ attributes ,
16711709 $ changes ->post_content
16721710 );
1673- $ content = apply_block_hooks_to_content ( $ content , $ template , 'set_ignored_hooked_blocks_metadata ' );
1711+ $ content = apply_block_hooks_to_content ( $ content , $ template , 'set_ignored_hooked_blocks_metadata ' );
16741712 $ changes ->post_content = remove_serialized_parent_block ( $ content );
16751713
16761714 $ wrapper_block_markup = extract_serialized_parent_block ( $ content );
0 commit comments