General: Add more error checking to `WP_List_Util::pluck()`.

Values for the input array in `WP_List_Util::pluck()` or `wp_list_pluck()` must be either objects or arrays.

This commit adds a check to ensure that the value retrieved in the loop is an array before treating it as such, and throws a `_doing_it_wrong()` notice if it is neither an object nor an array.

Follow-up to [14108], [15686], [18602], [28900], [38928].

Props afragen, costdev, audrasjb.
Fixes #56650.
Built from https://develop.svn.wordpress.org/trunk@55423


git-svn-id: http://core.svn.wordpress.org/trunk@54956 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-02-25 10:59:22 +00:00
parent 5f0e3de1f8
commit 66937c7dce
2 changed files with 15 additions and 3 deletions

View File

@ -166,8 +166,14 @@ class WP_List_Util {
foreach ( $this->output as $key => $value ) {
if ( is_object( $value ) ) {
$newlist[ $key ] = $value->$field;
} else {
} elseif ( is_array( $value ) ) {
$newlist[ $key ] = $value[ $field ];
} else {
_doing_it_wrong(
__METHOD__,
__( 'Values for the input array must be either objects or arrays.' ),
'6.2.0'
);
}
}
@ -187,12 +193,18 @@ class WP_List_Util {
} else {
$newlist[] = $value->$field;
}
} else {
} elseif ( is_array( $value ) ) {
if ( isset( $value[ $index_key ] ) ) {
$newlist[ $value[ $index_key ] ] = $value[ $field ];
} else {
$newlist[] = $value[ $field ];
}
} else {
_doing_it_wrong(
__METHOD__,
__( 'Values for the input array must be either objects or arrays.' ),
'6.2.0'
);
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-beta3-55422';
$wp_version = '6.2-beta3-55423';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.