-
Notifications
You must be signed in to change notification settings - Fork 3.2k
widgets: check if $wp_registered_widgets[ $widget ]['id'] exists #10710
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
widgets: check if $wp_registered_widgets[ $widget ]['id'] exists #10710
Conversation
https://core.trac.wordpress.org/ticket/57518 This includes a test for this condition.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
There's one error: |
|
Thank you - I should have ran a broader set of the tests before pushing this. Updated. |
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.
Pull request overview
This pull request fixes a PHP warning that occurs when is_active_widget() attempts to access the 'id' key of an unregistered widget in the $wp_registered_widgets array. The issue arises when a widget exists in sidebars_widgets but hasn't been registered yet (e.g., during early plugin/theme loading).
Changes:
- Added an
isset()check before accessing$wp_registered_widgets[$widget]['id']to prevent warnings - Included a comprehensive test case to verify the fix handles unregistered widgets correctly
- Added defensive checks in existing sidebar tests to handle empty
$wp_registered_sidebarsarrays
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/wp-includes/widgets.php |
Adds isset() check to prevent accessing array offset on null when checking widget ID |
tests/phpunit/tests/widgets.php |
Adds test case to verify is_active_widget() handles unregistered widgets without warnings |
tests/phpunit/tests/widgets/wpBlockThemeRegisterClassicSidebar.php |
Adds safety guards to register test sidebars when none exist |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * $wp_registered_widgets[ $widget ] exists, then tries to access | ||
| * $wp_registered_widgets[ $widget ]['id'] on the next line. | ||
| */ | ||
| $result = is_active_widget( false, 'search-2', 'search', true ); |
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.
Confirmed that this causes a warning when the fix is not applied:
There was 1 error:
1) Tests_Widgets::test_is_active_widget_with_unregistered_widget_and_id_base_match
Undefined array key "search-2"
/var/www/src/wp-includes/widgets.php:928
/var/www/tests/phpunit/tests/widgets.php:1452
✅
…s_active_widget()`. This avoids a possible PHP warning due to an undefined array key. Developed in #10710 Follow-up to [11090], [7080]. Props sageth, josephscott, Ipstenu, mindctrl, westonruter. See #8441, #6023. Fixes #57518. git-svn-id: https://develop.svn.wordpress.org/trunk@61466 602fd350-edb4-49c9-b593-d223f7449a82
…s_active_widget()`. This avoids a possible PHP warning due to an undefined array key. Developed in WordPress/wordpress-develop#10710 Follow-up to [11090], [7080]. Props sageth, josephscott, Ipstenu, mindctrl, westonruter. See #8441, #6023. Fixes #57518. Built from https://develop.svn.wordpress.org/trunk@61466 git-svn-id: http://core.svn.wordpress.org/trunk@60778 1a063a9b-81f0-0310-95a4-ce76da25c4cd
…s_active_widget()`. This avoids a possible PHP warning due to an undefined array key. Developed in WordPress/wordpress-develop#10710 Follow-up to [11090], [7080]. Props sageth, josephscott, Ipstenu, mindctrl, westonruter. See #8441, #6023. Fixes #57518. Built from https://develop.svn.wordpress.org/trunk@61466 git-svn-id: https://core.svn.wordpress.org/trunk@60778 1a063a9b-81f0-0310-95a4-ce76da25c4cd
…s_active_widget()`. This avoids a possible PHP warning due to an undefined array key. Developed in WordPress#10710 Follow-up to [11090], [7080]. Props sageth, josephscott, Ipstenu, mindctrl, westonruter. See WordPress#8441, WordPress#6023. Fixes #57518. git-svn-id: https://develop.svn.wordpress.org/trunk@61466 602fd350-edb4-49c9-b593-d223f7449a82
https://core.trac.wordpress.org/ticket/57518
This includes a test for this condition.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.