Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport: Store legacy sidebars when switching to a block theme #3893

Closed
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@
add_action( 'after_setup_theme', 'wp_setup_widgets_block_editor', 1 );
add_action( 'init', 'wp_widgets_init', 1 );
add_action( 'change_locale', array( 'WP_Widget_Media', 'reset_default_labels' ) );
add_action( 'widgets_init', '_wp_block_theme_register_legacy_sidebars', 1 );

// Admin Bar.
// Don't remove. Wrong way to disable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ public function prepare_item_for_response( $item, $request ) {
$sidebar['class'] = '';
}

if ( wp_is_block_theme() ) {
$sidebar['status'] = 'inactive';
}

$fields = $this->get_fields_for_response( $request );
if ( rest_is_field_included( 'widgets', $fields ) ) {
$sidebars = wp_get_sidebars_widgets();
Expand Down
7 changes: 6 additions & 1 deletion src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ function locale_stylesheet() {
* @param string $stylesheet Stylesheet name.
*/
function switch_theme( $stylesheet ) {
global $wp_theme_directories, $wp_customize, $sidebars_widgets;
global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars;
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved

$requirements = validate_theme_requirements( $stylesheet );
if ( is_wp_error( $requirements ) ) {
Expand Down Expand Up @@ -814,6 +814,11 @@ function switch_theme( $stylesheet ) {
}
}

// Store legacy sidebars for later use by block themes.
if ( $new_theme->is_block_theme() ) {
set_theme_mod( 'wp_legacy_sidebars', $wp_registered_sidebars );
}

update_option( 'theme_switched', $old_theme->get_stylesheet() );

/**
Expand Down
26 changes: 26 additions & 0 deletions src/wp-includes/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2105,3 +2105,29 @@ function wp_check_widget_editor_deps() {
}
}
}

/**
* Register the previous theme's sidebars for the block themes.
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 6.2.0
* @access private
*
* @global array $wp_registered_sidebars Registered sidebars.
*/
function _wp_block_theme_register_legacy_sidebars() {
global $wp_registered_sidebars;

if ( ! wp_is_block_theme() ) {
return;
}

$legacy_sidebars = get_theme_mod( 'wp_legacy_sidebars' );
if ( empty( $legacy_sidebars ) ) {
return;
}

// Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
foreach ( $legacy_sidebars as $sidebar ) {
$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
}
}