-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix: #57469 fatal error when a sidebar widgets set to null #9603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
474077d
fff3b1e
e5d56b5
1c298fd
ce06687
484c479
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1350,6 +1350,13 @@ function retrieve_widgets( $theme_changed = false ) { | |||||||||||||||
| $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids ); | ||||||||||||||||
| $sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets ); | ||||||||||||||||
|
|
||||||||||||||||
| // Replace null values inside the array with an empty array. | ||||||||||||||||
| foreach ( $sidebars_widgets as $key => $value ) { | ||||||||||||||||
| if ( null === $value ) { | ||||||||||||||||
| $sidebars_widgets[ $key ] = array(); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
||||||||||||||||
| // Replace null values inside the array with an empty array. | |
| foreach ( $sidebars_widgets as $key => $value ) { | |
| if ( null === $value ) { | |
| $sidebars_widgets[ $key ] = array(); | |
| } | |
| } | |
| $sidebars_widgets = array_filter( $sidebars_widgets, 'is_array' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think checking for is_array will be a better solution and is a drop in replacement because it preserves sidebar keys and guarantees any downstream operations receive arrays, this is consistent with how older PHP version like 7.4 handle array_merge and array_search.
Using array_filter($sidebars_widgets, 'is_array') will remove entries that are not arrays, which can drop keys and change control flow in the downstream code that expects those keys to exist.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if ( null === $value ) { | |
| if ( ! is_array( $value ) ) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively:
| // Replace null values inside the array with an empty array. | |
| foreach ( $new_sidebars_widgets as $key => $value ) { | |
| if ( null === $value ) { | |
| $new_sidebars_widgets[ $key ] = array(); | |
| } | |
| } | |
| $new_sidebars_widgets = array_filter( $new_sidebars_widgets, 'is_array' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it was set to something other than an array or null, like a string, boolean, integer, or object? Wouldn't this be more appropriate:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moving forward with this approach