Make WordPress Core

Changeset 57732

Timestamp:
02/27/2024 10:36:09 PM (5 months ago)
Author:
hellofromTonya
Message:

General: Revert r57698 for WP_List_Util::pluck().

r57698 caused a regression for arrays of objects which have magic methods and dynamic properties. A fix is identified.

However, a deeper dive discovered additional scenarios which will require a different fix.

Reverting gives more time for resolving these scenarios and more soak time to discover if there are others.

Props dd32, jamescollins, swissspidy.
See #59774.

Location:
trunk
Files:
2 edited

Legend:

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

    r57698 r57732  
    166166            foreach ( $this->output as $key => $value ) {
    167167                if ( is_object( $value ) ) {
    168                     if ( property_exists( $value, $field ) ) {
    169                         $newlist[ $key ] = $value->$field;
    170                     }
     168                    $newlist[ $key ] = $value->$field;
    171169                } elseif ( is_array( $value ) ) {
    172                     if ( array_key_exists( $field, $value ) ) {
    173                         $newlist[ $key ] = $value[ $field ];
    174                     }
     170                    $newlist[ $key ] = $value[ $field ];
    175171                } else {
    176172                    _doing_it_wrong(
     
    193189        foreach ( $this->output as $value ) {
    194190            if ( is_object( $value ) ) {
    195                 if ( property_exists( $value, $field ) ) {
    196                     if ( property_exists( $value, $index_key ) ) {
    197                         $newlist[ $value->$index_key ] = $value->$field;
    198                     } else {
    199                         $newlist[] = $value->$field;
    200                     }
     191                if ( isset( $value->$index_key ) ) {
     192                    $newlist[ $value->$index_key ] = $value->$field;
     193                } else {
     194                    $newlist[] = $value->$field;
    201195                }
    202196            } elseif ( is_array( $value ) ) {
    203                 if ( array_key_exists( $field, $value ) ) {
    204                     if ( array_key_exists( $index_key, $value ) ) {
    205                         $newlist[ $value[ $index_key ] ] = $value[ $field ];
    206                     } else {
    207                         $newlist[] = $value[ $field ];
    208                     }
     197                if ( isset( $value[ $index_key ] ) ) {
     198                    $newlist[ $value[ $index_key ] ] = $value[ $field ];
     199                } else {
     200                    $newlist[] = $value[ $field ];
    209201                }
    210202            } else {
  • trunk/tests/phpunit/tests/functions/wpListPluck.php

    r57698 r57732  
    272272                    ),
    273273                    array(
     274
    274275                        '123'   => '456',
    275276                        'lorem' => 'ipsum',
     
    285286                array(
    286287                    'bar',
     288
    287289                    'value' => 'baz',
    288                 ),
    289             ),
    290             'arrays with key missing'        => array(
    291                 array(
    292                     array(
    293                         'foo' => 'bar',
    294                         'bar' => 'baz',
    295                         'abc' => 'xyz',
    296                     ),
    297                     array(
    298                         'foo'   => 'foo',
    299                         '123'   => '456',
    300                         'lorem' => 'ipsum',
    301                         'key'   => 'bar',
    302                     ),
    303                     array(
    304                         'foo' => 'baz',
    305                         'key' => 'value',
    306                     ),
    307                 ),
    308                 'key',
    309                 null,
    310                 array(
    311                     1 => 'bar',
    312                     2 => 'value',
    313290                ),
    314291            ),
     
    366343                    ),
    367344                    (object) array(
     345
    368346                        '123'   => '456',
    369347                        'lorem' => 'ipsum',
     
    379357                array(
    380358                    'bar',
     359
    381360                    'value' => 'baz',
    382361                ),
    383362            ),
    384             'objects with field missing'     => array(
    385                 array(
    386                     (object) array(
    387                         'foo' => 'bar',
    388                         'bar' => 'baz',
    389                         'abc' => 'xyz',
    390                     ),
    391                     (object) array(
    392                         'foo'   => 'foo',
    393                         '123'   => '456',
    394                         'lorem' => 'ipsum',
    395                         'key'   => 'bar',
    396                     ),
    397                     (object) array(
    398                         'foo' => 'baz',
    399                         'key' => 'value',
    400                     ),
    401                 ),
    402                 'key',
    403                 null,
    404                 array(
    405                     1 => 'bar',
    406                     2 => 'value',
    407                 ),
    408             ),
    409363        );
    410364    }
Note: See TracChangeset for help on using the changeset viewer.