Make WordPress Core


Ignore:
Timestamp:
02/22/2024 09:50:10 PM (12 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.

File:
1 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 {
Note: See TracChangeset for help on using the changeset viewer.