Skip to content

Commit

Permalink
Template Loader: Drop $_wp_current_template_id (#22196)
Browse files Browse the repository at this point in the history
This PR changes the `gutenberg_template_loader_filter_block_editor_settings` filter to detect if we're dealing with the Site Editor from the `$settings` array, allowing us to drop the `$_wp_current_template_id` global.
  • Loading branch information
ockham authored Jun 22, 2020
1 parent 3bb551c commit ded587c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ function get_template_hierarchy( $template_type ) {
* @return string The path to the Full Site Editing template canvas file.
*/
function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
global $_wp_current_template_id, $_wp_current_template_content;
global $_wp_current_template_content;

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

if ( $current_template ) {
$_wp_current_template_id = $current_template['template_post']->ID;
$_wp_current_template_content = empty( $current_template['template_post']->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template['template_post']->post_content;

if ( isset( $_GET['_wp-find-template'] ) ) {
Expand Down Expand Up @@ -416,16 +415,20 @@ function gutenberg_strip_php_suffix( $template_file ) {
* @return array Filtered editor settings.
*/
function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
global $post, $_wp_current_template_id;
global $post;

if ( ! $post || ! post_type_exists( 'wp_template' ) || ! post_type_exists( 'wp_template_part' ) ) {
return $settings;
}

// Create template part auto-drafts for the edited post.
$post = isset( $_wp_current_template_id )
? get_post( $_wp_current_template_id ) // It's a template.
: get_post(); // It's a post.
// If this is the Site Editor, auto-drafts for template parts have already been generated
// through `gutenberg_find_template_post_and_parts` in `gutenberg_edit_site_init`.
if ( isset( $settings['editSiteInitialState'] ) ) {
return $settings;
}

// Otherwise, create template part auto-drafts for the edited post.
$post = get_post();
foreach ( parse_blocks( $post->post_content ) as $block ) {
create_auto_draft_for_template_part_block( $block );
}
Expand Down

0 comments on commit ded587c

Please sign in to comment.