Skip to content

Commit 94286fd

Browse files
epiquerasockham
andauthored
Template Loader: Fix template part resolution for edited files. (#23591)
* Template Loader: Fix template part resolution for edited files. * Update lib/template-loader.php Co-authored-by: Bernie Reiter <ockham@raz.or.at> Co-authored-by: Bernie Reiter <ockham@raz.or.at>
1 parent 266ca8f commit 94286fd

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

lib/template-loader.php

+25-16
Original file line numberDiff line numberDiff line change
@@ -160,35 +160,44 @@ function create_auto_draft_for_template_part_block( $block ) {
160160
)
161161
);
162162
$template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
163-
if ( $template_part_post ) {
163+
if ( $template_part_post && 'auto-draft' !== $template_part_post->post_status ) {
164164
$template_part_id = $template_part_post->ID;
165165
} else {
166-
// Template part is not customized, get it from a file and make an auto-draft for it.
166+
// Template part is not customized, get it from a file and make an auto-draft for it, unless one already exists
167+
// and the underlying file hasn't changed.
167168
$template_part_file_path =
168169
get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html';
169170
if ( ! file_exists( $template_part_file_path ) ) {
170171
if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) {
171172
$template_part_file_path =
172173
dirname( __FILE__ ) . '/demo-block-template-parts/' . $block['attrs']['slug'] . '.html';
173174
if ( ! file_exists( $template_part_file_path ) ) {
174-
return;
175+
$template_part_file_path = false;
175176
}
176177
} else {
177-
return;
178+
$template_part_file_path = false;
179+
}
180+
}
181+
182+
if ( $template_part_file_path ) {
183+
$file_contents = file_get_contents( $template_part_file_path );
184+
if ( $template_part_post && $template_part_post->post_content === $file_contents ) {
185+
$template_part_id = $template_part_post->ID;
186+
} else {
187+
$template_part_id = wp_insert_post(
188+
array(
189+
'post_content' => $file_contents,
190+
'post_title' => $block['attrs']['slug'],
191+
'post_status' => 'auto-draft',
192+
'post_type' => 'wp_template_part',
193+
'post_name' => $block['attrs']['slug'],
194+
'meta_input' => array(
195+
'theme' => $block['attrs']['theme'],
196+
),
197+
)
198+
);
178199
}
179200
}
180-
$template_part_id = wp_insert_post(
181-
array(
182-
'post_content' => file_get_contents( $template_part_file_path ),
183-
'post_title' => $block['attrs']['slug'],
184-
'post_status' => 'auto-draft',
185-
'post_type' => 'wp_template_part',
186-
'post_name' => $block['attrs']['slug'],
187-
'meta_input' => array(
188-
'theme' => $block['attrs']['theme'],
189-
),
190-
)
191-
);
192201
}
193202
}
194203
$template_part_ids[ $block['attrs']['slug'] ] = $template_part_id;

0 commit comments

Comments
 (0)