クラス
RegisterWithAmazon
API for creating a Welcart account with Amazon account info
ソース ソース
ファイル: src/API/RegisterWithAmazon.php
class RegisterWithAmazon extends ErrorStore { const REGISTER_WITH_AMAZON_ERROR = 'amzreg_4000'; /** * Populates custom error store * * @author Evan D Shaw <evandanielshaw@gmail.com> */ public function __construct() { $this->populate(); } /** * Invokes `$this->registerNewMember` if `$_POST['regnewmember']` is `1` * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return Closure&#Function#c069ca86 */ public function registerNewMemberIfRequired() { return function () { $regnewmember = isset($_POST['regnewmember']) ? (int)$_POST['regnewmember'] : 0; if ($regnewmember === 1 && usces_is_login() === false) { $error = $this->registerNewMember(); if (!empty($error)) { return $error; } } }; } /** * Generates a random password that conforms to Welcart password rules. * * Even if no password rules are specified, this method creates a password that * works with any combination of possible rules. The password is also equal * to the max length setting for Welcart passwords. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return string */ public static function generateWelcartCompliantRandomPassword() { global $usces; $maxlength = 12; if (isset($usces->options['system']['member_pass_rule_max'])) { $maxlength = (int)$usces->options['system']['member_pass_rule_max']; } if ($maxlength < 4) { $maxlength = 4; } $password = ''; // add at least one lowercase letter $lowercasechars = 'abcdefghijklmnopqrstuvwxyz'; $password .= substr($lowercasechars, wp_rand(0, strlen($lowercasechars) - 1), 1); // add at least one uppercase letter $uppercasechars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $password .= substr($uppercasechars, wp_rand(0, strlen($uppercasechars) - 1), 1); // add at least one number $numchars = '0123456789'; $password .= substr($numchars, wp_rand(0, strlen($numchars) - 1), 1); // add at least one special character $specialchars = '!@#$%^&*()'; $password .= substr($specialchars, wp_rand(0, strlen($specialchars) - 1), 1); // add extra characters until the max character length rule is met $allchars = $lowercasechars . $uppercasechars . $numchars . $specialchars; for ($i = strlen($password); $i < $maxlength; $i++) { $password .= substr($allchars, wp_rand(0, strlen($allchars) - 1), 1); } // scramble order of characters for added security $password = str_shuffle($password); return $password; } /** * Creates Welcart account with Amazon account data. Returns `GenericError` instance on * failure. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \usc_e_shop $usces * @return GenericError|void */ public function registerNewMember() { global $usces; $password = self::generateWelcartCompliantRandomPassword(); // Add a message that a temporary password is being used to the body of the email add_filter('usces_filter_send_regmembermail_message', function ($message) use ($password) { return __('You are currently using a temporary password. Please log in with your Amazon account from My Page and change your password.', 'wcexaap') . "\r\n\r\n" . __('temporary password : ', 'wcexaap') . $password . "\r\n\r\n" . $message; }); $delivery = isset($_SESSION['usces_entry']['delivery']) ? $_SESSION['usces_entry']['delivery'] : []; $customer = isset($_SESSION['usces_entry']['customer']) ? $_SESSION['usces_entry']['customer'] : []; $giftShipping = isset($_REQUEST['giftShipping']) ? (bool)$_REQUEST['giftShipping'] : false; $_POST['customer']['password1'] = $password; $_POST['customer']['password2'] = $password; $_POST['customer']['mailaddress1'] = $customer['mailaddress1']; $_POST['customer']['mailaddress2'] = $customer['mailaddress2']; $_POST['member_regmode'] = 'newmemberfromcart'; if ($giftShipping === false) { $_POST['customer'] = $delivery; $_POST['customer']['password1'] = $password; $_POST['customer']['password2'] = $password; $_POST['customer']['mailaddress1'] = $customer['mailaddress1']; $_POST['customer']['mailaddress2'] = $customer['mailaddress2']; $_POST['customer']['name1'] = $customer['name1']; $_POST['customer']['name2'] = $customer['name2']; } $registerPayload = $_POST; if ($giftShipping === false) { $_POST['customer']['address1'] = !empty($delivery['address1']) ? $delivery['address1'] : 'temp'; $_POST['customer']['address2'] = !empty($delivery['address2']) ? $delivery['address2'] : 'temp'; $_POST['customer']['tel'] = !empty($delivery['tel']) ? $delivery['tel'] : '00000000000'; } // validate $_POST $errormessage = $usces->member_check_fromcart(); if (!empty($errormessage)) { return $this->getErrorResponse( self::REGISTER_WITH_AMAZON_ERROR, [$errormessage], [$errormessage] ); } // revert $_POST back to Amazon address which may have missing values $_POST = $registerPayload; add_action('usces_action_member_registered', [$this, 'onMemRegistrationSuccess'], 10, 2); if ($this->createAccount() !== 'newcompletion') { return $this->getErrorResponse( self::REGISTER_WITH_AMAZON_ERROR, [$usces->error_message], [$usces->error_message] ); } } /** * Copied from `usceshop.class.php` `regist_member` method since it's impossible * to skip the validation check * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \usc_e_shop $usces * @global \wpdb $wpdb * @return string */ private function createAccount() { global $usces, $wpdb; $mode = $_POST['member_regmode']; $member_table = $wpdb->prefix . 'usces_member'; $id = $usces->check_member_email($_POST['customer']['mailaddress1']); if (!empty($id)) { $usces->error_message = __('This e-mail address can not be registered.', 'usces'); return $mode; } else { $point = $usces->options['start_point']; $salt = usces_get_salt('', 1); $pass = usces_get_hash(trim($_POST['customer']['password1']), $salt); $name1 = isset($_POST['customer']['name1']) ? trim($_POST['customer']['name1']) : ''; $name2 = isset($_POST['customer']['name2']) ? trim($_POST['customer']['name2']) : ''; $name3 = isset($_POST['customer']['name3']) ? trim($_POST['customer']['name3']) : ''; $name4 = isset($_POST['customer']['name4']) ? trim($_POST['customer']['name4']) : ''; $zipcode = isset($_POST['customer']['zipcode']) ? trim($_POST['customer']['zipcode']) : ''; $pref = isset($_POST['customer']['pref']) ? trim($_POST['customer']['pref']) : ''; $address1 = isset($_POST['customer']['address1']) ? trim($_POST['customer']['address1']) : ''; $address2 = isset($_POST['customer']['address2']) ? trim($_POST['customer']['address2']) : ''; $address3 = isset($_POST['customer']['address3']) ? trim($_POST['customer']['address3']) : ''; $tel = isset($_POST['customer']['tel']) ? trim($_POST['customer']['tel']) : ''; $fax = isset($_POST['customer']['fax']) ? trim($_POST['customer']['fax']) : ''; $country = isset($_POST['customer']['country']) ? trim($_POST['customer']['country']) : ''; $query = $wpdb->prepare( "INSERT INTO $member_table (mem_email, mem_pass, mem_status, mem_cookie, mem_point, mem_name1, mem_name2, mem_name3, mem_name4, mem_zip, mem_pref, mem_address1, mem_address2, mem_address3, mem_tel, mem_fax, mem_delivery_flag, mem_delivery, mem_registered, mem_nicename) VALUES (%s, %s, %d, %s, %d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s, %s, %s)", trim($_POST['customer']['mailaddress1']), $pass, 0, '', $point, $name1, $name2, $name3, $name4, $zipcode, $pref, $address1, $address2, $address3, $tel, $fax, '', '', get_date_from_gmt(gmdate('Y-m-d H:i:s', time())), '' ); $res = $wpdb->query($query); if ($res !== false) { $member_id = $wpdb->insert_id; $user = $_POST['customer']; $user['ID'] = $member_id; $usces->set_member_meta_value('customer_country', $country, $member_id); if (!empty($salt)) { $usces->set_member_meta_value('mem_salt', $salt, $member_id); } $res = $usces->reg_custom_member($member_id); /** * Mirrored Welcart filter * * @ignore */ if (apply_filters('usces_filter_veirfyemail_newmemberfromcart', false, $user)) { return 'cartverifying'; } /** * Mirrored Welcart action hook * * @ignore */ do_action('usces_action_member_registered', $_POST['customer'], $member_id); usces_send_regmembermail($user); $_POST['loginmail'] = trim($_POST['customer']['mailaddress1']); $_POST['loginpass'] = trim($_POST['customer']['password1']); if ($usces->member_login() == 'member') { $_SESSION['usces_entry']['member_regmode'] = 'editmemberfromcart'; return 'newcompletion'; } } else { $usces->error_message = __('Error:failure in update', 'usces'); return $mode; } } } /** * Set meta value used in subsequent logins with Amazon to bypass * password entry * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \usc_e_shop $usces * @param array $member * @param int $member_id * @return void */ public function onMemRegistrationSuccess($member, $member_id) { MemberMeta::saveMemberMetaData($member['mailaddress1'], (int)$member_id); } /** * Populates error store * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void * @throws InvalidArgumentException Thrown if duplicate errorcodes exist. */ public function populate() { $this->addError(new GenericError( self::REGISTER_WITH_AMAZON_ERROR, self::REGISTER_WITH_AMAZON_ERROR, 400, function ($message) { return $message; }, function ($message) { return $message; } )); } /** * In the case of NewMember, the points awarded will be calculated and reflected in the order. * * @author Seiyu Inoue <s.inoue@aivec.co.jp> * @return Closure&#Function#b26079b3 */ public function setOrderGetPointIfNewMember() { return function () { global $usces; // New member registration check $regnewmember = isset($_POST['regnewmember']) ? (int)$_POST['regnewmember'] : 0; if ($regnewmember === 0 && !usces_is_login()) { return; } // Acquisition of member information $usces->get_current_member(); $customer_id = $usces->current_member['id']; // Point calculation $get_point = $usces->get_order_point($customer_id); // Reflect points in the order $array = [ 'getpoint' => $get_point, ]; $usces->cart->set_order_entry($array); }; } }
- __construct — Populates custom error store
- createAccount — Copied from usceshop.class.php regist_member method since it's impossible to skip the validation check
- generateWelcartCompliantRandomPassword — Generates a random password that conforms to Welcart password rules.
- onMemRegistrationSuccess — Set meta value used in subsequent logins with Amazon to bypass password entry
- populate — Populates error store
- registerNewMember — Creates Welcart account with Amazon account data. Returns GenericError instance on failure.
- registerNewMemberIfRequired — Invokes $this->registerNewMember if $_POST['regnewmember'] is 1
- setOrderGetPointIfNewMember — In the case of NewMember, the points awarded will be calculated and reflected in the order.