diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php index 656035529186e..a2b1f929690e6 100644 --- a/src/wp-includes/class-wp-list-util.php +++ b/src/wp-includes/class-wp-list-util.php @@ -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__, @@ -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( diff --git a/tests/phpunit/tests/functions/wpListPluck.php b/tests/phpunit/tests/functions/wpListPluck.php index aca29506a9abb..d7e3146ef35e6 100644 --- a/tests/phpunit/tests/functions/wpListPluck.php +++ b/tests/phpunit/tests/functions/wpListPluck.php @@ -271,7 +271,6 @@ public function data_wp_list_pluck() { 'abc' => 'xyz', ), array( - 'foo' => 'foo', '123' => '456', 'lorem' => 'ipsum', 'key' => 'bar', @@ -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( @@ -342,7 +365,6 @@ public function data_wp_list_pluck() { 'abc' => 'xyz', ), (object) array( - 'foo' => 'foo', '123' => '456', 'lorem' => 'ipsum', 'key' => 'bar', @@ -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', + ), + ), ); } }