Skip to content

Commit 9892a04

Browse files
committed
Add migration
1 parent 33fa916 commit 9892a04

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

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

src/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' );

0 commit comments

Comments
 (0)