Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ static function ( $classes ) {
);
}
}
} elseif ( isset( $_GET['p'] ) && '/' !== $_GET['p'] ) {
// Only prefetch for the root. If we preload it for all pages and it's not
// used it won't be possible to invalidate.
} else {
$preload_paths[] = '/wp/v2/templates/lookup?slug=front-page';
$preload_paths[] = '/wp/v2/templates/lookup?slug=home';
}
Expand Down
163 changes: 4 additions & 159 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,46 +1074,6 @@ function _build_block_template_result_from_post( $post ) {
return $template;
}

function get_registered_block_templates( $query ) {
$template_files = _get_block_templates_files( 'wp_template', $query );
$query_result = array();

// _get_block_templates_files seems broken, it does not obey the query.
if ( isset( $query['slug__in'] ) && is_array( $query['slug__in'] ) ) {
$template_files = array_filter(
$template_files,
function ( $template_file ) use ( $query ) {
return in_array( $template_file['slug'], $query['slug__in'], true );
}
);
}

foreach ( $template_files as $template_file ) {
$query_result[] = _build_block_template_result_from_file( $template_file, 'wp_template' );
}

// Add templates registered through the template registry. Filtering out the
// ones which have a theme file.
$registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
$matching_registered_templates = array_filter(
$registered_templates,
function ( $registered_template ) use ( $template_files ) {
foreach ( $template_files as $template_file ) {
if ( $template_file['slug'] === $registered_template->slug ) {
return false;
}
}
return true;
}
);

$query_result = array_merge( $query_result, $matching_registered_templates );

// Templates added by PHP filter also count as registered templates.
/** This filter is documented in wp-includes/block-template-utils.php */
return apply_filters( 'get_block_templates', $query_result, $query, 'wp_template' );
}

/**
* Retrieves a list of unified template objects based on a query.
*
Expand Down Expand Up @@ -1192,8 +1152,6 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
$wp_query_args['post_status'] = 'publish';
}

$active_templates = get_option( 'active_templates', array() );

$template_query = new WP_Query( $wp_query_args );
$query_result = array();
foreach ( $template_query->posts as $post ) {
Expand All @@ -1215,14 +1173,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
continue;
}

if ( $template->is_custom || isset( $query['wp_id'] ) ) {
// Custom templates don't need to be activated, leave them be.
// Also don't filter out templates when querying by wp_id.
$query_result[] = $template;
} elseif ( isset( $active_templates[ $template->slug ] ) && $active_templates[ $template->slug ] === $post->ID ) {
// Only include active templates.
$query_result[] = $template;
}
$query_result[] = $template;
}

if ( ! isset( $query['wp_id'] ) ) {
Expand Down Expand Up @@ -1345,23 +1296,7 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
return null;
}
list( $theme, $slug ) = $parts;

$active_templates = get_option( 'active_templates', array() );

if ( ! empty( $active_templates[ $slug ] ) ) {
if ( is_int( $active_templates[ $slug ] ) ) {
$post = get_post( $active_templates[ $slug ] );
if ( $post && 'publish' === $post->post_status ) {
$template = _build_block_template_result_from_post( $post );

if ( ! is_wp_error( $template ) && $theme === $template->theme ) {
return $template;
}
}
}
}

$wp_query_args = array(
$wp_query_args = array(
'post_name__in' => array( $slug ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
Expand All @@ -1375,18 +1310,12 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
),
),
);
$template_query = new WP_Query( $wp_query_args );
$posts = $template_query->posts;
$template_query = new WP_Query( $wp_query_args );
$posts = $template_query->posts;

if ( count( $posts ) > 0 ) {
$template = _build_block_template_result_from_post( $posts[0] );

// Custom templates don't need to be activated, so if it's a custom
// template, return it.
if ( ! is_wp_error( $template ) && $template->is_custom ) {
return $template;
}

if ( ! is_wp_error( $template ) ) {
return $template;
}
Expand Down Expand Up @@ -1850,87 +1779,3 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated

return $changes;
}

function wp_assign_new_template_to_theme( $changes, $request ) {
// Do not run this for templates created through the old enpoint.
$template = $request['id'] ? get_block_template( $request['id'], 'wp_template' ) : null;
if ( $template ) {
return $changes;
}
if ( ! isset( $changes->tax_input ) ) {
$changes->tax_input = array();
}
$changes->tax_input['wp_theme'] = isset( $request['theme'] ) ? $request['theme'] : get_stylesheet();
// All new templates saved will receive meta so we can distinguish between
// templates created the old way as edits and templates created the new way.
if ( ! isset( $changes->meta_input ) ) {
$changes->meta_input = array();
}
$changes->meta_input['is_inactive_by_default'] = true;
return $changes;
}

function wp_maybe_activate_template( $post_id ) {
$post = get_post( $post_id );
$is_inactive_by_default = get_post_meta( $post_id, 'is_inactive_by_default', true );
if ( $is_inactive_by_default ) {
return;
}
$active_templates = get_option( 'active_templates', array() );
$active_templates[ $post->post_name ] = $post->ID;
update_option( 'active_templates', $active_templates );
}

function _wp_migrate_active_templates() {
// Do not run during installation when the database is not yet available.
if ( wp_installing() ) {
return;
}

$active_templates = get_option( 'active_templates', false );

if ( false !== $active_templates ) {
return;
}

// Query all templates in the database. See `get_block_templates`.
$wp_query_args = array(
'post_status' => 'publish',
'post_type' => 'wp_template',
'posts_per_page' => -1,
'no_found_rows' => true,
'lazy_load_term_meta' => false,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => get_stylesheet(),
),
),
// Only get templates that are not inactive by default. We check these
// meta to make sure we don't fill the option with inactive templates
// created after the 6.9 release when for some reason the option is
// deleted.
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'is_inactive_by_default',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'is_inactive_by_default',
'value' => false,
'compare' => '=',
),
),
);

$template_query = new WP_Query( $wp_query_args );
$active_templates = array();

foreach ( $template_query->posts as $post ) {
$active_templates[ $post->post_name ] = $post->ID;
}

update_option( 'active_templates', $active_templates );
}
62 changes: 3 additions & 59 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,67 +164,11 @@ function resolve_block_template( $template_type, $template_hierarchy, $fallback_
$template_hierarchy
);

$object_id = get_queried_object_id();
$specific_template = $object_id && get_post( $object_id ) ? get_page_template_slug( $object_id ) : null;
$active_templates = (array) get_option( 'active_templates', array() );

// We expect one template for each slug. Use the active template if it is
// set and exists. Otherwise use the static template.
$templates = array();
$remaining_slugs = array();

foreach ( $slugs as $slug ) {
if ( $slug === $specific_template || empty( $active_templates[ $slug ] ) ) {
$remaining_slugs[] = $slug;
continue;
}

// TODO: it need to be possible to set a static template as active.
$post = get_post( $active_templates[ $slug ] );
if ( ! $post || 'publish' !== $post->post_status ) {
$remaining_slugs[] = $slug;
continue;
}

$template = _build_block_template_result_from_post( $post );

// Ensure the active templates are associated with the active theme.
// See _build_block_template_object_from_post_object.
if ( get_stylesheet() !== $template->theme ) {
$remaining_slugs[] = $slug;
continue;
}

$templates[] = $template;
}

// Apply the filter to the active templates for backward compatibility.
/** This filter is documented in wp-includes/block-template-utils.php */
if ( ! empty( $templates ) ) {
$templates = apply_filters(
'get_block_templates',
$templates,
array(
'slug__in' => array_map(
function ( $template ) {
return $template->slug;
},
$templates
),
),
'wp_template'
);
}

// For any remaining slugs, use the static template.
// Find all potential templates 'wp_template' post matching the hierarchy.
$query = array(
'slug__in' => $remaining_slugs,
'slug__in' => $slugs,
);
$templates = array_merge( $templates, get_registered_block_templates( $query ) );

if ( $specific_template && in_array( $specific_template, $remaining_slugs, true ) ) {
$templates = array_merge( $templates, get_block_templates( array( 'slug__in' => array( $specific_template ) ) ) );
}
$templates = get_block_templates( $query );

// Order these templates per slug priority.
// Build map of template slugs to their priority in the current hierarchy.
Expand Down
6 changes: 0 additions & 6 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@

// Post.
add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
add_action( 'init', '_wp_migrate_active_templates', 0 ); // Highest priority.
add_action( 'admin_menu', '_add_post_type_submenus' );
add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' );
Expand Down Expand Up @@ -747,7 +746,6 @@
// Block templates post type and rendering.
add_filter( 'render_block_context', '_block_template_render_without_post_block_context' );
add_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug', 10, 5 );
add_action( 'save_post_wp_template', 'wp_maybe_activate_template' );
add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_template_skip_link' );
add_action( 'wp_footer', 'the_block_template_skip_link' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_block_template_skip_link().
Expand Down Expand Up @@ -788,10 +786,6 @@
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes' );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );

// Assign the wp_theme term to any newly created wp_template with the new endpoint.
// Must run before `inject_ignored_hooked_blocks_metadata_attributes`.
add_action( 'rest_pre_insert_wp_template', 'wp_assign_new_template_to_theme', 9, 2 );

// Update ignoredHookedBlocks postmeta for some post types.
add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' );
add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' );
Expand Down
19 changes: 0 additions & 19 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2959,25 +2959,6 @@ function register_initial_settings() {
'description' => __( 'Allow people to submit comments on new posts.' ),
)
);

register_setting(
'reading',
'active_templates',
array(
'type' => 'object',
// Do not set the default value to an empty array! For some reason
// that will prevent the option from being set to an empty array.
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
// Properties can be integers, strings, or false
// (deactivated).
'additionalProperties' => true,
),
),
'label' => 'Active Templates',
)
);
}

/**
Expand Down
Loading
Loading