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
1 change: 0 additions & 1 deletion src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ static function ( $classes ) {
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',
Expand Down
56 changes: 54 additions & 2 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,6 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
return $template;
}
}
} elseif ( false === $active_templates[ $slug ] ) {
return null;
}
}

Expand Down Expand Up @@ -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() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was forgotten before Beta 1. There's a follow-up PR, but it still unsure what the final migration should look like. In the meantime, let's merge the changes we have worked with so far in Gutenberg.

// 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 );
}
16 changes: 3 additions & 13 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -232,7 +222,7 @@ function ( $template ) {
);
$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 ) ) ) );
}

Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@

// 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
2 changes: 1 addition & 1 deletion src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down
18 changes: 8 additions & 10 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Comment on lines -266 to -274
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great this is being removed. It caused issues due to wp_registered_template being longer than 20 characters, the max length for a post type. See PR comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I didn't know there's a max length

foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
$controller = $post_type->get_rest_controller();

Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing phpdoc. We should add it at some point during beta.

Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -81,9 +87,8 @@ public function get_items( $request ) {
$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 );
Expand All @@ -97,8 +102,6 @@ public function get_item( $request ) {
}

$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 );
}
}
16 changes: 8 additions & 8 deletions tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ public function test_expected_routes_in_schema() {
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions',
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/wp_registered_template',
'/wp/v2/wp_registered_template/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)',
'/wp/v2/wp_template',
'/wp/v2/wp_template/(?P<id>[\d]+)',
'/wp/v2/wp_template/(?P<id>[\d]+)/autosaves',
'/wp/v2/wp_template/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/wp_template/(?P<parent>[\d]+)/revisions',
'/wp/v2/wp_template/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/registered-templates',
'/wp/v2/registered-templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)',
'/wp/v2/created-templates',
'/wp/v2/created-templates/(?P<id>[\d]+)',
'/wp/v2/created-templates/(?P<id>[\d]+)/autosaves',
'/wp/v2/created-templates/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/created-templates/(?P<parent>[\d]+)/revisions',
'/wp/v2/created-templates/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/templates/lookup',
'/wp/v2/themes',
'/wp/v2/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
Expand Down
Loading
Loading