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

[Full Site Editing]: Include theme's templates in template list in post editor #41630

Merged
merged 1 commit into from
Jun 10, 2022
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
27 changes: 20 additions & 7 deletions lib/compat/wordpress-6.1/wp-theme-get-post-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_t
return $templates;
}

// `$templates` in core come from `get_block_templates`, but since we have udpated the logic there,
// we need to use and return the templates from `gutenberg_get_block_templates`.
$block_templates = gutenberg_get_block_templates( array( 'post_type' => $post_type ), 'wp_template' );
$new_templates = array();
foreach ( $block_templates as $template ) {
$new_templates[ $template->slug ] = $template->title;
// `get_post_templates` in core uses `get_block_templates` and since we have updated the
// logic in that function, we need to find and remove the templates that were added from
// the core function. That is why we need to call both functions to find which templates
// we should exclude from passed `$templates`.
$gutenberg_block_templates = array_map(
function( $template ) {
return $template->slug;
},
gutenberg_get_block_templates( array( 'post_type' => $post_type ), 'wp_template' )
);
$core_block_templates = array_map(
function( $template ) {
return $template->slug;
},
get_block_templates( array( 'post_type' => $post_type ), 'wp_template' )
);
$templates_to_exclude = array_diff( $core_block_templates, $gutenberg_block_templates );
foreach ( $templates_to_exclude as $template_slug ) {
unset( $templates[ $template_slug ] );
}
return $new_templates;
return $templates;
}
add_filter( 'theme_templates', 'gutenberg_load_block_page_templates', 10, 4 );