diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index 9a8268c3392d7..35fb273fc65a7 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -237,6 +237,104 @@ static function ( $classes ) { ), ); +$template_slugs = array(); +$front_page = null; +if ( $block_editor_context->post && 'page' === $block_editor_context->post->post_type ) { + $template_slugs[] = empty( $block_editor_context->post->post_name ) ? 'page' : 'page-' . $block_editor_context->post->post_name; + $template_slugs[] = 'page'; +} else { + $template_slugs[] = 'front-page'; + + $page_on_front = (int) get_option( 'page_on_front' ); + if ( 'page' === get_option( 'show_on_front' ) && $page_on_front > 0 ) { + $front_page = get_post( $page_on_front ); + if ( $front_page instanceof WP_Post ) { + $template_slugs[] = empty( $front_page->post_name ) ? 'page' : 'page-' . $front_page->post_name; + $template_slugs[] = 'page'; + } + } else { + $template_slugs[] = 'home'; + } + + $template_slugs[] = 'index'; +} + +$template_slugs = array_values( array_unique( $template_slugs ) ); +$templates = get_block_templates( + array( + 'slug__in' => $template_slugs, + ), + 'wp_template' +); +$priorities = array_flip( $template_slugs ); + +usort( + $templates, + static function ( $template_a, $template_b ) use ( $priorities ) { + return ( $priorities[ $template_a->slug ] ?? 999 ) - ( $priorities[ $template_b->slug ] ?? 999 ); + } +); + +$template_part_ids = array(); +$has_query = false; +$walk_blocks = static function ( array $blocks ) use ( &$walk_blocks, &$template_part_ids, &$has_query ) { + foreach ( $blocks as $block ) { + if ( 'core/template-part' === ( $block['blockName'] ?? '' ) && ! empty( $block['attrs']['slug'] ) ) { + $theme = ! empty( $block['attrs']['theme'] ) ? $block['attrs']['theme'] : get_stylesheet(); + $template_part_ids[] = $theme . '//' . $block['attrs']['slug']; + } + + if ( 'core/query' === ( $block['blockName'] ?? '' ) ) { + $has_query = true; + } + + if ( ! empty( $block['innerBlocks'] ) ) { + $walk_blocks( $block['innerBlocks'] ); + } + } +}; + +if ( ! empty( $templates ) && ! empty( $templates[0]->content ) ) { + $blocks = resolve_pattern_blocks( parse_blocks( $templates[0]->content ) ); + $walk_blocks( $blocks ); +} + +foreach ( array_unique( $template_part_ids ) as $template_part_id ) { + $preload_paths[] = '/wp/v2/template-parts/' . $template_part_id . '?context=edit'; +} + +if ( $front_page instanceof WP_Post ) { + $route_for_front_page = rest_get_route_for_post( $front_page ); + if ( $route_for_front_page ) { + $preload_paths[] = add_query_arg( 'context', 'edit', $route_for_front_page ); + } + $preload_paths[] = add_query_arg( + 'slug', + empty( $front_page->post_name ) ? 'page' : 'page-' . $front_page->post_name, + '/wp/v2/templates/lookup' + ); + $preload_paths[] = '/wp/v2/types/page?context=edit'; +} + +if ( $has_query ) { + $post_rest_route = rest_get_route_for_post_type_items( 'post' ); + foreach ( array( 10, 3 ) as $per_page ) { + $preload_paths[] = add_query_arg( + array( + 'context' => 'edit', + 'offset' => 0, + 'order' => 'desc', + 'orderby' => 'date', + 'per_page' => $per_page, + 'ignore_sticky' => 'false', + ), + $post_rest_route + ); + } +} + +$preload_paths[] = '/wp/v2/taxonomies?context=view'; + if ( $block_editor_context->post ) { $route_for_post = rest_get_route_for_post( $block_editor_context->post ); if ( $route_for_post ) {