mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
Widgets: Improved sidebar mapping on theme switch
Builds on efforts brought forward in #17979. This will send sidebars through three levels of mapping: 1. If both themes have only one sidebar, that gets mapped. 2. If both themes have sidebars with the same slug, they get mapped. 3. Sidebars that (even partially) match slugs from a similar kind of sidebar will get mapped. Finally, if the theme has previously been active and we have a record of its sidebar configuration then, any unmapped sidebar will be restored to its previous state. Props westonruter, obenland, alexvorn2, timmydcrawford. See #39693. git-svn-id: https://develop.svn.wordpress.org/trunk@41555 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -709,24 +709,27 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
global $sidebars_widgets, $_wp_sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
$this->register_sidebars( array( 'sidebar-1', 'sidebar-2','sidebar-3', 'wp_inactive_widgets' ) );
|
||||
$this->register_sidebars( array( 'sidebar-1', 'sidebar-2', 'sidebar-3', 'wp_inactive_widgets' ) );
|
||||
|
||||
// Test restoring sidebars widgets when previously activated.
|
||||
set_theme_mod( 'sidebars_widgets', array(
|
||||
'time' => time(),
|
||||
'data' => array(
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array( 'text-1' ),
|
||||
'sidebar-3' => array( 'unregistered_widget-1' ),
|
||||
'fantasy' => array( 'archives-2' ),
|
||||
'wp_inactive_widgets' => array(),
|
||||
),
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array(),
|
||||
'sidebar-3' => array( 'unregistered_widget-1', 'text-1', 'media_image-1' ),
|
||||
'orphaned_widgets_1' => array( 'media_video-2' ),
|
||||
) );
|
||||
|
||||
$sidebars_widgets = array(
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array( 'text-1' ),
|
||||
'fantasy' => array( 'archives-2' ),
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
|
||||
$result = retrieve_widgets( true );
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertInternalType( 'array', $result );
|
||||
$this->assertNotEmpty( $result );
|
||||
$this->assertEquals( $result, $sidebars_widgets );
|
||||
|
||||
foreach ( $sidebars_widgets as $widgets ) {
|
||||
$this->assertInternalType( 'array', $widgets );
|
||||
@@ -734,22 +737,22 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
|
||||
$this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
|
||||
$this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
|
||||
$this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] );
|
||||
$this->assertContains( 'media_image-1', $sidebars_widgets['sidebar-3'] );
|
||||
$this->assertArrayNotHasKey( 'orphaned_widgets_1', $sidebars_widgets );
|
||||
|
||||
// Unregistered widget should be filtered out.
|
||||
$this->assertEmpty( $sidebars_widgets['sidebar-3'] );
|
||||
$this->assertNotContains( 'unregistered_widget-1', $sidebars_widgets['sidebar-3'] );
|
||||
|
||||
// 6 default widgets - 1 active text widget + 1 orphaned widget = 6.
|
||||
$this->assertCount( 6, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
|
||||
// 6 default widgets - 1 active text widget = 5.
|
||||
$this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'meta-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'search-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'archives-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'categories-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'recent-posts-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
|
||||
// Theme mode with previous widgets was removed.
|
||||
$this->assertFalse( get_theme_mod( 'sidebars_widgets' ) );
|
||||
|
||||
// Sidebar_widgets option was updated.
|
||||
$this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
|
||||
}
|
||||
@@ -775,7 +778,8 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
$result = retrieve_widgets( true );
|
||||
|
||||
// $sidebars_widgets matches registered sidebars.
|
||||
$this->assertNull( $result );
|
||||
$this->assertInternalType( 'array', $result );
|
||||
$this->assertEquals( $result, $sidebars_widgets );
|
||||
|
||||
foreach ( $sidebars_widgets as $widgets ) {
|
||||
$this->assertInternalType( 'array', $widgets );
|
||||
@@ -784,8 +788,8 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
$this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
|
||||
$this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
|
||||
|
||||
// No widget validity check when $sidebars_widgets matches registered sidebars.
|
||||
$this->assertContains( 'custom_widget-1', $sidebars_widgets['sidebar-3'] );
|
||||
// Invalid widget removed, even when $sidebars_widgets matches registered sidebars.
|
||||
$this->assertEmpty( $sidebars_widgets['sidebar-3'] );
|
||||
|
||||
// No lost widgets when $sidebars_widgets matches registered sidebars.
|
||||
$this->assertEmpty( $sidebars_widgets['wp_inactive_widgets'] );
|
||||
@@ -814,7 +818,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertInternalType( 'array', $result );
|
||||
$this->assertNotEmpty( $result );
|
||||
$this->assertEquals( $result, $sidebars_widgets );
|
||||
|
||||
foreach ( $sidebars_widgets as $widgets ) {
|
||||
$this->assertInternalType( 'array', $widgets );
|
||||
@@ -857,29 +861,26 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertInternalType( 'array', $result );
|
||||
$this->assertNotEmpty( $result );
|
||||
$this->assertEquals( $result, $sidebars_widgets );
|
||||
|
||||
foreach ( $sidebars_widgets as $widgets ) {
|
||||
$this->assertInternalType( 'array', $widgets );
|
||||
}
|
||||
|
||||
/*
|
||||
* Only returns intersection of registered sidebars and saved sidebars,
|
||||
* so neither fantasy-sidebar nor sidebar-3 will make the cut.
|
||||
*/
|
||||
// This sidebar is not registered anymore.
|
||||
$this->assertArrayNotHasKey( 'fantasy', $sidebars_widgets );
|
||||
$this->assertArrayNotHasKey( 'sidebar-3', $sidebars_widgets );
|
||||
$this->assertArrayHasKey( 'sidebar-3', $sidebars_widgets );
|
||||
|
||||
// archives-2 ends up as an orphan because of the above behavior.
|
||||
$this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] );
|
||||
$this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
|
||||
$this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
|
||||
|
||||
// 6 default widgets - 1 active text widget = 5.
|
||||
$this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertCount( 6, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
|
||||
$this->assertContains( 'meta-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'search-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
// archives-2 ends up as inactive because fantasy sidebar doesn't exist.
|
||||
$this->assertContains( 'archives-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'categories-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'recent-posts-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
@@ -900,14 +901,11 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
$this->register_sidebars( array( 'sidebar-1', 'sidebar-2','sidebar-3', 'wp_inactive_widgets' ) );
|
||||
|
||||
$old_sidebars_widgets = array(
|
||||
'time' => time(),
|
||||
'data' => array(
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array( 'text-1' ),
|
||||
'sidebar-3' => array( 'unregistered_widget-1' ),
|
||||
'fantasy' => array( 'archives-2' ),
|
||||
'wp_inactive_widgets' => array(),
|
||||
),
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array( 'text-1' ),
|
||||
'sidebar-3' => array( 'unregistered_widget-1' ),
|
||||
'fantasy' => array( 'archives-2' ),
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
set_theme_mod( 'sidebars_widgets', $old_sidebars_widgets );
|
||||
|
||||
@@ -915,7 +913,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertInternalType( 'array', $result );
|
||||
$this->assertNotEmpty( $result );
|
||||
$this->assertEquals( $result, $sidebars_widgets );
|
||||
|
||||
foreach ( $sidebars_widgets as $widgets ) {
|
||||
$this->assertInternalType( 'array', $widgets );
|
||||
@@ -923,13 +921,13 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
|
||||
$this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
|
||||
$this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
|
||||
$this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] );
|
||||
$this->assertArrayHasKey( 'sidebar-3', $sidebars_widgets );
|
||||
$this->assertEmpty( $sidebars_widgets['sidebar-3'] );
|
||||
$this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertCount( 6, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
|
||||
$this->assertContains( 'meta-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'search-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'archives-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'categories-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'recent-posts-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
@@ -940,4 +938,161 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
// Sidebar_widgets option was not updated.
|
||||
$this->assertNotEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
|
||||
}
|
||||
|
||||
function test_retreive_widgets_with_single_widget() {
|
||||
global $sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
|
||||
// Register single-dimension widget.
|
||||
wp_register_sidebar_widget( 'single', 'Single', '__return_false', array(), array() );
|
||||
wp_register_widget_control( 'single', 'Single', '__return_false', array(), array() );
|
||||
|
||||
$this->register_sidebars( array( 'sidebar-1', 'sidebar-2', 'wp_inactive_widgets' ) );
|
||||
|
||||
$sidebars_widgets = array(
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
|
||||
// Theme changed.
|
||||
$result = retrieve_widgets( true );
|
||||
|
||||
$this->assertContains( 'single', $result['wp_inactive_widgets'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test _wp_remove_unregistered_widgets.
|
||||
*
|
||||
* @covers _wp_remove_unregistered_widgets()
|
||||
*/
|
||||
public function test__wp_remove_unregistered_widgets() {
|
||||
$widgets = array(
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array( 'text-1' ),
|
||||
'fantasy' => array( 'archives-2' ),
|
||||
'wp_inactive_widgets' => array(),
|
||||
'array_version' => 3,
|
||||
);
|
||||
|
||||
$whitelist = array( 'tag_cloud-1', 'text-1' );
|
||||
|
||||
$filtered_widgets = _wp_remove_unregistered_widgets( $widgets, $whitelist );
|
||||
|
||||
$this->assertInternalType( 'array', $filtered_widgets );
|
||||
$this->assertArrayHasKey( 'fantasy', $filtered_widgets );
|
||||
$this->assertEmpty( $filtered_widgets['fantasy'] );
|
||||
$this->assertArrayHasKey( 'array_version', $filtered_widgets );
|
||||
$this->assertEquals( 3, $filtered_widgets['array_version'] );
|
||||
$this->assertInternalType( 'integer', $filtered_widgets['array_version'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* wp_map_sidebars_widgets Tests.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Two themes with one sidebar each should just map, switching to a theme not previously-active.
|
||||
*
|
||||
* @covers wp_map_sidebars_widgets()
|
||||
*/
|
||||
public function test_one_sidebar_each() {
|
||||
$this->register_sidebars( array( 'primary' ) );
|
||||
$prev_theme_sidebars = array(
|
||||
'unique-slug' => 1,
|
||||
);
|
||||
|
||||
$new_next_theme_sidebars = wp_map_sidebars_widgets( $prev_theme_sidebars );
|
||||
|
||||
$expected_sidebars = array(
|
||||
'primary' => 1,
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
$this->assertEquals( $expected_sidebars, $new_next_theme_sidebars );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sidebars with the same name should map, switching to a theme not previously-active.
|
||||
*
|
||||
* @covers wp_map_sidebars_widgets()
|
||||
*/
|
||||
public function test_sidebars_with_same_slug() {
|
||||
$this->register_sidebars( array( 'primary', 'secondary' ) );
|
||||
$prev_theme_sidebars = array(
|
||||
'primary' => 1,
|
||||
'secondary' => 2,
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
|
||||
$new_next_theme_sidebars = wp_map_sidebars_widgets( $prev_theme_sidebars );
|
||||
|
||||
$this->assertEquals( $prev_theme_sidebars, $new_next_theme_sidebars );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make educated guesses on theme sidebars.
|
||||
*
|
||||
* @covers wp_map_sidebars_widgets()
|
||||
*/
|
||||
public function test_sidebar_guessing() {
|
||||
$this->register_sidebars( array( 'primary', 'secondary' ) );
|
||||
|
||||
$prev_theme_sidebars = array(
|
||||
'header' => array(),
|
||||
'footer' => array(),
|
||||
);
|
||||
|
||||
$new_next_theme_sidebars = wp_map_sidebars_widgets( $prev_theme_sidebars );
|
||||
|
||||
$expected_sidebars = array(
|
||||
'primary' => array(),
|
||||
'secondary' => array(),
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
$this->assertEquals( $expected_sidebars, $new_next_theme_sidebars );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure two sidebars that fall in the same group don't get the same menu assigned.
|
||||
*
|
||||
* @covers wp_map_sidebars_widgets()
|
||||
*/
|
||||
public function test_sidebar_guessing_one_menu_per_group() {
|
||||
$this->register_sidebars( array( 'primary' ) );
|
||||
$prev_theme_sidebars = array(
|
||||
'top-menu' => array(),
|
||||
'secondary' => array(),
|
||||
);
|
||||
|
||||
$new_next_theme_sidebars = wp_map_sidebars_widgets( $prev_theme_sidebars );
|
||||
|
||||
$expected_sidebars = array(
|
||||
'main' => array(),
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
$this->assertEqualSets( $expected_sidebars, $new_next_theme_sidebars );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure two sidebars that fall in the same group get menus assigned from the same group.
|
||||
*
|
||||
* @covers wp_map_sidebars_widgets()
|
||||
*/
|
||||
public function test_sidebar_guessing_one_menu_per_sidebar() {
|
||||
$this->register_sidebars( array( 'primary', 'main' ) );
|
||||
|
||||
$prev_theme_sidebars = array(
|
||||
'navigation-menu' => array(),
|
||||
'top-menu' => array(),
|
||||
);
|
||||
|
||||
$new_next_theme_sidebars = wp_map_sidebars_widgets( $prev_theme_sidebars );
|
||||
|
||||
$expected_sidebars = array(
|
||||
'main' => array(),
|
||||
'primary' => array(),
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
$this->assertEquals( $expected_sidebars, $new_next_theme_sidebars );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user