Skip to content

Commit 2970a10

Browse files
committed
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: WordPress/gutenberg#73025). Developed in #10473. Fixes #64214. Props ellatrix, t-hamano, mcsf, ramonjd. git-svn-id: https://develop.svn.wordpress.org/trunk@61178 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bb787ef commit 2970a10

15 files changed

+1239
-2787
lines changed

src/wp-admin/site-editor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ static function ( $classes ) {
244244
);
245245
}
246246
}
247-
} elseif ( isset( $_GET['p'] ) && '/' !== $_GET['p'] ) {
248-
// Only prefetch for the root. If we preload it for all pages and it's not
249-
// used it won't be possible to invalidate.
247+
} else {
250248
$preload_paths[] = '/wp/v2/templates/lookup?slug=front-page';
251249
$preload_paths[] = '/wp/v2/templates/lookup?slug=home';
252250
}

src/wp-includes/block-template-utils.php

Lines changed: 4 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,46 +1074,6 @@ function _build_block_template_result_from_post( $post ) {
10741074
return $template;
10751075
}
10761076

1077-
function get_registered_block_templates( $query ) {
1078-
$template_files = _get_block_templates_files( 'wp_template', $query );
1079-
$query_result = array();
1080-
1081-
// _get_block_templates_files seems broken, it does not obey the query.
1082-
if ( isset( $query['slug__in'] ) && is_array( $query['slug__in'] ) ) {
1083-
$template_files = array_filter(
1084-
$template_files,
1085-
function ( $template_file ) use ( $query ) {
1086-
return in_array( $template_file['slug'], $query['slug__in'], true );
1087-
}
1088-
);
1089-
}
1090-
1091-
foreach ( $template_files as $template_file ) {
1092-
$query_result[] = _build_block_template_result_from_file( $template_file, 'wp_template' );
1093-
}
1094-
1095-
// Add templates registered through the template registry. Filtering out the
1096-
// ones which have a theme file.
1097-
$registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
1098-
$matching_registered_templates = array_filter(
1099-
$registered_templates,
1100-
function ( $registered_template ) use ( $template_files ) {
1101-
foreach ( $template_files as $template_file ) {
1102-
if ( $template_file['slug'] === $registered_template->slug ) {
1103-
return false;
1104-
}
1105-
}
1106-
return true;
1107-
}
1108-
);
1109-
1110-
$query_result = array_merge( $query_result, $matching_registered_templates );
1111-
1112-
// Templates added by PHP filter also count as registered templates.
1113-
/** This filter is documented in wp-includes/block-template-utils.php */
1114-
return apply_filters( 'get_block_templates', $query_result, $query, 'wp_template' );
1115-
}
1116-
11171077
/**
11181078
* Retrieves a list of unified template objects based on a query.
11191079
*
@@ -1192,8 +1152,6 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
11921152
$wp_query_args['post_status'] = 'publish';
11931153
}
11941154

1195-
$active_templates = get_option( 'active_templates', array() );
1196-
11971155
$template_query = new WP_Query( $wp_query_args );
11981156
$query_result = array();
11991157
foreach ( $template_query->posts as $post ) {
@@ -1215,14 +1173,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
12151173
continue;
12161174
}
12171175

1218-
if ( $template->is_custom || isset( $query['wp_id'] ) ) {
1219-
// Custom templates don't need to be activated, leave them be.
1220-
// Also don't filter out templates when querying by wp_id.
1221-
$query_result[] = $template;
1222-
} elseif ( isset( $active_templates[ $template->slug ] ) && $active_templates[ $template->slug ] === $post->ID ) {
1223-
// Only include active templates.
1224-
$query_result[] = $template;
1225-
}
1176+
$query_result[] = $template;
12261177
}
12271178

12281179
if ( ! isset( $query['wp_id'] ) ) {
@@ -1345,23 +1296,7 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
13451296
return null;
13461297
}
13471298
list( $theme, $slug ) = $parts;
1348-
1349-
$active_templates = get_option( 'active_templates', array() );
1350-
1351-
if ( ! empty( $active_templates[ $slug ] ) ) {
1352-
if ( is_int( $active_templates[ $slug ] ) ) {
1353-
$post = get_post( $active_templates[ $slug ] );
1354-
if ( $post && 'publish' === $post->post_status ) {
1355-
$template = _build_block_template_result_from_post( $post );
1356-
1357-
if ( ! is_wp_error( $template ) && $theme === $template->theme ) {
1358-
return $template;
1359-
}
1360-
}
1361-
}
1362-
}
1363-
1364-
$wp_query_args = array(
1299+
$wp_query_args = array(
13651300
'post_name__in' => array( $slug ),
13661301
'post_type' => $template_type,
13671302
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
@@ -1375,18 +1310,12 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
13751310
),
13761311
),
13771312
);
1378-
$template_query = new WP_Query( $wp_query_args );
1379-
$posts = $template_query->posts;
1313+
$template_query = new WP_Query( $wp_query_args );
1314+
$posts = $template_query->posts;
13801315

13811316
if ( count( $posts ) > 0 ) {
13821317
$template = _build_block_template_result_from_post( $posts[0] );
13831318

1384-
// Custom templates don't need to be activated, so if it's a custom
1385-
// template, return it.
1386-
if ( ! is_wp_error( $template ) && $template->is_custom ) {
1387-
return $template;
1388-
}
1389-
13901319
if ( ! is_wp_error( $template ) ) {
13911320
return $template;
13921321
}
@@ -1850,87 +1779,3 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
18501779

18511780
return $changes;
18521781
}
1853-
1854-
function wp_assign_new_template_to_theme( $changes, $request ) {
1855-
// Do not run this for templates created through the old enpoint.
1856-
$template = $request['id'] ? get_block_template( $request['id'], 'wp_template' ) : null;
1857-
if ( $template ) {
1858-
return $changes;
1859-
}
1860-
if ( ! isset( $changes->tax_input ) ) {
1861-
$changes->tax_input = array();
1862-
}
1863-
$changes->tax_input['wp_theme'] = isset( $request['theme'] ) ? $request['theme'] : get_stylesheet();
1864-
// All new templates saved will receive meta so we can distinguish between
1865-
// templates created the old way as edits and templates created the new way.
1866-
if ( ! isset( $changes->meta_input ) ) {
1867-
$changes->meta_input = array();
1868-
}
1869-
$changes->meta_input['is_inactive_by_default'] = true;
1870-
return $changes;
1871-
}
1872-
1873-
function wp_maybe_activate_template( $post_id ) {
1874-
$post = get_post( $post_id );
1875-
$is_inactive_by_default = get_post_meta( $post_id, 'is_inactive_by_default', true );
1876-
if ( $is_inactive_by_default ) {
1877-
return;
1878-
}
1879-
$active_templates = get_option( 'active_templates', array() );
1880-
$active_templates[ $post->post_name ] = $post->ID;
1881-
update_option( 'active_templates', $active_templates );
1882-
}
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-
}

src/wp-includes/block-template.php

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

167-
$object_id = get_queried_object_id();
168-
$specific_template = $object_id && get_post( $object_id ) ? get_page_template_slug( $object_id ) : null;
169-
$active_templates = (array) get_option( 'active_templates', array() );
170-
171-
// We expect one template for each slug. Use the active template if it is
172-
// set and exists. Otherwise use the static template.
173-
$templates = array();
174-
$remaining_slugs = array();
175-
176-
foreach ( $slugs as $slug ) {
177-
if ( $slug === $specific_template || empty( $active_templates[ $slug ] ) ) {
178-
$remaining_slugs[] = $slug;
179-
continue;
180-
}
181-
182-
// TODO: it need to be possible to set a static template as active.
183-
$post = get_post( $active_templates[ $slug ] );
184-
if ( ! $post || 'publish' !== $post->post_status ) {
185-
$remaining_slugs[] = $slug;
186-
continue;
187-
}
188-
189-
$template = _build_block_template_result_from_post( $post );
190-
191-
// Ensure the active templates are associated with the active theme.
192-
// See _build_block_template_object_from_post_object.
193-
if ( get_stylesheet() !== $template->theme ) {
194-
$remaining_slugs[] = $slug;
195-
continue;
196-
}
197-
198-
$templates[] = $template;
199-
}
200-
201-
// Apply the filter to the active templates for backward compatibility.
202-
/** This filter is documented in wp-includes/block-template-utils.php */
203-
if ( ! empty( $templates ) ) {
204-
$templates = apply_filters(
205-
'get_block_templates',
206-
$templates,
207-
array(
208-
'slug__in' => array_map(
209-
function ( $template ) {
210-
return $template->slug;
211-
},
212-
$templates
213-
),
214-
),
215-
'wp_template'
216-
);
217-
}
218-
219-
// For any remaining slugs, use the static template.
167+
// Find all potential templates 'wp_template' post matching the hierarchy.
220168
$query = array(
221-
'slug__in' => $remaining_slugs,
169+
'slug__in' => $slugs,
222170
);
223-
$templates = array_merge( $templates, get_registered_block_templates( $query ) );
224-
225-
if ( $specific_template && in_array( $specific_template, $remaining_slugs, true ) ) {
226-
$templates = array_merge( $templates, get_block_templates( array( 'slug__in' => array( $specific_template ) ) ) );
227-
}
171+
$templates = get_block_templates( $query );
228172

229173
// Order these templates per slug priority.
230174
// Build map of template slugs to their priority in the current hierarchy.

src/wp-includes/default-filters.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@
571571

572572
// Post.
573573
add_action( 'init', 'create_initial_post_types', 0 ); // Highest priority.
574-
add_action( 'init', '_wp_migrate_active_templates', 0 ); // Highest priority.
575574
add_action( 'admin_menu', '_add_post_type_submenus' );
576575
add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
577576
add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' );
@@ -747,7 +746,6 @@
747746
// Block templates post type and rendering.
748747
add_filter( 'render_block_context', '_block_template_render_without_post_block_context' );
749748
add_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug', 10, 5 );
750-
add_action( 'save_post_wp_template', 'wp_maybe_activate_template' );
751749
add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
752750
add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_template_skip_link' );
753751
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 @@
788786
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes' );
789787
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
790788

791-
// Assign the wp_theme term to any newly created wp_template with the new endpoint.
792-
// Must run before `inject_ignored_hooked_blocks_metadata_attributes`.
793-
add_action( 'rest_pre_insert_wp_template', 'wp_assign_new_template_to_theme', 9, 2 );
794-
795789
// Update ignoredHookedBlocks postmeta for some post types.
796790
add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' );
797791
add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' );

src/wp-includes/option.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,25 +2959,6 @@ function register_initial_settings() {
29592959
'description' => __( 'Allow people to submit comments on new posts.' ),
29602960
)
29612961
);
2962-
2963-
register_setting(
2964-
'reading',
2965-
'active_templates',
2966-
array(
2967-
'type' => 'object',
2968-
// Do not set the default value to an empty array! For some reason
2969-
// that will prevent the option from being set to an empty array.
2970-
'show_in_rest' => array(
2971-
'schema' => array(
2972-
'type' => 'object',
2973-
// Properties can be integers, strings, or false
2974-
// (deactivated).
2975-
'additionalProperties' => true,
2976-
),
2977-
),
2978-
'label' => 'Active Templates',
2979-
)
2980-
);
29812962
}
29822963

29832964
/**

0 commit comments

Comments
 (0)