@@ -237,24 +237,102 @@ static function ( $classes ) {
237237 ),
238238);
239239
240- $ template_parts = get_block_templates ( array (), 'wp_template_part ' );
241- foreach ( $ template_parts as $ template_part ) {
242- $ preload_paths [] = '/wp/v2/template-parts/ ' . $ template_part ->id . '?context=edit ' ;
240+ $ template_slugs = array ();
241+ $ front_page = null ;
242+ if ( $ block_editor_context ->post && 'page ' === $ block_editor_context ->post ->post_type ) {
243+ $ template_slugs [] = empty ( $ block_editor_context ->post ->post_name ) ? 'page ' : 'page- ' . $ block_editor_context ->post ->post_name ;
244+ $ template_slugs [] = 'page ' ;
245+ } else {
246+ $ template_slugs [] = 'front-page ' ;
247+
248+ if ( 'page ' === get_option ( 'show_on_front ' ) ) {
249+ $ front_page = get_post ( (int ) get_option ( 'page_on_front ' ) );
250+ if ( $ front_page instanceof WP_Post ) {
251+ $ template_slugs [] = empty ( $ front_page ->post_name ) ? 'page ' : 'page- ' . $ front_page ->post_name ;
252+ $ template_slugs [] = 'page ' ;
253+ }
254+ } else {
255+ $ template_slugs [] = 'home ' ;
256+ }
257+
258+ $ template_slugs [] = 'index ' ;
243259}
244260
245- $ post_rest_route = rest_get_route_for_post_type_items ( 'post ' );
246- foreach ( array ( 10 , 3 ) as $ per_page ) {
261+ $ template_slugs = array_values ( array_unique ( $ template_slugs ) );
262+ $ templates = get_block_templates (
263+ array (
264+ 'slug__in ' => $ template_slugs ,
265+ ),
266+ 'wp_template '
267+ );
268+ $ priorities = array_flip ( $ template_slugs );
269+
270+ usort (
271+ $ templates ,
272+ static function ( $ template_a , $ template_b ) use ( $ priorities ) {
273+ return ( $ priorities [ $ template_a ->slug ] ?? 999 ) - ( $ priorities [ $ template_b ->slug ] ?? 999 );
274+ }
275+ );
276+
277+ $ template_part_ids = array ();
278+ $ has_query = false ;
279+ $ walk_blocks = static function ( $ blocks ) use ( &$ walk_blocks , &$ template_part_ids , &$ has_query ) {
280+ foreach ( $ blocks as $ block ) {
281+ if ( 'core/template-part ' === ( $ block ['blockName ' ] ?? '' ) && ! empty ( $ block ['attrs ' ]['slug ' ] ) ) {
282+ $ theme = ! empty ( $ block ['attrs ' ]['theme ' ] ) ? $ block ['attrs ' ]['theme ' ] : get_stylesheet ();
283+ $ template_part_ids [] = $ theme . '// ' . $ block ['attrs ' ]['slug ' ];
284+ }
285+
286+ if ( 'core/query ' === ( $ block ['blockName ' ] ?? '' ) ) {
287+ $ has_query = true ;
288+ }
289+
290+ if ( ! empty ( $ block ['innerBlocks ' ] ) ) {
291+ $ walk_blocks ( $ block ['innerBlocks ' ] );
292+ }
293+ }
294+ };
295+
296+ if ( ! empty ( $ templates ) && ! empty ( $ templates [0 ]->content ) ) {
297+ $ blocks = parse_blocks ( $ templates [0 ]->content );
298+ if ( function_exists ( 'resolve_pattern_blocks ' ) ) {
299+ $ blocks = resolve_pattern_blocks ( $ blocks );
300+ }
301+ $ walk_blocks ( $ blocks );
302+ }
303+
304+ foreach ( array_unique ( $ template_part_ids ) as $ template_part_id ) {
305+ $ preload_paths [] = '/wp/v2/template-parts/ ' . $ template_part_id . '?context=edit ' ;
306+ }
307+
308+ if ( $ front_page instanceof WP_Post ) {
309+ $ route_for_front_page = rest_get_route_for_post ( $ front_page );
310+ if ( $ route_for_front_page ) {
311+ $ preload_paths [] = add_query_arg ( 'context ' , 'edit ' , $ route_for_front_page );
312+ }
247313 $ preload_paths [] = add_query_arg (
248- array (
249- 'context ' => 'edit ' ,
250- 'offset ' => 0 ,
251- 'order ' => 'desc ' ,
252- 'orderby ' => 'date ' ,
253- 'per_page ' => $ per_page ,
254- 'ignore_sticky ' => 'false ' ,
255- ),
256- $ post_rest_route
314+ 'slug ' ,
315+ empty ( $ front_page ->post_name ) ? 'page ' : 'page- ' . $ front_page ->post_name ,
316+ '/wp/v2/templates/lookup '
257317 );
318+ $ preload_paths [] = '/wp/v2/types/page?context=edit ' ;
319+ }
320+
321+ if ( $ has_query ) {
322+ $ post_rest_route = rest_get_route_for_post_type_items ( 'post ' );
323+ foreach ( array ( 10 , 3 ) as $ per_page ) {
324+ $ preload_paths [] = add_query_arg (
325+ array (
326+ 'context ' => 'edit ' ,
327+ 'offset ' => 0 ,
328+ 'order ' => 'desc ' ,
329+ 'orderby ' => 'date ' ,
330+ 'per_page ' => $ per_page ,
331+ 'ignore_sticky ' => 'false ' ,
332+ ),
333+ $ post_rest_route
334+ );
335+ }
258336}
259337
260338$ preload_paths [] = '/wp/v2/taxonomies?context=view ' ;
0 commit comments