Make WordPress Core

Ticket #16895: 16895.2.diff

File 16895.2.diff, 4.4 KB (added by dd32, 7 years ago)

Tests: https://travis-ci.org/dd32/wordpress-develop/builds/330198311

  • src/wp-includes/class-wp-list-util.php

    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 b public function filter( $args = array(), $operator = 'AND' ) { 
    140140         *               `$list` will be preserved in the results.
    141141         */
    142142        public function pluck( $field, $index_key = null ) {
     143                $newlist = array();
     144
    143145                if ( ! $index_key ) {
    144146                        /*
    145147                         * This is simple. Could at some point wrap array_column()
    public function pluck( $field, $index_key = null ) { 
    147149                         */
    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 ];
     154                                        $newlist[ $key ] = $value[ $field ];
    153155                                }
    154156                        }
     157
     158                        $this->output = $newlist;
     159
    155160                        return $this->output;
    156161                }
    157162
    public function pluck( $field, $index_key = null ) { 
    159164                 * When index_key is not set for a particular item, push the value
    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 ) ) {
    165169                                if ( isset( $value->$index_key ) ) {
  • tests/phpunit/tests/functions/listFilter.php

    diff --git a/tests/phpunit/tests/functions/listFilter.php b/tests/phpunit/tests/functions/listFilter.php
    index 9be0a460a0..a736d62b3f 100644
    a b function test_filter_object_list_and_field() { 
    8383                                'field2' => true,
    8484                        ), 'AND', 'name'
    8585                );
    86                 $this->assertEquals( 2, count( $list ) );
    8786                $this->assertEquals(
    8887                        array(
    8988                                'foo' => 'foo',
    function test_filter_object_list_or_field() { 
    9998                                'field3' => true,
    10099                        ), 'OR', 'name'
    101100                );
    102                 $this->assertEquals( 2, count( $list ) );
    103101                $this->assertEquals(
    104102                        array(
    105103                                'foo' => 'foo',
    function test_filter_object_list_not_field() { 
    115113                                'field3' => true,
    116114                        ), 'NOT', 'name'
    117115                );
    118                 $this->assertEquals( 1, count( $list ) );
    119116                $this->assertEquals( array( 'baz' => 'baz' ), $list );
    120117        }
    121118
    function test_wp_list_pluck_mixed_index_key() { 
    213210                );
    214211        }
    215212
     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
    216263        function test_filter_object_list_nested_array_and() {
    217264                $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' );
    218265                $this->assertEquals( 1, count( $list ) );
    function test_filter_object_list_nested_array_or_singular() { 
    247294
    248295        function test_filter_object_list_nested_array_and_field() {
    249296                $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND', 'name' );
    250                 $this->assertEquals( 1, count( $list ) );
    251297                $this->assertEquals( array( 'baz' => 'baz' ), $list );
    252298        }
    253299
    254300        function test_filter_object_list_nested_array_not_field() {
    255301                $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'green' ) ), 'NOT', 'name' );
    256                 $this->assertEquals( 2, count( $list ) );
    257302                $this->assertEquals(
    258303                        array(
    259304                                'foo' => 'foo',
    function test_filter_object_list_nested_array_or_field() { 
    269314                                'field4' => array( 'blue' ),
    270315                        ), 'OR', 'name'
    271316                );
    272                 $this->assertEquals( 2, count( $list ) );
    273317                $this->assertEquals(
    274318                        array(
    275319                                'foo' => 'foo',