PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.8.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.8.0
2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 All 78 releases
ablocks / includes / api / form-builder-controller.php

form-builder-controller.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.8.0, at includes/api/form-builder-controller.php

637 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\API;
4
5 use WP_REST_Request;
6 use WP_REST_Response;
7 use WP_Error;
8 use ABlocks\Helper;
9 use ABlocks\Blocks\FormBuilder\ValidateFormData;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 class FormBuilderController {
16
17 public function register_routes() {
18
19 register_rest_route(
20 ABLOCKS_REST_NAMESPACE,
21 '/form-builder/login',
22 [
23 'methods' => 'POST',
24 'callback' => [ $this, 'login' ],
25 'permission_callback' => '__return_true',
26 'args' => $this->login_schema(),
27 ]
28 );
29
30 register_rest_route(
31 ABLOCKS_REST_NAMESPACE,
32 '/form-builder/registration',
33 [
34 'methods' => 'POST',
35 'callback' => [ $this, 'register' ],
36 'permission_callback' => '__return_true',
37 'args' => $this->register_schema(),
38 ]
39 );
40
41 register_rest_route(
42 ABLOCKS_REST_NAMESPACE,
43 '/form-builder/forget_password',
44 [
45 'methods' => 'POST',
46 'callback' => [ $this, 'forget_password' ],
47 'permission_callback' => '__return_true',
48 'args' => $this->forget_schema(),
49 ]
50 );
51
52 register_rest_route(
53 ABLOCKS_REST_NAMESPACE,
54 '/form-builder/submit',
55 [
56 'methods' => 'POST',
57 'callback' => [ $this, 'submit' ],
58 'permission_callback' => '__return_true',
59 'args' => $this->submit_schema(),
60 ]
61 );
62
63 register_rest_route(
64 ABLOCKS_REST_NAMESPACE,
65 '/form-builder/subscription',
66 [
67 'methods' => 'POST',
68 'callback' => [ $this, 'submit' ],
69 'permission_callback' => '__return_true',
70 'args' => $this->submit_schema(),
71 ]
72 );
73 }
74
75 private function prepare_res( array $data, array $block_data, string $redirect_url ) : array {
76
77 $formType = $block_data['parentAttributes']['formType'] ?? '';
78
79 if ( $formType === 'login' ) {
80 $confirmationType = ( $block_data['parentAttributes']['loginRedirect'] ?? false )
81 ? 'redirect'
82 : 'success';
83
84 } elseif ( $formType === 'registration' ) {
85 $confirmationType = ( $block_data['parentAttributes']['registerRedirect'] ?? false )
86 ? 'redirect'
87 : 'success';
88
89 } else {
90 $confirmationType = $block_data['parentAttributes']['confirmationType'] ?? 'success';
91 }
92
93 return array_merge(
94 $data,
95 [
96 'afterFormSubmission' => $block_data['parentAttributes']['afterFormSubmission'] ?? 'reset',
97 'confirmationType' => $confirmationType,
98 'confirmationNotice' => $block_data['parentAttributes']['confirmationNotice']
99 ?? $data['message']
100 ?? __( 'Form successfully submitted!', 'ablocks' ),
101 'redirect_url' => esc_url( $redirect_url ),
102 'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '',
103 'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '',
104 'formType' => $formType,
105 ]
106 );
107 }
108
109 public function login( WP_REST_Request $request ) {
110
111 $params = $request->get_params();
112
113 $block_data = Helper::get_block_attributes(
114 $params['current_post_id'],
115 $params['block_id'],
116 'ablocks/form-builder'
117 );
118
119 $redirect_url = '';
120
121 if ( $block_data['parentAttributes']['loginRedirect'] ?? false ) {
122 $redirect_url =
123 \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
124 $block_data['parentAttributes']['link']['href'] ?? '',
125 $block_data['parentAttributes']['link']['keyValue'] ?? '',
126 true
127 );
128 }
129
130 $user = wp_signon(
131 [
132 'user_login' => $params['username'],
133 'user_password' => $params['password'],
134 'remember' => (bool) $params['rememberme'],
135 ],
136 is_ssl()
137 );
138
139 if ( is_wp_error( $user ) ) {
140 return new WP_REST_Response(
141 [
142 'success' => false,
143 'data' => $this->prepare_res(
144 [ 'message' => $user->get_error_message() ],
145 $block_data,
146 $redirect_url
147 ),
148 ],
149 400
150 );
151 }
152
153 wp_set_current_user( $user->ID );
154
155 if ( empty( $redirect_url ) ) {
156 $redirect_url = home_url( '/' );
157 }
158
159 return new WP_REST_Response(
160 [
161 'success' => true,
162 'data' => $this->prepare_res(
163 [
164 'message' => __( 'You have logged in successfully. Redirecting...', 'ablocks' ),
165 ],
166 $block_data,
167 $redirect_url
168 ),
169 ],
170 200
171 );
172 }
173
174 public function register( WP_REST_Request $request ) {
175
176 $params = $request->get_params();
177
178 $block_data = Helper::get_block_attributes(
179 $params['current_post_id'],
180 $params['block_id'],
181 'ablocks/form-builder'
182 );
183
184 $redirect_url = '';
185
186 if ( $block_data['parentAttributes']['registerRedirect'] ?? false ) {
187 $redirect_url =
188 \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
189 $block_data['parentAttributes']['link']['href'] ?? '',
190 $block_data['parentAttributes']['link']['keyValue'] ?? '',
191 true
192 );
193 }
194
195 if ( ! get_option( 'users_can_register' ) ) {
196 return new WP_REST_Response(
197 [
198 'success' => false,
199 'data' => $this->prepare_res(
200 [ 'message' => __( 'User registration is turned off.', 'ablocks' ) ],
201 $block_data,
202 $redirect_url
203 ),
204 ],
205 403
206 );
207 }
208
209 if ( ! empty( $params['confirm_password'] ) && $params['password'] !== $params['confirm_password'] ) {
210 return new WP_REST_Response(
211 [
212 'success' => false,
213 'data' => $this->prepare_res(
214 [ 'message' => __( 'Passwords do not match.', 'ablocks' ) ],
215 $block_data,
216 $redirect_url
217 ),
218 ],
219 400
220 );
221 }
222
223 if ( username_exists( $params['username'] ) ) {
224 return new WP_REST_Response(
225 [
226 'success' => false,
227 'data' => $this->prepare_res(
228 [ 'message' => __( 'Username already exists.', 'ablocks' ) ],
229 $block_data,
230 $redirect_url
231 ),
232 ],
233 400
234 );
235 }
236
237 if ( email_exists( $params['email'] ) ) {
238 return new WP_REST_Response(
239 [
240 'success' => false,
241 'data' => $this->prepare_res(
242 [ 'message' => __( 'Email already exists.', 'ablocks' ) ],
243 $block_data,
244 $redirect_url
245 ),
246 ],
247 400
248 );
249 }
250
251 if ( strlen( $params['password'] ) < 6 ) {
252 return new WP_REST_Response(
253 [
254 'success' => false,
255 'data' => $this->prepare_res(
256 [ 'message' => __( 'Password must be at least 6 characters.', 'ablocks' ) ],
257 $block_data,
258 $redirect_url
259 ),
260 ],
261 400
262 );
263 }
264
265 $user_id = wp_create_user(
266 $params['username'],
267 $params['password'],
268 $params['email']
269 );
270
271 if ( is_wp_error( $user_id ) ) {
272 return new WP_REST_Response(
273 [
274 'success' => false,
275 'data' => $this->prepare_res(
276 [ 'message' => $user_id->get_error_message() ],
277 $block_data,
278 $redirect_url
279 ),
280 ],
281 400
282 );
283 }
284
285 // Assign role if defined
286 $role = $block_data['parentAttributes']['roleSlug'] ?? '';
287
288 if (
289 ! empty( $role ) &&
290 strtolower( $role ) !== 'default' &&
291 array_key_exists( $role, wp_roles()->roles )
292 ) {
293 ( new \WP_User( $user_id ) )->set_role( $role );
294 }
295
296 // Save custom fields
297 $reserved = [ 'username', 'email', 'password', 'current_post_id', 'block_id', 'confirm_password' ];
298 $custom = array_diff_key( $params, array_flip( $reserved ) );
299
300 foreach ( $custom as $key => $value ) {
301 update_user_meta(
302 $user_id,
303 'ablocks_' . sanitize_key( $key ),
304 sanitize_text_field( $value )
305 );
306 }
307
308 wp_set_current_user( $user_id );
309 wp_set_auth_cookie( $user_id );
310
311 if ( empty( $redirect_url ) ) {
312 $redirect_url = home_url( '/' );
313 }
314
315 return new WP_REST_Response(
316 [
317 'success' => true,
318 'data' => $this->prepare_res(
319 [
320 'message' => __( 'Registration completed successfully. Redirecting...', 'ablocks' ),
321 ],
322 $block_data,
323 $redirect_url
324 ),
325 ],
326 201
327 );
328 }
329
330 public function forget_password( WP_REST_Request $request ) {
331
332 $params = $request->get_params();
333
334 $block_data = Helper::get_block_attributes(
335 $params['current_post_id'],
336 $params['block_id'],
337 'ablocks/form-builder'
338 );
339
340 if ( empty( $block_data ) ) {
341 return new WP_REST_Response(
342 [
343 'success' => false,
344 'data' => [
345 'message' => __( 'Invalid form block.', 'ablocks' ),
346 ],
347 ],
348 400
349 );
350 }
351
352 $redirect_url = '';
353
354 if ( $block_data['parentAttributes']['registerRedirect'] ?? false ) {
355 $redirect_url = \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
356 $block_data['parentAttributes']['link']['href'] ?? '',
357 $block_data['parentAttributes']['link']['keyValue'] ?? '',
358 true
359 );
360 }
361
362 if ( empty( $params['email'] ?? '' ) ) {
363 return new WP_REST_Response(
364 [
365 'success' => false,
366 'data' => $this->prepare_res(
367 [ 'message' => __( 'Email field is required', 'ablocks' ) ],
368 $block_data,
369 $redirect_url
370 ),
371 ],
372 400
373 );
374 }
375
376 if ( ! is_email( $params['email'] ) ) {
377 return new WP_REST_Response(
378 [
379 'success' => false,
380 'data' => $this->prepare_res(
381 [ 'message' => __( 'Provide a valid email', 'ablocks' ) ],
382 $block_data,
383 $redirect_url
384 ),
385 ],
386 400
387 );
388 }
389
390 if ( ! email_exists( $params['email'] ) ) {
391 return new WP_REST_Response(
392 [
393 'success' => false,
394 'data' => $this->prepare_res(
395 [ 'message' => __( 'This email does not exist', 'ablocks' ) ],
396 $block_data,
397 $redirect_url
398 ),
399 ],
400 400
401 );
402 }
403
404 $result = retrieve_password( $params['email'] );
405
406 if ( is_wp_error( $result ) ) {
407 return new WP_REST_Response(
408 [
409 'success' => false,
410 'data' => $this->prepare_res(
411 [ 'message' => esc_html( $result->get_error_message() ) ],
412 $block_data,
413 $redirect_url
414 ),
415 ],
416 400
417 );
418 }
419
420 return new WP_REST_Response(
421 [
422 'success' => true,
423 'data' => $this->prepare_res(
424 [
425 'message' => __( 'Password reset email is sent', 'ablocks' ),
426 ],
427 $block_data,
428 $redirect_url
429 ),
430 ],
431 200
432 );
433 }
434
435
436 public function submit( WP_REST_Request $request ) {
437
438 $params = $request->get_params();
439
440 $block_data = Helper::get_block_attributes(
441 $params['current_post_id'],
442 $params['block_id'],
443 'ablocks/form-builder'
444 );
445
446 if ( empty( $block_data ) ) {
447 return new WP_Error(
448 'invalid_block',
449 __( 'Invalid form block.', 'ablocks' ),
450 [ 'status' => 400 ]
451 );
452 }
453
454 $fields_to_skip = [ 'current_post_id', 'block_id' ];
455 $all_fields = array_diff_key( $params, array_flip( $fields_to_skip ) );
456
457 $actions = apply_filters(
458 'ablocks/form_builder/actions',
459 [
460 \ABlocks\Blocks\FormBuilder\Actions\SendEmails::class,
461 \ABlocks\Blocks\FormBuilder\Actions\SaveFormData::class,
462 \ABlocks\Blocks\FormBuilder\Actions\SendEmail::class,
463 \ABlocks\Blocks\FormBuilder\Actions\Subscribe::class,
464 ]
465 );
466
467 $validate = new ValidateFormData( $block_data, $all_fields );
468 $validate->actions( $actions );
469
470 $output = $validate->get_output();
471 $output['afterFormSubmission'] = $block_data['parentAttributes']['afterFormSubmission'] ?? 'reset';
472 $output['confirmationType'] = $block_data['parentAttributes']['confirmationType'] ?? 'success';
473 $output['formType'] = $block_data['parentAttributes']['formType'] ?? '';
474 $output['redirect_url'] = $block_data['parentAttributes']['link']['href'] ?? '';
475 $output['link_target'] = $block_data['parentAttributes']['link']['linkTarget'] ?? '';
476 if ( $validate->has_error() ) {
477 $output['message'] = $validate->get_error_message();
478 wp_send_json_error( $output );
479 } elseif ( $validate->has_message() ) {
480 $output['confirmationNotice'] = $validate->apply_vars( $block_data['parentAttributes']['confirmationNotice'] ?? __( 'Form successfully submitted!', 'ablocks' ) );
481 $output['message'] = $validate->get_message();
482
483 wp_send_json_success( $output );
484 }
485
486 wp_send_json_error( [ 'message' => __( 'Action is not defined.', 'ablocks' ) ] );
487
488 if ( $validate->has_error() ) {
489 return new WP_Error(
490 'form_error',
491 $validate->get_error_message(),
492 [ 'status' => 400 ]
493 );
494 }
495
496 return new WP_REST_Response( $output, 200 );
497 }
498
499 private function login_schema() {
500 return [
501
502 'username' => [
503 'required' => true,
504 'type' => 'string',
505 'minLength' => 3,
506 'maxLength' => 60,
507 'sanitize_callback' => 'sanitize_user',
508 'validate_callback' => function( $value ) {
509 return validate_username( $value );
510 },
511 ],
512
513 'password' => [
514 'required' => true,
515 'type' => 'string',
516 'minLength' => 6,
517 'maxLength' => 128,
518 ],
519
520 'rememberme' => [
521 'required' => false,
522 'type' => 'boolean',
523 'sanitize_callback' => 'rest_sanitize_boolean',
524 ],
525
526 'current_post_id' => [
527 'required' => true,
528 'type' => 'integer',
529 'sanitize_callback' => 'absint',
530 'validate_callback' => function( $value ) {
531 return $value > 0 && get_post( $value );
532 },
533 ],
534
535 'block_id' => [
536 'required' => true,
537 'type' => 'string',
538 'sanitize_callback' => 'sanitize_text_field',
539 'validate_callback' => function( $value ) {
540 return preg_match( '/^[a-zA-Z0-9_\-]+$/', $value );
541 },
542 ],
543 ];
544 }
545
546
547 private function register_schema() {
548 return [
549
550 'username' => [
551 'required' => true,
552 'type' => 'string',
553 'minLength' => 3,
554 'maxLength' => 60,
555 'sanitize_callback' => 'sanitize_user',
556 'validate_callback' => function( $value ) {
557 return validate_username( $value );
558 },
559 ],
560
561 'email' => [
562 'required' => true,
563 'type' => 'string',
564 'sanitize_callback' => 'sanitize_email',
565 'validate_callback' => function( $value ) {
566 return is_email( $value );
567 },
568 ],
569
570 'password' => [
571 'required' => true,
572 'type' => 'string',
573 'minLength' => 6,
574 'maxLength' => 128,
575 'validate_callback' => function( $value ) {
576 return strlen( $value ) >= 6;
577 },
578 ],
579
580 'current_post_id' => [
581 'required' => true,
582 'type' => 'integer',
583 'sanitize_callback' => 'absint',
584 'validate_callback' => function( $value ) {
585 return $value > 0 && get_post( $value );
586 },
587 ],
588
589 'block_id' => [
590 'required' => true,
591 'type' => 'string',
592 'sanitize_callback' => 'sanitize_text_field',
593 'validate_callback' => function( $value ) {
594 return preg_match( '/^[a-zA-Z0-9_\-]+$/', $value );
595 },
596 ],
597 ];
598 }
599
600
601 private function forget_schema() {
602 return [
603 'email' => [
604 'required' => true,
605 'type' => 'string',
606 'sanitize_callback' => 'sanitize_email',
607 ],
608 'current_post_id' => [
609 'required' => true,
610 'type' => 'integer',
611 'sanitize_callback' => 'absint',
612 ],
613 'block_id' => [
614 'required' => true,
615 'type' => 'string',
616 'sanitize_callback' => 'sanitize_text_field',
617 ],
618 ];
619 }
620
621
622 private function submit_schema() {
623 return [
624 'current_post_id' => [
625 'required' => true,
626 'type' => 'string',
627 'sanitize_callback' => 'sanitize_text_field',
628 ],
629 'block_id' => [
630 'required' => true,
631 'type' => 'string',
632 'sanitize_callback' => 'sanitize_text_field',
633 ],
634 ];
635 }
636 }
637