Support callback for index

This commit is contained in:
Mike Jolley 2017-08-11 13:52:28 +01:00
parent c9ceb9c62c
commit 7620473fa5
1 changed files with 4 additions and 1 deletions

View File

@ -1650,7 +1650,10 @@ function wc_list_pluck( $list, $callback_or_field, $index_key = null ) {
*/ */
$newlist = array(); $newlist = array();
foreach ( $list as $value ) { foreach ( $list as $value ) {
if ( isset( $value->$index_key ) ) { // Get index. @since 3.2.0 this supports a callback.
if ( is_callable( array( $value, $index_key ) ) ) {
$newlist[ $value->{$index_key}() ] = $value->{$callback_or_field}();
} elseif ( isset( $value->$index_key ) ) {
$newlist[ $value->$index_key ] = $value->{$callback_or_field}(); $newlist[ $value->$index_key ] = $value->{$callback_or_field}();
} else { } else {
$newlist[] = $value->{$callback_or_field}(); $newlist[] = $value->{$callback_or_field}();