Template activation: merge changes for Beta 2.

Developed in https://github.com/WordPress/wordpress-develop/pull/10425.
See https://core.trac.wordpress.org/ticket/62755.

* Rename new endpoints, https://github.com/WordPress/gutenberg/pull/72700.
* Remove fake post type for registered templates, https://github.com/WordPress/gutenberg/pull/72674.
* Remove the ability to deactivate registered templates, https://github.com/WordPress/gutenberg/pull/72636,
* Fix undefined array key PHP warning, https://github.com/WordPress/gutenberg/pull/72729.
* Add migration logic (to be refined), see https://core.trac.wordpress.org/ticket/64133 and https://github.com/WordPress/wordpress-develop/pull/10418.

Fixes #62755.
Props ellatrix, priethor.
Built from https://develop.svn.wordpress.org/trunk@61078


git-svn-id: http://core.svn.wordpress.org/trunk@60414 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ellatrix
2025-10-28 13:35:28 +00:00
parent 5ed26df259
commit ead0b93afd
8 changed files with 76 additions and 33 deletions

View File

@@ -182,7 +182,6 @@ $preload_paths = array(
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
'/wp/v2/types?context=view',
'/wp/v2/wp_registered_template?context=edit',
'/wp/v2/types/wp_template?context=edit',
'/wp/v2/types/wp_template_part?context=edit',
'/wp/v2/templates?context=edit&per_page=-1',

View File

@@ -1358,8 +1358,6 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
return $template;
}
}
} elseif ( false === $active_templates[ $slug ] ) {
return null;
}
}
@@ -1882,3 +1880,57 @@ function wp_maybe_activate_template( $post_id ) {
$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,20 +164,10 @@ function resolve_block_template( $template_type, $template_hierarchy, $fallback_
$template_hierarchy
);
$object = get_queried_object();
$specific_template = $object ? get_page_template_slug( $object ) : null;
$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() );
// Remove templates slugs that are deactivated, except if it's the specific
// template or index.
$slugs = array_filter(
$slugs,
function ( $slug ) use ( $specific_template, $active_templates ) {
$should_ignore = $slug === $specific_template || 'index' === $slug;
return $should_ignore || ( ! isset( $active_templates[ $slug ] ) || false !== $active_templates[ $slug ] );
}
);
// We expect one template for each slug. Use the active template if it is
// set and exists. Otherwise use the static template.
$templates = array();
@@ -232,7 +222,7 @@ function resolve_block_template( $template_type, $template_hierarchy, $fallback_
);
$templates = array_merge( $templates, get_registered_block_templates( $query ) );
if ( $specific_template ) {
if ( $specific_template && in_array( $specific_template, $remaining_slugs, true ) ) {
$templates = array_merge( $templates, get_block_templates( array( 'slug__in' => array( $specific_template ) ) ) );
}

View File

@@ -570,6 +570,7 @@ 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' );

View File

@@ -398,7 +398,7 @@ function create_initial_post_types() {
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'wp_template',
'rest_base' => 'created-templates',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'late_route_registration' => true,
'capability_type' => array( 'template', 'templates' ),

View File

@@ -263,15 +263,6 @@ function rest_api_default_filters() {
* @since 4.7.0
*/
function create_initial_rest_routes() {
global $wp_post_types;
// Register the registered templates endpoint. For that we need to copy the
// wp_template post type so that it's available as an entity in core-data.
$wp_post_types['wp_registered_template'] = clone $wp_post_types['wp_template'];
$wp_post_types['wp_registered_template']->name = 'wp_registered_template';
$wp_post_types['wp_registered_template']->rest_base = 'wp_registered_template';
$wp_post_types['wp_registered_template']->rest_controller_class = 'WP_REST_Registered_Templates_Controller';
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
$controller = $post_type->get_rest_controller();
@@ -298,11 +289,14 @@ 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;
@@ -328,7 +322,7 @@ function create_initial_rest_routes() {
$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 = 'wp_template';
$wp_post_types['wp_template']->rest_base = $current_wp_template_rest_base;
// Register the old routes.
$autosaves_controller->register_routes();
@@ -356,6 +350,10 @@ function create_initial_rest_routes() {
)
);
// 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,6 +1,12 @@
<?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(
@@ -81,9 +87,8 @@ class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controll
$query_result = get_registered_block_templates( $query );
$templates = array();
foreach ( $query_result as $template ) {
$item = $this->prepare_item_for_response( $template, $request );
$item->data['type'] = 'wp_registered_template';
$templates[] = $this->prepare_response_for_collection( $item );
$item = $this->prepare_item_for_response( $template, $request );
$templates[] = $this->prepare_response_for_collection( $item );
}
return rest_ensure_response( $templates );
@@ -97,8 +102,6 @@ class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controll
}
$item = $this->prepare_item_for_response( $template, $request );
// adjust the template type here instead
$item->data['type'] = 'wp_registered_template';
return rest_ensure_response( $item );
}
}

View File

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