From 525bc23651e879496186d7753e90e1eb6abd6a1c Mon Sep 17 00:00:00 2001 From: Mohamed Safouan Besrour Date: Tue, 6 Aug 2024 14:52:24 +0100 Subject: [PATCH 1/2] Fix array_merge error in widgets.php for null sidebar values This commit addresses a fatal TypeError occurring in widgets.php when merging sidebar widgets. The error was caused by null values being passed to array_merge(), which expects only arrays. Changes made: Replace the problematic array_merge() call with a more robust loop Add explicit checks for array type before merging Implement array_unique() to remove potential duplicates This fix prevents the "Uncaught TypeError: array_merge(): Argument #5 must be of type array, null given" error, improving stability when handling widget data, especially during theme switching or widget updates. --- src/wp-includes/widgets.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index 32d8a95e1b984..5e6bae68ff2bf 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1351,7 +1351,12 @@ function retrieve_widgets( $theme_changed = false ) { $sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets ); // Find hidden/lost multi-widget instances. - $shown_widgets = array_merge( ...array_values( $sidebars_widgets ) ); + $shown_widgets = array(); + foreach ($sidebars_widgets as $sidebar) { + if (is_array($sidebar)) { + $shown_widgets = array_merge($shown_widgets, $sidebar); + } + } $lost_widgets = array_diff( $registered_widgets_ids, $shown_widgets ); foreach ( $lost_widgets as $key => $widget_id ) { From f008afc8e71ad90bf33a0a770971088fa23260f7 Mon Sep 17 00:00:00 2001 From: Mohamed Safouan Besrour Date: Wed, 7 Aug 2024 13:55:07 +0100 Subject: [PATCH 2/2] add spaces --- src/wp-includes/widgets.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index 5e6bae68ff2bf..893f2c7883636 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1352,9 +1352,9 @@ function retrieve_widgets( $theme_changed = false ) { // Find hidden/lost multi-widget instances. $shown_widgets = array(); - foreach ($sidebars_widgets as $sidebar) { - if (is_array($sidebar)) { - $shown_widgets = array_merge($shown_widgets, $sidebar); + foreach ( $sidebars_widgets as $sidebar ) { + if ( is_array( $sidebar ) ) { + $shown_widgets = array_merge( $shown_widgets, $sidebar ); } } $lost_widgets = array_diff( $registered_widgets_ids, $shown_widgets );