WordPress Version: 5.7
/**
* Gets the error of combining operation.
*
* @since 5.6.0
*
* @param array $value The value to validate.
* @param string $param The parameter name, used in error messages.
* @param array $errors The errors array, to search for possible error.
* @return WP_Error The combining operation error.
*/
function rest_get_combining_operation_error($value, $param, $errors)
{
// If there is only one error, simply return it.
if (1 === count($errors)) {
return rest_format_combining_operation_error($param, $errors[0]);
}
// Filter out all errors related to type validation.
$filtered_errors = array();
foreach ($errors as $error) {
$error_code = $error['error_object']->get_error_code();
$error_data = $error['error_object']->get_error_data();
if ('rest_invalid_type' !== $error_code || isset($error_data['param']) && $param !== $error_data['param']) {
$filtered_errors[] = $error;
}
}
// If there is only one error left, simply return it.
if (1 === count($filtered_errors)) {
return rest_format_combining_operation_error($param, $filtered_errors[0]);
}
// If there are only errors related to object validation, try choosing the most appropriate one.
if (count($filtered_errors) > 1 && 'object' === $filtered_errors[0]['schema']['type']) {
$result = null;
$number = 0;
foreach ($filtered_errors as $error) {
if (isset($error['schema']['properties'])) {
$n = count(array_intersect_key($error['schema']['properties'], $value));
if ($n > $number) {
$result = $error;
$number = $n;
}
}
}
if (null !== $result) {
return rest_format_combining_operation_error($param, $result);
}
}
// If each schema has a title, include those titles in the error message.
$schema_titles = array();
foreach ($errors as $error) {
if (isset($error['schema']['title'])) {
$schema_titles[] = $error['schema']['title'];
}
}
if (count($schema_titles) === count($errors)) {
/* translators: 1: Parameter, 2: Schema titles. */
return new WP_Error('rest_no_matching_schema', wp_sprintf(__('%1$s is not a valid %2$l.'), $param, $schema_titles));
}
/* translators: %s: Parameter. */
return new WP_Error('rest_no_matching_schema', sprintf(__('%s does not match any of the expected formats.'), $param));
}