| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Meta; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
use FluentSupport\Framework\Support\Arr; |
| 8 |
|
| 9 |
|
| 10 |
class EmailVerificationHandler |
| 11 |
{ |
| 12 |
public static function sendSignupEmailVerificationHtml($formData) |
| 13 |
{ |
| 14 |
try { |
| 15 |
$verifcationCode = str_pad(random_int(100123, 900987), 6, 0, STR_PAD_LEFT); |
| 16 |
} catch (\Exception $e) { |
| 17 |
$verifcationCode = str_pad(wp_rand(100123, 900987), 6, 0, STR_PAD_LEFT); |
| 18 |
} |
| 19 |
|
| 20 |
$string = $formData['email'] . '-' . wp_generate_uuid4() . wp_rand(1, 99999999); |
| 21 |
$hash = wp_hash_password($string); |
| 22 |
$hash = sanitize_title($hash, '', 'display'); |
| 23 |
$hash .= $formData['email'] . '-' . time(); |
| 24 |
|
| 25 |
$data = array( |
| 26 |
'login_hash' => $hash, |
| 27 |
'status' => 'issued', |
| 28 |
'ip_address' => Helper::getIp(), |
| 29 |
'use_type' => 'signup_verification', |
| 30 |
'used_count' => 0, |
| 31 |
'two_fa_code_hash' => wp_hash_password($verifcationCode), |
| 32 |
'valid_till' => date('Y-m-d H:i:s', current_time('timestamp') + 10 * 60), |
| 33 |
'created_at' => current_time('mysql'), |
| 34 |
'updated_at' => current_time('mysql') |
| 35 |
); |
| 36 |
|
| 37 |
Meta::insert([ |
| 38 |
'object_type' => 'fs_login_hashes', |
| 39 |
'key' => $hash, |
| 40 |
'value' => maybe_serialize($data) |
| 41 |
]); |
| 42 |
|
| 43 |
// translators: %s is the site name |
| 44 |
$mailSubject = apply_filters("fluent_support/signup_verification_mail_subject", sprintf(__('Your registration verification code for %s', 'fluent-support'), get_bloginfo('name'))); |
| 45 |
|
| 46 |
$pStart = '<p style="font-family: Arial, sans-serif; font-size: 16px; font-weight: normal; margin: 0; margin-bottom: 16px;">'; |
| 47 |
|
| 48 |
// translators: %s is the user's first name |
| 49 |
$message = $pStart . sprintf(__('Hello %s,', 'fluent-support'), Arr::get($formData, 'first_name')) . '</p>' . |
| 50 |
$pStart . __('Thank you for registering with us! To complete the setup of your account, please enter the verification code below on the registration page.', 'fluent-support') . '</p>' . |
| 51 |
// translators: %s is the verification code |
| 52 |
$pStart . '<b>' . sprintf(__('Verification Code: %s', 'fluent-support'), $verifcationCode) . '</b></p>' . |
| 53 |
'<br />' . |
| 54 |
$pStart . __('This code is valid for 10 minutes and is meant to ensure the security of your account. If you did not initiate this request, please ignore this email.', 'fluent-support') . '</p>'; |
| 55 |
|
| 56 |
$message = apply_filters('fluent_support/signup_verification_email_body', $message, $verifcationCode, $formData); |
| 57 |
|
| 58 |
$data = [ |
| 59 |
'body' => $message, |
| 60 |
'pre_header' => __('Activate your account', 'fluent-support'), |
| 61 |
'show_footer' => false |
| 62 |
]; |
| 63 |
|
| 64 |
$message = Helper::loadView('notification', $data); |
| 65 |
$headers = array('Content-Type: text/html; charset=UTF-8'); |
| 66 |
|
| 67 |
\wp_mail($formData['email'], $mailSubject, $message, $headers); |
| 68 |
|
| 69 |
ob_start(); |
| 70 |
?> |
| 71 |
<div class="fs_signup_verification"> |
| 72 |
<div class="fs_field_group fs_field_verification"> |
| 73 |
<?php // translators: %s is the email address ?> |
| 74 |
<p><?php echo esc_html(sprintf(__('A verification code has been sent to %s. Please provide the code below:', 'fluent-support'), $formData['email'])); ?></p> |
| 75 |
<input type="hidden" name="_email_verification_hash" value="<?php echo esc_attr($hash); ?>"/> |
| 76 |
<div class="fs_field_label is-required"> |
| 77 |
<label for="fs_field_verification"><?php esc_html_e('Verification Code', 'fluent-support'); ?></label> |
| 78 |
</div> |
| 79 |
<div class="fs_input_wrap"> |
| 80 |
<input type="text" id="fs_field_verification" placeholder="" name="_email_verification_token" required> |
| 81 |
</div> |
| 82 |
</div> |
| 83 |
<button |
| 84 |
style="display: inline-block; cursor: pointer; border: 0; background: #2271b1; color: #fff; text-decoration: none; text-shadow: none; min-height: 32px; padding: 8px 24px; font-size: 14px; border-radius: 3px; margin-top: 10px;" |
| 85 |
id="fs_verification_submit" type="submit"> |
| 86 |
<?php esc_html_e('Complete Signup', 'fluent-support'); ?> |
| 87 |
</button> |
| 88 |
</div> |
| 89 |
<?php |
| 90 |
return ob_get_clean(); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|