Opened 5 weeks ago
Last modified 5 weeks ago
#59774 new defect (bug)
Undefined array key when using wp_list_pluck function
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 6.5 | Priority: | normal |
Severity: | normal | Version: | 5.1 |
Component: | Script Loader | Keywords: | has-patch needs-unit-tests has-testing-info |
Focuses: | Cc: |
Description
Bug Description:
Issue: When using the wp_list_pluck
function to extract values from an array, a PHP warning is triggered if the specified key is not found within the array. The warning message is as follows:
PHP Warning: Undefined array key "required" in /var/www/html/wp-includes/class-wp-list-util.php on line 171
Steps to Reproduce:
- Use the
wp_list_pluck
function on an array. - Specify a key that may or may not exist within the array.
- Observe the PHP warning generated when the key is not found.
Expected Behavior:
The wp_list_pluck
function should gracefully handle cases where the specified key is not present in the array and not trigger a PHP warning.
Actual Behavior:
The function generates a PHP warning with an "Undefined array key" message, which could lead to unnecessary log clutter and confusion.
Environment:
- WordPress version: 6.3 and Higher
- PHP version: >= 8
- Operating system: UBUNTU
Proposed Solution:
To handle this situation gracefully without errors, you can check if the key exists in the array before attempting to access it. You can use the isset() function to determine if the key exists. Here's how you can modify the code:
Code highlighting:
if (is_object($value)) {
$newlist[$key] = isset($value->$field) ? $value->$field : array();
} elseif (is_array($value)) {
$newlist[$key] = isset($value[$field]) ? $value[$field] : array();
} else {
_doing_it_wrong(
__METHOD__,
__('Values for the input array must be either objects or arrays.'),
'6.2.0'
);
}
In the modified code, we use isset() to check if the key $field exists in either the object or the array. If the key exists, it assigns the value to $newlist[$key]. If the key doesn't exist, it assigns empty array().
This modification will prevent the code from triggering an error when attempting to access non-existent keys in an array or object. Instead, it will gracefully assign empty array().
Attachments (1)
Change History (4)
This ticket was mentioned in PR #5596 on WordPress/wordpress-develop by @iamarunchaitanyajami.
5 weeks ago
#1
#2
in reply to:
↑ description
@
5 weeks ago
Instead of using isset we will be using coalescing operator ?? to check if the key $field exists.
So the updated code will be
Code highlighting:
if (is_object($value)) { $newlist[$key] = $value->$field ?? array(); } elseif (is_array($value)) { $newlist[$key] = $value[ $field ] ?? array(); } else { _doing_it_wrong( __METHOD__, __('Values for the input array must be either objects or arrays.'), '6.2.0' ); }
Replying to iamarunchaitanyajami:
Bug Description:
Issue: When using the
wp_list_pluck
function to extract values from an array, a PHP warning is triggered if the specified key is not found within the array. The warning message is as follows:
PHP Warning: Undefined array key "required" in /var/www/html/wp-includes/class-wp-list-util.php on line 171
Steps to Reproduce:
- Use the
wp_list_pluck
function on an array.- Specify a key that may or may not exist within the array.
- Observe the PHP warning generated when the key is not found.
Expected Behavior:
Thewp_list_pluck
function should gracefully handle cases where the specified key is not present in the array and not trigger a PHP warning.
Actual Behavior:
The function generates a PHP warning with an "Undefined array key" message, which could lead to unnecessary log clutter and confusion.
Environment:
- WordPress version: 6.3 and Higher
- PHP version: >= 8
- Operating system: UBUNTU
Proposed Solution:
To handle this situation gracefully without errors, you can check if the key exists in the array before attempting to access it. You can use the isset() function to determine if the key exists. Here's how you can modify the code:
Code highlighting:
if (is_object($value)) { $newlist[$key] = isset($value->$field) ? $value->$field : array(); } elseif (is_array($value)) { $newlist[$key] = isset($value[$field]) ? $value[$field] : array(); } else { _doing_it_wrong( __METHOD__, __('Values for the input array must be either objects or arrays.'), '6.2.0' ); }
In the modified code, we use isset() to check if the key $field exists in either the object or the array. If the key exists, it assigns the value to $newlist[$key]. If the key doesn't exist, it assigns empty array().
This modification will prevent the code from triggering an error when attempting to access non-existent keys in an array or object. Instead, it will gracefully assign empty array().
#3
@
5 weeks ago
- Keywords needs-unit-tests has-testing-info added
- Milestone changed from Awaiting Review to 6.5
- Version changed from trunk to 5.1
Hello @iamarunchaitanyajami,
Welcome to WordPress Core's Trac :) Thank you for opening this ticket.
I'm doing some ticket triage to help this ticket be in a ready state for consideration:
Version
is the WP version that introduced the code or issue. The code in question was introduced in [42527] via #16895 during WP 5.1.- Moving into 6.5 for consideration.
- Keywords:
- Adding
needs-unit-tests
as tests can help to show the issue, confirm it is resolved, and help with future regressions. - Adding
has-testing-info
as you shared the steps to reproduce in the description - thank you :)
- Adding
In this PR, we use coalescing operator
??
to check if the key$field
exists in either the object or the array. If the key exists, it assigns the value to$newlist[$key]
. If the key doesn't exist, it assigns emptyarray()
.This modification will prevent the code from triggering an error when attempting to access non-existent keys in an array or object. Instead, it will gracefully assign empty
array()
.Trac ticket: https://core.trac.wordpress.org/ticket/59774