Skip to content

Commit

Permalink
Fix conflict with Cyr2Lat.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 17, 2023
1 parent 9304889 commit c91268e
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .tests/php/unit/HCaptchaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,89 @@ protected function set_method_accessibility( $object, $method_name, $accessible
return $method;
}

/**
* Plucks a certain field out of each object or array in an array.
* Taken from WP Core.
*
* @param array $input_list List of objects or arrays.
* @param int|string $field Field from the object to place instead of the entire object.
* @param int|string $index_key Optional. Field from the object to use as keys for the new array.
* Default null.
*
* @return array Array of found values. If `$index_key` is set, an array of found values with keys
* corresponding to `$index_key`. If `$index_key` is null, array keys from the original
* `$input_list` will be preserved in the results.
*/
protected function wp_list_pluck( $input_list, $field, $index_key = null ) {
if ( ! is_array( $input_list ) ) {
return [];
}

return $this->pluck( $input_list, $field, $index_key );
}

/**
* Plucks a certain field out of each element in the input array.
* Taken from WP Core.
*
* @param array $input_list List of objects or arrays.
* @param int|string $field Field to fetch from the object or array.
* @param int|string $index_key Optional. Field from the element to use as keys for the new array.
* Default null.
*
* @return array Array of found values. If `$index_key` is set, an array of found values with keys
* corresponding to `$index_key`. If `$index_key` is null, array keys from the original
* `$list` will be preserved in the results.
*/
private function pluck( $input_list, $field, $index_key = null ) {
$output = $input_list;
$new_list = [];

if ( ! $index_key ) {
/*
* This is simple. Could at some point wrap array_column()
* if we knew we had an array of arrays.
*/
foreach ( $output as $key => $value ) {
if ( is_object( $value ) ) {
$new_list[ $key ] = $value->$field;
} elseif ( is_array( $value ) ) {
$new_list[ $key ] = $value[ $field ];
} else {
// Error.
return [];
}
}

return $new_list;
}

/*
* When index_key is not set for a particular item, push the value
* to the end of the stack. This is how array_column() behaves.
*/
foreach ( $output as $value ) {
if ( is_object( $value ) ) {
if ( isset( $value->$index_key ) ) {
$new_list[ $value->$index_key ] = $value->$field;
} else {
$new_list[] = $value->$field;
}
} elseif ( is_array( $value ) ) {
if ( isset( $value[ $index_key ] ) ) {
$new_list[ $value[ $index_key ] ] = $value[ $field ];
} else {
$new_list[] = $value[ $field ];
}
} else {
// Error.
return [];
}
}

return $new_list;
}

/**
* Get test settings.
*
Expand Down

0 comments on commit c91268e

Please sign in to comment.