Skip to content

Commit fbfd2b4

Browse files
Widgets: Preserve classic sidebars when switching to a block theme.
When switching to a block theme, classic sidebars were orphaned and their widgets remapping to the `'wp_inactive_widgets'` sidebar . This changeset preserves the sidebars and their widgets, providing a migration path to a block theme without losing the widgets. Classic sidebars are now: * Stored in a new theme mod called `'wp_classic_sidebars'`; * Restored to the `$wp_registered_sidebars` global variable when the `'widgets_init'` action fires (via a new internal function called `_wp_block_theme_register_classic_sidebars()`); * And marked as `'inactive'` when interacting with sidebars REST API endpoint. References: * [WordPress/gutenberg#45509 Gutenberg PR 45509] which adds an option for importing widgets from sidebars into template parts. Follow-up to [50995], [6334]. Props mamaduka, audrasjb, hellofromTonya, ironprogrammer, jameskoster, joen, matveb, mukesh27, noisysocks, poena, youknowriad. Fixes #57531. Built from https://develop.svn.wordpress.org/trunk@55200 git-svn-id: http://core.svn.wordpress.org/trunk@54733 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 848a832 commit fbfd2b4

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

wp-includes/default-filters.php

+1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@
626626
add_action( 'after_setup_theme', 'wp_setup_widgets_block_editor', 1 );
627627
add_action( 'init', 'wp_widgets_init', 1 );
628628
add_action( 'change_locale', array( 'WP_Widget_Media', 'reset_default_labels' ) );
629+
add_action( 'widgets_init', '_wp_block_theme_register_classic_sidebars', 1 );
629630

630631
// Admin Bar.
631632
// Don't remove. Wrong way to disable.

wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php

+4
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ public function prepare_item_for_response( $item, $request ) {
339339
$sidebar['class'] = '';
340340
}
341341

342+
if ( wp_is_block_theme() ) {
343+
$sidebar['status'] = 'inactive';
344+
}
345+
342346
$fields = $this->get_fields_for_response( $request );
343347
if ( rest_is_field_included( 'widgets', $fields ) ) {
344348
$sidebars = wp_get_sidebars_widgets();

wp-includes/theme.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,12 @@ function locale_stylesheet() {
733733
* @global array $wp_theme_directories
734734
* @global WP_Customize_Manager $wp_customize
735735
* @global array $sidebars_widgets
736+
* @global array $wp_registered_sidebars
736737
*
737738
* @param string $stylesheet Stylesheet name.
738739
*/
739740
function switch_theme( $stylesheet ) {
740-
global $wp_theme_directories, $wp_customize, $sidebars_widgets;
741+
global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
741742

742743
$requirements = validate_theme_requirements( $stylesheet );
743744
if ( is_wp_error( $requirements ) ) {
@@ -814,6 +815,11 @@ function switch_theme( $stylesheet ) {
814815
}
815816
}
816817

818+
// Stores classic sidebars for later use by block themes.
819+
if ( $new_theme->is_block_theme() ) {
820+
set_theme_mod( 'wp_classic_sidebars', $wp_registered_sidebars );
821+
}
822+
817823
update_option( 'theme_switched', $old_theme->get_stylesheet() );
818824

819825
/**

wp-includes/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.2-alpha-55199';
19+
$wp_version = '6.2-alpha-55200';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

wp-includes/widgets.php

+26
Original file line numberDiff line numberDiff line change
@@ -2105,3 +2105,29 @@ function wp_check_widget_editor_deps() {
21052105
}
21062106
}
21072107
}
2108+
2109+
/**
2110+
* Registers the previous theme's sidebars for the block themes.
2111+
*
2112+
* @since 6.2.0
2113+
* @access private
2114+
*
2115+
* @global array $wp_registered_sidebars Registered sidebars.
2116+
*/
2117+
function _wp_block_theme_register_classic_sidebars() {
2118+
global $wp_registered_sidebars;
2119+
2120+
if ( ! wp_is_block_theme() ) {
2121+
return;
2122+
}
2123+
2124+
$classic_sidebars = get_theme_mod( 'wp_classic_sidebars' );
2125+
if ( empty( $classic_sidebars ) ) {
2126+
return;
2127+
}
2128+
2129+
// Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
2130+
foreach ( $classic_sidebars as $sidebar ) {
2131+
$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
2132+
}
2133+
}

0 commit comments

Comments
 (0)