diff --git a/wp-admin/site-editor.php b/wp-admin/site-editor.php index d27f461c9f..5fc06a4078 100644 --- a/wp-admin/site-editor.php +++ b/wp-admin/site-editor.php @@ -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', diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index b0ca8c6caf..53954b7ff4 100644 --- a/wp-includes/block-template-utils.php +++ b/wp-includes/block-template-utils.php @@ -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 ); +} diff --git a/wp-includes/block-template.php b/wp-includes/block-template.php index 2dd7b2e3c0..220cb0b2a2 100644 --- a/wp-includes/block-template.php +++ b/wp-includes/block-template.php @@ -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 ) ) ) ); } diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 4a386eb53c..5dc54c3686 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -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' ); diff --git a/wp-includes/post.php b/wp-includes/post.php index 3940519d4d..1ab71ae53a 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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' ), diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index d23931a589..7e3ed578ec 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -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(); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-registered-templates-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-registered-templates-controller.php index 97b65af757..fd912bfb45 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-registered-templates-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-registered-templates-controller.php @@ -1,6 +1,12 @@ 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 ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 25bf53c181..68e18256b5 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.