Widgets: Merge orphaned widgets on theme switch
Merges orphaned widgets into the inactive sidebar when switching themes. Props bpayton. See #39693. git-svn-id: https://develop.svn.wordpress.org/trunk@41594 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
db2f3a9b1a
commit
d33ab87371
@ -1147,7 +1147,6 @@ function retrieve_widgets( $theme_changed = false ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Discard invalid, theme-specific widgets from sidebars.
|
||||
$sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
|
||||
$sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets );
|
||||
@ -1214,7 +1213,7 @@ function wp_map_sidebars_widgets( $existing_sidebars_widgets ) {
|
||||
if ( in_array( $sidebar, $existing_sidebars, true ) ) {
|
||||
$new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ];
|
||||
unset( $existing_sidebars_widgets[ $sidebar ] );
|
||||
} else {
|
||||
} else if ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) {
|
||||
$new_sidebars_widgets[ $sidebar ] = array();
|
||||
}
|
||||
}
|
||||
|
@ -961,6 +961,41 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
$this->assertContains( 'single', $result['wp_inactive_widgets'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for orphaned widgets being moved into inactive widgets.
|
||||
*
|
||||
* @covers retrieve_widgets()
|
||||
*/
|
||||
function test_retrieve_widgets_move_orphaned_widgets_to_inactive() {
|
||||
global $sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
$this->register_sidebars( array( 'sidebar-1', 'sidebar-2', 'sidebar-3', 'wp_inactive_widgets' ) );
|
||||
|
||||
$sidebars_widgets = array(
|
||||
'sidebar-1' => array( 'tag_cloud-1' ),
|
||||
'sidebar-2' => array( 'text-1' ),
|
||||
'wp_inactive_widgets' => array( 'search-2', 'archives-2' ),
|
||||
'orphaned_widgets_1' => array( 'calendar-1' ),
|
||||
);
|
||||
|
||||
retrieve_widgets();
|
||||
|
||||
$this->assertInternalType( 'array', $sidebars_widgets );
|
||||
|
||||
foreach ( $sidebars_widgets as $widgets ) {
|
||||
$this->assertInternalType( 'array', $widgets );
|
||||
}
|
||||
|
||||
// 6 default widgets + 1 orphaned calendar widget = 7.
|
||||
$this->assertCount( 7, $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertContains( 'calendar-1', $sidebars_widgets['wp_inactive_widgets'] );
|
||||
$this->assertArrayNotHasKey( 'orphaned_widgets_1', $sidebars_widgets );
|
||||
|
||||
// Sidebar_widgets option was updated.
|
||||
$this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test _wp_remove_unregistered_widgets.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user