PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
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 / ajax / form-builder.php

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

340 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Ajax;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use ABlocks\Classes\AbstractAjaxHandler;
10 use ABlocks\Helper;
11 use ABlocks\Classes\Sanitizer;
12
13 use ABlocks\Blocks\FormBuilder\ValidateFormData;
14 use ABlocks\Blocks\FormBuilder\Actions\SaveFormData;
15 use ABlocks\Blocks\FormBuilder\Actions\SendEmail;
16 use ABlocks\Blocks\FormBuilder\Actions\Subscribe;
17
18 use ABlocks\Blocks\FormBuilder\Subscribe\Mailchimp;
19 use Exception;
20
21 class FormBuilder extends AbstractAjaxHandler {
22 public function __construct() {
23 $this->actions = array(
24 'form_builder_login_handler' => array(
25 'callback' => array( $this, 'form_builder_login_handler' ),
26 'allow_visitor_action' => true,
27 'fields' => array(
28 'username' => 'string',
29 'password' => 'string',
30 'rememberme' => 'string',
31 'current_post_id' => 'integer',
32 'block_id' => 'string',
33 ),
34 ),
35 'form_builder_registration_handler' => array(
36 'callback' => array( $this, 'form_builder_registration_handler' ),
37 'allow_visitor_action' => true,
38 'fields' => array(
39 'current_post_id' => 'integer',
40 'block_id' => 'string',
41 'username' => 'string',
42 'email' => 'email',
43 'password' => 'string',
44 'confirm_password' => 'string',
45 'current_post_id' => 'integer',
46 )
47 ),
48 'form_builder_forget_password_handler' => array(
49 'callback' => array( $this, 'form_builder_forget_password_handler' ),
50 'allow_visitor_action' => true,
51 'fields' => array(
52 'email' => 'string',
53 )
54 ),
55 'form_builder_submit_handler' => array(
56 'callback' => array( $this, 'handle_form_submission' ),
57 'allow_visitor_action' => true,
58 'fields' => array(
59 'current_post_id' => 'string',
60 'block_id' => 'string',
61 )
62 ),
63 'form_builder_action_setting_data' => array(
64 'callback' => array( $this, 'action_setting_data' ),
65 'fields' => array(
66 'type' => 'string',
67 'api' => 'string',
68 'list_id' => 'string',
69 )
70 ),
71 'registration_form_setting_data' => array(
72 'callback' => array( $this, 'registration_form_setting_data' ),
73 'capability' => 'edit_posts'
74 ),
75 );
76
77 }
78 public function form_builder_login_handler( $payload ) {
79 $block_data = Helper::get_block_attributes( $payload['current_post_id'], $payload['block_id'], 'ablocks/form-builder' );
80
81 $redirect_url = '';
82 if ( $block_data['parentAttributes']['loginRedirect'] ?? false ) {
83 $redirect_url = \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
84 $block_data['parentAttributes']['link']['href'] ?? '',
85 $block_data['parentAttributes']['link']['keyValue'] ?? '',
86 true
87 );
88 }
89
90 $username = $payload['username'];
91 $password = $payload['password'];
92 $remember = isset( $payload['rememberme'] ) && $payload['rememberme'] === 'on';
93
94 $secure_cookie = is_ssl();
95 $user_signon = wp_signon( array(
96 'user_login' => $username,
97 'user_password' => $password,
98 'remember' => $remember,
99 ), $secure_cookie );
100
101 if ( is_wp_error( $user_signon ) ) {
102 wp_send_json_error( [ 'message' => $user_signon->get_error_message() ] );
103 }
104
105 wp_set_current_user( $user_signon->ID );
106
107 if ( empty( $redirect_url ) ) {
108 $redirect_url = home_url( '/' );
109 }
110 wp_send_json_success([
111 'message' => __( 'You have logged in successfully. Redirecting...', 'ablocks' ),
112 'redirect_url' => esc_url( $redirect_url ),
113 'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '',
114 'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '',
115 ]);
116 }
117
118 public function registration_form_setting_data( $payload ) {
119 $roles = [
120 'default' => __( 'Default', 'ablocks' )
121 ];
122
123 if ( current_user_can( 'manage_options' ) ) {
124 foreach ( wp_roles()->roles ?? [] as $slug => ['name' => $name] ) {
125 $roles[ $slug ] = $name;
126 }
127 }
128 wp_send_json_success( $roles );
129 }
130 public function form_builder_registration_handler( $payload ) {
131 $block_data = Helper::get_block_attributes( $payload['current_post_id'], $payload['block_id'], 'ablocks/form-builder' );
132
133 if ( ! get_option( 'users_can_register' ) ) {
134 wp_send_json_error([
135 'message' => __( 'User registration is turned off. Turn it on to allow user registration.', 'ablocks' )
136 ]);
137 }
138
139 if ( ( $block_data['parentAttributes']['formType'] ?? '' ) !== 'registration' ) {
140 wp_send_json_error([
141 'message' => __( 'Error.', 'ablocks' )
142 ]);
143 }
144
145 $redirect_url = '';
146 if ( $block_data['parentAttributes']['registerRedirect'] ?? false ) {
147 $redirect_url = \ABlocks\Blocks\FormBuilder\Helper::merge_query_params(
148 $block_data['parentAttributes']['link']['href'] ?? '',
149 $block_data['parentAttributes']['link']['keyValue'] ?? '',
150 true
151 );
152 }
153
154 $errors = $this->validate_registration_data( $payload );
155 if ( $errors ) {
156 wp_send_json_error( [ 'message' => $errors ] );
157 }
158
159 $user_id = wp_create_user( $payload['username'], $payload['password'], $payload['email'] );
160 if ( is_wp_error( $user_id ) ) {
161 wp_send_json_error( [ 'message' => $user_id->get_error_message() ] );
162 }
163 $role = ( $block_data['parentAttributes']['roleSlug'] ?? '' );
164 if (
165 ! empty( $role ) &&
166 strtolower( $role ) !== 'default' &&
167 array_key_exists( $role, wp_roles()->roles )
168 ) {
169 $user = new \WP_User( $user_id );
170 $user->set_role( $role );
171 }
172
173 $custom_fields = $this->get_custom_fields( $_POST );
174
175 if ( ! empty( $block_data ) ) {
176 $sorted_fields = Helper::sorted_input_fields_by_input_type( $block_data['innerBlocks'] );
177 $schema = Helper::generate_schema_using_form_data( $sorted_fields );
178 $sanitized_fields = Sanitizer::sanitize_payload( $schema, $custom_fields );
179
180 array_walk($sanitized_fields, function ( $value, $key ) use ( $user_id ) {
181 update_user_meta( $user_id, 'ablocks_' . $key, $value );
182 });
183 }
184
185 wp_set_current_user( $user_id );
186 wp_set_auth_cookie( $user_id );
187
188 if ( empty( $redirect_url ) ) {
189 $redirect_url = home_url( '/' );
190 }
191 wp_send_json_success([
192 'message' => __( 'Registration completed successfully. Redirecting...', 'ablocks' ),
193 'redirect_url' => esc_url( $redirect_url ),
194 'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '',
195 'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '',
196 ]);
197 }
198
199 private function validate_registration_data( $payload ) {
200 if ( empty( $payload['username'] ) || ! validate_username( $payload['username'] ) ) {
201 return 'Invalid username.';
202 } elseif ( username_exists( $payload['username'] ) ) {
203 return 'Username already exists.';
204 }
205
206 if ( empty( $payload['email'] ) || ! is_email( $payload['email'] ) ) {
207 return 'Invalid email address.';
208 } elseif ( email_exists( $payload['email'] ) ) {
209 return 'Email already exists.';
210 }
211
212 if ( empty( $payload['password'] ) || strlen( $payload['password'] ) < 6 ) {
213 return 'Password must be at least 6 characters.';
214 } elseif ( $payload['password'] !== $payload['confirm_password'] ) {
215 return 'Passwords do not match.';
216 }
217
218 return null;
219 }
220
221 private function get_custom_fields( $form_data ) {
222 $reserved_keys = [ 'username', 'email', 'password', 'confirm_password', 'current_post_id', 'block_id', 'security', 'action' ];
223 return array_diff_key( $form_data, array_flip( $reserved_keys ) );
224 }
225
226 public function form_builder_forget_password_handler( $payload ) {
227 if ( empty( $payload['email'] ?? '' ) ) {
228 wp_send_json_error([
229 'message' => __( 'email field is required.', 'ablocks' )
230 ], 400);
231 }
232
233 if ( ! is_email( $payload['email'] ) ) {
234 wp_send_json_error([
235 'message' => __( 'provide a valid email.', 'ablocks' )
236 ], 400);
237 }
238
239 if ( ! email_exists( $payload['email'] ) ) {
240 wp_send_json_error([
241 'message' => __( 'this email is not exists.', 'ablocks' )
242 ], 400);
243 }
244 $error = retrieve_password( $payload['email'] );
245 if ( is_wp_error( $error ) ) {
246 wp_send_json_error([
247 'message' => esc_html( $error->get_error_message() )
248 ], 500);
249 }
250 wp_send_json_success([
251 'message' => __( 'Password reset email is sent.', 'ablocks' )
252 ]);
253 }
254
255 public function handle_form_submission( $payload ) {
256 $fields_to_skip = [ 'current_post_id', 'block_id', 'security', 'action' ];
257 $all_fields = array_diff_key( $_POST, array_flip( $fields_to_skip ) );
258 $block_data = Helper::get_block_attributes( $payload['current_post_id'], $payload['block_id'], 'ablocks/form-builder' );
259
260 $actions = apply_filters('ablocks/form_builder/actions', [
261 SaveFormData::class,
262 SendEmail::class,
263 Subscribe::class,
264 ]);
265
266 $validate_data = new ValidateFormData( $block_data, $all_fields );
267
268 $validate_data->actions( $actions );
269
270 $output = $validate_data->get_output();
271 if ( $validate_data->has_error() ) {
272 $output['message'] = $validate_data->get_error_message();
273 wp_send_json_error( $output );
274 } elseif ( $validate_data->has_message() ) {
275 $output['message'] = $validate_data->get_message();
276 wp_send_json_success( $output );
277 }
278
279 wp_send_json_error( [ 'message' => __( 'Action is not defined.', 'ablocks' ) ] );
280 }
281
282 public function action_setting_data( $payload ) {
283 $type = $payload['type'] ?? '';
284 $settings_key = $type . '_api_key';
285 $api = $payload['api'] ?? $GLOBALS['ablocks_settings']->{$settings_key} ?? '';
286 $list_id = $payload['list_id'] ?? '';
287
288 $api_key = $api;
289
290 if ( empty( $api_key ) ) {
291 wp_send_json_error( [ 'message' => __( 'api key is required', 'ablocks' ) ] );
292 }
293
294 $action_config = apply_filters('ablocks/form_builder/action_config', [
295 'mailchimp' => function( $api_key ) use ( $list_id ) {
296 if ( ! class_exists( Mailchimp::class ) ) {
297 return;
298 }
299
300 if ( empty( $list_id ) ) {
301 $data = ( new Mailchimp(
302 $api_key,
303 '',
304 ''
305 ) )
306 ->get_lists();
307 wp_send_json_success( $data );
308 } else {
309
310 $data = ( new Mailchimp(
311 $api_key,
312 '',
313 ''
314 ) )
315 ->get_groups( $list_id );
316 }
317
318 wp_send_json_success( $data );
319 }
320 ]);
321
322 if ( ! array_key_exists( $type, $action_config ) ) {
323 wp_send_json_error( [ 'message' => __( 'Not a valid type', 'ablocks' ) ] );
324 }
325
326 try {
327 if (
328 array_key_exists( $type, $action_config ) &&
329 is_callable( $action_config[ $type ] )
330 ) {
331 $action_config[ $type ]( $api_key, $payload );
332 }
333 } catch ( Exception $e ) {
334 wp_send_json_error( [ 'message' => esc_html( $e->getMessage() ) ], 400 );
335 }
336
337 wp_send_json_error( [ 'message' => __( 'Unspecified service type', 'ablocks' ) ], 400 );
338 }
339 }
340