Skip to content

Commit b2eee1e

Browse files
committed
Template activation: merge changes for Beta 2.
Developed in WordPress/wordpress-develop#10425. See https://core.trac.wordpress.org/ticket/62755. * Rename new endpoints, WordPress/gutenberg#72700. * Remove fake post type for registered templates, WordPress/gutenberg#72674. * Remove the ability to deactivate registered templates, WordPress/gutenberg#72636, * Fix undefined array key PHP warning, WordPress/gutenberg#72729. * Add migration logic (to be refined), see https://core.trac.wordpress.org/ticket/64133 and WordPress/wordpress-develop#10418. Fixes #62755. Props ellatrix, priethor. Built from https://develop.svn.wordpress.org/trunk@61078 git-svn-id: https://core.svn.wordpress.org/trunk@60414 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 100e9c2 commit b2eee1e

File tree

8 files changed

+76
-33
lines changed

8 files changed

+76
-33
lines changed

wp-admin/site-editor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ static function ( $classes ) {
182182
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
183183
array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
184184
'/wp/v2/types?context=view',
185-
'/wp/v2/wp_registered_template?context=edit',
186185
'/wp/v2/types/wp_template?context=edit',
187186
'/wp/v2/types/wp_template_part?context=edit',
188187
'/wp/v2/templates?context=edit&per_page=-1',

wp-includes/block-template-utils.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,6 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
13581358
return $template;
13591359
}
13601360
}
1361-
} elseif ( false === $active_templates[ $slug ] ) {
1362-
return null;
13631361
}
13641362
}
13651363

@@ -1882,3 +1880,57 @@ function wp_maybe_activate_template( $post_id ) {
18821880
$active_templates[ $post->post_name ] = $post->ID;
18831881
update_option( 'active_templates', $active_templates );
18841882
}
1883+
1884+
function _wp_migrate_active_templates() {
1885+
// Do not run during installation when the database is not yet available.
1886+
if ( wp_installing() ) {
1887+
return;
1888+
}
1889+
1890+
$active_templates = get_option( 'active_templates', false );
1891+
1892+
if ( false !== $active_templates ) {
1893+
return;
1894+
}
1895+
1896+
// Query all templates in the database. See `get_block_templates`.
1897+
$wp_query_args = array(
1898+
'post_status' => 'publish',
1899+
'post_type' => 'wp_template',
1900+
'posts_per_page' => -1,
1901+
'no_found_rows' => true,
1902+
'lazy_load_term_meta' => false,
1903+
'tax_query' => array(
1904+
array(
1905+
'taxonomy' => 'wp_theme',
1906+
'field' => 'name',
1907+
'terms' => get_stylesheet(),
1908+
),
1909+
),
1910+
// Only get templates that are not inactive by default. We check these
1911+
// meta to make sure we don't fill the option with inactive templates
1912+
// created after the 6.9 release when for some reason the option is
1913+
// deleted.
1914+
'meta_query' => array(
1915+
'relation' => 'OR',
1916+
array(
1917+
'key' => 'is_inactive_by_default',
1918+
'compare' => 'NOT EXISTS',
1919+
),
1920+
array(
1921+
'key' => 'is_inactive_by_default',
1922+
'value' => false,
1923+
'compare' => '=',
1924+
),
1925+
),
1926+
);
1927+
1928+
$template_query = new WP_Query( $wp_query_args );
1929+
$active_templates = array();
1930+
1931+
foreach ( $template_query->posts as $post ) {
1932+
$active_templates[ $post->post_name ] = $post->ID;
1933+
}
1934+
1935+
update_option( 'active_templates', $active_templates );
1936+
}

wp-includes/block-template.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,10 @@ function resolve_block_template( $template_type, $template_hierarchy, $fallback_
164164
$template_hierarchy
165165
);
166166

167-
$object = get_queried_object();
168-
$specific_template = $object ? get_page_template_slug( $object ) : null;
167+
$object_id = get_queried_object_id();
168+
$specific_template = $object_id && get_post( $object_id ) ? get_page_template_slug( $object_id ) : null;
169169
$active_templates = (array) get_option( 'active_templates', array() );
170170

171-
// Remove templates slugs that are deactivated, except if it's the specific
172-
// template or index.
173-
$slugs = array_filter(
174-
$slugs,
175-
function ( $slug ) use ( $specific_template, $active_templates ) {
176-
$should_ignore = $slug === $specific_template || 'index' === $slug;
177-
return $should_ignore || ( ! isset( $active_templates[ $slug ] ) || false !== $active_templates[ $slug ] );
178-
}
179-
);
180-
181171
// We expect one template for each slug. Use the active template if it is
182172
// set and exists. Otherwise use the static template.
183173
$templates = array();
@@ -232,7 +222,7 @@ function ( $template ) {
232222
);
233223
$templates = array_merge( $templates, get_registered_block_templates( $query ) );
234224

235-
if ( $specific_template ) {
225+
if ( $specific_template && in_array( $specific_template, $remaining_slugs, true ) ) {
236226
$templates = array_merge( $templates, get_block_templates( array( 'slug__in' => array( $specific_template ) ) ) );
237227
}
238228

wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@
570570

571571
// Post.
572572
add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
573+
add_action( 'init', '_wp_migrate_active_templates', 0 ); // Highest priority.
573574
add_action( 'admin_menu', '_add_post_type_submenus' );
574575
add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
575576
add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' );

wp-includes/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function create_initial_post_types() {
398398
'show_in_menu' => false,
399399
'show_in_rest' => true,
400400
'rewrite' => false,
401-
'rest_base' => 'wp_template',
401+
'rest_base' => 'created-templates',
402402
'rest_controller_class' => 'WP_REST_Posts_Controller',
403403
'late_route_registration' => true,
404404
'capability_type' => array( 'template', 'templates' ),

wp-includes/rest-api.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,6 @@ function rest_api_default_filters() {
263263
* @since 4.7.0
264264
*/
265265
function create_initial_rest_routes() {
266-
global $wp_post_types;
267-
268-
// Register the registered templates endpoint. For that we need to copy the
269-
// wp_template post type so that it's available as an entity in core-data.
270-
$wp_post_types['wp_registered_template'] = clone $wp_post_types['wp_template'];
271-
$wp_post_types['wp_registered_template']->name = 'wp_registered_template';
272-
$wp_post_types['wp_registered_template']->rest_base = 'wp_registered_template';
273-
$wp_post_types['wp_registered_template']->rest_controller_class = 'WP_REST_Registered_Templates_Controller';
274-
275266
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
276267
$controller = $post_type->get_rest_controller();
277268

@@ -298,11 +289,14 @@ function create_initial_rest_routes() {
298289
}
299290
}
300291

292+
global $wp_post_types;
293+
301294
// Register the old templates endpoints. The WP_REST_Templates_Controller
302295
// and sub-controllers used linked to the wp_template post type, but are no
303296
// longer. They still require a post type object when contructing the class.
304297
// To maintain backward and changes to these controller classes, we make use
305298
// that the wp_template post type has the right information it needs.
299+
$current_wp_template_rest_base = $wp_post_types['wp_template']->rest_base;
306300
$wp_post_types['wp_template']->rest_base = 'templates';
307301
// Store the classes so they can be restored.
308302
$original_rest_controller_class = $wp_post_types['wp_template']->rest_controller_class;
@@ -328,7 +322,7 @@ function create_initial_rest_routes() {
328322
$wp_post_types['wp_template']->autosave_rest_controller_class = $original_autosave_rest_controller_class;
329323
$wp_post_types['wp_template']->revisions_rest_controller_class = $original_revisions_rest_controller_class;
330324
// Restore the original base.
331-
$wp_post_types['wp_template']->rest_base = 'wp_template';
325+
$wp_post_types['wp_template']->rest_base = $current_wp_template_rest_base;
332326

333327
// Register the old routes.
334328
$autosaves_controller->register_routes();
@@ -356,6 +350,10 @@ function create_initial_rest_routes() {
356350
)
357351
);
358352

353+
// Registered templates.
354+
$controller = new WP_REST_Registered_Templates_Controller();
355+
$controller->register_routes();
356+
359357
// Post types.
360358
$controller = new WP_REST_Post_Types_Controller();
361359
$controller->register_routes();

wp-includes/rest-api/endpoints/class-wp-rest-registered-templates-controller.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
class WP_REST_Registered_Templates_Controller extends WP_REST_Templates_Controller {
4+
public function __construct() {
5+
parent::__construct( 'wp_template' );
6+
$this->rest_base = 'registered-templates';
7+
$this->namespace = 'wp/v2';
8+
}
9+
410
public function register_routes() {
511
// Lists all templates.
612
register_rest_route(
@@ -81,9 +87,8 @@ public function get_items( $request ) {
8187
$query_result = get_registered_block_templates( $query );
8288
$templates = array();
8389
foreach ( $query_result as $template ) {
84-
$item = $this->prepare_item_for_response( $template, $request );
85-
$item->data['type'] = 'wp_registered_template';
86-
$templates[] = $this->prepare_response_for_collection( $item );
90+
$item = $this->prepare_item_for_response( $template, $request );
91+
$templates[] = $this->prepare_response_for_collection( $item );
8792
}
8893

8994
return rest_ensure_response( $templates );
@@ -97,8 +102,6 @@ public function get_item( $request ) {
97102
}
98103

99104
$item = $this->prepare_item_for_response( $template, $request );
100-
// adjust the template type here instead
101-
$item->data['type'] = 'wp_registered_template';
102105
return rest_ensure_response( $item );
103106
}
104107
}

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.9-beta1-61077';
19+
$wp_version = '6.9-beta1-61078';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)