/**
 * Purchase Form Validate New User
 *
 * @access      private
 * @since       1.0.8.1
 * @return      array
 */
function edd_purchase_form_validate_new_user()
{
    $registering_new_user = false;
    // Start an empty array to collect valid user data
    $valid_user_data = array('user_id' => -1, 'user_first' => isset($_POST["edd_first"]) ? sanitize_text_field($_POST["edd_first"]) : '', 'user_last' => isset($_POST["edd_last"]) ? sanitize_text_field($_POST["edd_last"]) : '');
    // Check the new user's credentials against existing ones
    $user_login = isset($_POST["edd_user_login"]) ? trim($_POST["edd_user_login"]) : false;
    $user_email = isset($_POST['edd_email']) ? trim($_POST['edd_email']) : false;
    $user_pass = isset($_POST["edd_user_pass"]) ? trim($_POST["edd_user_pass"]) : false;
    $pass_confirm = isset($_POST["edd_user_pass_confirm"]) ? trim($_POST["edd_user_pass_confirm"]) : false;
    // Loop through required fields and show error messages
    foreach (edd_purchase_form_required_fields() as $field_name => $value) {
        if (in_array($value, edd_purchase_form_required_fields()) && empty($_POST[$field_name])) {
            edd_set_error($value['error_id'], $value['error_message']);
        }
    }
    // Check if we have an username to register
    if ($user_login && strlen($user_login) > 0) {
        $registering_new_user = true;
        // We have an user name, check if it already exists
        if (username_exists($user_login)) {
            // Username already registered
            edd_set_error('username_unavailable', __('Username already taken', 'easy-digital-downloads'));
            // Check if it's valid
        } else {
            if (!edd_validate_username($user_login)) {
                // Invalid username
                if (is_multisite()) {
                    edd_set_error('username_invalid', __('Invalid username. Only lowercase letters (a-z) and numbers are allowed', 'easy-digital-downloads'));
                } else {
                    edd_set_error('username_invalid', __('Invalid username', 'easy-digital-downloads'));
                }
            } else {
                // All the checks have run and it's good to go
                $valid_user_data['user_login'] = $user_login;
            }
        }
    } else {
        if (edd_no_guest_checkout()) {
            edd_set_error('registration_required', __('You must register or login to complete your purchase', 'easy-digital-downloads'));
        }
    }
    // Check if we have an email to verify
    if ($user_email && strlen($user_email) > 0) {
        // Validate email
        if (!is_email($user_email)) {
            edd_set_error('email_invalid', __('Invalid email', 'easy-digital-downloads'));
            // Check if email exists
        } else {
            if (email_exists($user_email) && $registering_new_user) {
                edd_set_error('email_used', __('Email already used', 'easy-digital-downloads'));
            } else {
                // All the checks have run and it's good to go
                $valid_user_data['user_email'] = $user_email;
            }
        }
    } else {
        // No email
        edd_set_error('email_empty', __('Enter an email', 'easy-digital-downloads'));
    }
    // Check password
    if ($user_pass && $pass_confirm) {
        // Verify confirmation matches
        if ($user_pass != $pass_confirm) {
            // Passwords do not match
            edd_set_error('password_mismatch', __('Passwords don\'t match', 'easy-digital-downloads'));
        } else {
            // All is good to go
            $valid_user_data['user_pass'] = $user_pass;
        }
    } else {
        // Password or confirmation missing
        if (!$user_pass && $registering_new_user) {
            // The password is invalid
            edd_set_error('password_empty', __('Enter a password', 'easy-digital-downloads'));
        } else {
            if (!$pass_confirm && $registering_new_user) {
                // Confirmation password is invalid
                edd_set_error('confirmation_empty', __('Enter the password confirmation', 'easy-digital-downloads'));
            }
        }
    }
    return $valid_user_data;
}
/**
 * Purchase Form Validate New User
 *
 * @access      private
 * @since       1.0.8.1
 * @return      array
*/
function edd_purchase_form_validate_new_user()
{
    $registering_new_user = false;
    // Start an empty array to collect valid user data
    $valid_user_data = array('user_id' => -1, 'user_first' => isset($_POST["edd_first"]) ? strip_tags(trim($_POST["edd_first"])) : '', 'user_last' => isset($_POST["edd_last"]) ? strip_tags(trim($_POST["edd_last"])) : '');
    // Check the new user's credentials against existing ones
    $user_login = isset($_POST["edd_user_login"]) ? trim($_POST["edd_user_login"]) : false;
    $user_email = isset($_POST['edd_email']) ? trim($_POST['edd_email']) : false;
    $user_pass = isset($_POST["edd_user_pass"]) ? trim($_POST["edd_user_pass"]) : false;
    $pass_confirm = isset($_POST["edd_user_pass_confirm"]) ? trim($_POST["edd_user_pass_confirm"]) : false;
    // Check if we have an username to register
    if ($user_login && strlen($user_login) > 0) {
        $registering_new_user = true;
        // We have an user name, check if it already exists
        if (username_exists($user_login)) {
            // username already registered
            edd_set_error('username_unavailable', __('Username already taken', 'edd'));
            // Check if it's valid
        } else {
            if (!edd_validate_username($user_login)) {
                // invalid username
                edd_set_error('username_invalid', __('Invalid username', 'edd'));
            } else {
                // All the checks have run and it's good to go
                $valid_user_data['user_login'] = $user_login;
            }
        }
    } else {
        if (edd_no_guest_checkout()) {
            edd_set_error('registration_required', __('You must register or login to complete your purchase', 'edd'));
        }
    }
    // Check if we have an email to verify
    if ($user_email && strlen($user_email) > 0) {
        // Validate email
        if (!is_email($user_email)) {
            edd_set_error('email_invalid', __('Invalid email', 'edd'));
            // Check if email exists
        } else {
            if (email_exists($user_email) && $registering_new_user) {
                edd_set_error('email_used', __('Email already used', 'edd'));
            } else {
                // All the checks have run and it's good to go
                $valid_user_data['user_email'] = $user_email;
            }
        }
    } else {
        // no email
        edd_set_error('email_empty', __('Enter an email', 'edd'));
    }
    // Check password
    if ($user_pass && $pass_confirm) {
        // Verify confirmation matches
        if ($user_pass != $pass_confirm) {
            // Passwords do not match
            edd_set_error('password_mismatch', __('Passwords don\'t match', 'edd'));
        } else {
            // All is good to go
            $valid_user_data['user_pass'] = $user_pass;
        }
    } else {
        // Password or confrimation missing
        if (!$user_pass && $registering_new_user) {
            // The password is invalid
            edd_set_error('password_empty', __('Enter a password', 'edd'));
        } else {
            if (!$pass_confirm && $registering_new_user) {
                // Confirmation password is invalid
                edd_set_error('confirmation_empty', __('Enter the password confirmation', 'edd'));
            }
        }
    }
    return $valid_user_data;
}