/**
 * Triggers the mechanism to upload files.
 *
 * @copyright mikejolley
 * @since 1.0.0
 * @param array   $file_data Array of $_FILE data to upload.
 * @return array|WP_Error Array of objects containing either file information or an error
 */
function wpum_trigger_upload_file($field_key, $field)
{
    if (isset($_FILES[$field_key]) && !empty($_FILES[$field_key]) && !empty($_FILES[$field_key]['name'])) {
        if (!empty($field['allowed_mime_types'])) {
            $allowed_mime_types = $field['allowed_mime_types'];
        } else {
            $allowed_mime_types = get_allowed_mime_types();
        }
        $file_urls = array();
        $files_to_upload = wpum_prepare_uploaded_files($_FILES[$field_key]);
        foreach ($files_to_upload as $file_key => $file_to_upload) {
            if (!in_array($file_to_upload['type'], $allowed_mime_types)) {
                return new WP_Error('validation-error', sprintf(__('Allowed files types are: %s', 'wpum'), implode(', ', array_keys($field['allowed_mime_types']))));
            }
            if (defined('WPUM_MAX_AVATAR_SIZE') && $field_key == 'user_avatar' && $file_to_upload['size'] > WPUM_MAX_AVATAR_SIZE) {
                return new WP_Error('avatar-too-big', __('The uploaded file is too big.', 'wpum'));
            }
            $uploaded_file = wpum_upload_file($file_to_upload, array('file_key' => $file_key));
            if (is_wp_error($uploaded_file)) {
                return new WP_Error('validation-error', $uploaded_file->get_error_message());
            } else {
                $file_urls[] = array('url' => $uploaded_file->url, 'path' => $uploaded_file->path, 'size' => $uploaded_file->size);
            }
        }
        if (!empty($field['multiple'])) {
            return $file_urls;
        } else {
            return current($file_urls);
        }
        return $files_to_upload;
    }
}
/**
 * Triggers the mechanism to upload files.
 *
 * @copyright mikejolley
 * @since 1.0.0
 * @param array   $file_data Array of $_FILE data to upload.
 * @return array|WP_Error Array of objects containing either file information or an error
 */
function wpum_trigger_upload_file($field_key, $field)
{
    if (isset($_FILES[$field_key]) && !empty($_FILES[$field_key]) && !empty($_FILES[$field_key]['name'])) {
        if ($field_key == 'user_avatar') {
            add_filter('upload_mimes', 'wpum_adjust_mime_types');
        }
        $allowed_mime_types = get_allowed_mime_types();
        $file_urls = array();
        $files_to_upload = wpum_prepare_uploaded_files($_FILES[$field_key]);
        foreach ($files_to_upload as $file_key => $file_to_upload) {
            // Trigger validation rules for avatar only.
            if ($field_key == 'user_avatar') {
                if (!in_array($file_to_upload['type'], $allowed_mime_types)) {
                    return new WP_Error('validation-error', sprintf(__('Allowed files types are: %s', 'wpum'), implode(', ', array_keys($allowed_mime_types))));
                }
                if (defined('WPUM_MAX_AVATAR_SIZE') && $field_key == 'user_avatar' && $file_to_upload['size'] > WPUM_MAX_AVATAR_SIZE) {
                    return new WP_Error('avatar-too-big', __('The uploaded file is too big.', 'wpum'));
                }
            } else {
                // Trigger verification for other file fields.
                if (array_key_exists('allowed_extensions', $field) && is_array($field['allowed_extensions'])) {
                    $allowed_field_extensions = $field['allowed_extensions'];
                    $uploaded_file_extension = pathinfo($file_to_upload['name']);
                    $uploaded_file_extension = $uploaded_file_extension['extension'];
                    if (!in_array($uploaded_file_extension, $allowed_field_extensions)) {
                        return new WP_Error('validation-error', sprintf(esc_html__('Error: the "%s" field allows only %s files to be uploaded.', 'wpum'), $field['label'], implode(", ", $allowed_field_extensions)));
                    }
                }
            }
            $uploaded_file = wpum_upload_file($file_to_upload, array('file_key' => $file_key));
            if (is_wp_error($uploaded_file)) {
                return new WP_Error('validation-error', $uploaded_file->get_error_message());
            } else {
                $file_urls[] = array('url' => $uploaded_file->url, 'path' => $uploaded_file->path, 'size' => $uploaded_file->size);
            }
        }
        if (!empty($field['multiple'])) {
            return $file_urls;
        } else {
            return current($file_urls);
        }
        if ($field_key == 'user_avatar') {
            remove_filter('upload_mimes', 'wpum_adjust_mime_types');
        }
        return $files_to_upload;
    }
}