PluginProbe ʕ •ᴥ•ʔ
Essential Addons for Elementor – Popular Elementor Templates & Widgets / 5.8.7
Essential Addons for Elementor – Popular Elementor Templates & Widgets v5.8.7
6.6.5 6.6.4 6.6.3 5.7.3 5.7.4 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.15 5.8.16 5.8.18 5.8.2 5.8.3 5.8.4 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.10 5.9.11 5.9.12 5.9.13 5.9.14 5.9.15 5.9.16 5.9.17 5.9.18 5.9.19 5.9.2 5.9.20 5.9.21 5.9.22 5.9.23 5.9.24 5.9.25 5.9.26 5.9.27 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.14 6.0.15 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.1.0 6.1.1 6.1.10 6.1.11 trunk 6.1.12 1.0.0 6.1.13 1.0.1 6.1.14 1.1.0 6.1.15 2.0 6.1.17 2.1 6.1.18 2.10.0 6.1.19 2.10.1 6.1.2 2.10.2 6.1.20 2.10.3 6.1.3 2.10.4 6.1.4 2.10.5 6.1.5 2.2.0 6.1.6 2.2.1 6.1.7 2.2.2 6.1.8 2.2.3 6.1.9 2.2.4 6.2.0 2.2.5 6.2.1 2.3.0 6.2.2 2.3.1 6.2.3 2.4.0 6.2.4 2.4.1 6.3.0 2.4.2 6.3.1 2.4.3 6.3.2 2.5.0 6.3.3 2.6.0 6.4.0 2.7.0 6.5.0 2.7.1 6.5.1 2.7.10 6.5.10 2.7.11 6.5.11 2.7.2 6.5.12 2.7.3 6.5.13 2.7.4 6.5.2 2.7.5 6.5.3 2.7.6 6.5.4 2.7.7 6.5.5 2.7.8 6.5.6 2.7.9 6.5.7 2.8.0 6.5.8 2.8.1 6.5.9 2.8.2 6.6.0 2.8.3 6.6.1 2.8.4 6.6.2 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.4.1 3.1.5 3.2.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 5.0.0 5.0.1 5.0.10 5.0.11 5.0.12 5.0.13 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.4.6 5.5.5 5.6.5 5.7.2
essential-addons-for-elementor-lite / includes / Traits / Login_Registration.php
essential-addons-for-elementor-lite / includes / Traits Last commit date
Admin.php 3 years ago Ajax_Handler.php 2 years ago Controls.php 2 years ago Core.php 3 years ago Elements.php 2 years ago Enqueue.php 3 years ago Facebook_Feed.php 3 years ago Helper.php 2 years ago Library.php 3 years ago Login_Registration.php 2 years ago Shared.php 5 years ago Template_Query.php 4 years ago Twitter_Feed.php 2 years ago Woo_Product_Comparable.php 3 years ago index.php 3 years ago
Login_Registration.php
1698 lines
1 <?php
2
3 namespace Essential_Addons_Elementor\Traits;
4
5 use Elementor\Plugin;
6 use Essential_Addons_Elementor\Classes\Helper;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Trait Login_Registration is responsible for login or registering user using custom login | register widget.
14 * @package Essential_Addons_Elementor\Traits
15 */
16 trait Login_Registration {
17 /**
18 * @var bool
19 */
20 public static $send_custom_email = false;
21 public static $send_custom_email_admin = false;
22 public static $send_custom_email_lostpassword = false;
23 public static $eael_custom_profile_field_prefix = 'eael_custom_profile_field_';
24
25 /**
26 * It will contain all email related options like email subject, content, email content type etc.
27 * @var array $email_options {
28 * Used to build wp_mail().
29 * @type string $template_type The type of the email template; custom | default.
30 * @type string $subject The subject of the email.
31 * @type string $message The body of the email.
32 * @type string $content_type The type of the email body; plain | html
33 * }
34 */
35 public static $email_options = [];
36 public static $email_options_lostpassword = [];
37
38 public static $recaptcha_v3_default_action = 'eael_login_register_form';
39
40 public static function get_recaptcha_threshold() {
41 return apply_filters( 'eael_recaptcha_threshold', 0.5 );
42 }
43
44 public function login_or_register_user() {
45 do_action( 'eael/login-register/before-processing-login-register', $_POST );
46 // login or register form?
47 if ( isset( $_POST['eael-login-submit'] ) ) {
48 $this->log_user_in();
49 } else if ( isset( $_POST['eael-register-submit'] ) ) {
50 $this->register_user();
51 } else if ( isset( $_POST['eael-lostpassword-submit'] ) ) {
52 $this->send_password_reset();
53 } else if ( isset( $_POST['eael-resetpassword-submit'] ) ) {
54 $this->reset_password();
55 }
56 do_action( 'eael/login-register/after-processing-login-register', $_POST );
57
58 }
59
60 /**
61 * It logs the user in when the login form is submitted normally without AJAX.
62 */
63 public function log_user_in() {
64 $ajax = wp_doing_ajax();
65 // before even thinking about login, check security and exit early if something is not right.
66 $page_id = 0;
67 if ( ! empty( $_POST['page_id'] ) ) {
68 $page_id = intval( $_POST['page_id'], 10 );
69 } else {
70 $err_msg = __( 'Page ID is missing', 'essential-addons-for-elementor-lite' );
71 }
72
73 $widget_id = 0;
74 if ( ! empty( $_POST['widget_id'] ) ) {
75 $widget_id = sanitize_text_field( $_POST['widget_id'] );
76 } else {
77 $err_msg = __( 'Widget ID is missing', 'essential-addons-for-elementor-lite' );
78 }
79
80 if (!empty( $err_msg )){
81 if ( $ajax ) {
82 wp_send_json_error( $err_msg );
83 }
84 update_option( 'eael_login_error_' . $widget_id, $err_msg, false );
85
86 if (isset($_SERVER['HTTP_REFERER'])) {
87 wp_safe_redirect($_SERVER['HTTP_REFERER']);
88 exit();
89 }
90 }
91
92
93 if ( empty( $_POST['eael-login-nonce'] ) ) {
94 $err_msg = __( 'Insecure form submitted without security token', 'essential-addons-for-elementor-lite' );
95 if ( $ajax ) {
96 wp_send_json_error( $err_msg );
97 }
98 update_option( 'eael_login_error_' . $widget_id, $err_msg, false );
99
100 if (isset($_SERVER['HTTP_REFERER'])) {
101 wp_safe_redirect($_SERVER['HTTP_REFERER']);
102 exit();
103 }
104 }
105 if ( ! wp_verify_nonce( $_POST['eael-login-nonce'], 'essential-addons-elementor' ) ) {
106 $err_msg = __( 'Security token did not match', 'essential-addons-for-elementor-lite' );
107 if ( $ajax ) {
108 wp_send_json_error( $err_msg );
109 }
110 update_option( 'eael_login_error_' . $widget_id, $err_msg, false );
111
112 if (isset($_SERVER['HTTP_REFERER'])) {
113 wp_safe_redirect($_SERVER['HTTP_REFERER']);
114 exit();
115 }
116 }
117 $settings = $this->lr_get_widget_settings( $page_id, $widget_id);
118
119 if ( is_user_logged_in() ) {
120 $err_msg = isset( $settings['err_loggedin'] ) ? Helper::eael_wp_kses( $settings['err_loggedin'] ) : __( 'You are already logged in', 'essential-addons-for-elementor-lite' );
121 if ( $ajax ) {
122 wp_send_json_error( $err_msg );
123 }
124 update_option( 'eael_login_error_' . $widget_id, $err_msg, false );
125
126 if (isset($_SERVER['HTTP_REFERER'])) {
127 wp_safe_redirect($_SERVER['HTTP_REFERER']);
128 exit();
129 }
130 }
131
132 do_action( 'eael/login-register/before-login', $_POST, $settings, $this );
133
134 $widget_id = ! empty( $_POST['widget_id'] ) ? sanitize_text_field( $_POST['widget_id'] ) : '';
135 //v2 or v3
136 if ( isset( $_POST['g-recaptcha-enabled'] ) ) {
137 $ld_recaptcha_version = ( isset( $settings['login_recaptcha_version'] ) && 'v3' === $settings['login_recaptcha_version'] ) ? 'v3' : 'v2';
138
139 if( ! $this->lr_validate_recaptcha($ld_recaptcha_version) ) {
140 $err_msg = isset( $settings['err_recaptcha'] ) ? Helper::eael_wp_kses( $settings['err_recaptcha'] ) : __( 'You did not pass recaptcha challenge.', 'essential-addons-for-elementor-lite' );
141 if ( $ajax ) {
142 wp_send_json_error( $err_msg );
143 }
144 update_option( 'eael_login_error_' . $widget_id, $err_msg, false );
145
146 if (isset($_SERVER['HTTP_REFERER'])) {
147 wp_safe_redirect($_SERVER['HTTP_REFERER']);
148 exit();
149 } // fail early if recaptcha failed
150 }
151 }
152
153 $user_login = ! empty( $_POST['eael-user-login'] ) ? sanitize_text_field( $_POST['eael-user-login'] ) : '';
154 if ( is_email( $user_login ) ) {
155 $user_login = sanitize_email( $user_login );
156 }
157
158 $password = ! empty( $_POST['eael-user-password'] ) ? sanitize_text_field( $_POST['eael-user-password'] ) : '';
159 $rememberme = ! empty( $_POST['eael-rememberme'] ) ? sanitize_text_field( $_POST['eael-rememberme'] ) : '';
160
161 $credentials = [
162 'user_login' => $user_login,
163 'user_password' => $password,
164 'remember' => ( 'forever' === $rememberme ),
165 ];
166 $user_data = wp_signon( $credentials );
167
168 if ( is_wp_error( $user_data ) ) {
169 $err_msg = '';
170 if ( isset( $user_data->errors['invalid_email'][0] ) ) {
171 $err_msg = isset( $settings['err_email'] ) ? Helper::eael_wp_kses( $settings['err_email'] ) : __( 'Invalid Email. Please check your email or try again with your username.', 'essential-addons-for-elementor-lite' );
172 } elseif ( isset( $user_data->errors['invalid_username'][0] )) {
173 $err_msg = isset( $settings['err_username'] ) ? Helper::eael_wp_kses( $settings['err_username'] ) : __( 'Invalid Username. Please check your username or try again with your email.', 'essential-addons-for-elementor-lite' );
174
175 } elseif ( isset( $user_data->errors['incorrect_password'][0] ) || isset( $user_data->errors['empty_password'][0] ) ) {
176 $err_msg = isset( $settings['err_pass'] ) ? Helper::eael_wp_kses( $settings['err_pass'] ) : __( 'Invalid Password', 'essential-addons-for-elementor-lite' );
177 } else {
178 if( ! empty( $user_data->errors ) ){
179 foreach( $user_data->errors as $error ) {
180 $err_msg = is_array( $error ) && ! empty( $error[0] ) ? Helper::eael_wp_kses( $error[0] ) : __('Something went wrong!', 'essential-addons-for-elementor-lite');
181 break;
182 }
183 }
184 }
185
186 $err_msg = apply_filters('eael/login-register/login-validatiob-error-message', $err_msg, $user_data);
187 $err_msg = is_array( $err_msg ) && ! empty( $err_msg[0] ) ? Helper::eael_wp_kses( $err_msg[0] ) : Helper::eael_wp_kses( $err_msg );
188
189 if ( $ajax ) {
190 wp_send_json_error( $err_msg );
191 }
192 update_option( 'eael_login_error_' . $widget_id, $err_msg, false );
193 } else {
194 wp_set_current_user( $user_data->ID, $user_login );
195 $current_user_role = ! empty( $user_data->roles[0] ) ? $user_data->roles[0] : '';
196
197 $redirect_to = '';
198 if ( ! empty( $_POST['redirect_to'] ) ) {
199 $redirect_to = esc_url_raw( $_POST['redirect_to'] );
200 if( ! empty( $current_user_role ) ){
201 $redirect_to = ! empty( $_POST['redirect_to_' . esc_html( $current_user_role )] ) ? esc_url_raw( $_POST['redirect_to_' . esc_html( $current_user_role )] ) : $redirect_to;
202 }
203 }
204
205 do_action( 'wp_login', $user_data->user_login, $user_data );
206 do_action( 'eael/login-register/after-login', $user_data->user_login, $user_data );
207 if ( $ajax ) {
208
209 $data = [
210 'message' => isset( $settings['success_login'] ) ? Helper::eael_wp_kses( $settings['success_login'] ) : __( 'You are logged in successfully', 'essential-addons-for-elementor-lite' ),
211 ];
212 if ( ! empty( $redirect_to ) ) {
213 $data['redirect_to'] = esc_url_raw( $redirect_to );
214 }
215 wp_send_json_success( $data );
216 }
217
218 if ( ! empty( $redirect_to ) ) {
219 wp_safe_redirect( esc_url_raw( $redirect_to ) );
220 exit();
221 }
222 }
223 if (isset($_SERVER['HTTP_REFERER'])) {
224 wp_safe_redirect($_SERVER['HTTP_REFERER']);
225 exit();
226 }
227 }
228
229 /**
230 * It register the user in when the registration form is submitted normally without AJAX.
231 */
232 public function register_user() {
233 $ajax = wp_doing_ajax();
234
235 // validate & sanitize the request data
236 if ( empty( $_POST['eael-register-nonce'] ) ) {
237 if ( $ajax ) {
238 wp_send_json_error( __( 'Insecure form submitted without security token', 'essential-addons-for-elementor-lite' ) );
239 }
240
241 if (isset($_SERVER['HTTP_REFERER'])) {
242 wp_safe_redirect($_SERVER['HTTP_REFERER']);
243 exit();
244 }
245 }
246 if ( ! wp_verify_nonce( $_POST['eael-register-nonce'], 'essential-addons-elementor' ) ) {
247 if ( $ajax ) {
248 wp_send_json_error( __( 'Security token did not match', 'essential-addons-for-elementor-lite' ) );
249 }
250
251 if (isset($_SERVER['HTTP_REFERER'])) {
252 wp_safe_redirect($_SERVER['HTTP_REFERER']);
253 exit();
254 }
255 }
256 $page_id = $widget_id = 0;
257 if ( ! empty( $_POST['page_id'] ) ) {
258 $page_id = intval( $_POST['page_id'] );
259 } else {
260 $err_msg = __( 'Page ID is missing', 'essential-addons-for-elementor-lite' );
261 }
262 if ( ! empty( $_POST['widget_id'] ) ) {
263 $widget_id = sanitize_text_field( $_POST['widget_id'] );
264 } else {
265 $err_msg = __( 'Widget ID is missing', 'essential-addons-for-elementor-lite' );
266 }
267
268 if (!empty( $err_msg )){
269 if ( $ajax ) {
270 wp_send_json_error( $err_msg );
271 }
272 update_option( 'eael_register_errors_' . $widget_id, $err_msg, false );
273
274 if (isset($_SERVER['HTTP_REFERER'])) {
275 wp_safe_redirect($_SERVER['HTTP_REFERER']);
276 exit();
277 }
278 return false;
279 }
280
281
282
283 $settings = $this->lr_get_widget_settings( $page_id, $widget_id);
284
285 if ( is_user_logged_in() ) {
286 $err_msg = isset( $settings['err_loggedin'] ) ? Helper::eael_wp_kses( $settings['err_loggedin'] ) : __( 'You are already logged in.', 'essential-addons-for-elementor-lite' );
287 if ( $ajax ) {
288 wp_send_json_error( $err_msg );
289 }
290
291 if (isset($_SERVER['HTTP_REFERER'])) {
292 wp_safe_redirect($_SERVER['HTTP_REFERER']);
293 exit();
294 }
295 }
296
297 do_action( 'eael/login-register/before-register' );
298
299 // prepare the data
300 $errors = [];
301 $registration_allowed = get_option( 'users_can_register' );
302 $protocol = is_ssl() ? "https://" : "http://";
303 $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
304
305 // vail early if reg is closed.
306 if ( ! $registration_allowed ) {
307 $errors['registration'] = __( 'Registration is closed on this site', 'essential-addons-for-elementor-lite' );
308 if ( $ajax ) {
309 wp_send_json_error( $errors['registration'] );
310 }
311
312 //update_option( 'eael_register_errors_' . $widget_id, $errors, false );// if we redirect to other page, we dont need to save value
313 wp_safe_redirect(
314 add_query_arg(
315 array(
316 'registration' => 'disabled',
317 ),
318 esc_url_raw( $this->eael_wp_login_url() )
319 )
320 );
321 exit();
322 }
323 // prepare vars and flag errors
324 $settings_register_fields = isset($settings['register_fields']) ? $settings['register_fields'] : array();
325
326 $eael_custom_profile_fields_text = $this->get_eael_custom_profile_fields('text');
327 $eael_custom_profile_fields_image = $this->get_eael_custom_profile_fields('image');
328 $eael_custom_profile_fields = array_merge( $eael_custom_profile_fields_text, $eael_custom_profile_fields_image );
329
330 $eael_custom_profile_fields_image_keys = array_keys( $eael_custom_profile_fields_image );
331
332 if( count($settings_register_fields) ){
333 foreach($settings_register_fields as $register_field){
334 if( isset( $register_field['field_type'] ) && 'eael_phone_number' === $register_field['field_type'] ){
335 //Phone number field
336 if( !empty( $register_field['required'] ) && 'yes' === $register_field['required'] && empty( $_POST['eael_phone_number'] ) ) {
337 $errors['eael_phone_number'] = isset( $settings['err_phone_number_missing'] ) ? $settings['err_phone_number_missing'] : __( 'Phone number is required', 'essential-addons-for-elementor-lite' );
338 }
339 }
340
341 if( isset( $register_field['field_type'] ) && in_array( $register_field['field_type'], $eael_custom_profile_fields_image_keys ) ){
342
343 if ( ! empty( $_FILES[ $register_field['field_type'] ] ) && 4 !== $_FILES[ $register_field['field_type'] ]["error"] ) {
344 $custom_field_file_name = sanitize_text_field( $_FILES[ $register_field['field_type'] ]["name"] );
345 $custom_field_file_extension = end( ( explode( ".", $custom_field_file_name ) ) ); # extra () to prevent notice
346 $custom_field_file_size = floatval( $_FILES[ $register_field['field_type'] ]["size"] );
347
348 $unsupported_extensions = ['svg', 'php', 'js', 'aiff', 'psd', 'exr', 'wma', 'sql', 'm2v', 'swf', 'py', 'java', 'json', 'html', 'yaml', 'css', 'rb', 'cpp', 'c', 'cs', 'swift', 'kt', 'go', 'ts'];
349
350 if( ! empty ( $register_field['field_type_custom_image_extensions'] ) || in_array($custom_field_file_extension, $unsupported_extensions) ){
351 $field_type_custom_image_extensions_trimmed = trim( sanitize_text_field( $register_field['field_type_custom_image_extensions'] ), ' ,\n\r\0\x0B' );
352 $field_type_custom_image_extensions_array = array_unique( explode( ',', $field_type_custom_image_extensions_trimmed ) );
353
354 foreach( $field_type_custom_image_extensions_array as $item_key => $field_type_custom_image_extension ){
355 $field_type_custom_image_extensions_array[$item_key] = strtolower( trim( sanitize_text_field( $field_type_custom_image_extension ), ' ,\n\r\0\x0B' ) );
356 }
357
358 if( ! in_array( '.' . strtolower( $custom_field_file_extension ), $field_type_custom_image_extensions_array ) ) {
359 $errors[ $register_field['field_type'] ] = isset( $settings['field_type_custom_image_extensions_error'] ) ? $settings['field_type_custom_image_extensions_error'] : __( 'Sorry, you are not allowed to upload this file type.', 'essential-addons-for-elementor-lite' );
360 }
361 }
362 $register_field['field_type_custom_image_filesize'] = empty ( $register_field['field_type_custom_image_filesize'] ) ? 5 : $register_field['field_type_custom_image_filesize'];
363 $register_field['field_type_custom_image_filename_length'] = empty ( $register_field['field_type_custom_image_filename_length'] ) ? 128 : $register_field['field_type_custom_image_filename_length'];
364
365 if( ! empty ( $register_field['field_type_custom_image_filesize'] ) ){
366 $field_type_custom_image_filesize = floatval( $register_field['field_type_custom_image_filesize'] );
367 $field_type_custom_image_filesize = $field_type_custom_image_filesize > 512 ? 512 : $field_type_custom_image_filesize;
368 $field_type_custom_image_filesize_kb = $field_type_custom_image_filesize * 1000000;
369
370 if( $custom_field_file_size > $field_type_custom_image_filesize_kb ) {
371 $errors[ $register_field['field_type'] ] = isset( $settings['field_type_custom_image_filesize_error'] ) ? $settings['field_type_custom_image_filesize_error'] : __( 'File size exceeded. Maximum size is ' . floatval( $field_type_custom_image_filesize ) . 'MB' , 'essential-addons-for-elementor-lite' );
372 }
373 }
374
375 if( ! empty ( $register_field['field_type_custom_image_filename_length'] ) ){
376 $field_type_custom_image_filename_length = intval( $register_field['field_type_custom_image_filename_length'] );
377
378 if( strlen( $custom_field_file_name ) > $field_type_custom_image_filename_length ) {
379 $errors[ $register_field['field_type'] ] = isset( $settings['field_type_custom_image_filename_length_error'] ) ? $settings['field_type_custom_image_filename_length_error'] : __( 'Filename length exceeded. Maximum length is ' . intval( $field_type_custom_image_filename_length ), 'essential-addons-for-elementor-lite' );
380 }
381 }
382 }
383 }
384
385 //Validate HTML tags on input fields; Throw error if found (Although we are sanitizing before saving)
386 if( isset( $register_field['field_type'] ) && !empty( $_POST[$register_field['field_type']] ) ){
387 if( preg_match('/<[^<]+>/', $_POST[ $register_field['field_type'] ] ) ){
388 $errors[ sanitize_text_field( $register_field['field_type'] ) ] = __( sprintf('%s can not contain HTML tags', sanitize_text_field( $register_field['field_label'] ) ), 'essential-addons-for-elementor-lite' );
389 }
390 }
391 }
392 }
393
394 if ( isset( $_POST['eael_tnc_active'] ) && empty( $_POST['eael_accept_tnc'] ) ) {
395 $errors['terms_conditions'] = isset( $settings['err_tc'] ) ? Helper::eael_wp_kses( $settings['err_tc'] ) : __( 'You did not accept the Terms and Conditions. Please accept it and try again.', 'essential-addons-for-elementor-lite' );
396 }
397 //v2 or v3
398 if ( isset( $_POST['g-recaptcha-enabled'] ) ) {
399 $ld_recaptcha_version = ( isset( $settings['register_recaptcha_version'] ) && 'v3' === $settings['register_recaptcha_version'] ) ? 'v3' : 'v2';
400
401 if( ! $this->lr_validate_recaptcha($ld_recaptcha_version) ) {
402 $errors['recaptcha'] = isset( $settings['err_recaptcha'] ) ? Helper::eael_wp_kses( $settings['err_recaptcha'] ) : __( 'You did not pass recaptcha challenge.', 'essential-addons-for-elementor-lite' );
403 }
404 }
405
406 if ( !empty( $_POST['eael_phone_number'] ) && ! $this->eael_is_phone( sanitize_text_field( $_POST['eael_phone_number'] )) ) {
407 $errors['eael_phone_number'] = isset( $settings['err_phone_number_invalid'] ) ? $settings['err_phone_number_invalid'] : __( 'Invalid phone number provided', 'essential-addons-for-elementor-lite' );
408 }
409
410 if ( ! empty( $_POST['email'] ) && is_email( $_POST['email'] ) ) {
411 $email = sanitize_email( $_POST['email'] );
412 if ( email_exists( $email ) ) {
413 $errors['email'] = isset( $settings['err_email_used'] ) ? Helper::eael_wp_kses( $settings['err_email_used'] ) : __( 'The provided email is already registered with other account. Please login or reset password or use another email.', 'essential-addons-for-elementor-lite' );
414 }
415 } else {
416 $errors['email'] = isset( $settings['err_email_missing'] ) ? Helper::eael_wp_kses( $settings['err_email_missing'] ) : __( 'Email is missing or Invalid', 'essential-addons-for-elementor-lite' );
417 }
418
419 // if user provided user name, validate & sanitize it
420 if ( isset( $_POST['user_name'] ) ) {
421 $username = sanitize_user( $_POST['user_name'] );
422 if ( ! validate_username( $username ) || mb_strlen( $username ) > 60 ) {
423 $errors['user_name'] = isset( $settings['err_username'] ) ? Helper::eael_wp_kses( $settings['err_username'] ) : __( 'Invalid username provided.', 'essential-addons-for-elementor-lite' );
424 }elseif(username_exists( $username )){
425 $errors['user_name'] = isset( $settings['err_username_used'] ) ? Helper::eael_wp_kses( $settings['err_username_used'] ) : __( 'The username already registered.', 'essential-addons-for-elementor-lite' );
426
427 }
428 } else {
429 // user has not provided username, so generate one from the provided email.
430 if ( empty( $errors['email'] ) && isset( $email ) ) {
431 $username = $this->generate_username_from_email( $email );
432 }
433 }
434
435 // Dynamic Password Generation
436 $is_pass_auto_generated = false; // emailing is must for autogen pass
437 if ( ! empty( $_POST['password'] ) ) {
438 $password = sanitize_text_field( $_POST['password'] );
439 } else {
440 $password = wp_generate_password();
441 $is_pass_auto_generated = true;
442 }
443
444 if ( isset( $_POST['confirm_pass'] ) ) {
445 $confirm_pass = sanitize_text_field( $_POST['confirm_pass'] );
446 if ( $confirm_pass !== $password ) {
447 $errors['confirm_pass'] = isset( $settings['err_conf_pass'] ) ? Helper::eael_wp_kses( $settings['err_conf_pass'] ) : __( 'The confirmed password did not match.', 'essential-addons-for-elementor-lite' );
448 }
449 }
450
451 if(!$is_pass_auto_generated){
452 $errors = apply_filters( 'eael/login-register/register-user-password-validation', $errors, $settings, $password );
453 }
454
455 // if any error found, abort
456 if ( ! empty( $errors ) ) {
457 if ( $ajax ) {
458 $err_msg = '<ol>';
459 if ( count( $errors ) === 1 ) {
460 $err_msg = '<ol class="'. esc_attr('eael-list-style-none-wrap').'">';
461 }
462
463 foreach ( $errors as $error ) {
464 $err_msg .= "<li>{$error}</li>";
465 }
466 $err_msg .= '</ol>';
467 wp_send_json_error( $err_msg );
468 }
469 update_option( 'eael_register_errors_' . $widget_id, $errors, false );
470 wp_safe_redirect( esc_url_raw( $url ) );
471 exit();
472 }
473
474 /*------General Mail Related Stuff------*/
475 self::$email_options['username'] = $username;
476 self::$email_options['password'] = $password;
477 self::$email_options['email'] = $email;
478 self::$email_options['firstname'] = '';
479 self::$email_options['lastname'] = '';
480 self::$email_options['website'] = '';
481 self::$email_options['password_reset_link'] = '';
482 self::$email_options['eael_phone_number'] = '';
483
484 // handle registration...
485 $user_data = [
486 'user_login' => $username,
487 'user_pass' => $password,
488 'user_email' => $email,
489 ];
490
491 if ( ! empty( $_POST['first_name'] ) ) {
492 $user_data['first_name'] = self::$email_options['firstname'] = sanitize_text_field( $_POST['first_name'] );
493 }
494 if ( ! empty( $_POST['last_name'] ) ) {
495 $user_data['last_name'] = self::$email_options['lastname'] = sanitize_text_field( $_POST['last_name'] );
496 }
497 if ( ! empty( $_POST['website'] ) ) {
498 $user_data['user_url'] = self::$email_options['website'] = esc_url_raw( $_POST['website'] );
499 }
500
501 if ( ! empty( $_POST['eael_phone_number'] ) ) {
502 $user_data['eael_phone_number'] = self::$email_options['eael_phone_number'] = sanitize_text_field( $_POST['eael_phone_number'] );
503 }
504
505 if( count( $eael_custom_profile_fields_text ) ){
506 foreach( $eael_custom_profile_fields_text as $eael_custom_profile_field_text_key => $eael_custom_profile_field_text_value ){
507 self::$email_options[$eael_custom_profile_field_text_key] = '';
508
509 if ( ! empty( $_POST[ $eael_custom_profile_field_text_key ] ) ) {
510 $user_data[$eael_custom_profile_field_text_key] = self::$email_options[$eael_custom_profile_field_text_key] = sanitize_text_field( $_POST[ $eael_custom_profile_field_text_key ] );
511 }
512 }
513 }
514
515 $register_actions = [];
516 $custom_redirect_url = '';
517 if ( !empty( $settings) ) {
518 $register_actions = ! empty( $settings['register_action'] ) ? (array) $settings['register_action'] : [];
519 $custom_redirect_url = ! empty( $settings['register_redirect_url']['url'] ) ? esc_url_raw( $settings['register_redirect_url']['url'] ) : '/';
520 if ( ! empty( $settings['register_user_role'] ) ) {
521 $user_data['role'] = sanitize_text_field( $settings['register_user_role'] );
522 }
523
524
525 // set email related stuff
526 /*------User Mail Related Stuff------*/
527 if ( $is_pass_auto_generated || ( in_array( 'send_email', $register_actions ) && 'custom' === $settings['reg_email_template_type'] ) ) {
528 self::$send_custom_email = true;
529 }
530 if ( isset( $settings['reg_email_subject'] ) ) {
531 self::$email_options['subject'] = Helper::eael_wp_kses( $settings['reg_email_subject'] );
532 }
533 if ( isset( $settings['reg_email_message'] ) ) {
534 self::$email_options['message'] = $settings['reg_email_message'];
535 }
536 if ( isset( $settings['reg_email_content_type'] ) ) {
537 self::$email_options['headers'] = 'Content-Type: text/' . wp_strip_all_tags( $settings['reg_email_content_type'] ) . '; charset=UTF-8' . "\r\n";
538 }
539
540
541 /*------Admin Mail Related Stuff------*/
542 self::$send_custom_email_admin = ( ! empty( $settings['reg_admin_email_template_type'] ) && 'custom' === $settings['reg_admin_email_template_type'] );
543 if ( isset( $settings['reg_admin_email_subject'] ) ) {
544 self::$email_options['admin_subject'] = Helper::eael_wp_kses( $settings['reg_admin_email_subject'] );
545 }
546 if ( isset( $settings['reg_admin_email_message'] ) ) {
547 self::$email_options['admin_message'] = Helper::eael_wp_kses( $settings['reg_admin_email_message'] );
548 }
549 if ( isset( $settings['reg_admin_email_content_type'] ) ) {
550 self::$email_options['admin_headers'] = 'Content-Type: text/' . wp_strip_all_tags( $settings['reg_admin_email_content_type'] ) . '; charset=UTF-8' . "\r\n";
551 }
552 }
553
554 $custom_redirect_url = apply_filters( 'eael/login-register/register-redirect-url', $custom_redirect_url, $this );
555
556 $user_data = apply_filters( 'eael/login-register/new-user-data', $user_data );
557
558 do_action( 'eael/login-register/before-insert-user', $user_data );
559 $user_default_role = get_option( 'default_role' );
560
561 if(!empty($user_default_role) && empty($user_data['role'])){
562 $user_data['role'] = $user_default_role;
563 }
564
565 if ('administrator' == strtolower($user_data['role'])) {
566 $user_data['role'] = !empty($settings['register_user_role']) ? wp_strip_all_tags( $settings['register_user_role'] ) : get_option('default_role');
567 }
568
569 $user_id = wp_insert_user( $user_data );
570
571 if( count( $eael_custom_profile_fields_image ) ){
572 require_once( ABSPATH . 'wp-admin/includes/image.php' );
573 require_once( ABSPATH . 'wp-admin/includes/file.php' );
574 require_once( ABSPATH . 'wp-admin/includes/media.php' );
575
576 foreach( $eael_custom_profile_fields_image as $eael_custom_profile_field_image_key => $eael_custom_profile_field_value ){
577 self::$email_options[$eael_custom_profile_field_image_key] = '';
578
579 if ( ! empty( $_FILES[ $eael_custom_profile_field_image_key ] ) ) {
580 $attachment_id = media_handle_upload( $eael_custom_profile_field_image_key, 0, [ 'post_author' => $user_id ] );
581 if ( ! is_wp_error( $attachment_id ) ) {
582 $user_data[ $eael_custom_profile_field_image_key ] = sanitize_text_field( $attachment_id );
583 self::$email_options[$eael_custom_profile_field_image_key] = wp_get_attachment_image_url( sanitize_text_field( $attachment_id ) );
584 }
585 }
586 }
587 }
588
589 if ( ! empty( $user_data['eael_phone_number'] ) ) {
590 update_user_meta( $user_id, 'eael_phone_number', $user_data['eael_phone_number'] );
591 }
592
593 if( count( $eael_custom_profile_fields ) ){
594 foreach( $eael_custom_profile_fields as $eael_custom_profile_field_key => $eael_custom_profile_field_value ){
595 if ( ! empty( $user_data[ $eael_custom_profile_field_key ] ) ) {
596 update_user_meta( $user_id, self::$eael_custom_profile_field_prefix . $eael_custom_profile_field_key, $user_data[ $eael_custom_profile_field_key ] );
597 }
598 }
599 }
600
601 do_action( 'eael/login-register/after-insert-user', $user_id, $user_data );
602
603 if ( is_wp_error( $user_id ) ) {
604 // error happened during user creation
605 $errors['user_create'] = isset( $settings['err_unknown'] ) ? Helper::eael_wp_kses( $settings['err_unknown'] ) : __( 'Sorry, something went wrong. User could not be registered.', 'essential-addons-for-elementor-lite' );
606 if ( $ajax ) {
607 wp_send_json_error( $errors['user_create'] );
608 }
609 update_option( 'eael_register_errors_' . $widget_id, $errors, false );
610 wp_safe_redirect( esc_url_raw( $url ) );
611 exit();
612 }
613
614 do_action( 'eael/login-register/mailchimp-integration-action', $user_id, $user_data, $settings );
615
616 // generate password reset link for autogenerated password
617 if ( $is_pass_auto_generated ) {
618 update_user_option( $user_id, 'default_password_nag', true, true ); // Set up the password change nag.
619 $user = get_user_by( 'id', $user_id );
620 $key = get_password_reset_key( $user );
621 if ( ! is_wp_error( $key ) ) {
622 self::$email_options['password_reset_link'] = add_query_arg(
623 array(
624 'action' => 'rp',
625 'key' => $key,
626 'login' => rawurlencode( $user->user_login ),
627 ),
628 esc_url_raw( $this->eael_wp_login_url() )
629 );
630 self::$email_options['password_reset_link'] = self::$email_options['password_reset_link'] . "\r\n\r\n";
631 }
632 }
633
634 $admin_or_both = $is_pass_auto_generated || in_array( 'send_email', $register_actions ) ? 'both' : 'admin';
635
636
637 /**
638 * Fires after a new user registration has been recorded.
639 *
640 * @param int $user_id ID of the newly registered user.
641 *
642 * @since 4.4.0
643 */
644 remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
645 do_action( 'register_new_user', $user_id );
646
647 wp_new_user_notification( $user_id, null, $admin_or_both );
648
649 // success & handle after registration action as defined by user in the widget
650 if ( ! $ajax && !in_array( 'redirect', $register_actions ) ) {
651 update_option( 'eael_register_success_' . $widget_id, 1, false );
652 }
653
654
655 // Handle after registration action
656 $data = [
657 'message' => isset( $settings['success_register'] ) ? Helper::eael_wp_kses( $settings['success_register'] ) : __( 'Your registration completed successfully.', 'essential-addons-for-elementor-lite' ),
658 ];
659 // should user be auto logged in?
660 if ( in_array( 'auto_login', $register_actions ) && ! is_user_logged_in() ) {
661 wp_signon( [
662 'user_login' => $username,
663 'user_password' => $password,
664 'remember' => true,
665 ] );
666 $this->delete_registration_options($widget_id);
667
668 if ( $ajax ) {
669 if ( in_array( 'redirect', $register_actions ) ) {
670 $data['redirect_to'] = $custom_redirect_url;
671 }
672 wp_send_json_success( $data );
673 }
674
675 // if custom redirect not available then refresh the current page to show admin bar
676 if ( ! in_array( 'redirect', $register_actions ) ) {
677 wp_safe_redirect( esc_url_raw( $url ) );
678 exit();
679 }
680 }
681
682 // custom redirect?
683 if ( $ajax ) {
684 if ( in_array( 'redirect', $register_actions ) ) {
685 $data['redirect_to'] = $custom_redirect_url;
686 }
687 wp_send_json_success( $data );
688 }
689
690 if ( in_array( 'redirect', $register_actions ) ) {
691 wp_safe_redirect( $custom_redirect_url );
692 exit();
693 }
694
695 if (isset($_SERVER['HTTP_REFERER'])) {
696 wp_safe_redirect($_SERVER['HTTP_REFERER']);
697 exit();
698 }
699
700 }
701
702 /**
703 * It sends the user an email with reset password link. Lost Password form is submitted normally without AJAX.
704 */
705 public function send_password_reset() {
706 $ajax = wp_doing_ajax();
707 // before even thinking about sending mail, check security and exit early if something is not right.
708 $page_id = 0;
709 $page_id_for_popup = 0;
710 $resetpassword_in_popup_selector = '';
711 if ( ! empty( $_POST['page_id'] ) ) {
712 $page_id = intval( $_POST['page_id'], 10 );
713 $page_id_for_popup = ! empty( $_POST['page_id_for_popup'] ) ? intval( $_POST['page_id_for_popup'], 10 ) : $page_id;
714 $resetpassword_in_popup_selector = ! empty( $_POST['resetpassword_in_popup_selector'] ) ? sanitize_text_field( $_POST['resetpassword_in_popup_selector'] ) : '';
715 } else {
716 $err_msg = esc_html__( 'Page ID is missing', 'essential-addons-for-elementor-lite' );
717 }
718
719 $widget_id = 0;
720 if ( ! empty( $_POST['widget_id'] ) ) {
721 $widget_id = sanitize_text_field( $_POST['widget_id'] );
722 } else {
723 $err_msg = esc_html__( 'Widget ID is missing', 'essential-addons-for-elementor-lite' );
724 }
725
726 if (!empty( $err_msg )){
727 if ( $ajax ) {
728 wp_send_json_error( $err_msg );
729 }
730 update_option( 'eael_losstpassword_error_' . $widget_id, $err_msg, false );
731
732 if (isset($_SERVER['HTTP_REFERER'])) {
733 wp_safe_redirect($_SERVER['HTTP_REFERER']);
734 exit();
735 }
736 }
737
738
739 if ( empty( $_POST['eael-lostpassword-nonce'] ) ) {
740 $err_msg = esc_html__( 'Insecure form submitted without security token', 'essential-addons-for-elementor-lite' );
741 if ( $ajax ) {
742 wp_send_json_error( $err_msg );
743 }
744 update_option( 'eael_lostpassword_error_' . $widget_id, $err_msg, false );
745
746 if (isset($_SERVER['HTTP_REFERER'])) {
747 wp_safe_redirect($_SERVER['HTTP_REFERER']);
748 exit();
749 }
750 }
751 if ( ! wp_verify_nonce( $_POST['eael-lostpassword-nonce'], 'essential-addons-elementor' ) ) {
752 $err_msg = esc_html__( 'Security token did not match', 'essential-addons-for-elementor-lite' );
753 if ( $ajax ) {
754 wp_send_json_error( $err_msg );
755 }
756 update_option( 'eael_lostpassword_error_' . $widget_id, $err_msg, false );
757
758 if (isset($_SERVER['HTTP_REFERER'])) {
759 wp_safe_redirect($_SERVER['HTTP_REFERER']);
760 exit();
761 }
762 }
763
764 $settings = $this->lr_get_widget_settings( $page_id, $widget_id);
765
766 if ( is_user_logged_in() ) {
767 $err_msg = isset( $settings['err_loggedin'] ) ? Helper::eael_wp_kses( $settings['err_loggedin'] ) : esc_html__( 'You are already logged in', 'essential-addons-for-elementor-lite' );
768 if ( $ajax ) {
769 wp_send_json_error( $err_msg );
770 }
771 update_option( 'eael_lostpassword_error_' . $widget_id, $err_msg, false );
772
773 if (isset($_SERVER['HTTP_REFERER'])) {
774 wp_safe_redirect($_SERVER['HTTP_REFERER']);
775 exit();
776 }
777 }
778
779 do_action( 'eael/login-register/before-lostpassword-email' );
780
781 $widget_id = ! empty( $_POST['widget_id'] ) ? sanitize_text_field( $_POST['widget_id'] ) : '';
782
783 if( $_POST['eael-user-lostpassword'] != wp_strip_all_tags( $_POST['eael-user-lostpassword'] ) ){
784 // contains html tag
785 $err_msg = esc_html__( 'There is no account with that username or email address.', 'essential-addons-for-elementor-lite' );
786 if ( $ajax ) {
787 wp_send_json_error( $err_msg );
788 }
789 update_option( 'eael_lostpassword_error_' . $widget_id, $err_msg, false );
790
791 if (isset($_SERVER['HTTP_REFERER'])) {
792 wp_safe_redirect($_SERVER['HTTP_REFERER']);
793 exit();
794 }
795 }
796
797 $user_login = ! empty( $_POST['eael-user-lostpassword'] ) ? sanitize_text_field( $_POST['eael-user-lostpassword'] ) : '';
798 if ( is_email( $user_login ) ) {
799 $user_login = sanitize_email( $user_login );
800 }
801
802 // set email related stuff
803 if ( ! empty( $settings['enable_reset_password'] ) && 'yes' === $settings['enable_reset_password'] ) {
804 self::$send_custom_email_lostpassword = true;
805 }
806 if ( isset( $settings['lostpassword_email_subject'] ) ) {
807 self::$email_options_lostpassword['subject'] = Helper::eael_wp_kses( $settings['lostpassword_email_subject'] );
808 }
809 if ( isset( $settings['lostpassword_email_message_reset_link_text'] ) ) {
810 self::$email_options_lostpassword['reset_link_text'] = Helper::eael_wp_kses( $settings['lostpassword_email_message_reset_link_text'] );
811 }
812 if ( isset( $settings['lostpassword_email_message'] ) ) {
813 self::$email_options_lostpassword['message'] = $settings['lostpassword_email_message'];
814 }
815 if ( isset( $settings['lostpassword_email_content_type'] ) ) {
816 self::$email_options_lostpassword['headers'] = 'Content-Type: text/' . Helper::eael_wp_kses( $settings['lostpassword_email_content_type'] ) . '; charset=UTF-8' . "\r\n";
817 }
818
819 if ( isset($_SERVER['HTTP_REFERER']) ) {
820 self::$email_options_lostpassword['http_referer'] = esc_url_raw( strtok( $_SERVER['HTTP_REFERER'], '?' ) );
821 }
822
823 if ( isset($page_id) ) {
824 self::$email_options_lostpassword['page_id'] = sanitize_text_field( $page_id );
825 }
826
827 if ( ! empty( $page_id_for_popup ) ) {
828 self::$email_options_lostpassword['page_id'] = sanitize_text_field( $page_id_for_popup );
829 }
830
831 if ( ! empty( $resetpassword_in_popup_selector ) ) {
832 self::$email_options_lostpassword['resetpassword_in_popup_selector'] = sanitize_text_field( $resetpassword_in_popup_selector );
833 }
834
835 if ( isset($widget_id) ) {
836 self::$email_options_lostpassword['widget_id'] = sanitize_text_field( $widget_id );
837 }
838
839 add_filter( 'retrieve_password_notification_email', [ $this, 'eael_retrieve_password_notification_email' ], 10, 4 );
840
841 $results = retrieve_password( $user_login );
842
843 if ( is_wp_error( $results ) ) {
844 $err_msg = '';
845 if ( isset( $results->errors['invalidcombo'][0] ) ) {
846 $err_msg = esc_html__( 'There is no account with that username or email address.', 'essential-addons-for-elementor-lite' );
847 }else if( isset( $results->errors ) && count( $results->errors ) ) {
848 $err_msg = esc_html__( 'There is no account with that username or email address.', 'essential-addons-for-elementor-lite' );
849 }
850
851 if ( $ajax ) {
852 wp_send_json_error( $err_msg );
853 }
854 update_option( 'eael_lostpassword_error_' . $widget_id, $err_msg, false );
855 } else {
856 $lostpassword_success_message = ! empty( $settings['success_lostpassword'] ) ? Helper::eael_wp_kses( $settings['success_lostpassword'] ) : Helper::eael_wp_kses( 'Check your email for the confirmation link.' );
857 $data = [
858 'message' => $lostpassword_success_message,
859 ];
860
861 if ( $ajax ) {
862 if ( ! empty( $_POST['redirect_to'] ) ) {
863 $data['redirect_to'] = esc_url_raw( $_POST['redirect_to'] );
864 }
865 wp_send_json_success( $data );
866 } else {
867 update_option( 'eael_lostpassword_success_' . $widget_id, $data['message'], false );
868 }
869
870 if ( ! empty( $_POST['redirect_to'] ) ) {
871 wp_safe_redirect( esc_url_raw( $_POST['redirect_to'] ) );
872 exit();
873 }
874 }
875 if (isset($_SERVER['HTTP_REFERER'])) {
876 wp_safe_redirect($_SERVER['HTTP_REFERER']);
877 exit();
878 }
879 }
880
881 /**
882 * It reset the password with user submitted new password.
883 */
884 public function reset_password() {
885 $ajax = wp_doing_ajax();
886 $page_id = 0;
887 if ( ! empty( $_POST['page_id'] ) ) {
888 $page_id = intval( $_POST['page_id'], 10 );
889 } else {
890 $err_msg = esc_html__( 'Page ID is missing', 'essential-addons-for-elementor-lite' );
891 }
892
893 $widget_id = 0;
894 if ( ! empty( $_POST['widget_id'] ) ) {
895 $widget_id = sanitize_text_field( $_POST['widget_id'] );
896 } else {
897 $err_msg = esc_html__( 'Widget ID is missing', 'essential-addons-for-elementor-lite' );
898 }
899
900 update_option( 'eael_show_reset_password_on_form_submit_' . $widget_id, true, false );
901
902 if (!empty( $err_msg )){
903 if ( $ajax ) {
904 wp_send_json_error( $err_msg );
905 }
906 update_option( 'eael_resetpassword_error_' . $widget_id, $err_msg, false );
907
908 if (isset($_SERVER['HTTP_REFERER'])) {
909 wp_safe_redirect($_SERVER['HTTP_REFERER']);
910 exit();
911 }
912 }
913
914 if ( empty( $_POST['eael-resetpassword-nonce'] ) ) {
915 $err_msg = esc_html__( 'Insecure form submitted without security token', 'essential-addons-for-elementor-lite' );
916 if ( $ajax ) {
917 wp_send_json_error( $err_msg );
918 }
919 update_option( 'eael_resetpassword_error_' . $widget_id, $err_msg, false );
920
921 if (isset($_SERVER['HTTP_REFERER'])) {
922 wp_safe_redirect($_SERVER['HTTP_REFERER']);
923 exit();
924 }
925 }
926 if ( ! wp_verify_nonce( $_POST['eael-resetpassword-nonce'], 'essential-addons-elementor' ) ) {
927 $err_msg = esc_html__( 'Security token did not match', 'essential-addons-for-elementor-lite' );
928 if ( $ajax ) {
929 wp_send_json_error( $err_msg );
930 }
931 update_option( 'eael_resetpassword_error_' . $widget_id, $err_msg, false );
932
933 if (isset($_SERVER['HTTP_REFERER'])) {
934 wp_safe_redirect($_SERVER['HTTP_REFERER']);
935 exit();
936 }
937 }
938 $settings = $this->lr_get_widget_settings( $page_id, $widget_id);
939
940 if ( is_user_logged_in() ) {
941 $err_msg = isset( $settings['err_loggedin'] ) ? Helper::eael_wp_kses( $settings['err_loggedin'] ) : esc_html__( 'You are already logged in', 'essential-addons-for-elementor-lite' );
942 if ( $ajax ) {
943 wp_send_json_error( $err_msg );
944 }
945 update_option( 'eael_resetpassword_error_' . $widget_id, $err_msg, false );
946
947 if (isset($_SERVER['HTTP_REFERER'])) {
948 wp_safe_redirect($_SERVER['HTTP_REFERER']);
949 exit();
950 }
951 }
952
953 do_action( 'eael/login-register/before-resetpassword-email' );
954
955 $widget_id = ! empty( $_POST['widget_id'] ) ? sanitize_text_field( $_POST['widget_id'] ) : '';
956 // Check if password is one or all empty spaces.
957 $errors = [];
958 if ( ! empty( $_POST['eael-pass1'] ) ) {
959 $post_eael_pass1 = trim( $_POST['eael-pass1'] );
960
961 if ( empty( $post_eael_pass1 ) ) {
962 $errors['password_reset_empty_space'] = isset( $settings['err_pass'] ) ? Helper::eael_wp_kses( $settings['err_pass'] ) : esc_html__( 'The password cannot be a space or all spaces.', 'essential-addons-for-elementor-lite' );
963 }
964 } else {
965 if ( empty( $_POST['eael-pass1'] ) ) {
966 $errors['password_reset_empty_space'] = isset( $settings['err_pass'] ) ? Helper::eael_wp_kses( $settings['err_pass'] ) : esc_html__( 'The password cannot be a space or all spaces.', 'essential-addons-for-elementor-lite' );
967 }
968 }
969
970 if( ! empty( $_POST['eael-pass1'] ) && strlen( trim( $_POST['eael-pass1'] ) ) == 0 ){
971 $errors['password_reset_empty'] = esc_html__( 'The password cannot be empty.', 'essential-addons-for-elementor-lite' );
972 }
973
974 // Check if password fields do not match.
975 if ( ! empty( $_POST['eael-pass1'] ) && $_POST['eael-pass2'] !== $_POST['eael-pass1'] ) {
976 $errors['password_reset_mismatch'] = isset( $settings['err_conf_pass'] ) ? Helper::eael_wp_kses( $settings['err_conf_pass'] ) : esc_html__( 'The passwords do not match.', 'essential-addons-for-elementor-lite' );
977 }
978
979 if ( ( ! count( $errors ) ) && isset( $_POST['eael-pass1'] ) && ! empty( $_POST['eael-pass1'] ) ) {
980 $rp_data_db['rp_key'] = ! empty( $_POST['rp_key'] ) ? sanitize_text_field( $_POST['rp_key'] ) : '';
981 $rp_data_db['rp_login'] = ! empty( $_POST['rp_login'] ) ? sanitize_text_field( $_POST['rp_login'] ) : '';
982
983 $user = check_password_reset_key( $rp_data_db['rp_key'], $rp_data_db['rp_login'] );
984
985 if( is_wp_error( $user ) || ! $user ){
986 $data['message'] = isset( $settings['error_resetpassword'] ) ? Helper::eael_wp_kses( $settings['error_resetpassword'] ) : esc_html__( 'Invalid user name found!', 'essential-addons-for-elementor-lite' );
987
988 $success_key = 'eael_resetpassword_success_' . esc_attr( $widget_id );
989 delete_option( $success_key );
990
991 if($ajax){
992 wp_send_json_error( $data['message'] );
993 }else {
994 update_option( 'eael_resetpassword_error_' . $widget_id, $data['message'], false );
995 }
996 }
997
998 if( $user && ! is_wp_error( $user ) ){
999 try {
1000 reset_password( $user, sanitize_text_field( $_POST['eael-pass1'] ) );
1001 $data['message'] = isset( $settings['success_resetpassword'] ) ? Helper::eael_wp_kses( $settings['success_resetpassword'] ) : esc_html__( 'Your password has been reset.', 'essential-addons-for-elementor-lite' );
1002
1003 $error_key = 'eael_resetpassword_error_' . esc_attr( $widget_id );
1004 delete_option( $error_key );
1005 delete_option( 'eael_show_reset_password_on_form_submit_' . $widget_id );
1006
1007 if($ajax){
1008 // $custom_redirect_url = ! empty( $settings['resetpassword_redirect_url']['url'] ) ? $settings['resetpassword_redirect_url']['url'] : '/';
1009 if( ! empty( $_POST['resetpassword_redirect_to'] ) ){
1010 $data['redirect_to'] = esc_url_raw( $_POST['resetpassword_redirect_to'] );
1011 }
1012
1013 wp_send_json_success( $data );
1014 } else {
1015 update_option( 'eael_resetpassword_success_' . $widget_id, $data['message'], false );
1016 }
1017
1018 if ( ! empty( $_POST['resetpassword_redirect_to'] ) ) {
1019 wp_safe_redirect( esc_url_raw( $_POST['resetpassword_redirect_to'] ) );
1020 exit();
1021 }
1022 } catch ( \Exception $e ) {
1023 // Do nothing
1024 unset( $e );
1025 }
1026 }
1027
1028 if (isset($_SERVER['HTTP_REFERER'])) {
1029 wp_safe_redirect( strtok( $_SERVER['HTTP_REFERER'], '?' ) );
1030 exit();
1031 }
1032 } else {
1033 // if any error found, abort
1034 if ( ! empty( $errors ) ) {
1035 if ( $ajax ) {
1036 $err_msg = '<ol>';
1037 foreach ( $errors as $error ) {
1038 $err_msg .= "<li>{$error}</li>";
1039 }
1040 $err_msg .= '</ol>';
1041 wp_send_json_error( $err_msg );
1042 }
1043 update_option( 'eael_resetpassword_error_' . $widget_id, maybe_serialize( $errors ), false );
1044
1045 if (isset( $_SERVER['HTTP_REFERER'] )) {
1046 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
1047 exit();
1048 }
1049 }
1050 }
1051
1052 }
1053
1054 public function eael_redirect_to_reset_password(){
1055 if( empty($_GET['eael-resetpassword']) ){
1056 return;
1057 }
1058
1059 $this->page_id = isset( $_GET['page_id'] ) ? intval( $_GET['page_id'] ) : 0;
1060 $this->widget_id = isset( $_GET['widget_id'] ) ? sanitize_text_field( $_GET['widget_id'] ) : '';
1061 $this->resetpassword_in_popup_selector = isset( $_GET['popup-selector'] ) ? sanitize_text_field( $_GET['popup-selector'] ) : '';
1062 $rp_page_url = get_permalink( $this->page_id );
1063
1064 $rp_path = '/';
1065 $rp_cookie = 'wp-resetpass-' . COOKIEHASH;
1066
1067 if ( isset( $_GET['key'] ) && isset( $_GET['login'] ) ) {
1068 $value = sprintf( '%s:%s', wp_unslash( $_GET['login'] ), wp_unslash( $_GET['key'] ) );
1069 setcookie( $rp_cookie, $value, 0, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
1070
1071 wp_safe_redirect( remove_query_arg( array( 'key', 'login' ) ) );
1072 exit;
1073 }
1074
1075 if ( isset( $_COOKIE[ $rp_cookie ] ) && 0 < strpos( $_COOKIE[ $rp_cookie ], ':' ) ) {
1076 list( $rp_login, $rp_key ) = explode( ':', wp_unslash( $_COOKIE[ $rp_cookie ] ), 2 );
1077
1078 $user = check_password_reset_key( $rp_key, $rp_login );
1079
1080 if ( isset( $_POST['eael-pass1'] ) && isset( $_POST['rp_key'] ) && ! hash_equals( $rp_key, $_POST['rp_key'] ) ) {
1081 $user = false;
1082 }
1083 } else {
1084 $user = false;
1085 }
1086
1087 if ( ! $user || is_wp_error( $user ) ) {
1088 setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
1089 update_option( 'eael_lostpassword_error_' . esc_attr( $this->widget_id ) . '_show', 1, false );
1090
1091 if ( $user && $user->get_error_code() === 'expired_key' ) {
1092 wp_redirect( $rp_page_url . '?eael-lostpassword=1&error=expiredkey' );
1093 } else {
1094 wp_redirect( $rp_page_url . '?eael-lostpassword=1&error=expiredkey' );
1095 }
1096
1097 exit;
1098 }
1099
1100 if( $this->resetpassword_in_popup_selector ){
1101 wp_redirect( $rp_page_url . '?eael-resetpassword=1&popup-selector=' . $this->resetpassword_in_popup_selector );
1102 } else {
1103 wp_redirect( $rp_page_url . '?eael-resetpassword=1' );
1104 }
1105
1106 exit;
1107 }
1108
1109 public function eael_retrieve_password_notification_email( $defaults, $key, $user_login, $user_data ){
1110 if ( ! self::$send_custom_email_lostpassword ) {
1111 return $defaults;
1112 }
1113
1114 if ( ! empty( self::$email_options_lostpassword['subject'] ) ) {
1115 $defaults['subject'] = self::$email_options_lostpassword['subject'];
1116 }
1117
1118 $page_id = self::$email_options_lostpassword['page_id'] ? self::$email_options_lostpassword['page_id'] : 0;
1119 $widget_id = self::$email_options_lostpassword['widget_id'] ? self::$email_options_lostpassword['widget_id'] : '';
1120 $resetpassword_in_popup_selector = self::$email_options_lostpassword['resetpassword_in_popup_selector'] ? str_replace(' ', '_', self::$email_options_lostpassword['resetpassword_in_popup_selector']) : '';
1121
1122 if ( ! empty( self::$email_options_lostpassword['message'] ) ) {
1123 if ( ! empty( $key ) ) {
1124 $locale = get_user_locale( $user_data );
1125 self::$email_options_lostpassword['password_reset_link'] = add_query_arg(
1126 array(
1127 'action' => 'rp',
1128 'eael-resetpassword' => 1,
1129 'key' => $key,
1130 'login' => rawurlencode( $user_login ),
1131 ),
1132 esc_url_raw( $this->eael_wp_login_url() )
1133 );
1134 self::$email_options_lostpassword['password_reset_link'] = self::$email_options_lostpassword['password_reset_link'] . '&page_id='. $page_id . '&widget_id='. $widget_id .'&wp_lang=' . $locale . "\r\n\r\n";
1135
1136 if( ! empty( $resetpassword_in_popup_selector ) ){
1137 self::$email_options_lostpassword['password_reset_link'] = add_query_arg(
1138 array(
1139 'action' => 'rp',
1140 'eael-resetpassword' => '1',
1141 'key' => $key,
1142 'login' => rawurlencode( $user_login ),
1143 'page_id' => $page_id,
1144 'widget_id' => $widget_id,
1145 'popup-selector' => $resetpassword_in_popup_selector,
1146 'wp_lang' => $locale,
1147 ),
1148 esc_url_raw( $this->eael_wp_login_url() )
1149 );
1150 }
1151 }
1152
1153 if( is_object($user_data) ) {
1154 $user_meta = get_user_meta( $user_data->ID );
1155 self::$email_options_lostpassword['username'] = $user_login;
1156 self::$email_options_lostpassword['firstname'] = !empty( $user_meta['first_name'][0] ) ? $user_meta['first_name'][0] : '';
1157 self::$email_options_lostpassword['lastname'] = !empty( $user_meta['last_name'][0] ) ? $user_meta['last_name'][0] : '';
1158 self::$email_options_lostpassword['email'] = $user_data->user_email;
1159 self::$email_options_lostpassword['website'] = $user_data->user_url;
1160 }
1161 $defaults['message'] = $this->replace_placeholders_lostpassword( self::$email_options_lostpassword['message'] );
1162 }
1163
1164 if ( ! empty( self::$email_options_lostpassword['headers'] ) ) {
1165 $defaults['headers'] = self::$email_options_lostpassword['headers'];
1166 }
1167
1168 $defaults['message'] = wpautop( $defaults['message'] );
1169
1170 return $defaults;
1171 }
1172
1173 public function generate_username_from_email( $email, $suffix = '' ) {
1174
1175 $username_parts = [];
1176 if ( empty( $username_parts ) ) {
1177 $email_parts = explode( '@', $email );
1178 $email_username = $email_parts[0];
1179
1180 // Exclude common prefixes.
1181 if ( in_array( $email_username, [
1182 'sales',
1183 'hello',
1184 'mail',
1185 'contact',
1186 'info',
1187 ], true ) ) {
1188 // Get the domain part.
1189 $email_username = $email_parts[1];
1190 }
1191
1192 $username_parts[] = sanitize_user( $email_username, true );
1193 }
1194 $username = strtolower( implode( '', $username_parts ) );
1195
1196 if ( $suffix ) {
1197 $username .= $suffix;
1198 }
1199
1200 $username = sanitize_user( $username, true );
1201 if ( username_exists( $username ) ) {
1202 // Generate something unique to append to the username in case of a conflict with another user.
1203 $suffix = '-' . zeroise( wp_rand( 0, 9999 ), 4 );
1204
1205 return $this->generate_username_from_email( $email, $suffix );
1206 }
1207
1208 return $username;
1209 }
1210
1211 /**
1212 * Get Widget data.
1213 *
1214 * @param array $elements Element array.
1215 * @param string $form_id Element ID.
1216 *
1217 * @return bool|array
1218 */
1219 public function find_element_recursive( $elements, $form_id ) {
1220
1221 foreach ( $elements as $element ) {
1222 if ( $form_id === $element['id'] ) {
1223 return $element;
1224 }
1225
1226 if ( ! empty( $element['elements'] ) ) {
1227 $element = $this->find_element_recursive( $element['elements'], $form_id );
1228
1229 if ( $element ) {
1230 return $element;
1231 }
1232 }
1233 }
1234
1235 return false;
1236 }
1237
1238 public function get_user_roles() {
1239 $user_roles[''] = __( 'Default', 'essential-addons-for-elementor-lite' );
1240 if ( function_exists( 'get_editable_roles' ) ) {
1241 $wp_roles = get_editable_roles();
1242 $roles = $wp_roles ? $wp_roles : [];
1243 if ( ! empty( $roles ) && is_array( $roles ) ) {
1244
1245 foreach ( $wp_roles as $role_key => $role ) {
1246 $user_roles[ $role_key ] = $role['name'];
1247 }
1248 }
1249 }
1250 return apply_filters( 'eael/login-register/new-user-roles', $user_roles );
1251 }
1252
1253 /**
1254 * It store data temporarily,5 minutes by default
1255 *
1256 * @param $name
1257 * @param $data
1258 * @param int $time time in seconds. Default is 300s = 5 minutes
1259 *
1260 * @return bool it returns true if the data saved, otherwise, false returned.
1261 */
1262 public function set_transient( $name, $data, $time = 300 ) {
1263 $time = empty( $time ) ? (int) $time : ( 5 * MINUTE_IN_SECONDS );
1264
1265 return set_transient( $name, $data, $time );
1266 }
1267
1268 /**
1269 * Filters the contents of the new user notification email sent to the new user.
1270 *
1271 * @param array $email_data It contains, to, subject, message, headers etc.
1272 * @param \WP_User $user User object for new user.
1273 * @param string $blogname The site title.
1274 *
1275 * @return array
1276 * @since 4.9.0
1277 */
1278 public function new_user_notification_email( $email_data, $user, $blogname ) {
1279 if ( ! self::$send_custom_email ) {
1280 return $email_data;
1281 }
1282
1283 if ( ! empty( self::$email_options['subject'] ) ) {
1284 $email_data['subject'] = self::$email_options['subject'];
1285 }
1286
1287 if ( ! empty( self::$email_options['message'] ) ) {
1288 if ( isset( self::$email_options['password_reset_link'] ) && self::$email_options['password_reset_link'] != '' ) {
1289 $_message = $email_data['message'];
1290 $start = 'action=rp&key=';
1291 $end = '&login=';
1292 $_message = substr( $_message, strpos( $_message, $start ) + strlen( $start ) );
1293 $key = substr( $_message, 0, strpos( $_message, $end ) );
1294 if ( ! empty( $key ) ) {
1295 self::$email_options_lostpassword['password_reset_link'] = add_query_arg(
1296 array(
1297 'action' => 'rp',
1298 'key' => $key,
1299 'login' => rawurlencode( $user->user_login ),
1300 ),
1301 esc_url_raw( $this->eael_wp_login_url() )
1302 );
1303 self::$email_options['password_reset_link'] = self::$email_options['password_reset_link'] . "\r\n\r\n";
1304 }
1305 }
1306 $email_data['message'] = $this->replace_placeholders( self::$email_options['message'], 'user' );
1307 }
1308
1309 if ( ! empty( self::$email_options['headers'] ) ) {
1310 $email_data['headers'] = self::$email_options['headers'];
1311 }
1312
1313 $email_data['message'] = wpautop( $email_data['message'] );
1314
1315 return apply_filters( 'eael/login-register/new-user-email-data', $email_data, $user, $blogname );
1316
1317 }
1318
1319 /**
1320 * Filters the contents of the new user notification email sent to the site admin.
1321 *
1322 * @param array $email_data It contains, to, subject, message, headers etc.
1323 * @param \WP_User $user User object for new user.
1324 * @param string $blogname The site title.
1325 *
1326 * @return array
1327 * @since 4.9.0
1328 */
1329 public function new_user_notification_email_admin( $email_data, $user, $blogname ) {
1330
1331 if ( ! self::$send_custom_email_admin ) {
1332 return $email_data;
1333 }
1334
1335 if ( ! empty( self::$email_options['admin_subject'] ) ) {
1336 $email_data['subject'] = self::$email_options['admin_subject'];
1337 }
1338
1339 if ( ! empty( self::$email_options['admin_message'] ) ) {
1340 $email_data['message'] = $this->replace_placeholders( self::$email_options['admin_message'], 'admin' );
1341 }
1342
1343 if ( ! empty( self::$email_options['admin_headers'] ) ) {
1344 $email_data['headers'] = self::$email_options['admin_headers'];
1345 }
1346
1347 $email_data['message'] = wpautop( $email_data['message'] );
1348
1349 return apply_filters( 'eael/login-register/new-user-admin-email-data', $email_data, $user, $blogname );
1350 }
1351
1352 /**
1353 * It replaces placeholders with dynamic value and returns it.
1354 *
1355 * @param $message
1356 * @param string $receiver
1357 *
1358 * @return null|string|string[]
1359 */
1360 public function replace_placeholders( $message, $receiver = 'user' ) {
1361 $placeholders = [
1362 '/\[eael_phone_number\]/',
1363 '/\[password\]/',
1364 '/\[password_reset_link\]/',
1365 '/\[username\]/',
1366 '/\[email\]/',
1367 '/\[firstname\]/',
1368 '/\[lastname\]/',
1369 '/\[website\]/',
1370 '/\[loginurl\]/',
1371 '/\[sitetitle\]/',
1372 ];
1373 $replacement = [
1374 self::$email_options['eael_phone_number'],
1375 self::$email_options['password'],
1376 self::$email_options['password_reset_link'],
1377 self::$email_options['username'],
1378 self::$email_options['email'],
1379 self::$email_options['firstname'],
1380 self::$email_options['lastname'],
1381 self::$email_options['website'],
1382 esc_url_raw( $this->eael_wp_login_url() ),
1383 get_option( 'blogname' ),
1384 ];
1385
1386 if ( 'user' !== $receiver ) {
1387 // remove password from admin mail, because admin should not see user's plain password
1388 unset( $placeholders[0] );
1389 unset( $placeholders[1] );
1390 unset( $replacement[0] );
1391 unset( $replacement[1] );
1392 }
1393
1394 $message = preg_replace( $placeholders, $replacement, $message );
1395
1396 $message = $this->replace_placeholders_custom_fields($message);
1397
1398 return $message;
1399 }
1400
1401 public function replace_placeholders_custom_fields( $message ){
1402 // replace custom profile field shortcodes
1403 $eael_custom_profile_fields_text = $this->get_eael_custom_profile_fields('text');
1404 $eael_custom_profile_fields_image = $this->get_eael_custom_profile_fields('image');
1405 $eael_custom_profile_fields_text_keys = count( $eael_custom_profile_fields_text ) ? array_keys( $eael_custom_profile_fields_text ) : [];
1406 $eael_custom_profile_fields_image_keys = count( $eael_custom_profile_fields_image ) ? array_keys( $eael_custom_profile_fields_image ) : [];
1407
1408 $custom_field_placeholders = $custom_field_replacements = [];
1409
1410 if( count( $eael_custom_profile_fields_text_keys ) ){
1411 foreach( $eael_custom_profile_fields_text_keys as $eael_custom_profile_fields_text_key){
1412 $custom_field_placeholders[] = '/\[' . esc_html( $eael_custom_profile_fields_text_key ) . '\]/';
1413 $custom_field_replacements[] = esc_html( self::$email_options[$eael_custom_profile_fields_text_key] );
1414 }
1415 }
1416
1417 if( count( $eael_custom_profile_fields_image_keys ) ){
1418 foreach( $eael_custom_profile_fields_image_keys as $eael_custom_profile_fields_image_key){
1419 $custom_field_placeholders[] = '/\[' . esc_html( $eael_custom_profile_fields_image_key ) . '\]/';
1420 $custom_field_replacements[] = esc_url( self::$email_options[$eael_custom_profile_fields_image_key] );
1421 }
1422 }
1423
1424 if( count( $custom_field_placeholders ) ){
1425 $message = preg_replace( $custom_field_placeholders, $custom_field_replacements, $message );
1426 }
1427
1428 return $message;
1429 }
1430
1431 /**
1432 * It replaces placeholders with dynamic value and returns it.
1433 *
1434 * @param $message
1435 * @param string $receiver
1436 *
1437 * @return null|string|string[]
1438 */
1439 public function replace_placeholders_lostpassword( $message ) {
1440 $reset_link_text = !empty( self::$email_options_lostpassword['reset_link_text'] ) ? self::$email_options_lostpassword['reset_link_text'] : esc_html__('Click here to reset your password', 'essential-addons-for-elementor-lite');
1441 $password_reset_link = !empty( self::$email_options_lostpassword['password_reset_link'] ) ? '<a href="'.esc_url_raw( self::$email_options_lostpassword['password_reset_link'] ).'">' . esc_html( $reset_link_text ) . '</a>' : '';
1442 $username = !empty( self::$email_options_lostpassword['username'] ) ? self::$email_options_lostpassword['username'] : '';
1443 $email = !empty( self::$email_options_lostpassword['email'] ) ? self::$email_options_lostpassword['email'] : '';
1444 $firstname = !empty( self::$email_options_lostpassword['firstname'] ) ? self::$email_options_lostpassword['firstname'] : '';
1445 $lastname = !empty( self::$email_options_lostpassword['lastname'] ) ? self::$email_options_lostpassword['lastname'] : '';
1446 $website = !empty( self::$email_options_lostpassword['website'] ) ? self::$email_options_lostpassword['website'] : '';
1447
1448 $placeholders = [
1449 '/\[password_reset_link\]/',
1450 '/\[username\]/',
1451 '/\[email\]/',
1452 '/\[firstname\]/',
1453 '/\[lastname\]/',
1454 '/\[website\]/',
1455 '/\[loginurl\]/',
1456 '/\[sitetitle\]/',
1457 ];
1458 $replacement = [
1459 $password_reset_link,
1460 $username,
1461 $email,
1462 $firstname,
1463 $lastname,
1464 $website,
1465 esc_url_raw( $this->eael_wp_login_url() ),
1466 get_option( 'blogname' ),
1467 ];
1468
1469 return preg_replace( $placeholders, $replacement, $message );
1470 }
1471
1472 /**
1473 * It replaces placeholders with dynamic value and returns it.
1474 *
1475 * @param $text
1476 * @param string $receiver
1477 *
1478 * @return null|string|string[]
1479 */
1480 public function replace_placeholders_logout_link_text( $text ) {
1481 $current_user = wp_get_current_user()->display_name;
1482 $logout_link = sprintf( '<a href="%1$s">%2$s</a>', esc_url( wp_logout_url() ), __( 'Logout', 'essential-addons-for-elementor-lite' ) );
1483
1484 $placeholders = [
1485 '/\[username\]/',
1486 '/\[logout_link\]/',
1487 ];
1488 $replacement = [
1489 $current_user,
1490 $logout_link,
1491 ];
1492
1493 return preg_replace( $placeholders, $replacement, $text );
1494 }
1495
1496 public function lr_validate_recaptcha($version = 'v2') {
1497 if ( ! isset( $_REQUEST['g-recaptcha-response'] ) ) {
1498 return false;
1499 }
1500 $endpoint = 'https://www.google.com/recaptcha/api/siteverify';
1501 $data = [
1502 'secret' => 'v3' === $version ? get_option( 'eael_recaptcha_secret_v3' ) : get_option( 'eael_recaptcha_secret' ),
1503 'response' => sanitize_text_field( $_REQUEST['g-recaptcha-response'] ),
1504 'ip' => $_SERVER['REMOTE_ADDR'],
1505 ];
1506
1507 $res = json_decode( wp_remote_retrieve_body( wp_remote_post( $endpoint, [ 'body' => $data ] ) ), 1 );
1508 if ( isset( $res['success'] ) ) {
1509 if('v3' === $version ) {
1510 $action = self::$recaptcha_v3_default_action;
1511 $action_ok = ! isset( $res['action'] ) ? true : $action === $res['action'];
1512 return $action_ok && ( $res['score'] > self::get_recaptcha_threshold() );
1513 }else {
1514 return $res['success'];
1515 }
1516 }
1517
1518 return false;
1519 }
1520
1521 public function lr_get_widget_settings( $page_id, $widget_id ) {
1522 $document = Plugin::$instance->documents->get( $page_id );
1523 $settings = [];
1524 if ( $document ) {
1525 $elements = Plugin::instance()->documents->get( $page_id )->get_elements_data();
1526 $widget_data = $this->find_element_recursive( $elements, $widget_id );
1527
1528 if(!empty($widget_data)) {
1529 $widget = Plugin::instance()->elements_manager->create_element_instance( $widget_data );
1530 if ( $widget ) {
1531 $settings = $widget->get_settings_for_display();
1532 }
1533 }
1534
1535 }
1536 return $settings;
1537 }
1538
1539 public function delete_registration_options($widget_id)
1540 {
1541 delete_option('eael_register_success_' . $widget_id);
1542 delete_option('eael_register_errors_' . $widget_id);
1543 }
1544
1545 /**
1546 * Add extra custom fields on user profile (e.x. edit page and Registration form).
1547 * @param \WP_User $user
1548 *
1549 * @since 5.1.4
1550 */
1551 public function eael_extra_user_profile_fields( $user ){ ?>
1552 <?php $eael_custom_profile_fields_text = $this->get_eael_custom_profile_fields('text'); ?>
1553 <?php $eael_custom_profile_fields_image = $this->get_eael_custom_profile_fields('image'); ?>
1554
1555 <?php //if ( count( $eael_custom_profile_fields_text ) || count( $eael_custom_profile_fields_image ) ): ?>
1556 <h3><?php _e("EA Login | Register Form", "blank"); ?></h3>
1557 <?php // endif; ?>
1558
1559 <table class="form-table">
1560 <tr>
1561 <th><label for="eael_phone_number"><?php _e("Phone"); ?></label></th>
1562 <td>
1563 <input type="text" name="eael_phone_number" id="eael_phone_number" value="<?php echo esc_attr( get_the_author_meta( 'eael_phone_number', $user->ID ) ); ?>" class="regular-text" /><br />
1564 <p class="description"><?php esc_html_e("Please enter your phone number."); ?></p>
1565 </td>
1566 </tr>
1567 <?php
1568 if( count( $eael_custom_profile_fields_text ) ) :
1569 foreach( $eael_custom_profile_fields_text as $eael_custom_profile_field_text_key => $eael_custom_profile_field_value ) :
1570 ?>
1571 <tr>
1572 <th><label for="<?php echo esc_attr( $eael_custom_profile_field_text_key ); ?>"><?php _e( esc_html( $eael_custom_profile_field_value ) ); ?></label></th>
1573 <td>
1574 <input type="text" name="<?php echo esc_attr( $eael_custom_profile_field_text_key ); ?>" id="<?php echo esc_attr( $eael_custom_profile_field_text_key ); ?>" value="<?php echo esc_attr( get_the_author_meta( self::$eael_custom_profile_field_prefix . $eael_custom_profile_field_text_key, $user->ID ) ); ?>" class="regular-text" /><br />
1575 <!-- <p class="description"><?php //printf( __( "Please Enter %s", 'essential-addons-for-elementor-lite'), esc_html( $custom_profile_fields_text )); ?></p> -->
1576 </td>
1577 </tr>
1578 <?php
1579 endforeach;
1580 endif;
1581 ?>
1582
1583 <?php
1584 if( count( $eael_custom_profile_fields_image ) ) :
1585 foreach( $eael_custom_profile_fields_image as $eael_custom_profile_field_image_key => $eael_custom_profile_field_value ) :
1586 $user_meta_attachment_id = get_the_author_meta( self::$eael_custom_profile_field_prefix . $eael_custom_profile_field_image_key, $user->ID );
1587 ?>
1588 <tr>
1589 <th><label for="<?php echo esc_attr( $eael_custom_profile_field_image_key ); ?>"><?php _e( esc_html( $eael_custom_profile_field_value ) ); ?></label></th>
1590 <td>
1591 <input type="text" name="<?php echo esc_attr( $eael_custom_profile_field_image_key ); ?>" id="<?php echo esc_attr( $eael_custom_profile_field_image_key ); ?>" value="<?php echo esc_attr( $user_meta_attachment_id ); ?>" class="regular-text" /><br />
1592 <p class="description"><?php printf( __( "Above, input the %s of the attachment.", 'essential-addons-for-elementor-lite'), esc_html( 'ID' )); ?></p>
1593 <?php
1594 if( ! empty( $user_meta_attachment_id ) ){
1595 echo Helper::eael_wp_kses( wp_get_attachment_image($user_meta_attachment_id, 'thumbnail', 1) );
1596 }
1597 ?>
1598 </td>
1599 </tr>
1600 <?php
1601 endforeach;
1602 endif;
1603 ?>
1604 </table>
1605 <?php }
1606
1607 /**
1608 * Save extra custom fields of user profile
1609 * @param int $user_id
1610 *
1611 * @since 5.1.4
1612 */
1613 public function eael_save_extra_user_profile_fields( $user_id ){
1614 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
1615 return;
1616 }
1617
1618 if ( !current_user_can( 'edit_user', $user_id ) ) {
1619 return false;
1620 }
1621 update_user_meta( $user_id, 'eael_phone_number', sanitize_text_field( $_POST['eael_phone_number'] ) );
1622
1623 $eael_custom_profile_fields = $this->get_eael_custom_profile_fields('all');
1624
1625 if( count( $eael_custom_profile_fields ) ){
1626 foreach( $eael_custom_profile_fields as $eael_custom_profile_field_key => $eael_custom_profile_field_value ){
1627 if( empty( $_POST[ $eael_custom_profile_field_key ] ) ){
1628 continue;
1629 }
1630
1631 update_user_meta( $user_id, sanitize_key( self::$eael_custom_profile_field_prefix . $eael_custom_profile_field_key ), sanitize_text_field( $_POST[ $eael_custom_profile_field_key ] ) );
1632 }
1633 }
1634 }
1635
1636 public function eael_is_phone($phone){
1637 if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)\.]/', '', $phone ) ) ) ) {
1638 return false;
1639 }
1640
1641 if( strlen( str_replace(['+', '00', ' ', '(', ')', '-', '.', '_', '/'], '', $phone) ) === 0 ) {
1642 return false;
1643 }
1644
1645 //Phone number length can't be more than 15
1646 if( strlen( str_replace(['+', '00', ' ', '(', ')', '-', '.', '_', '/'], '', $phone) ) > 15 ) {
1647 return false;
1648 }
1649
1650 return true;
1651 }
1652
1653 public function eael_wp_login_url(){
1654 return apply_filters( 'eael/login-register/wp-login-url', wp_login_url() );
1655 }
1656
1657 public function get_eael_custom_profile_fields( $type = 'text' ){
1658 $eael_custom_profile_fields = [];
1659 $custom_profile_fields_arr = [];
1660
1661 $eael_custom_profile_field_text_trimmed = trim( get_option( 'eael_custom_profile_fields_text' ), ' ,\n\r\0\x0B' );
1662 $eael_custom_profile_field_image_trimmed = trim( get_option( 'eael_custom_profile_fields_img' ), ' ,\n\r\0\x0B' );
1663 $eael_custom_profile_field_text_trimmed = str_replace(self::$eael_custom_profile_field_prefix, '', $eael_custom_profile_field_text_trimmed);
1664 $eael_custom_profile_field_image_trimmed = str_replace(self::$eael_custom_profile_field_prefix, '', $eael_custom_profile_field_image_trimmed);
1665
1666 $custom_profile_fields_text_arr = ! empty ( $eael_custom_profile_field_text_trimmed ) ? array_unique( explode( ',', $eael_custom_profile_field_text_trimmed ) ) : [];
1667 $custom_profile_fields_img_arr = ! empty( $eael_custom_profile_field_image_trimmed ) ? array_unique( explode( ',', $eael_custom_profile_field_image_trimmed ) ) : [];
1668 $custom_profile_fields_all_arr = array_merge( $custom_profile_fields_text_arr, $custom_profile_fields_img_arr );
1669
1670 switch( $type ){
1671 case 'text':
1672 $custom_profile_fields_arr = $custom_profile_fields_text_arr;
1673 break;
1674
1675 case 'image':
1676 $custom_profile_fields_arr = $custom_profile_fields_img_arr;
1677 break;
1678
1679 case 'all':
1680 $custom_profile_fields_arr = $custom_profile_fields_all_arr;
1681 break;
1682
1683 default:
1684 break;
1685 }
1686
1687 if( count( $custom_profile_fields_arr ) ){
1688 foreach( $custom_profile_fields_arr as $custom_profile_field_text ){
1689 $custom_profile_field_slug = str_replace(' ', '_', trim( strtolower( sanitize_text_field( $custom_profile_field_text ) ), ' ' ));
1690 $eael_custom_profile_fields[ sanitize_text_field( $custom_profile_field_slug ) ] = __( esc_html( $custom_profile_field_text ), 'essential-addons-for-elementor-lite' );
1691 }
1692 }
1693
1694 return $eael_custom_profile_fields;
1695 }
1696
1697 }
1698