Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,103 @@ 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';

if ( 'page' === get_option( 'show_on_front' ) ) {
$front_page = get_post( (int) get_option( '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 ( $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 ) {
Expand Down
Loading