From 6cb2a0269553effe600f3a867eb910e9c70165d5 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Mon, 4 Dec 2017 20:11:11 +0000 Subject: [PATCH] Widgets: Don't try mapping empty sidebars. Fixes a bug where the mapping logic would try mapping empty sidebars, resulting in PHP warnings. Props ionvv, chetan200891 for initial patch. Fixes #42603. git-svn-id: https://develop.svn.wordpress.org/trunk@42362 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/widgets.php | 3 +++ tests/phpunit/tests/widgets.php | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index b78ba89f8b..8cfa1a6130 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1333,6 +1333,9 @@ function wp_map_sidebars_widgets( $existing_sidebars_widgets ) { if ( is_array( $old_sidebars_widgets ) ) { + // Remove empty sidebars, no need to map those. + $old_sidebars_widgets = array_filter( $old_sidebars_widgets ); + // Only check sidebars that are empty or have not been mapped to yet. foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) { if ( array_key_exists( $new_sidebar, $old_sidebars_widgets ) && ! empty( $new_widgets ) ) { diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index 027b6588d1..2883f3fa70 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -729,7 +729,7 @@ 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', 'sidebar-4', 'wp_inactive_widgets' ) ); // Test restoring sidebars widgets when previously activated. set_theme_mod( @@ -739,6 +739,7 @@ class Tests_Widgets extends WP_UnitTestCase { 'sidebar-1' => array( 'tag_cloud-1' ), 'sidebar-2' => array(), 'sidebar-3' => array( 'unregistered_widget-1', 'text-1', 'media_image-1' ), + 'sidebar-4' => null, // Should be converted to array. 'orphaned_widgets_1' => array( 'media_video-2' ), ), )