| 1 |
<?php |
| 2 |
defined('ABSPATH') || exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Module Name: Sign Up Form |
| 6 |
* Description: Displays sign up form |
| 7 |
*/ |
| 8 |
class TB_Signup_Form_Module extends Themify_Builder_Component_Module { |
| 9 |
|
| 10 |
public static function init():void { |
| 11 |
// Sign Up module action for processing sign up form |
| 12 |
add_action('wp_ajax_nopriv_tb_signup_process', array(__CLASS__, 'signup_process')); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
public static function get_module_name():string { |
| 17 |
return __('Sign Up Form', 'themify'); |
| 18 |
} |
| 19 |
|
| 20 |
public static function get_module_icon():string { |
| 21 |
return 'pencil-alt'; |
| 22 |
} |
| 23 |
|
| 24 |
public static function get_js_css():array { |
| 25 |
$_arr = array( |
| 26 |
'css' => 1 |
| 27 |
); |
| 28 |
if (!Themify_Builder_Model::is_front_builder_activate()) { |
| 29 |
$_arr['js'] = 1; |
| 30 |
} |
| 31 |
return $_arr; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Render plain content for static content. |
| 36 |
* |
| 37 |
* @param array $module |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public static function get_static_content(array $module):string { |
| 42 |
return sprintf( __( '<a href="%s" target="_blank">Click here to Register</a>', 'themify' ), esc_url( wp_registration_url() ) ); |
| 43 |
} |
| 44 |
|
| 45 |
public static function signup_enabled() { |
| 46 |
return get_option( 'users_can_register' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Actions to perform when sign up via Sign Up module is sent |
| 51 |
* |
| 52 |
*/ |
| 53 |
public static function signup_process() { |
| 54 |
check_ajax_referer( 'tf_nonce', 'nonce' ); |
| 55 |
if ( empty( $_POST['tb_post_id'] ) || ! self::signup_enabled() ) { |
| 56 |
die(-1); |
| 57 |
} |
| 58 |
$error = false; |
| 59 |
$module = self::get_element_settings( $_POST['tb_post_id'], $_POST['tb_element_id'] ); |
| 60 |
if ( empty( $module ) ) { |
| 61 |
$error = __( 'Error retrieving the module.', 'themify' ); |
| 62 |
} else if ( $module['captcha'] ) { |
| 63 |
|
| 64 |
$response = null; |
| 65 |
if ( $module['captcha'] === 'recaptcha' && ! empty( $_POST['g-recaptcha-response'] ) ) { |
| 66 |
$response = $_POST['g-recaptcha-response']; |
| 67 |
} else if ( $module['captcha'] === 'hcaptcha' && ! empty( $_POST['h-captcha-response'] ) ) { |
| 68 |
$response = $_POST['h-captcha-response']; |
| 69 |
} else if ( $module['captcha'] === 'turnstile' && ! empty( $_POST['cf-turnstile-response'] ) ) { |
| 70 |
$response = $_POST['cf-turnstile-response']; |
| 71 |
} |
| 72 |
|
| 73 |
if ( $response ) { |
| 74 |
$result = Themify_Builder_Model::validate_captcha( $module['captcha'], $response ); |
| 75 |
if ( is_wp_error( $result ) ) { |
| 76 |
$error = $result->get_error_message(); |
| 77 |
} |
| 78 |
} else { |
| 79 |
$error = __( 'Captcha response missing.', 'themify' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ( $error === false && self::flood_check() ) { |
| 84 |
$error = __( 'Please wait, and try again.', 'themify' ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( empty( $_POST['user_login'] ) ) { |
| 88 |
$error = __('Please enter a username', 'themify'); |
| 89 |
} elseif (!validate_username($_POST['user_login'])) { |
| 90 |
$error = __('Invalid username', 'themify'); |
| 91 |
} elseif ( username_exists($_POST['user_login'] ) ) { |
| 92 |
$error = __('Username already taken', 'themify'); |
| 93 |
} |
| 94 |
if ( empty( $_POST['user_email'] ) || ! is_email( $_POST['user_email'] ) ) { |
| 95 |
$error = __('Invalid email', 'themify'); |
| 96 |
} elseif ( email_exists( $_POST['user_email'] ) ) { |
| 97 |
$error = __('Email already registered', 'themify'); |
| 98 |
} |
| 99 |
if ( empty( $_POST['pwd'] ) ) { |
| 100 |
$error = __('Please enter a password', 'themify'); |
| 101 |
} |
| 102 |
|
| 103 |
if ( $error === false ) { |
| 104 |
global $wpdb; |
| 105 |
$wpdb->query( 'START TRANSACTION' ); |
| 106 |
$first_name = sanitize_text_field( $_POST['first_n'] ); |
| 107 |
$last_name = sanitize_text_field( $_POST['last_n'] ); |
| 108 |
$email = sanitize_email( $_POST['user_email'] ); |
| 109 |
try { |
| 110 |
$new_user_id = wp_insert_user(array( |
| 111 |
'user_login' => sanitize_text_field( $_POST['user_login'] ), |
| 112 |
'user_pass' => sanitize_text_field( $_POST['pwd'] ), |
| 113 |
'user_email' => $email, |
| 114 |
'first_name' => $first_name, |
| 115 |
'last_name' => $last_name, |
| 116 |
'description' => isset($_POST['bio']) ? sanitize_textarea_field( $_POST['bio'] ) : '', |
| 117 |
'user_registered' => date('Y-m-d H:i:s'), |
| 118 |
) |
| 119 |
); |
| 120 |
if ( is_wp_error( $new_user_id ) ) { |
| 121 |
throw new Exception( $new_user_id->get_error_message() ); |
| 122 |
} else { |
| 123 |
$wpdb->query( 'COMMIT' ); |
| 124 |
} |
| 125 |
} |
| 126 |
catch ( \Throwable $e ) { |
| 127 |
$wpdb->query( 'ROLLBACK' ); |
| 128 |
$error = __('Problem creating the new user. Please try again.', 'themify'); |
| 129 |
} |
| 130 |
|
| 131 |
if ( $error === false ) { |
| 132 |
|
| 133 |
// newsletter subscription |
| 134 |
if (isset($_POST['optin']) && $_POST['optin'] == '1') { |
| 135 |
if (!class_exists('Builder_Optin_Service',false)) { |
| 136 |
include_once( THEMIFY_BUILDER_INCLUDES_DIR . '/optin-services/base.php' ); |
| 137 |
} |
| 138 |
$optin_instance = Builder_Optin_Service::get_providers($_POST['optin-provider'],true); |
| 139 |
if ($optin_instance) { |
| 140 |
// collect the data for optin service |
| 141 |
$data = array( |
| 142 |
'user_email' => $email, |
| 143 |
'fname' => $first_name, |
| 144 |
'lname' => $last_name, |
| 145 |
); |
| 146 |
foreach ($_POST as $key => $value) { |
| 147 |
if (preg_match('/^optin-/', $key)) { |
| 148 |
$key = preg_replace('/^optin-/', '', $key); |
| 149 |
$data[ $key ] = sanitize_text_field( trim( $value ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
$optin_instance::subscribe($data); |
| 153 |
} |
| 154 |
unset($optin_instance); |
| 155 |
} |
| 156 |
|
| 157 |
$admin_notification = ! isset( $module['e_user'] ) || $module['e_user'] !== false; |
| 158 |
$user_notification = ! isset( $module['e_user'] ) || $module['e_user'] !== false; |
| 159 |
if ( $admin_notification || $user_notification ) { |
| 160 |
$notify = ''; |
| 161 |
if ( $admin_notification && $user_notification ) { |
| 162 |
$notify = 'both'; |
| 163 |
} elseif ( $user_notification ) { |
| 164 |
$notify = 'user'; |
| 165 |
} |
| 166 |
wp_new_user_notification($new_user_id, null, $notify); |
| 167 |
} |
| 168 |
|
| 169 |
$key = 'tb_signup_' . md5( ( $_SERVER['REMOTE_ADDR'] ?? '' ) . ( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ); |
| 170 |
set_transient( $key, 1, MINUTE_IN_SECONDS ); |
| 171 |
|
| 172 |
wp_send_json_success( [ |
| 173 |
'user_id' => $new_user_id |
| 174 |
] ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
wp_send_json_error( $error ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns true if too many register requests from same IP is detected |
| 183 |
* |
| 184 |
* @return bool |
| 185 |
*/ |
| 186 |
private static function flood_check():bool { |
| 187 |
$key = 'tb_signup_' . md5( ( $_SERVER['REMOTE_ADDR'] ?? '' ) . ( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ); |
| 188 |
if ( get_transient( $key ) ) { |
| 189 |
return true; |
| 190 |
} |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
public static function get_styling_image_fields() : array { |
| 195 |
return [ |
| 196 |
'b_i' => '' |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
public static function get_translatable_text_fields( $module ) : array { |
| 201 |
return [ 'mod_title', 'optin_label', 'l_name', 'l_firstname', 'l_lastname', 'l_username', 'l_email', 'l_password', 'l_submit' ]; |
| 202 |
} |
| 203 |
|
| 204 |
public static function get_translatable_textarea_fields( $module ) : array { |
| 205 |
return [ 'msg_success', 'gdpr_label', 'welcome', 'l_bio', 'desc' ]; |
| 206 |
} |
| 207 |
|
| 208 |
public static function get_translatable_link_fields( $module ) : array { |
| 209 |
return [ 'redirect_to' ]; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
TB_Signup_Form_Module::init(); |