General: Allow wp_list_pluck() to operate on arrays of references without overwriting the referenced items.

Fixes #16895.

Built from https://develop.svn.wordpress.org/trunk@42527


git-svn-id: http://core.svn.wordpress.org/trunk@42356 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dion Hulse 2018-01-18 05:18:31 +00:00
parent 26a5d758d5
commit 44c89cac5a
2 changed files with 8 additions and 4 deletions

View File

@ -140,6 +140,8 @@ class WP_List_Util {
* `$list` will be preserved in the results.
*/
public function pluck( $field, $index_key = null ) {
$newlist = array();
if ( ! $index_key ) {
/*
* This is simple. Could at some point wrap array_column()
@ -147,11 +149,14 @@ class WP_List_Util {
*/
foreach ( $this->output as $key => $value ) {
if ( is_object( $value ) ) {
$this->output[ $key ] = $value->$field;
$newlist[ $key ] = $value->$field;
} else {
$this->output[ $key ] = $value[ $field ];
$newlist[ $key ] = $value[ $field ];
}
}
$this->output = $newlist;
return $this->output;
}
@ -159,7 +164,6 @@ class WP_List_Util {
* When index_key is not set for a particular item, push the value
* to the end of the stack. This is how array_column() behaves.
*/
$newlist = array();
foreach ( $this->output as $value ) {
if ( is_object( $value ) ) {
if ( isset( $value->$index_key ) ) {

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-42526';
$wp_version = '5.0-alpha-42527';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.