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_classic_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
8 changes: 7 additions & 1 deletion src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,12 @@ function locale_stylesheet() {
* @global array $wp_theme_directories
* @global WP_Customize_Manager $wp_customize
* @global array $sidebars_widgets
* @global array $wp_registered_sidebars
*
* @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;

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

// Stores classic sidebars for later use by block themes.
if ( $new_theme->is_block_theme() ) {
set_theme_mod( 'wp_classic_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() {
}
}
}

/**
* Registers the previous theme's sidebars for the block themes.
*
* @since 6.2.0
* @access private
*
* @global array $wp_registered_sidebars Registered sidebars.
*/
function _wp_block_theme_register_classic_sidebars() {
global $wp_registered_sidebars;

if ( ! wp_is_block_theme() ) {
return;
}

$classic_sidebars = get_theme_mod( 'wp_classic_sidebars' );
if ( empty( $classic_sidebars ) ) {
return;
}

// Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
foreach ( $classic_sidebars as $sidebar ) {
$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
}
}
20 changes: 20 additions & 0 deletions tests/phpunit/tests/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -1364,4 +1364,24 @@ public function test_sidebar_guessing_one_menu_per_sidebar() {
);
$this->assertSameSetsWithIndex( $expected_sidebars, $new_next_theme_sidebars );
}

/**
* @ticket 57531
*/
public function test_stores_classic_sidebars_when_switching_to_block_theme() {
global $wp_registered_sidebars;

$this->register_sidebars( array( 'sidebar-1', 'sidebar-2' ) );

switch_theme( 'block-theme' );

unregister_sidebar( 'sidebar-1' );
unregister_sidebar( 'sidebar-2' );

// Register stored classic sidebars for block themes..
wp_widgets_init();

$this->assertArrayHasKey( 'sidebar-1', $wp_registered_sidebars );
$this->assertArrayHasKey( 'sidebar-2', $wp_registered_sidebars );
}
}