@@ -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+ }
0 commit comments