Revert template activation.

This commit reverts the PHP changes that were made to support the template activation feature. The UI was already reverted through the packages sync in [61177] (Gutenberg PR: https://github.com/WordPress/gutenberg/pull/73025).

Developed in https://github.com/WordPress/wordpress-develop/pull/10473.

Fixes #64214.
Props ellatrix, t-hamano, mcsf, ramonjd.
Built from https://develop.svn.wordpress.org/trunk@61178


git-svn-id: http://core.svn.wordpress.org/trunk@60514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ellatrix
2025-11-07 12:42:34 +00:00
parent 40dbf5effd
commit bc7f6b462d
11 changed files with 28 additions and 455 deletions

View File

@@ -244,9 +244,7 @@ if ( $block_editor_context->post ) {
);
}
}
} 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';
}

View File

@@ -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.
*
@@ -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 ) {
@@ -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'] ) ) {
@@ -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' ),
@@ -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;
}
@@ -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 );
}

View File

@@ -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.

View File

@@ -571,7 +571,6 @@ add_action( 'transition_post_status', '__clear_multi_author_cache' );
// 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' );
@@ -747,7 +746,6 @@ add_filter( 'user_has_cap', 'wp_maybe_grant_site_health_caps', 1, 4 );
// 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().
@@ -788,10 +786,6 @@ add_action( 'init', '_wp_register_default_font_collections' );
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' );

View File

@@ -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',
)
);
}
/**

View File

@@ -368,7 +368,7 @@ function create_initial_post_types() {
register_post_type(
'wp_template',
array(
'labels' => array(
'labels' => array(
'name' => _x( 'Templates', 'post type general name' ),
'singular_name' => _x( 'Template', 'post type singular name' ),
'add_new' => __( 'Add Template' ),
@@ -389,20 +389,22 @@ function create_initial_post_types() {
'items_list' => __( 'Templates list' ),
'item_updated' => __( 'Template updated.' ),
),
'description' => __( 'Templates to include in your theme.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'created-templates',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'late_route_registration' => true,
'capability_type' => array( 'template', 'templates' ),
'capabilities' => array(
'description' => __( 'Templates to include in your theme.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'templates',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'autosave_rest_controller_class' => 'WP_REST_Template_Autosaves_Controller',
'revisions_rest_controller_class' => 'WP_REST_Template_Revisions_Controller',
'late_route_registration' => true,
'capability_type' => array( 'template', 'templates' ),
'capabilities' => array(
'create_posts' => 'edit_theme_options',
'delete_posts' => 'edit_theme_options',
'delete_others_posts' => 'edit_theme_options',
@@ -416,15 +418,14 @@ function create_initial_post_types() {
'read' => 'edit_theme_options',
'read_private_posts' => 'edit_theme_options',
),
'map_meta_cap' => true,
'supports' => array(
'map_meta_cap' => true,
'supports' => array(
'title',
'slug',
'excerpt',
'editor',
'revisions',
'author',
'custom-fields',
),
)
);
@@ -8635,16 +8636,4 @@ function wp_create_initial_post_meta() {
),
)
);
// Allow setting the is_wp_suggestion meta field, which partly determines if
// a template is a custom template.
register_post_meta(
'wp_template',
'is_wp_suggestion',
array(
'type' => 'boolean',
'show_in_rest' => true,
'single' => true,
)
);
}

View File

@@ -289,71 +289,6 @@ function create_initial_rest_routes() {
}
}
global $wp_post_types;
// Register the old templates endpoints. The WP_REST_Templates_Controller
// and sub-controllers used linked to the wp_template post type, but are no
// longer. They still require a post type object when contructing the class.
// To maintain backward and changes to these controller classes, we make use
// that the wp_template post type has the right information it needs.
$current_wp_template_rest_base = $wp_post_types['wp_template']->rest_base;
$wp_post_types['wp_template']->rest_base = 'templates';
// Store the classes so they can be restored.
$original_rest_controller_class = $wp_post_types['wp_template']->rest_controller_class;
$original_autosave_rest_controller_class = $wp_post_types['wp_template']->autosave_rest_controller_class;
$original_revisions_rest_controller_class = $wp_post_types['wp_template']->revisions_rest_controller_class;
// Temporarily set the old classes.
$wp_post_types['wp_template']->rest_controller_class = 'WP_REST_Templates_Controller';
$wp_post_types['wp_template']->autosave_rest_controller_class = 'WP_REST_Template_Autosaves_Controller';
$wp_post_types['wp_template']->revisions_rest_controller_class = 'WP_REST_Template_Revisions_Controller';
// Initialize the controllers. The order is important: the autosave
// controller needs both the templates and revisions controllers.
$controller = new WP_REST_Templates_Controller( 'wp_template' );
$wp_post_types['wp_template']->rest_controller = $controller;
$revisions_controller = new WP_REST_Template_Revisions_Controller( 'wp_template' );
$wp_post_types['wp_template']->revisions_rest_controller = $revisions_controller;
$autosaves_controller = new WP_REST_Template_Autosaves_Controller( 'wp_template' );
// Unset the controller cache, it will be re-initialized when
// get_rest_controller is called.
$wp_post_types['wp_template']->rest_controller = null;
$wp_post_types['wp_template']->revisions_rest_controller = null;
// Restore the original classes.
$wp_post_types['wp_template']->rest_controller_class = $original_rest_controller_class;
$wp_post_types['wp_template']->autosave_rest_controller_class = $original_autosave_rest_controller_class;
$wp_post_types['wp_template']->revisions_rest_controller_class = $original_revisions_rest_controller_class;
// Restore the original base.
$wp_post_types['wp_template']->rest_base = $current_wp_template_rest_base;
// Register the old routes.
$autosaves_controller->register_routes();
$revisions_controller->register_routes();
$controller->register_routes();
register_rest_field(
'wp_template',
'theme',
array(
'get_callback' => function ( $post_arr ) {
// add_additional_fields_to_object is also called for the old
// templates controller, so we need to check if the id is an
// integer to make sure it's the proper post type endpoint.
if ( ! is_int( $post_arr['id'] ) ) {
$template = get_block_template( $post_arr['id'], 'wp_template' );
return $template ? $template->theme : null;
}
$terms = get_the_terms( $post_arr['id'], 'wp_theme' );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return null;
}
return $terms[0]->slug;
},
)
);
// Registered templates.
$controller = new WP_REST_Registered_Templates_Controller();
$controller->register_routes();
// Post types.
$controller = new WP_REST_Post_Types_Controller();
$controller->register_routes();

View File

@@ -1,107 +0,0 @@
<?php
class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controller {
public function __construct() {
parent::__construct( 'wp_template' );
$this->rest_base = 'registered-templates';
$this->namespace = 'wp/v2';
}
public function register_routes() {
// Lists all templates.
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
// Lists/updates a single template based on the given id.
register_rest_route(
$this->namespace,
// The route.
sprintf(
'/%s/(?P<id>%s%s)',
$this->rest_base,
/*
* Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
* Excludes invalid directory name characters: `/:<>*?"|`.
*/
'([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
// Matches the template name.
'[\/\w%-]+'
),
array(
'args' => array(
'id' => array(
'description' => __( 'The id of a template' ),
'type' => 'string',
'sanitize_callback' => array( $this, '_sanitize_template_id' ),
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
public function get_item_schema() {
$schema = parent::get_item_schema();
$schema['properties']['is_custom'] = array(
'description' => __( 'Whether a template is a custom template.' ),
'type' => 'bool',
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
);
$schema['properties']['plugin'] = array(
'type' => 'string',
'description' => __( 'Plugin that registered the template.' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
);
return $schema;
}
public function get_items( $request ) {
$query = array();
if ( isset( $request['area'] ) ) {
$query['area'] = $request['area'];
}
if ( isset( $request['post_type'] ) ) {
$query['post_type'] = $request['post_type'];
}
$query_result = get_registered_block_templates( $query );
$templates = array();
foreach ( $query_result as $template ) {
$item = $this->prepare_item_for_response( $template, $request );
$templates[] = $this->prepare_response_for_collection( $item );
}
return rest_ensure_response( $templates );
}
public function get_item( $request ) {
$template = get_block_file_template( $request['id'], 'wp_template' );
if ( ! $template ) {
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
}
$item = $this->prepare_item_for_response( $template, $request );
return rest_ensure_response( $item );
}
}

View File

@@ -49,11 +49,6 @@ function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_id
return $override_slug;
}
// For wp_template, slugs no longer have to be unique within the same theme.
if ( 'wp_template' === $post_type ) {
return $slug;
}
if ( ! $override_slug ) {
$override_slug = $slug;
}

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.9-beta3-61177';
$wp_version = '6.9-beta3-61178';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@@ -331,7 +331,6 @@ require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-sidebars-controller
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-widget-types-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-widgets-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-templates-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-registered-templates-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-url-details-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-font-families-controller.php';