From 7f3f95543525962277f3143debceabeec66e50f2 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 30 Dec 2024 13:35:21 +0100 Subject: [PATCH] Migrate to active templates --- .../wordpress-6.8/template-activate.php | 35 +++++++++++++++++++ .../src/components/page-templates/index.js | 2 -- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.8/template-activate.php b/lib/compat/wordpress-6.8/template-activate.php index 5c44a065da1abe..64dcf1b52a1544 100644 --- a/lib/compat/wordpress-6.8/template-activate.php +++ b/lib/compat/wordpress-6.8/template-activate.php @@ -128,3 +128,38 @@ function gutenberg_set_active_template_theme( $changes, $request ) { ); return $changes; } + +// Migrate existing "edited" templates. By existing, it means that the template +// is active. +add_action( 'init', 'gutenberg_migrate_existing_templates' ); +function gutenberg_migrate_existing_templates() { + $active_templates = get_option( 'active_templates' ); + + if ( $active_templates ) { + return; + } + + // Query all templates in the database. See `get_block_templates`. + $template_query = array( + 'post_status' => array( '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(), + ), + ), + ); + + $active_templates = array(); + + foreach ( $templates as $template ) { + $active_templates[ $template->post_name ] = $template->ID; + } + + update_option( 'active_templates', $active_templates ); +} diff --git a/packages/edit-site/src/components/page-templates/index.js b/packages/edit-site/src/components/page-templates/index.js index a0a950994be5c2..76a5cbd96d8999 100644 --- a/packages/edit-site/src/components/page-templates/index.js +++ b/packages/edit-site/src/components/page-templates/index.js @@ -110,8 +110,6 @@ export default function PageTemplates() { }, [ layout, activeView ] ); const [ view, setView ] = useState( defaultView ); - console.log( view ); - // Sync the layout from the URL to the view state. useEffect( () => { setView( ( currentView ) => ( {