PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.4.2
JetFormBuilder — Dynamic Blocks Form Builder v3.6.4.2
3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / actions-v2 / register-user / register-user-action.php
jetformbuilder / modules / actions-v2 / register-user Last commit date
assets 2 months ago messages 1 year ago register-user-action.php 2 months ago register-user.php 1 year ago
register-user-action.php
313 lines
1 <?php
2
3 namespace JFB_Modules\Actions_V2\Register_User;
4
5 use Jet_Form_Builder\Actions\Action_Handler;
6 use JFB_Modules\Actions_V2\Update_User\Properties\User_Meta_Property;
7 use Jet_Form_Builder\Actions\Types\Base;
8 use Jet_Form_Builder\Classes\Tools;
9 use Jet_Form_Builder\Classes\Value_Normalizers\Single_Value_As_Array;
10 use Jet_Form_Builder\Exceptions\Action_Exception;
11
12 /**
13 * Define Base_Type class
14 */
15 class Register_User_Action extends Base {
16
17 public function get_name() {
18 return __( 'Register User', 'jet-form-builder' );
19 }
20
21 public function get_id() {
22 return 'register_user';
23 }
24
25 /**
26 * @throws Action_Exception
27 */
28 public function can_register() {
29 $allow_register = $this->settings['allow_register'] ?? false;
30 $role_can_register = $this->settings['role_can_register'] ?? false;
31
32 if ( $allow_register && ! $role_can_register ) {
33 throw new Action_Exception( 'failed', '`role_can_register` is empty' );
34 }
35
36 if ( is_user_logged_in() ) {
37 $user = wp_get_current_user();
38
39 if ( $allow_register && ! in_array( $role_can_register, $user->roles, true ) ) {
40 throw new Action_Exception( 'not_enough_cap' );
41 }
42
43 if ( ! $allow_register ) {
44 if ( isset( $this->settings['add_user_id'] ) && $this->settings['add_user_id'] ) {
45 jet_fb_action_handler()->response_data['user_id'] = (int) $user->ID;
46 jet_fb_context()->update_request( $user->ID, 'user_id' );
47 return false;
48 }
49
50 throw new Action_Exception( 'already_logged_in' );
51 }
52 } elseif ( $allow_register ) {
53 throw new Action_Exception( 'not_logged_in' );
54 }
55
56 return true;
57 }
58
59 /**
60 * @param array $request
61 * @param Action_Handler $handler
62 *
63 * @return mixed|void
64 * @throws Action_Exception
65 */
66 public function do_action( array $request, Action_Handler $handler ) {
67 if ( ! $this->can_register() ) {
68 return;
69 }
70
71 $fields_map = ! empty( $this->settings['fields_map'] ) ? $this->settings['fields_map'] : array();
72
73 // Prepare fields
74 $username = false;
75 $email = false;
76 $password = false;
77 $fname = false;
78 $lname = false;
79 $user_url = false;
80 $remember_field = $this->settings['remember_me_field'] ?? '';
81 $is_remember = ! empty( $request[ $remember_field ] );
82
83 // If fields map for login, password or email is not set - abort but allow submit form (its not user fault)
84 if ( empty( $fields_map['login'] ) || empty( $fields_map['email'] ) || empty( $fields_map['password'] ) ) {
85 return;
86 }
87
88 /**
89 * Validate username
90 */
91 $raw_username = ! empty( $request[ $fields_map['login'] ] ) ? $request[ $fields_map['login'] ] : false;
92
93 if ( ! $raw_username ) {
94 throw new Action_Exception( 'empty_username' );
95 }
96
97 $username = sanitize_user( $raw_username );
98
99 if ( $username !== $raw_username ) {
100 throw new Action_Exception( 'sanitize_user' );
101 }
102
103 if ( username_exists( $username ) ) {
104 throw new Action_Exception( 'username_exists' );
105 }
106 // username - ok
107
108 /**
109 * Validate email
110 */
111 $raw_email = ! empty( $request[ $fields_map['email'] ] ) ? $request[ $fields_map['email'] ] : false;
112
113 if ( ! $raw_email ) {
114 throw new Action_Exception( 'empty_email' );
115 }
116
117 $email = sanitize_email( $raw_email );
118
119 if ( $email !== $raw_email ) {
120 throw new Action_Exception( 'empty_email' );
121 }
122
123 if ( email_exists( $email ) ) {
124
125 throw new Action_Exception( 'email_exists' );
126 }
127 // email - ok
128
129 /**
130 * Validate password
131 */
132 $password = ! empty( $request[ $fields_map['password'] ] ) ? $request[ $fields_map['password'] ] : false;
133
134 if ( ! $password ) {
135 throw new Action_Exception( 'empty_password' );
136 }
137
138 $is_password_hashed = Tools::is_wp_password_hash( $password );
139
140 if ( ! empty( $fields_map['confirm_password'] ) ) {
141 // Skip password validation for GATEWAY.SUCCESS event - passwords were already validated during form submission
142 $is_gateway_success = jet_fb_events()->is_current( 'GATEWAY.SUCCESS' );
143 $is_gateway_failed = jet_fb_events()->is_current( 'GATEWAY.FAILED' );
144
145 if ( $is_gateway_success || $is_gateway_failed || $is_password_hashed ) {
146 $confirm_password = $password;
147 } else {
148 $confirm_password = ! empty( $request[ $fields_map['confirm_password'] ] ) ? $request[ $fields_map['confirm_password'] ] : false;
149
150 if ( $confirm_password !== $password ) {
151 throw new Action_Exception( 'password_mismatch' );
152 }
153 }
154 }
155 // password - ok
156 if ( ! empty( $fields_map['first_name'] ) ) {
157 $fname = ! empty( $request[ $fields_map['first_name'] ] ) ? $request[ $fields_map['first_name'] ] : false;
158 }
159
160 if ( ! empty( $fields_map['last_name'] ) ) {
161 $lname = ! empty( $request[ $fields_map['last_name'] ] ) ? $request[ $fields_map['last_name'] ] : false;
162 }
163
164 if ( ! empty( $fields_map['user_url'] ) ) {
165 $user_url = ! empty( $request[ $fields_map['user_url'] ] ) ? $request[ $fields_map['user_url'] ] : false;
166 }
167
168 $metafields_map = ! empty( $this->settings['meta_fields_map'] ) ? $this->settings['meta_fields_map'] : array();
169 $single_value_as_array = Single_Value_As_Array::prepare_flags(
170 $this->settings['single_value_as_array'] ?? array()
171 );
172
173 $metadata = array();
174
175 if ( ! empty( $metafields_map ) ) {
176 foreach ( $metafields_map as $form_field => $meta_field ) {
177 /**
178 * We need this because WordPress automatically use this on insert to the database
179 */
180 $meta_field = remove_accents( $meta_field );
181 /**
182 * @since 3.1.6
183 */
184 if ( in_array( $meta_field, User_Meta_Property::get_restricted_keys(), true ) ) {
185 continue;
186 }
187 if ( array_key_exists( $form_field, $request ) ) {
188 $metadata[ $meta_field ] = Single_Value_As_Array::maybe_wrap(
189 $request[ $form_field ],
190 $form_field,
191 $single_value_as_array
192 );
193 }
194 }
195 }
196
197 $userarr = array(
198 'user_pass' => $password,
199 'user_login' => $username,
200 'user_email' => $email,
201 'first_name' => $fname,
202 'last_name' => $lname,
203 'user_url' => $user_url,
204 );
205
206 $user_roles = isset( $this->settings['user_role'] ) ? Tools::get_array_of_user_roles( $this->settings['user_role'] ) : '';
207
208 if ( ! empty( $user_roles ) ) {
209 $main_role = Tools::get_main_user_role_by_priority( $user_roles );
210 if ( ! empty( $main_role ) ) {
211 $userarr['role'] = $main_role;
212 }
213 }
214
215 $user_id = wp_insert_user( $userarr );
216
217 if ( ! is_wp_error( $user_id ) ) {
218
219 if ( ! empty( $user_roles ) ) {
220 $user = new \WP_User( $user_id );
221 foreach ( $user_roles as $role ) {
222 if ( $role !== $main_role && 'administrator' !== $role ) {
223 $user->add_role( $role );
224 }
225 }
226 }
227
228 if ( $is_password_hashed ) {
229 global $wpdb;
230
231 $wpdb->update(
232 $wpdb->users,
233 array( 'user_pass' => $password ),
234 array( 'ID' => $user_id ),
235 array( '%s' ),
236 array( '%d' )
237 );
238 }
239
240 if ( ! empty( $metadata ) ) {
241 foreach ( $metadata as $meta_key => $meta_value ) {
242
243 if ( $this->is_repeater_val( $meta_value ) ) {
244
245 $prepared_value = array();
246
247 foreach ( $meta_value as $index => $row ) {
248
249 $prepared_row = array();
250
251 foreach ( $row as $item_key => $item_value ) {
252
253 $item_key = ! empty( $metafields_map[ $item_key ] ) ? Tools::sanitize_text_field( $metafields_map[ $item_key ] ) : $item_key;
254
255 $prepared_row[ $item_key ] = $item_value;
256 }
257
258 $prepared_value[ 'item-' . $index ] = $prepared_row;
259 }
260
261 $meta_value = $prepared_value;
262 }
263
264 update_user_meta( $user_id, $meta_key, $meta_value );
265 }
266 }
267
268 if ( ! empty( $this->settings['log_in'] ) ) {
269
270 if ( $is_password_hashed ) {
271 // Clear user cache to ensure fresh data
272 clean_user_cache( $user_id );
273
274 // Get fresh user object
275 $user_obj = get_userdata( $user_id );
276
277 if ( $user_obj ) {
278 // Set current user first
279 wp_set_current_user( $user_id, $user_obj->user_login );
280
281 wp_set_auth_cookie( $user_id, $is_remember, is_ssl() );
282 // Trigger wp_login action for compatibility with other plugins
283 do_action( 'wp_login', $user_obj->user_login, $user_obj );
284 }
285 } else {
286 $user = wp_signon(
287 array(
288 'user_login' => $username,
289 'user_password' => $password,
290 'remember' => $is_remember,
291 )
292 );
293
294 if ( ! is_wp_error( $user ) ) {
295 wp_set_current_user( $user->ID );
296 }
297 }
298 }
299
300 if ( ! empty( $this->settings['add_user_id'] ) && $this->settings['add_user_id'] ) {
301 $handler->response_data['user_id'] = $user_id;
302 jet_fb_context()->update_request( $user_id, 'user_id' );
303 }
304 } else {
305 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
306 throw new Action_Exception( 'failed', $userarr );
307 }
308 }
309
310
311
312 }
313