diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index e4fcc527b7..37e8a01c52 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -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(); } } diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index aa1ba6c493..a56caefb95 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -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. *