@@ -160,35 +160,44 @@ function create_auto_draft_for_template_part_block( $block ) {
160
160
)
161
161
);
162
162
$ 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 ) {
164
164
$ template_part_id = $ template_part_post ->ID ;
165
165
} 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.
167
168
$ template_part_file_path =
168
169
get_stylesheet_directory () . '/block-template-parts/ ' . $ block ['attrs ' ]['slug ' ] . '.html ' ;
169
170
if ( ! file_exists ( $ template_part_file_path ) ) {
170
171
if ( gutenberg_is_experiment_enabled ( 'gutenberg-full-site-editing-demo ' ) ) {
171
172
$ template_part_file_path =
172
173
dirname ( __FILE__ ) . '/demo-block-template-parts/ ' . $ block ['attrs ' ]['slug ' ] . '.html ' ;
173
174
if ( ! file_exists ( $ template_part_file_path ) ) {
174
- return ;
175
+ $ template_part_file_path = false ;
175
176
}
176
177
} 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
+ );
178
199
}
179
200
}
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
- );
192
201
}
193
202
}
194
203
$ template_part_ids [ $ block ['attrs ' ]['slug ' ] ] = $ template_part_id ;
0 commit comments