Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gianghl1983/6b66193d9618c715a2e8ef8b44bb6361 to your computer and use it in GitHub Desktop.
Save gianghl1983/6b66193d9618c715a2e8ef8b44bb6361 to your computer and use it in GitHub Desktop.
Email verification add-on plugin for user created with Fluent Form Plugin
<?php
/*
Plugin Name: Fluent Form User Email Verification
Description: Fluent Form User Email Verification
Version: 1.0
Author: WPManageNinja Support Team
Author URI: https://wpmanageninja.com
Plugin URI: https://wpmanageninja.com
License: GPLv2 or later
Text Domain: fluentform
*/
class FluentFormUserApproval
{
// Just a workaround Can be activated as plugin use inside function.php or any PHP snippet plugin
// Add your target role and message
// Do not use the auto login feature of FF Registration Module
private $pending_message = 'You account is not activated yet, check your email !';
private $check_role = 'editor';
public function boot()
{
// send email with a activation key
add_action ( 'fluentform_user_registration_completed', array($this, 'send_email_verification_key'), 11, 4 );
// check verification link
add_action ( 'init', array($this, 'verify_user_code') );
// check un verified user login
add_filter ( 'wp_authenticate_user', array($this, 'authenticate_user'), 10, 1 );
}
public function authenticate_user($userdata)
{
$current_user = new WP_User( $userdata->ID );
if ( !metadata_exists( 'user', $userdata->ID, 'is_activated_ff' ) ) {
//The user does not have this meta key so let him login
return $userdata;
}
$stat = (boolean)get_user_meta ( $userdata->ID, 'is_activated_ff', true );
if ( !in_array( $this->check_role, (array) $current_user->roles ) ) {
//The user does not have this role so let him login
return $userdata;
}
if ($stat == false) {
$message = __ ( '<strong>ERROR</strong>: ' . $this->pending_message . ' ', 'fluent-form' );
$message = new WP_Error( 'fluent-form', $message );
return $message;
}
return $userdata;
}
public function send_email_verification_key($userId, $feed, $formData, $form)
{
$user_info = get_userdata ( $userId );
// create md5 code to verify later
$code = md5 ( time () );
// make it into a code to send it to user via email
$string = array('id' => $userId, 'code' => $code);
// create the activation code and activation status
update_user_meta ( $userId, 'is_activated_ff', 0 );
update_user_meta ( $userId, 'activation_code', $code );
// create the url
$url = get_site_url () . '/?act=' . base64_encode ( serialize ( $string ) );
$link = 'Please click the following link to activate your account <br/> <a href="' . $url . '">ACTIVATION LINK</a>';
$to = $user_info->user_email;
$subject = 'Registration';
$html = '<table
border="0" cellpadding="0" cellspacing="0" width="100%" style="-webkit-text-size-adjust: none; background-color: #ffffff; border: 1px solid #eee; margin-bottom: 25px;">
<tbody>
<tr style="-webkit-text-size-adjust: none;">
<td align="center" valign="top" style="-webkit-text-size-adjust: none;background-color: #fff;padding:20px 20px;margin:0 auto;">
' . $link . '
</td>
</tr>
</tbody>
</table>
' . $emailBody;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail ( $to, $subject, $html, $headers );
wp_send_json_success ( [
'result' => [
'message' => 'Success ! Please check your email ' . $to . ' for activation link.'
]
] );
}
public function verify_user_code()
{
if (isset( $_GET['act'] )) {
$data = unserialize ( base64_decode ( $_GET['act'] ) );
$code = get_user_meta ( $data['id'], 'activation_code', true );
// verify whether the code given is the same as ours
if ($code == $data['code']) {
// update the user meta
update_user_meta ( $data['id'], 'is_activated_ff', 1 );
$message = "Account activated! Please login now!";
echo "<script type='text/javascript'>alert('$message');</script>";
} else {
$message = "Invalid URL";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
}
add_action ( 'plugins_loaded', function () {
(new FluentFormUserApproval())->boot ();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment