From 2cb36cab728faa4e10a06e7fb4ed02179f4b05ab Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Thu, 18 Jan 2018 05:17:23 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-list-util.php | 10 ++-- tests/phpunit/tests/functions/listFilter.php | 50 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php index 0fbefd08e4..da2a897987 100644 --- a/src/wp-includes/class-wp-list-util.php +++ b/src/wp-includes/class-wp-list-util.php @@ -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 ) ) { diff --git a/tests/phpunit/tests/functions/listFilter.php b/tests/phpunit/tests/functions/listFilter.php index 364b31d99d..a736d62b3f 100644 --- a/tests/phpunit/tests/functions/listFilter.php +++ b/tests/phpunit/tests/functions/listFilter.php @@ -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() { $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' ); $this->assertEquals( 1, count( $list ) );