@@ -1074,6 +1074,46 @@ function _build_block_template_result_from_post( $post ) {
10741074 return $ template ;
10751075}
10761076
1077+ function get_registered_block_templates ( $ query ) {
1078+ $ template_files = _get_block_templates_files ( 'wp_template ' , $ query );
1079+ $ query_result = array ();
1080+
1081+ // _get_block_templates_files seems broken, it does not obey the query.
1082+ if ( isset ( $ query ['slug__in ' ] ) && is_array ( $ query ['slug__in ' ] ) ) {
1083+ $ template_files = array_filter (
1084+ $ template_files ,
1085+ function ( $ template_file ) use ( $ query ) {
1086+ return in_array ( $ template_file ['slug ' ], $ query ['slug__in ' ], true );
1087+ }
1088+ );
1089+ }
1090+
1091+ foreach ( $ template_files as $ template_file ) {
1092+ $ query_result [] = _build_block_template_result_from_file ( $ template_file , 'wp_template ' );
1093+ }
1094+
1095+ // Add templates registered through the template registry. Filtering out the
1096+ // ones which have a theme file.
1097+ $ registered_templates = WP_Block_Templates_Registry::get_instance ()->get_by_query ( $ query );
1098+ $ matching_registered_templates = array_filter (
1099+ $ registered_templates ,
1100+ function ( $ registered_template ) use ( $ template_files ) {
1101+ foreach ( $ template_files as $ template_file ) {
1102+ if ( $ template_file ['slug ' ] === $ registered_template ->slug ) {
1103+ return false ;
1104+ }
1105+ }
1106+ return true ;
1107+ }
1108+ );
1109+
1110+ $ query_result = array_merge ( $ query_result , $ matching_registered_templates );
1111+
1112+ // Templates added by PHP filter also count as registered templates.
1113+ /** This filter is documented in wp-includes/block-template-utils.php */
1114+ return apply_filters ( 'get_block_templates ' , $ query_result , $ query , 'wp_template ' );
1115+ }
1116+
10771117/**
10781118 * Retrieves a list of unified template objects based on a query.
10791119 *
@@ -1152,6 +1192,8 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
11521192 $ wp_query_args ['post_status ' ] = 'publish ' ;
11531193 }
11541194
1195+ $ active_templates = get_option ( 'active_templates ' , array () );
1196+
11551197 $ template_query = new WP_Query ( $ wp_query_args );
11561198 $ query_result = array ();
11571199 foreach ( $ template_query ->posts as $ post ) {
@@ -1173,7 +1215,14 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
11731215 continue ;
11741216 }
11751217
1176- $ query_result [] = $ template ;
1218+ if ( $ template ->is_custom || isset ( $ query ['wp_id ' ] ) ) {
1219+ // Custom templates don't need to be activated, leave them be.
1220+ // Also don't filter out templates when querying by wp_id.
1221+ $ query_result [] = $ template ;
1222+ } elseif ( isset ( $ active_templates [ $ template ->slug ] ) && $ active_templates [ $ template ->slug ] === $ post ->ID ) {
1223+ // Only include active templates.
1224+ $ query_result [] = $ template ;
1225+ }
11771226 }
11781227
11791228 if ( ! isset ( $ query ['wp_id ' ] ) ) {
@@ -1296,6 +1345,24 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
12961345 return null ;
12971346 }
12981347 list ( $ theme , $ slug ) = $ parts ;
1348+
1349+ $ active_templates = get_option ( 'active_templates ' , array () );
1350+
1351+ if ( ! empty ( $ active_templates [ $ slug ] ) ) {
1352+ if ( is_int ( $ active_templates [ $ slug ] ) ) {
1353+ $ post = get_post ( $ active_templates [ $ slug ] );
1354+ if ( $ post && 'publish ' === $ post ->post_status ) {
1355+ $ template = _build_block_template_result_from_post ( $ post );
1356+
1357+ if ( ! is_wp_error ( $ template ) && $ theme === $ template ->theme ) {
1358+ return $ template ;
1359+ }
1360+ }
1361+ } elseif ( false === $ active_templates [ $ slug ] ) {
1362+ return null ;
1363+ }
1364+ }
1365+
12991366 $ wp_query_args = array (
13001367 'post_name__in ' => array ( $ slug ),
13011368 'post_type ' => $ template_type ,
@@ -1316,6 +1383,12 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
13161383 if ( count ( $ posts ) > 0 ) {
13171384 $ template = _build_block_template_result_from_post ( $ posts [0 ] );
13181385
1386+ // Custom templates don't need to be activated, so if it's a custom
1387+ // template, return it.
1388+ if ( ! is_wp_error ( $ template ) && $ template ->is_custom ) {
1389+ return $ template ;
1390+ }
1391+
13191392 if ( ! is_wp_error ( $ template ) ) {
13201393 return $ template ;
13211394 }
@@ -1779,3 +1852,31 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
17791852
17801853 return $ changes ;
17811854}
1855+
1856+ function wp_assign_new_template_to_theme ( $ changes , $ request ) {
1857+ // Do not run this for templates created through the old enpoint.
1858+ $ template = $ request ['id ' ] ? get_block_template ( $ request ['id ' ], 'wp_template ' ) : null ;
1859+ if ( $ template ) {
1860+ return $ changes ;
1861+ }
1862+ $ changes ->tax_input = array (
1863+ 'wp_theme ' => isset ( $ request ['theme ' ] ) ? $ request ['theme ' ] : get_stylesheet (),
1864+ );
1865+ // All new templates saved will receive meta so we can distinguish between
1866+ // templates created the old way as edits and templates created the new way.
1867+ $ changes ->meta_input = array (
1868+ 'is_inactive_by_default ' => true ,
1869+ );
1870+ return $ changes ;
1871+ }
1872+
1873+ function wp_maybe_activate_template ( $ post_id ) {
1874+ $ post = get_post ( $ post_id );
1875+ $ is_inactive_by_default = get_post_meta ( $ post_id , 'is_inactive_by_default ' , true );
1876+ if ( $ is_inactive_by_default ) {
1877+ return ;
1878+ }
1879+ $ active_templates = get_option ( 'active_templates ' , array () );
1880+ $ active_templates [ $ post ->post_name ] = $ post ->ID ;
1881+ update_option ( 'active_templates ' , $ active_templates );
1882+ }
0 commit comments