PluginProbe
Easy Elements for Elementor – Addons & Website Templates / 1.5.2
Easy Elements for Elementor – Addons & Website Templates v1.5.2
1.5.3 1.5.2 1.5.1 1.5.0 1.4.9 1.4.6 1.4.7 1.4.8 1.4.5 1.4.4 1.4.3 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 All 47 releases
easy-elements / widgets / login-register / class.login-register.php

class.login-register.php in Easy Elements for Elementor – Addons & Website Templates 1.5.2, at widgets/login-register/class.login-register.php

285 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3
4 class Easyel_Login_Register {
5 public function __construct() {
6 add_action( 'wp_ajax_eel_login', [$this, 'easyel_handle_login'] );
7 add_action( 'wp_ajax_nopriv_eel_login', [$this, 'easyel_handle_login'] );
8 add_action( 'wp_ajax_eel_register', [$this, 'easyel_handle_register'] );
9 add_action( 'wp_ajax_nopriv_eel_register', [$this, 'easyel_handle_register'] );
10 }
11
12 /**
13 * Handle login form submission
14 */
15 public function easyel_handle_login() {
16
17 $posted_nonce = '';
18 if ( ! empty( $_POST['eel_login_nonce'] ) ) {
19 $posted_nonce = sanitize_text_field( wp_unslash( $_POST['eel_login_nonce'] ) );
20 } elseif ( ! empty( $_POST['nonce'] ) ) {
21 $posted_nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ) );
22 }
23 if ( ! $posted_nonce || ! wp_verify_nonce( $posted_nonce, 'easy_elements_nonce' ) ) {
24 return wp_send_json_error( ['msg' => 'security failed!'] );
25 }
26
27 if ( is_user_logged_in() ) {
28 return wp_send_json_error( ['msg' => 'You are already logged in.'] );
29 }
30
31 $user_login = !empty($_POST['user']) ? sanitize_user( wp_unslash( $_POST['user'] ) ) : '';
32 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
33 $user_pass = !empty($_POST['pwd']) ? wp_unslash( $_POST['pwd'] ) : '';
34 $remember = !empty($_POST['remember']);
35
36 $creds = [
37 'user_login' => $user_login,
38 'user_password' => $user_pass,
39 'remember' => $remember,
40 ];
41
42 $user = wp_signon( $creds, is_ssl() );
43
44 if ( is_wp_error( $user ) ) {
45
46 return wp_send_json_error( ['msg' => 'Invalid username or password.'] );
47 }
48
49 wp_set_current_user( $user->ID );
50
51 return wp_send_json_success();
52 }
53
54 /**
55 * Handle registration form submission
56 */
57 public function easyel_handle_register() {
58
59 $posted_nonce = '';
60 if ( ! empty( $_POST['eel_register_nonce'] ) ) {
61 $posted_nonce = sanitize_text_field( wp_unslash( $_POST['eel_register_nonce'] ) );
62 } elseif ( ! empty( $_POST['nonce'] ) ) {
63 $posted_nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ) );
64 }
65 if ( ! $posted_nonce || ! wp_verify_nonce( $posted_nonce, 'easy_elements_nonce' ) ) {
66 return wp_send_json_error( ['msg' => 'Security failed!'] );
67 }
68
69 if ( is_user_logged_in() ) {
70 return wp_send_json_error( ['msg' => 'You are already logged in.'] );
71 }
72
73 if ( ! get_option( 'users_can_register' ) ) {
74 return wp_send_json_error( ['msg' => 'User registration is currently disabled.'] );
75 }
76
77 /**
78 * Fires before the registration request is processed.
79 */
80 do_action( 'easyel/login-register/before-register' );
81
82 $custom_meta = !empty($_POST['custom_meta'])
83 ? map_deep( wp_unslash( $_POST['custom_meta'] ), 'sanitize_text_field' )
84 : [];
85
86 $easyel_consent = !empty($_POST['easyel_consent']) ? 'yes' : 'no';
87
88 $default_role = get_option( 'default_role', 'subscriber' );
89
90 $user_data = [
91 'user_login' => ! empty( $_POST['user_login'] )
92 ? sanitize_user( wp_unslash( $_POST['user_login'] ), true )
93 : '',
94 'user_email' => !empty($_POST['user_email']) ? sanitize_email( wp_unslash($_POST['user_email']) ) : '',
95 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
96 'user_pass' => !empty($_POST['user_pass']) ? wp_unslash($_POST['user_pass']) : '',
97 'role' => $default_role,
98 'first_name' => !empty($_POST['first_name']) ? sanitize_text_field( wp_unslash($_POST['first_name']) ) : '',
99 'last_name' => !empty($_POST['last_name']) ? sanitize_text_field( wp_unslash($_POST['last_name']) ) : '',
100 'display_name' => !empty($_POST['display_name']) ? sanitize_text_field( wp_unslash($_POST['display_name']) ) : '',
101 'user_nicename' => !empty($_POST['user_nicename']) ? sanitize_text_field( wp_unslash($_POST['user_nicename']) ) : '',
102 'nickname' => !empty($_POST['nickname']) ? sanitize_text_field( wp_unslash($_POST['nickname']) ) : '',
103 'user_url' => !empty($_POST['user_url']) ? esc_url_raw( wp_unslash($_POST['user_url']) ) : '',
104 'description' => !empty($_POST['description']) ? sanitize_textarea_field( wp_unslash($_POST['description']) ) : '',
105 ];
106
107 $auto_login_raw = isset( $_POST['auto_login'] ) ? sanitize_text_field( wp_unslash( $_POST['auto_login'] ) ) : 'no';
108
109 if ( ! in_array( $auto_login_raw, [ 'yes', 'no' ], true ) ) {
110 return wp_send_json_error( ['msg' => 'Registration failed: invalid request.'] );
111 }
112
113 $auto_login = $auto_login_raw;
114
115 $send_new_user_email = ( isset( $_POST['send_new_user_email'] ) && 'yes' === $_POST['send_new_user_email'] ) ? 'yes' : 'no';
116
117 $notify_admin_email = ( isset( $_POST['notify_admin_email'] ) && 'yes' === $_POST['notify_admin_email'] ) ? 'yes' : 'no';
118
119 if ( empty( $user_data['user_login'] ) ) {
120 return wp_send_json_error( ['msg' => 'Username is required.'] );
121 }
122 if ( ! validate_username( $user_data['user_login'] ) ) {
123 return wp_send_json_error( ['msg' => 'Invalid username.'] );
124 }
125 if ( username_exists( $user_data['user_login'] ) ) {
126 return wp_send_json_error( ['msg' => 'Username already exists.'] );
127 }
128
129 if ( empty( $user_data['user_email'] ) || ! is_email( $user_data['user_email'] ) ) {
130 return wp_send_json_error( ['msg' => 'Invalid email address.'] );
131 }
132 if ( email_exists( $user_data['user_email'] ) ) {
133 return wp_send_json_error( ['msg' => 'Email already exists.'] );
134 }
135
136 if ( empty( $user_data['user_pass'] ) ) {
137 return wp_send_json_error( ['msg' => 'Password is required.'] );
138 }
139 if ( strlen( $user_data['user_pass'] ) < 8 ) {
140 return wp_send_json_error( ['msg' => 'Password must be at least 8 characters.'] );
141 }
142
143 if ( isset( $_POST['confirm_password'] ) ) {
144 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
145 $confirm_password = wp_unslash( $_POST['confirm_password'] );
146 if ( ! hash_equals( $user_data['user_pass'], (string) $confirm_password ) ) {
147 return wp_send_json_error( ['msg' => 'Confirm password does not match.'] );
148 }
149 }
150
151 if ( isset( $_POST['eel_math_captcha_hash'] ) ) {
152 $captcha_answer = isset( $_POST['eel_math_captcha'] )
153 ? trim( sanitize_text_field( wp_unslash( $_POST['eel_math_captcha'] ) ) )
154 : '';
155 $captcha_hash = sanitize_text_field( wp_unslash( $_POST['eel_math_captcha_hash'] ) );
156
157 if ( '' === $captcha_answer || ! hash_equals( wp_hash( $captcha_answer ), $captcha_hash ) ) {
158 $captcha_error = ! empty( $_POST['math_captcha_error_msg'] )
159 ? sanitize_text_field( wp_unslash( $_POST['math_captcha_error_msg'] ) )
160 : 'Incorrect answer to the math question.';
161 return wp_send_json_error( ['msg' => $captcha_error] );
162 }
163 }
164
165 /**
166 * Filter the user data array right before it is inserted, so
167 * integrations can add/override fields
168 */
169 $user_data = apply_filters( 'easyel/login-register/new-user-data', $user_data );
170
171 $user_data['role'] = $default_role;
172
173 /**
174 * Fires immediately before the new user is created.
175 */
176 do_action( 'easyel/login-register/before-insert-user', $user_data );
177
178 $user_id = wp_insert_user( $user_data );
179
180 if ( is_wp_error( $user_id ) ) {
181 return wp_send_json_error( ['msg' => $user_id->get_error_message()] );
182 }
183
184 if ( is_array( $custom_meta ) ) {
185 foreach ( $custom_meta as $key => $value ) {
186 if ( $this->easyel_is_meta_key_allowed( $key ) ) {
187 update_user_meta( $user_id, $key, $value );
188 }
189 }
190 }
191
192 update_user_meta( $user_id, 'easyel_consent', $easyel_consent );
193
194 /**
195 * Fires after the new user and all of its meta have been stored
196 */
197 do_action( 'easyel/login-register/after-insert-user', $user_id, $user_data );
198
199 /**
200 * Fire WordPress core's `register_new_user` action so third-party and
201 * security plugins that hook the standard registration flow also run
202 * for this form.
203 *
204 * The core default notifier (`wp_send_new_user_notifications`) is
205 * detached first so it does not send a duplicate email — this handler
206 * sends its own notification below using the scope chosen in the widget.
207 */
208 remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
209 // This is a WordPress core hook, fired intentionally with its core name so listeners run; it cannot be prefixed.
210 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
211 do_action( 'register_new_user', $user_id );
212
213 $notify_scope = [];
214 if ( 'yes' === $send_new_user_email ) {
215 $notify_scope[] = 'user';
216 }
217 if ( 'yes' === $notify_admin_email ) {
218 $notify_scope[] = 'admin';
219 }
220 if ( ! empty( $notify_scope ) ) {
221 $scope = ( count( $notify_scope ) === 2 ) ? 'both' : $notify_scope[0];
222 wp_new_user_notification( $user_id, null, $scope );
223 }
224
225 $msg = 'User created successfully';
226
227 if ( $auto_login === 'yes' ) {
228 wp_set_current_user( $user_id );
229 wp_set_auth_cookie( $user_id, true, is_ssl() );
230 }
231
232 return wp_send_json_success( ['msg' => $msg] );
233 }
234
235 /**
236 * Decide whether a user meta key is safe to write from the public
237 * registration request.
238 *
239 * Blocks WordPress protected meta (keys beginning with "_"), capability /
240 * user-level / role / session meta, and anything that could be used to
241 * escalate privileges. Everything else (plain custom profile fields) is
242 * allowed.
243 *
244 * @param mixed $key The meta key coming from $_POST['custom_meta'].
245 * @return bool
246 */
247 private function easyel_is_meta_key_allowed( $key ) {
248
249 if ( ! is_string( $key ) || $key === '' ) {
250 return false;
251 }
252
253 // Reject WordPress protected meta (e.g. anything starting with "_").
254 if ( function_exists( 'is_protected_meta' ) && is_protected_meta( $key, 'user' ) ) {
255 return false;
256 }
257
258 $blocked_keys = [
259 'wp_capabilities',
260 'wp_user_level',
261 'session_tokens',
262 'role',
263 'roles',
264 'default_password_nag',
265 'user_status',
266 ];
267
268 if ( in_array( strtolower( $key ), $blocked_keys, true ) ) {
269 return false;
270 }
271
272 if ( preg_match( '/(^|_)(capabilities|user_level)$/i', $key ) ) {
273 return false;
274 }
275
276 if ( ! preg_match( '/^[A-Za-z0-9_]+$/', $key ) ) {
277 return false;
278 }
279
280 return true;
281 }
282
283 }
284
285 new Easyel_Login_Register();