/** * Process Login Form * * @since 1.0 * @param array $data Data sent from the login form * @return void */ function edd_process_login_form($data) { if (wp_verify_nonce($data['edd_login_nonce'], 'edd-login-nonce')) { $user_data = get_user_by('login', $data['edd_user_login']); if (!$user_data) { $user_data = get_user_by('email', $data['edd_user_login']); } if ($user_data) { $user_ID = $user_data->ID; $user_email = $user_data->user_email; if (wp_check_password($data['edd_user_pass'], $user_data->user_pass, $user_data->ID)) { edd_log_user_in($user_data->ID, $data['edd_user_login'], $data['edd_user_pass']); } else { edd_set_error('password_incorrect', __('The password you entered is incorrect', 'edd')); } } else { edd_set_error('username_incorrect', __('The username you entered does not exist', 'edd')); } // Check for errors and redirect if none present $errors = edd_get_errors(); if (!$errors) { $redirect = apply_filters('edd_login_redirect', $data['edd_redirect'], $user_ID); wp_redirect($redirect); edd_die(); } } }
/** * Set customer details after authentication * * @access public * @since 2.4 * @return void */ public function signin_redirect() { if (!isset($_GET['edd-listener']) || $_GET['edd-listener'] !== 'amazon') { return; } if (!isset($_GET['state']) || $_GET['state'] !== 'signed-in') { return; } $profile = EDD()->session->get('amazon_profile'); $reference = $_GET['amazon_reference_id']; if (!is_user_logged_in()) { $user = get_user_by('email', $profile['email']); if ($user) { edd_log_user_in($user->ID, $user->user_login, ''); $customer = array('first_name' => $user->first_name, 'last_name' => $user->last_name, 'email' => $user->user_email); } else { $names = explode(' ', $profile['name']); $customer = array('first_name' => $names[0], 'last_name' => isset($names[1]) ? $names[1] : '', 'email' => $profile['email']); if ('none' !== edd_get_option('show_register_form')) { // Create a customer account if registration is not disabled $args = array('user_email' => $profile['email'], 'user_login' => $profile['email'], 'display_name' => $profile['name'], 'first_name' => $customer['first_name'], 'last_name' => $customer['last_name'], 'user_pass' => wp_generate_password(20)); $user_id = wp_insert_user($args); edd_log_user_in($user_id, $args['user_login'], $args['user_pass']); } } EDD()->session->set('customer', $customer); } wp_redirect(edd_get_checkout_uri(array('payment-mode' => 'amazon', 'state' => 'authorized', 'amazon_reference_id' => $reference))); exit; }
/** * Maybe create a user when payment is created * * @since 1.3 */ public function maybe_insert_user($payment_id, $payment_data) { // User account already associated if ($payment_data['user_info']['id'] > 0) { return; } // User account already exists if (get_user_by('email', $payment_data['user_info']['email'])) { return; } $user_name = sanitize_user($payment_data['user_info']['email']); // Username already exists if (username_exists($user_name)) { return; } // Okay we need to create a user and possibly log them in $user_args = apply_filters('edd_auto_register_insert_user_args', array('user_login' => $user_name, 'user_pass' => wp_generate_password(32), 'user_email' => $payment_data['user_info']['email'], 'first_name' => $payment_data['user_info']['first_name'], 'last_name' => $payment_data['user_info']['last_name'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role')), $payment_id, $payment_data); // Insert new user $user_id = wp_insert_user($user_args); // Validate inserted user if (is_wp_error($user_id)) { return; } $payment_meta = edd_get_payment_meta($payment_id); $payment_meta['user_info']['id'] = $user_id; edd_update_payment_meta($payment_id, '_edd_payment_user_id', $user_id); edd_update_payment_meta($payment_id, '_edd_payment_meta', $payment_meta); $customer = new EDD_Customer($payment_data['user_info']['email']); $customer->update(array('user_id' => $user_id)); // Allow themes and plugins to hook do_action('edd_auto_register_insert_user', $user_id, $user_args, $payment_id); if (function_exists('did_action') && did_action('edd_purchase')) { // Only log user in if processing checkout screen edd_log_user_in($user_id, $user_args['user_login'], $user_args['user_pass']); } }
/** * Get Purchase Form User * * @param array $valid_data * * @access private * @since 1.0.8.1 * @return array */ function edd_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'] = edd_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 edd_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 customer is logged in correctly if they click "Purchase" * instead of submitting the login form, meaning the customer is logged in during the purchase process. */ // Set user $user = $valid_data['login_user_data']; // Login user edd_log_user_in($user['user_id'], $user['user_login'], $user['user_pass']); } } } } } // Check guest checkout if (false === $user && false === edd_no_guest_checkout()) { // 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["edd_first"]) ? strip_tags(trim($_POST["edd_first"])) : ''; } // Get user last name if (!isset($user['user_last']) || strlen(trim($user['user_last'])) < 1) { $user['user_last'] = isset($_POST["edd_last"]) ? strip_tags(trim($_POST["edd_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 cart can be pre-populated with it on return purchases update_user_meta($user['user_id'], '_edd_user_address', $user['address']); } // Return valid user return $user; }
/** * Processes the supplied payment data to possibly register a user * * @since 1.3.3 * @param array $payment_data The Payment data * @param int $payment_id The payment ID * @return int|WP_Error The User ID created or an instance of WP_Error if the insert fails */ public function create_user($payment_data = array(), $payment_id = 0) { // User account already associated if ($payment_data['user_info']['id'] > 0) { return false; } // User account already exists if (get_user_by('email', $payment_data['user_info']['email'])) { return false; } $user_name = sanitize_user($payment_data['user_info']['email']); // Username already exists if (username_exists($user_name)) { return false; } // Okay we need to create a user and possibly log them in // Since this filter existed before, we must send in a $payment_id, which we default to false if none is supplied $user_args = apply_filters('edd_auto_register_insert_user_args', array('user_login' => $user_name, 'user_pass' => wp_generate_password(32), 'user_email' => $payment_data['user_info']['email'], 'first_name' => $payment_data['user_info']['first_name'], 'last_name' => $payment_data['user_info']['last_name'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role')), $payment_id, $payment_data); // Insert new user $user_id = wp_insert_user($user_args); if (!is_wp_error($user_id)) { // Allow themes and plugins to hook do_action('edd_auto_register_insert_user', $user_id, $user_args, $payment_id); $maybe_login_user = function_exists('did_action') && did_action('edd_purchase'); $maybe_login_user = apply_filters('edd_auto_register_login_user', $maybe_login_user); if (true === $maybe_login_user) { edd_log_user_in($user_id, $user_args['user_login'], $user_args['user_pass']); } $customer = new EDD_Customer($payment_data['user_info']['email']); $customer->update(array('user_id' => $user_id)); } return $user_id; }
/** * Get Purchase Form User * * @param array $valid_data * * @access private * @since 1.0.8.1 * @return array */ function edd_get_purchase_form_user($valid_data = array()) { // Initialize user $user = false; // Check if user is logged in 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'] = edd_register_and_login_new_user($user); // user login } else { if ($valid_data['need_user_login'] === true) { // set user $user = $valid_data['login_user_data']; // login user edd_log_user_in($user['user_id'], $user['user_login'], $user['user_pass']); } } } } // check guest checkout if (false === $user && false === edd_no_guest_checkout()) { // 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["edd_first"]) ? strip_tags(trim($_POST["edd_first"])) : ''; } // get user last name if (!isset($user['user_last']) || strlen(trim($user['user_last'])) < 1) { $user['user_last'] = isset($_POST["edd_last"]) ? strip_tags(trim($_POST["edd_last"])) : ''; } // return valid user return $user; }
function affwp_edd_auto_create_user($payment_id, $payment_data) { if (is_user_logged_in()) { return; } if ($payment_data['user_info']['id'] > 0) { return; } if (get_user_by('email', $payment_data['user_email'])) { return; } if (!is_array($payment_data['cart_details'])) { return; } foreach ($payment_data['cart_details'] as $item) { if (!isset($item['item_number']['options'])) { return; } if (2 !== (int) $item['item_number']['options']['price_id']) { return; } } $user_args = array('user_login' => $payment_data['user_email'], 'user_pass' => wp_generate_password(24), 'user_email' => $payment_data['user_email'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role')); // Insert new user $user_id = wp_insert_user($user_args); // Login new user edd_log_user_in($user_id, $payment_data['user_email'], $user_args['user_pass']); }
/** * Get Purchase Form User * * @param array $valid_data * * @access private * @since 1.0.8.1 * @return array */ function edd_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'] = edd_register_and_login_new_user($user); // User login } else { if ($valid_data['need_user_login'] === true && !$is_ajax) { // Set user $user = $valid_data['login_user_data']; // Login user edd_log_user_in($user['user_id'], $user['user_login'], $user['user_pass']); } } } } } // Check guest checkout if (false === $user && false === edd_no_guest_checkout()) { // 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["edd_first"]) ? strip_tags(trim($_POST["edd_first"])) : ''; } // Get user last name if (!isset($user['user_last']) || strlen(trim($user['user_last'])) < 1) { $user['user_last'] = isset($_POST["edd_last"]) ? strip_tags(trim($_POST["edd_last"])) : ''; } // Return valid user return $user; }