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

Fixes #16895.


git-svn-id: https://develop.svn.wordpress.org/trunk@42527 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2018-01-18 05:17:23 +00:00
parent ade2dd5dfe
commit 2cb36cab72
2 changed files with 57 additions and 3 deletions

View File

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

View File

@ -210,6 +210,56 @@ class Tests_Functions_ListFilter extends WP_UnitTestCase {
); );
} }
/**
* @ticket 16895
*/
function test_wp_list_pluck_containing_references() {
$ref_list = array(
& $this->object_list['foo'],
& $this->object_list['bar'],
);
$this->assertInstanceOf( 'stdClass', $ref_list[0] );
$this->assertInstanceOf( 'stdClass', $ref_list[1] );
$list = wp_list_pluck( $ref_list, 'name' );
$this->assertEquals(
array(
'foo',
'bar',
),
$list
);
$this->assertInstanceOf( 'stdClass', $ref_list[0] );
$this->assertInstanceOf( 'stdClass', $ref_list[1] );
}
/**
* @ticket 16895
*/
function test_wp_list_pluck_containing_references_keys() {
$ref_list = array(
& $this->object_list['foo'],
& $this->object_list['bar'],
);
$this->assertInstanceOf( 'stdClass', $ref_list[0] );
$this->assertInstanceOf( 'stdClass', $ref_list[1] );
$list = wp_list_pluck( $ref_list, 'name', 'id' );
$this->assertEquals(
array(
'f' => 'foo',
'b' => 'bar',
),
$list
);
$this->assertInstanceOf( 'stdClass', $ref_list[0] );
$this->assertInstanceOf( 'stdClass', $ref_list[1] );
}
function test_filter_object_list_nested_array_and() { function test_filter_object_list_nested_array_and() {
$list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' ); $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' );
$this->assertEquals( 1, count( $list ) ); $this->assertEquals( 1, count( $list ) );