Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom templates in FSE themes #27778

Merged
merged 5 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
Comment on lines +41 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad What about other post types? IMO the generic theme_templates filter should be used here.

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