Skip to content

Commit

Permalink
Widgets: Preserve classic sidebars when switching to a block theme.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
VenusPR committed Feb 3, 2023
1 parent 2b0dcb3 commit 85d1019
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,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 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
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55199';
$wp_version = '6.2-alpha-55200';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down
26 changes: 26 additions & 0 deletions 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;
}
}

0 comments on commit 85d1019

Please sign in to comment.