Make WordPress Core

Changeset 42527


Ignore:
Timestamp:
01/18/2018 05:17:23 AM (9 years ago)
Author:
dd32
Message:

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

Fixes #16895.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-list-util.php

    r42343 r42527  
    141141         */
    142142        public function pluck( $field, $index_key = null ) {
     143                $newlist = array();
     144
    143145                if ( ! $index_key ) {
    144146                        /*
     
    148150                        foreach ( $this->output as $key => $value ) {
    149151                                if ( is_object( $value ) ) {
    150                                         $this->output[ $key ] = $value->$field;
     152                                        $newlist[ $key ] = $value->$field;
    151153                                } else {
    152                                         $this->output[ $key ] = $value[ $field ];
    153                                 }
    154                         }
     154                                        $newlist[ $key ] = $value[ $field ];
     155                                }
     156                        }
     157
     158                        $this->output = $newlist;
     159
    155160                        return $this->output;
    156161                }
     
    160165                 * to the end of the stack. This is how array_column() behaves.
    161166                 */
    162                 $newlist = array();
    163167                foreach ( $this->output as $value ) {
    164168                        if ( is_object( $value ) ) {
  • trunk/tests/phpunit/tests/functions/listFilter.php

    r42526 r42527  
    211211        }
    212212
     213        /**
     214         * @ticket 16895
     215         */
     216        function test_wp_list_pluck_containing_references() {
     217                $ref_list = array(
     218                        & $this->object_list['foo'],
     219                        & $this->object_list['bar'],
     220                );
     221
     222                $this->assertInstanceOf( 'stdClass', $ref_list[0] );
     223                $this->assertInstanceOf( 'stdClass', $ref_list[1] );
     224
     225                $list = wp_list_pluck( $ref_list, 'name' );
     226                $this->assertEquals(
     227                        array(
     228                                'foo',
     229                                'bar',
     230                        ),
     231                        $list
     232                );
     233
     234                $this->assertInstanceOf( 'stdClass', $ref_list[0] );
     235                $this->assertInstanceOf( 'stdClass', $ref_list[1] );
     236        }
     237
     238        /**
     239         * @ticket 16895
     240         */
     241        function test_wp_list_pluck_containing_references_keys() {
     242                $ref_list = array(
     243                        & $this->object_list['foo'],
     244                        & $this->object_list['bar'],
     245                );
     246
     247                $this->assertInstanceOf( 'stdClass', $ref_list[0] );
     248                $this->assertInstanceOf( 'stdClass', $ref_list[1] );
     249
     250                $list = wp_list_pluck( $ref_list, 'name', 'id' );
     251                $this->assertEquals(
     252                        array(
     253                                'f' => 'foo',
     254                                'b' => 'bar',
     255                        ),
     256                        $list
     257                );
     258
     259                $this->assertInstanceOf( 'stdClass', $ref_list[0] );
     260                $this->assertInstanceOf( 'stdClass', $ref_list[1] );
     261        }
     262
    213263        function test_filter_object_list_nested_array_and() {
    214264                $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' );
Note: See TracChangeset for help on using the changeset viewer.