mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55200 602fd350-edb4-49c9-b593-d223f7449a82
- Loading branch information
1 parent
a501f4f
commit 8ff5899
Showing
6 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
tests/phpunit/tests/widgets/wpBlockThemeRegisterClassicSidebar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Tests for _wp_block_theme_register_classic_sidebars(). | ||
* | ||
* @group widgets | ||
* @covers ::_wp_block_theme_register_classic_sidebars | ||
*/ | ||
class Tests_Widgets_WpBlockThemeRegisterClassicSidebars extends WP_UnitTestCase { | ||
/** | ||
* Original global $wp_registered_sidebars. | ||
* | ||
* @var array | ||
*/ | ||
private static $wp_registered_sidebars; | ||
|
||
public static function set_up_before_class() { | ||
global $wp_registered_sidebars; | ||
parent::set_up_before_class(); | ||
|
||
// Store the original global before running tests. | ||
static::$wp_registered_sidebars = $wp_registered_sidebars; | ||
} | ||
|
||
public function tear_down() { | ||
// Restore the global after each test. | ||
global $wp_registered_sidebars; | ||
$wp_registered_sidebars = static::$wp_registered_sidebars; | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
public function test_a_sidebar_should_be_registered() { | ||
global $wp_registered_sidebars; | ||
|
||
$sidebar_id = array_key_first( $wp_registered_sidebars ); | ||
$this->assertNotEmpty( $sidebar_id ); | ||
} | ||
|
||
/** | ||
* @ticket 57531 | ||
*/ | ||
public function test_should_reregister_previous_theme_sidebar() { | ||
global $wp_registered_sidebars; | ||
|
||
$sidebar_id = array_key_first( $wp_registered_sidebars ); | ||
|
||
switch_theme( 'block-theme' ); | ||
unregister_sidebar( $sidebar_id ); | ||
|
||
// Test before. | ||
$this->assertArrayNotHasKey( | ||
$sidebar_id, | ||
$wp_registered_sidebars, | ||
'Sidebar should not be in registered sidebars after unregister' | ||
); | ||
|
||
_wp_block_theme_register_classic_sidebars(); | ||
|
||
// Test after. | ||
$this->assertArrayHasKey( | ||
$sidebar_id, | ||
$wp_registered_sidebars, | ||
'Sidebar should be in registered sidebars after invoking _wp_block_theme_register_classic_sidebars()' | ||
); | ||
} | ||
|
||
/** | ||
* @ticket 57531 | ||
*/ | ||
public function test_should_bail_out_when_theme_mod_is_empty() { | ||
global $wp_registered_sidebars; | ||
|
||
// Test state before invoking. | ||
$this->assertFalse( | ||
get_theme_mod( 'wp_classic_sidebars' ), | ||
'Theme mod should not be set before invoking _wp_block_theme_register_classic_sidebars()' | ||
); | ||
|
||
$before = $wp_registered_sidebars; | ||
_wp_block_theme_register_classic_sidebars(); | ||
|
||
// Test state after invoking. | ||
$this->assertSameSetsWithIndex( | ||
$before, | ||
$wp_registered_sidebars, | ||
'No change should happen after invoking _wp_block_theme_register_classic_sidebars()' | ||
); | ||
} | ||
} |