Beispiel #1
0
/**
 * Get Purchase Form User
 *
 * @param array $valid_data
 *
 * @access  private
 * @since   1.0
 * @return  array
 */
function give_get_purchase_form_user($valid_data = array())
{
    // Initialize user
    $user = false;
    $is_ajax = defined('DOING_AJAX') && DOING_AJAX;
    if ($is_ajax) {
        // Do not create or login the user during the ajax submission (check for errors only)
        return true;
    } else {
        if (is_user_logged_in()) {
            // Set the valid user as the logged in collected data
            $user = $valid_data['logged_in_user'];
        } else {
            if ($valid_data['need_new_user'] === true || $valid_data['need_user_login'] === true) {
                // New user registration
                if ($valid_data['need_new_user'] === true) {
                    // Set user
                    $user = $valid_data['new_user_data'];
                    // Register and login new user
                    $user['user_id'] = give_register_and_login_new_user($user);
                    // User login
                } else {
                    if ($valid_data['need_user_login'] === true && !$is_ajax) {
                        /*
                         * The login form is now processed in the give_process_purchase_login() function.
                         * This is still here for backwards compatibility.
                         * This also allows the old login process to still work if a user removes the
                         * checkout login submit button.
                         *
                         * This also ensures that the donor is logged in correctly if they click "Purchase"
                         * instead of submitting the login form, meaning the donor is logged in during the purchase process.
                         */
                        // Set user
                        $user = $valid_data['login_user_data'];
                        // Login user
                        give_log_user_in($user['user_id'], $user['user_login'], $user['user_pass']);
                    }
                }
            }
        }
    }
    // Check guest checkout
    if (false === $user && false === give_logged_in_only($_POST['give-form-id'])) {
        // Set user
        $user = $valid_data['guest_user_data'];
    }
    // Verify we have an user
    if (false === $user || empty($user)) {
        // Return false
        return false;
    }
    // Get user first name
    if (!isset($user['user_first']) || strlen(trim($user['user_first'])) < 1) {
        $user['user_first'] = isset($_POST['give_first']) ? strip_tags(trim($_POST['give_first'])) : '';
    }
    // Get user last name
    if (!isset($user['user_last']) || strlen(trim($user['user_last'])) < 1) {
        $user['user_last'] = isset($_POST['give_last']) ? strip_tags(trim($_POST['give_last'])) : '';
    }
    // Get the user's billing address details
    $user['address'] = array();
    $user['address']['line1'] = !empty($_POST['card_address']) ? sanitize_text_field($_POST['card_address']) : false;
    $user['address']['line2'] = !empty($_POST['card_address_2']) ? sanitize_text_field($_POST['card_address_2']) : false;
    $user['address']['city'] = !empty($_POST['card_city']) ? sanitize_text_field($_POST['card_city']) : false;
    $user['address']['state'] = !empty($_POST['card_state']) ? sanitize_text_field($_POST['card_state']) : false;
    $user['address']['country'] = !empty($_POST['billing_country']) ? sanitize_text_field($_POST['billing_country']) : false;
    $user['address']['zip'] = !empty($_POST['card_zip']) ? sanitize_text_field($_POST['card_zip']) : false;
    if (empty($user['address']['country'])) {
        $user['address'] = false;
    }
    // Country will always be set if address fields are present
    if (!empty($user['user_id']) && $user['user_id'] > 0 && !empty($user['address'])) {
        // Store the address in the user's meta so the donation form can be pre-populated with it on return purchases
        update_user_meta($user['user_id'], '_give_user_address', $user['address']);
    }
    // Return valid user
    return $user;
}
/**
 * Process Register Form
 *
 * @since 2.0
 *
 * @param array $data Data sent from the register form
 *
 * @return void
 */
function give_process_register_form($data)
{
    if (is_user_logged_in()) {
        return;
    }
    if (empty($_POST['give_register_submit'])) {
        return;
    }
    do_action('give_pre_process_register_form');
    if (empty($data['give_user_login'])) {
        give_set_error('empty_username', __('Invalid username', 'give'));
    }
    if (username_exists($data['give_user_login'])) {
        give_set_error('username_unavailable', __('Username already taken', 'give'));
    }
    if (!validate_username($data['give_user_login'])) {
        give_set_error('username_invalid', __('Invalid username', 'give'));
    }
    if (email_exists($data['give_user_email'])) {
        give_set_error('email_unavailable', __('Email address already taken', 'give'));
    }
    if (empty($data['give_user_email']) || !is_email($data['give_user_email'])) {
        give_set_error('email_invalid', __('Invalid email', 'give'));
    }
    if (!empty($data['give_payment_email']) && $data['give_payment_email'] != $data['give_user_email'] && !is_email($data['give_payment_email'])) {
        give_set_error('payment_email_invalid', __('Invalid payment email', 'give'));
    }
    if (empty($_POST['give_user_pass'])) {
        give_set_error('empty_password', __('Please enter a password', 'give'));
    }
    if (!empty($_POST['give_user_pass']) && empty($_POST['give_user_pass2']) || $_POST['give_user_pass'] !== $_POST['give_user_pass2']) {
        give_set_error('password_mismatch', __('Passwords do not match', 'give'));
    }
    do_action('give_process_register_form');
    // Check for errors and redirect if none present
    $errors = give_get_errors();
    if (empty($errors)) {
        $redirect = apply_filters('give_register_redirect', $data['give_redirect']);
        give_register_and_login_new_user(array('user_login' => $data['give_user_login'], 'user_pass' => $data['give_user_pass'], 'user_email' => $data['give_user_email'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role')));
        wp_redirect($redirect);
        give_die();
    }
}