Skip to content

Commit

Permalink
Add support for custom templates in FSE themes (#27778)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Dec 21, 2020
1 parent 86a9dc7 commit 8fc3e71
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Support for page templates.
*
* @package gutenberg
*/

/**
* Load the page templates in Gutenberg.
*
* @param array $templates Page templates.
* @param WP_Theme $theme WP_Theme instance.
* @param WP_Post $post The post being edited, provided for context, or null.
* @return array (Maybe) modified page templates array.
*/
function gutenberg_load_block_page_templates( $templates, $theme, $post ) {
if ( ! gutenberg_is_fse_theme() ) {
return $templates;
}
$config_file = locate_template( 'experimental-theme.json' );
if ( ! file_exists( $config_file ) ) {
return $templates;
}
$data = json_decode(
file_get_contents( $config_file ),
true
);
$page_templates = array();
if ( isset( $data['pageTemplates'] ) ) {
foreach ( $data['pageTemplates'] as $key => $page_template ) {
if ( ( ! isset( $page_template['postTypes'] ) && 'page' === $post->post_type ) ||
( isset( $page_template['postTypes'] ) && in_array( $post->post_type, $page_template['postTypes'], true ) )
) {
$page_templates[ $key ] = $page_template['title'];
}
}
}

return $page_templates;
}
add_filter( 'theme_post_templates', 'gutenberg_load_block_page_templates', 10, 3 );
add_filter( 'theme_page_templates', 'gutenberg_load_block_page_templates', 10, 3 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/full-site-editing.php';
require __DIR__ . '/full-site-editing/default-template-types.php';
require __DIR__ . '/full-site-editing/templates-utils.php';
require __DIR__ . '/full-site-editing/page-templates.php';
require __DIR__ . '/templates-sync.php';
require __DIR__ . '/templates.php';
require __DIR__ . '/template-parts.php';
Expand Down
3 changes: 1 addition & 2 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function get_template_hierarchy( $template_type ) {
*/
function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
global $_wp_current_template_content;

$current_template = gutenberg_resolve_template( $type, $templates );

if ( $current_template ) {
Expand Down Expand Up @@ -215,7 +214,7 @@ function gutenberg_viewport_meta_tag() {
* @return string Template file name without extension.
*/
function gutenberg_strip_php_suffix( $template_file ) {
return preg_replace( '/\.php$/', '', $template_file );
return preg_replace( '/\.(php|html)$/', '', $template_file );
}

/**
Expand Down

0 comments on commit 8fc3e71

Please sign in to comment.