PluginProbe
Easy Elements for Elementor – Addons & Website Templates / 1.4.5
Easy Elements for Elementor – Addons & Website Templates v1.4.5
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.4.5, at widgets/login-register/class.login-register.php

190 lines 7.7 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 $user_login = !empty($_POST['user']) ? sanitize_user( wp_unslash( $_POST['user'] ) ) : '';
28 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
29 $user_pass = !empty($_POST['pwd']) ? wp_unslash( $_POST['pwd'] ) : '';
30 $remember = !empty($_POST['remember']);
31
32
33
34 $user = wp_authenticate( $user_login, $user_pass );
35
36 if ( is_wp_error( $user ) ) {
37 wp_send_json_error( ['msg' => $user->get_error_message()] );
38 }
39
40 wp_set_current_user( $user->ID );
41 wp_set_auth_cookie( $user->ID, $remember, is_ssl() );
42
43 wp_send_json_success();
44 }
45
46 /**
47 * Handle registration form submission
48 */
49 public function easyel_handle_register() {
50
51 $posted_nonce = '';
52 if ( ! empty( $_POST['eel_register_nonce'] ) ) {
53 $posted_nonce = sanitize_text_field( wp_unslash( $_POST['eel_register_nonce'] ) );
54 } elseif ( ! empty( $_POST['nonce'] ) ) {
55 $posted_nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ) );
56 }
57 if ( ! $posted_nonce || ! wp_verify_nonce( $posted_nonce, 'easy_elements_nonce' ) ) {
58 return wp_send_json_error( ['msg' => 'Security failed!'] );
59 }
60
61 if ( ! get_option( 'users_can_register' ) ) {
62 return wp_send_json_error( ['msg' => 'User registration is currently disabled.'] );
63 }
64
65 $custom_meta = !empty($_POST['custom_meta'])
66 ? map_deep( wp_unslash( $_POST['custom_meta'] ), 'sanitize_text_field' )
67 : [];
68
69 $consent = !empty($_POST['consent']) ? 'yes' : 'no';
70
71 $safe_role = $this->easyel_get_safe_registration_role();
72
73 $user_data = [
74 'user_login' => ! empty( $_POST['user_login'] )
75 ? sanitize_user( wp_unslash( $_POST['user_login'] ), true )
76 : '',
77 'user_email' => !empty($_POST['user_email']) ? sanitize_email( wp_unslash($_POST['user_email']) ) : '',
78 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
79 'user_pass' => !empty($_POST['user_pass']) ? wp_unslash($_POST['user_pass']) : '',
80 'role' => $safe_role,
81 'first_name' => !empty($_POST['first_name']) ? sanitize_text_field( wp_unslash($_POST['first_name']) ) : '',
82 'last_name' => !empty($_POST['last_name']) ? sanitize_text_field( wp_unslash($_POST['last_name']) ) : '',
83 'display_name' => !empty($_POST['display_name']) ? sanitize_text_field( wp_unslash($_POST['display_name']) ) : '',
84 'user_nicename' => !empty($_POST['user_nicename']) ? sanitize_text_field( wp_unslash($_POST['user_nicename']) ) : '',
85 'nickname' => !empty($_POST['nickname']) ? sanitize_text_field( wp_unslash($_POST['nickname']) ) : '',
86 'user_url' => !empty($_POST['user_url']) ? esc_url_raw( wp_unslash($_POST['user_url']) ) : '',
87 'description' => !empty($_POST['description']) ? sanitize_textarea_field( wp_unslash($_POST['description']) ) : '',
88 ];
89
90 $auto_login_raw = isset( $_POST['auto_login'] ) ? sanitize_text_field( wp_unslash( $_POST['auto_login'] ) ) : 'no';
91 if ( ! in_array( $auto_login_raw, [ 'yes', 'no' ], true ) ) {
92 return wp_send_json_error( ['msg' => 'Registration failed: invalid request.'] );
93 }
94 $auto_login = $auto_login_raw;
95
96 if ( empty( $user_data['user_login'] ) ) {
97 return wp_send_json_error( ['msg' => 'Username is required.'] );
98 }
99 if ( ! validate_username( $user_data['user_login'] ) ) {
100 return wp_send_json_error( ['msg' => 'Invalid username.'] );
101 }
102 if ( username_exists( $user_data['user_login'] ) ) {
103 return wp_send_json_error( ['msg' => 'Username already exists.'] );
104 }
105
106 if ( empty( $user_data['user_email'] ) || ! is_email( $user_data['user_email'] ) ) {
107 return wp_send_json_error( ['msg' => 'Invalid email address.'] );
108 }
109 if ( email_exists( $user_data['user_email'] ) ) {
110 return wp_send_json_error( ['msg' => 'Email already exists.'] );
111 }
112
113 if ( empty( $user_data['user_pass'] ) ) {
114 return wp_send_json_error( ['msg' => 'Password is required.'] );
115 }
116 if ( strlen( $user_data['user_pass'] ) < 8 ) {
117 return wp_send_json_error( ['msg' => 'Password must be at least 8 characters.'] );
118 }
119
120 $user_id = wp_insert_user( $user_data );
121
122 if ( is_wp_error( $user_id ) ) {
123 return wp_send_json_error( ['msg' => $user_id->get_error_message()] );
124 }
125
126 // Save meta
127 foreach ( $custom_meta as $key => $value ) {
128 update_user_meta( $user_id, $key, $value );
129 }
130
131 update_user_meta( $user_id, 'consent', $consent );
132
133 $msg = 'User created successfully';
134
135 // Auto login
136 if ( $auto_login === 'yes' ) {
137 wp_set_current_user( $user_id );
138 wp_set_auth_cookie( $user_id, true, is_ssl() );
139 }
140
141 return wp_send_json_success( ['msg' => $msg] );
142 }
143
144 /**
145 * Resolve a safe role for front-end registration.
146 *
147 * The role is never read from $_POST. It defaults to the site's configured
148 * default registration role and can be overridden server-side via the
149 * 'easyel_registration_role' filter. Any role that holds a privileged
150 * capability is rejected and replaced with 'subscriber'.
151 */
152 private function easyel_get_safe_registration_role() {
153 $role = get_option( 'default_role', 'subscriber' );
154
155 $role = apply_filters( 'easyel_registration_role', $role );
156
157 if ( ! is_string( $role ) || $role === '' ) {
158 return 'subscriber';
159 }
160
161 $role_obj = get_role( $role );
162 if ( ! $role_obj ) {
163 return 'subscriber';
164 }
165
166 $blocked_caps = [
167 'manage_options',
168 'promote_users',
169 'edit_users',
170 'create_users',
171 'delete_users',
172 'edit_others_posts',
173 'publish_posts',
174 'edit_published_posts',
175 'edit_theme_options',
176 'install_plugins',
177 'activate_plugins',
178 'unfiltered_html',
179 ];
180 foreach ( $blocked_caps as $cap ) {
181 if ( ! empty( $role_obj->capabilities[ $cap ] ) ) {
182 return 'subscriber';
183 }
184 }
185
186 return $role;
187 }
188
189 }
190 new Easyel_Login_Register();