Skip to content

Commit

Permalink
General: Handle missing field in WP_List_Util::pluck().
Browse files Browse the repository at this point in the history
Handles when the `$field` (i.e. key or property) is missing in one of the `$input_list` items by checking the key (array) or property (object) exists before using it for assignment.

Resolves the following bugs:

* a PHP warning for undefined key|property.
* `null` being set for that array or object within the returned list.

The changes resolve the issues in both `WP_List_Util::pluck()` (if invoked directly) and `wp_list_pluck()`.

Also includes an additional test for the scenario where the `wp_list_pluck()` `$index_key` is not `null`, the `$field` is missing in one of the `$input_list` items.

Follow-up to [55423], [51663], [42527], [38928].

Props iamarunchaitanyajami, davidbinda, hellofromTonya, helgatheviking.
Fixes #59774.

git-svn-id: https://develop.svn.wordpress.org/trunk@57698 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Feb 22, 2024
1 parent ec459dc commit 8448d06
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
28 changes: 18 additions & 10 deletions src/wp-includes/class-wp-list-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ public function pluck( $field, $index_key = null ) {
*/
foreach ( $this->output as $key => $value ) {
if ( is_object( $value ) ) {
$newlist[ $key ] = $value->$field;
if ( property_exists( $value, $field ) ) {
$newlist[ $key ] = $value->$field;
}
} elseif ( is_array( $value ) ) {
$newlist[ $key ] = $value[ $field ];
if ( array_key_exists( $field, $value ) ) {
$newlist[ $key ] = $value[ $field ];
}
} else {
_doing_it_wrong(
__METHOD__,
Expand All @@ -188,16 +192,20 @@ public function pluck( $field, $index_key = null ) {
*/
foreach ( $this->output as $value ) {
if ( is_object( $value ) ) {
if ( isset( $value->$index_key ) ) {
$newlist[ $value->$index_key ] = $value->$field;
} else {
$newlist[] = $value->$field;
if ( property_exists( $value, $field ) ) {
if ( property_exists( $value, $index_key ) ) {
$newlist[ $value->$index_key ] = $value->$field;
} else {
$newlist[] = $value->$field;
}
}
} elseif ( is_array( $value ) ) {
if ( isset( $value[ $index_key ] ) ) {
$newlist[ $value[ $index_key ] ] = $value[ $field ];
} else {
$newlist[] = $value[ $field ];
if ( array_key_exists( $field, $value ) ) {
if ( array_key_exists( $index_key, $value ) ) {
$newlist[ $value[ $index_key ] ] = $value[ $field ];
} else {
$newlist[] = $value[ $field ];
}
}
} else {
_doing_it_wrong(
Expand Down
54 changes: 50 additions & 4 deletions tests/phpunit/tests/functions/wpListPluck.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ public function data_wp_list_pluck() {
'abc' => 'xyz',
),
array(
'foo' => 'foo',
'123' => '456',
'lorem' => 'ipsum',
'key' => 'bar',
Expand All @@ -285,10 +284,34 @@ public function data_wp_list_pluck() {
'key',
array(
'bar',
'bar' => 'foo',
'value' => 'baz',
),
),
'arrays with key missing' => array(
array(
array(
'foo' => 'bar',
'bar' => 'baz',
'abc' => 'xyz',
),
array(
'foo' => 'foo',
'123' => '456',
'lorem' => 'ipsum',
'key' => 'bar',
),
array(
'foo' => 'baz',
'key' => 'value',
),
),
'key',
null,
array(
1 => 'bar',
2 => 'value',
),
),
'objects' => array(
array(
(object) array(
Expand Down Expand Up @@ -342,7 +365,6 @@ public function data_wp_list_pluck() {
'abc' => 'xyz',
),
(object) array(
'foo' => 'foo',
'123' => '456',
'lorem' => 'ipsum',
'key' => 'bar',
Expand All @@ -356,10 +378,34 @@ public function data_wp_list_pluck() {
'key',
array(
'bar',
'bar' => 'foo',
'value' => 'baz',
),
),
'objects with field missing' => array(
array(
(object) array(
'foo' => 'bar',
'bar' => 'baz',
'abc' => 'xyz',
),
(object) array(
'foo' => 'foo',
'123' => '456',
'lorem' => 'ipsum',
'key' => 'bar',
),
(object) array(
'foo' => 'baz',
'key' => 'value',
),
),
'key',
null,
array(
1 => 'bar',
2 => 'value',
),
),
);
}
}

0 comments on commit 8448d06

Please sign in to comment.