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

Template Loader: Add theme block template resolution. #18247

Merged
merged 5 commits into from
Nov 4, 2019
Merged
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
62 changes: 54 additions & 8 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ function gutenberg_override_query_template( $template, $type, array $templates =
* @return string Path to the canvas file to include.
*/
function gutenberg_find_template( $template_file ) {
global $_wp_current_template_post, $_wp_current_template_hierarchy;
global $_wp_current_template_content, $_wp_current_template_hierarchy;

// Bail if no relevant template hierarchy was determined, or if the template file
// was overridden another way.
@@ -105,8 +105,56 @@ function gutenberg_find_template( $template_file ) {
);

if ( $template_query->have_posts() ) {
$template_posts = $template_query->get_posts();
$_wp_current_template_post = array_shift( $template_posts );
$template_posts = $template_query->get_posts();
$current_template_post = array_shift( $template_posts );

// Build map of template slugs to their priority in the current hierarchy.
$slug_priorities = array_flip( $slugs );

// See if there is a theme block template with higher priority than the resolved template post.
$higher_priority_block_template_path = null;
$higher_priority_block_template_priority = PHP_INT_MAX;
$block_template_files = glob( get_stylesheet_directory() . '/block-templates/*.html', 1 );
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really need this to be in a separate folder? Can't we use the root folder? @mtias may have thoughts here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It makes it easier to resolve files and avoids potential future naming conflicts.

if ( is_child_theme() ) {
$block_template_files = array_merge( $block_template_files, glob( get_template_directory() . '/block-templates/*.html', 1 ) );
}
foreach ( $block_template_files as $path ) {
$theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
if (
isset( $theme_block_template_priority ) &&
$theme_block_template_priority < $higher_priority_block_template_priority &&
$theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ]
) {
$higher_priority_block_template_path = $path;
$higher_priority_block_template_priority = $theme_block_template_priority;
}
}

// If there is, use it instead.
if ( isset( $higher_priority_block_template_path ) ) {
$post_name = basename( $path, '.html' );
$current_template_post = array(
'post_content' => file_get_contents( $higher_priority_block_template_path ),
'post_title' => ucfirst( $post_name ),
'post_status' => 'auto-draft',
'post_type' => 'wp_template',
'post_name' => $post_name,
);
if ( is_admin() ) {
// Only create auto-draft of block template for editing
// in admin screens, similarly to how we do it for new
// posts in the editor.
$current_template_post = get_post(
wp_insert_post( $current_template_post )
);
} else {
$current_template_post = new WP_Post(
(object) $current_template_post
);
}
}

$_wp_current_template_content = $current_template_post->post_content;
}

// Add extra hooks for template canvas.
@@ -131,17 +179,15 @@ function gutenberg_render_title_tag() {
* Renders the markup for the current template.
*/
function gutenberg_render_the_template() {
global $_wp_current_template_post;
global $_wp_current_template_content;
global $wp_embed;

if ( ! $_wp_current_template_post || 'wp_template' !== $_wp_current_template_post->post_type ) {
if ( ! $_wp_current_template_content ) {
echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
return;
}

$content = $_wp_current_template_post->post_content;

$content = $wp_embed->run_shortcode( $content );
$content = $wp_embed->run_shortcode( $_wp_current_template_content );
$content = $wp_embed->autoembed( $content );
$content = do_blocks( $content );
$content = wptexturize( $content );