PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.0
2.13.0 2.13.1 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 All 80 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.9.0, at includes/api/form-builder-controller.php

673 lines 15.7 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 $post_id = $params['current_post_id'];
179
180 if ( is_numeric( $post_id ) &&
181 ! current_user_can( 'manage_options' ) &&
182 get_post_status( $post_id ) !== 'publish'
183 ) {
184 return new WP_REST_Response(
185 [
186 'success' => false,
187 'data' => [ 'message' => __( 'Invalid post.', 'ablocks' ) ],
188 ],
189 400
190 );
191 }
192
193 $block_data = Helper::get_block_attributes(
194 $post_id,
195 $params['block_id'],
196 'ablocks/form-builder'
197 );
198
199 $redirect_url = '';
200
201 if ( $block_data['parentAttributes']['registerRedirect'] ?? false ) {
202 $redirect_url =
203 \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
204 $block_data['parentAttributes']['link']['href'] ?? '',
205 $block_data['parentAttributes']['link']['keyValue'] ?? '',
206 true
207 );
208 }
209
210 if ( ! get_option( 'users_can_register' ) ) {
211 return new WP_REST_Response(
212 [
213 'success' => false,
214 'data' => $this->prepare_res(
215 [ 'message' => __( 'User registration is turned off.', 'ablocks' ) ],
216 $block_data,
217 $redirect_url
218 ),
219 ],
220 403
221 );
222 }
223
224 if ( ! empty( $params['confirm_password'] ) && $params['password'] !== $params['confirm_password'] ) {
225 return new WP_REST_Response(
226 [
227 'success' => false,
228 'data' => $this->prepare_res(
229 [ 'message' => __( 'Passwords do not match.', 'ablocks' ) ],
230 $block_data,
231 $redirect_url
232 ),
233 ],
234 400
235 );
236 }
237
238 if ( username_exists( $params['username'] ) ) {
239 return new WP_REST_Response(
240 [
241 'success' => false,
242 'data' => $this->prepare_res(
243 [ 'message' => __( 'Username already exists.', 'ablocks' ) ],
244 $block_data,
245 $redirect_url
246 ),
247 ],
248 400
249 );
250 }
251
252 if ( email_exists( $params['email'] ) ) {
253 return new WP_REST_Response(
254 [
255 'success' => false,
256 'data' => $this->prepare_res(
257 [ 'message' => __( 'Email already exists.', 'ablocks' ) ],
258 $block_data,
259 $redirect_url
260 ),
261 ],
262 400
263 );
264 }
265
266 if ( strlen( $params['password'] ) < 6 ) {
267 return new WP_REST_Response(
268 [
269 'success' => false,
270 'data' => $this->prepare_res(
271 [ 'message' => __( 'Password must be at least 6 characters.', 'ablocks' ) ],
272 $block_data,
273 $redirect_url
274 ),
275 ],
276 400
277 );
278 }
279
280 $user_id = wp_create_user(
281 $params['username'],
282 $params['password'],
283 $params['email']
284 );
285
286 if ( is_wp_error( $user_id ) ) {
287 return new WP_REST_Response(
288 [
289 'success' => false,
290 'data' => $this->prepare_res(
291 [ 'message' => $user_id->get_error_message() ],
292 $block_data,
293 $redirect_url
294 ),
295 ],
296 400
297 );
298 }
299
300 // Assign role if defined
301 $role = $block_data['parentAttributes']['roleSlug'] ?? '';
302
303 if (
304 ! empty( $role ) &&
305 strtolower( $role ) !== 'default' &&
306 array_key_exists( $role, wp_roles()->roles ) &&
307 $this->is_safe_registration_role( $role )
308 ) {
309 ( new \WP_User( $user_id ) )->set_role( $role );
310 }
311
312 // Save custom fields
313 $reserved = [ 'username', 'email', 'password', 'current_post_id', 'block_id', 'confirm_password' ];
314 $custom = array_diff_key( $params, array_flip( $reserved ) );
315
316 foreach ( $custom as $key => $value ) {
317 update_user_meta(
318 $user_id,
319 'ablocks_' . sanitize_key( $key ),
320 sanitize_text_field( $value )
321 );
322 }
323
324 wp_set_current_user( $user_id );
325 wp_set_auth_cookie( $user_id );
326
327 if ( empty( $redirect_url ) ) {
328 $redirect_url = home_url( '/' );
329 }
330
331 return new WP_REST_Response(
332 [
333 'success' => true,
334 'data' => $this->prepare_res(
335 [
336 'message' => __( 'Registration completed successfully. Redirecting...', 'ablocks' ),
337 ],
338 $block_data,
339 $redirect_url
340 ),
341 ],
342 201
343 );
344 }
345
346 public function forget_password( WP_REST_Request $request ) {
347
348 $params = $request->get_params();
349
350 $block_data = Helper::get_block_attributes(
351 $params['current_post_id'],
352 $params['block_id'],
353 'ablocks/form-builder'
354 );
355
356 if ( empty( $block_data ) ) {
357 return new WP_REST_Response(
358 [
359 'success' => false,
360 'data' => [
361 'message' => __( 'Invalid form block.', 'ablocks' ),
362 ],
363 ],
364 400
365 );
366 }
367
368 $redirect_url = '';
369
370 if ( $block_data['parentAttributes']['registerRedirect'] ?? false ) {
371 $redirect_url = \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
372 $block_data['parentAttributes']['link']['href'] ?? '',
373 $block_data['parentAttributes']['link']['keyValue'] ?? '',
374 true
375 );
376 }
377
378 if ( empty( $params['email'] ?? '' ) ) {
379 return new WP_REST_Response(
380 [
381 'success' => false,
382 'data' => $this->prepare_res(
383 [ 'message' => __( 'Email field is required', 'ablocks' ) ],
384 $block_data,
385 $redirect_url
386 ),
387 ],
388 400
389 );
390 }
391
392 if ( ! is_email( $params['email'] ) ) {
393 return new WP_REST_Response(
394 [
395 'success' => false,
396 'data' => $this->prepare_res(
397 [ 'message' => __( 'Provide a valid email', 'ablocks' ) ],
398 $block_data,
399 $redirect_url
400 ),
401 ],
402 400
403 );
404 }
405
406 if ( ! email_exists( $params['email'] ) ) {
407 return new WP_REST_Response(
408 [
409 'success' => false,
410 'data' => $this->prepare_res(
411 [ 'message' => __( 'This email does not exist', 'ablocks' ) ],
412 $block_data,
413 $redirect_url
414 ),
415 ],
416 400
417 );
418 }
419
420 $result = retrieve_password( $params['email'] );
421
422 if ( is_wp_error( $result ) ) {
423 return new WP_REST_Response(
424 [
425 'success' => false,
426 'data' => $this->prepare_res(
427 [ 'message' => esc_html( $result->get_error_message() ) ],
428 $block_data,
429 $redirect_url
430 ),
431 ],
432 400
433 );
434 }
435
436 return new WP_REST_Response(
437 [
438 'success' => true,
439 'data' => $this->prepare_res(
440 [
441 'message' => __( 'Password reset email is sent', 'ablocks' ),
442 ],
443 $block_data,
444 $redirect_url
445 ),
446 ],
447 200
448 );
449 }
450
451
452 public function submit( WP_REST_Request $request ) {
453
454 $params = $request->get_params();
455
456 $block_data = Helper::get_block_attributes(
457 $params['current_post_id'],
458 $params['block_id'],
459 'ablocks/form-builder'
460 );
461
462 if ( empty( $block_data ) ) {
463 return new WP_Error(
464 'invalid_block',
465 __( 'Invalid form block.', 'ablocks' ),
466 [ 'status' => 400 ]
467 );
468 }
469
470 $fields_to_skip = [ 'current_post_id', 'block_id' ];
471 $all_fields = array_diff_key( $params, array_flip( $fields_to_skip ) );
472
473 $actions = apply_filters(
474 'ablocks/form_builder/actions',
475 [
476 \ABlocks\Blocks\FormBuilder\Actions\SendEmails::class,
477 \ABlocks\Blocks\FormBuilder\Actions\SaveFormData::class,
478 \ABlocks\Blocks\FormBuilder\Actions\SendEmail::class,
479 \ABlocks\Blocks\FormBuilder\Actions\Subscribe::class,
480 ]
481 );
482
483 $validate = new ValidateFormData( $block_data, $all_fields );
484 $validate->actions( $actions );
485
486 $output = $validate->get_output();
487 $output['afterFormSubmission'] = $block_data['parentAttributes']['afterFormSubmission'] ?? 'reset';
488 $output['confirmationType'] = $block_data['parentAttributes']['confirmationType'] ?? 'success';
489 $output['formType'] = $block_data['parentAttributes']['formType'] ?? '';
490 $output['redirect_url'] = $block_data['parentAttributes']['link']['href'] ?? '';
491 $output['link_target'] = $block_data['parentAttributes']['link']['linkTarget'] ?? '';
492 if ( $validate->has_error() ) {
493 $output['message'] = $validate->get_error_message();
494 wp_send_json_error( $output );
495 } elseif ( $validate->has_message() ) {
496 $output['confirmationNotice'] = $validate->apply_vars( $block_data['parentAttributes']['confirmationNotice'] ?? __( 'Form successfully submitted!', 'ablocks' ) );
497 $output['message'] = $validate->get_message();
498
499 wp_send_json_success( $output );
500 }
501
502 wp_send_json_error( [ 'message' => __( 'Action is not defined.', 'ablocks' ) ] );
503
504 if ( $validate->has_error() ) {
505 return new WP_Error(
506 'form_error',
507 $validate->get_error_message(),
508 [ 'status' => 400 ]
509 );
510 }
511
512 return new WP_REST_Response( $output, 200 );
513 }
514
515 private function login_schema() {
516 return [
517
518 'username' => [
519 'required' => true,
520 'type' => 'string',
521 'minLength' => 3,
522 'maxLength' => 60,
523 'sanitize_callback' => 'sanitize_user',
524 'validate_callback' => function( $value ) {
525 return validate_username( $value );
526 },
527 ],
528
529 'password' => [
530 'required' => true,
531 'type' => 'string',
532 'minLength' => 6,
533 'maxLength' => 128,
534 ],
535
536 'rememberme' => [
537 'required' => false,
538 'type' => 'boolean',
539 'sanitize_callback' => 'rest_sanitize_boolean',
540 ],
541
542 'current_post_id' => [
543 'required' => true,
544 'type' => 'integer',
545 'sanitize_callback' => 'absint',
546 'validate_callback' => function( $value ) {
547 return $value > 0 && get_post( $value );
548 },
549 ],
550
551 'block_id' => [
552 'required' => true,
553 'type' => 'string',
554 'sanitize_callback' => 'sanitize_text_field',
555 'validate_callback' => function( $value ) {
556 return preg_match( '/^[a-zA-Z0-9_\-]+$/', $value );
557 },
558 ],
559 ];
560 }
561
562
563 private function register_schema() {
564 return [
565
566 'username' => [
567 'required' => true,
568 'type' => 'string',
569 'minLength' => 3,
570 'maxLength' => 60,
571 'sanitize_callback' => 'sanitize_user',
572 'validate_callback' => function( $value ) {
573 return validate_username( $value );
574 },
575 ],
576
577 'email' => [
578 'required' => true,
579 'type' => 'string',
580 'sanitize_callback' => 'sanitize_email',
581 'validate_callback' => function( $value ) {
582 return is_email( $value );
583 },
584 ],
585
586 'password' => [
587 'required' => true,
588 'type' => 'string',
589 'minLength' => 6,
590 'maxLength' => 128,
591 'validate_callback' => function( $value ) {
592 return strlen( $value ) >= 6;
593 },
594 ],
595
596 'current_post_id' => [
597 'required' => true,
598 'type' => 'integer',
599 'sanitize_callback' => 'absint',
600 'validate_callback' => function( $value ) {
601 return $value > 0 && get_post( $value );
602 },
603 ],
604
605 'block_id' => [
606 'required' => true,
607 'type' => 'string',
608 'sanitize_callback' => 'sanitize_text_field',
609 'validate_callback' => function( $value ) {
610 return preg_match( '/^[a-zA-Z0-9_\-]+$/', $value );
611 },
612 ],
613 ];
614 }
615
616
617 private function forget_schema() {
618 return [
619 'email' => [
620 'required' => true,
621 'type' => 'string',
622 'sanitize_callback' => 'sanitize_email',
623 ],
624 'current_post_id' => [
625 'required' => true,
626 'type' => 'integer',
627 'sanitize_callback' => 'absint',
628 ],
629 'block_id' => [
630 'required' => true,
631 'type' => 'string',
632 'sanitize_callback' => 'sanitize_text_field',
633 ],
634 ];
635 }
636
637
638 private function is_safe_registration_role( string $role ) : bool {
639 $role_obj = get_role( $role );
640 if ( ! $role_obj ) {
641 return false;
642 }
643 $privileged_caps = [
644 'manage_options',
645 'edit_users',
646 'delete_users',
647 'create_users',
648 'promote_users',
649 ];
650 foreach ( $privileged_caps as $cap ) {
651 if ( ! empty( $role_obj->capabilities[ $cap ] ) ) {
652 return false;
653 }
654 }
655 return true;
656 }
657
658 private function submit_schema() {
659 return [
660 'current_post_id' => [
661 'required' => true,
662 'type' => 'string',
663 'sanitize_callback' => 'sanitize_text_field',
664 ],
665 'block_id' => [
666 'required' => true,
667 'type' => 'string',
668 'sanitize_callback' => 'sanitize_text_field',
669 ],
670 ];
671 }
672 }
673