Make WordPress Core

Changeset 57698

Timestamp:
02/22/2024 09:50:10 PM (6 months ago)
Author:
hellofromTonya
Message:

General: Handle missing field in WP_List_Util::pluck().

Handles when the $field (i.e. key or property) is missing in one of the $input_list items by checking the key (array) or property (object) exists before using it for assignment.

Resolves the following bugs:

  • a PHP warning for undefined key|property.
  • null being set for that array or object within the returned list.

The changes resolve the issues in both WP_List_Util::pluck() (if invoked directly) and wp_list_pluck().

Also includes an additional test for the scenario where the wp_list_pluck() $index_key is not null, the $field is missing in one of the $input_list items.

Follow-up to [55423], [51663], [42527], [38928].

Props iamarunchaitanyajami, davidbinda, hellofromTonya, helgatheviking.
Fixes #59774.

Location:
trunk
Files:
2 edited

Legend:

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

    r56549 r57698  
    166166            foreach ( $this->output as $key => $value ) {
    167167                if ( is_object( $value ) ) {
    168                     $newlist[ $key ] = $value->$field;
     168                    if ( property_exists( $value, $field ) ) {
     169                        $newlist[ $key ] = $value->$field;
     170                    }
    169171                } elseif ( is_array( $value ) ) {
    170                     $newlist[ $key ] = $value[ $field ];
     172                    if ( array_key_exists( $field, $value ) ) {
     173                        $newlist[ $key ] = $value[ $field ];
     174                    }
    171175                } else {
    172176                    _doing_it_wrong(
     
    189193        foreach ( $this->output as $value ) {
    190194            if ( is_object( $value ) ) {
    191                 if ( isset( $value->$index_key ) ) {
    192                     $newlist[ $value->$index_key ] = $value->$field;
    193                 } else {
    194                     $newlist[] = $value->$field;
     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                    }
    195201                }
    196202            } elseif ( is_array( $value ) ) {
    197                 if ( isset( $value[ $index_key ] ) ) {
    198                     $newlist[ $value[ $index_key ] ] = $value[ $field ];
    199                 } else {
    200                     $newlist[] = $value[ $field ];
     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                    }
    201209                }
    202210            } else {
  • trunk/tests/phpunit/tests/functions/wpListPluck.php

    r56971 r57698  
    272272                    ),
    273273                    array(
    274                         'foo'   => 'foo',
    275274                        '123'   => '456',
    276275                        'lorem' => 'ipsum',
     
    286285                array(
    287286                    'bar',
    288                     'bar'   => 'foo',
    289287                    'value' => 'baz',
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
    290313                ),
    291314            ),
     
    343366                    ),
    344367                    (object) array(
    345                         'foo'   => 'foo',
    346368                        '123'   => '456',
    347369                        'lorem' => 'ipsum',
     
    357379                array(
    358380                    'bar',
    359                     'bar'   => 'foo',
    360381                    'value' => 'baz',
    361382                ),
    362383            ),
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
    363409        );
    364410    }
Note: See TracChangeset for help on using the changeset viewer.