PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 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 All 134 releases
← All changes | includes/form-handler.php +422 -272 1.0.3 → 3.6.6 View file →
@@ -1,272 +1,422 @@
1 -<?php
2 -
3 -namespace Jet_Form_Builder;
4 -
5 -
6 -use Jet_Form_Builder\Actions\Action_Handler;
7 -use Jet_Form_Builder\Classes\Tools;
8 -use Jet_Form_Builder\Exceptions\Handler_Exception;
9 -
10 -/**
11 - * Form builder class
12 - */
13 -
14 -// If this file is called directly, abort.
15 -if ( ! defined( 'WPINC' ) ) {
16 - die;
17 -}
18 -
19 -/**
20 - * Define Jet_Engine_Booking_Forms_Handler class
21 - */
22 -class Form_Handler {
23 -
24 - public $hook_key = 'jet_form_builder_submit';
25 - public $hook_val = 'submit';
26 - public $form_data = array();
27 - public $response_data = array();
28 - public $is_ajax = false;
29 - public $is_success = false;
30 - public $response_status = 'failed';
31 -
32 - public $form_id;
33 - public $refer;
34 - public $manager;
35 - public $action_handler;
36 - public $request_data;
37 -
38 - /**
39 - * Constructor for the class
40 - */
41 - function __construct() {
42 -
43 - if ( wp_doing_ajax() ) {
44 -
45 - add_action(
46 - 'wp_ajax_' . $this->hook_key,
47 - array( $this, 'process_ajax_form' )
48 - );
49 - add_action(
50 - 'wp_ajax_nopriv_' . $this->hook_key,
51 - array( $this, 'process_ajax_form' )
52 - );
53 -
54 - } else {
55 -
56 - if ( ! isset( $_REQUEST[ $this->hook_key ] ) || $this->hook_val !== $_REQUEST[ $this->hook_key ] ) {
57 - return;
58 - }
59 -
60 - add_action( 'wp_loaded', array( $this, 'process_form' ), 0 );
61 - }
62 - }
63 -
64 - /**
65 - * Is ajax form processing or not
66 - *
67 - * @return boolean [description]
68 - */
69 - public function is_ajax() {
70 - return $this->is_ajax;
71 - }
72 -
73 - /**
74 - * Setup form data
75 - *
76 - * @return [type] [description]
77 - */
78 - public function setup_form() {
79 -
80 - $form_key = '_jet_engine_booking_form_id';
81 - $refer_key = '_jet_engine_refer';
82 -
83 - if ( ! $this->is_ajax ) {
84 - $this->form_id = ! empty( $_REQUEST[ $form_key ] ) ? absint( $_REQUEST[ $form_key ] ) : false;
85 - $this->refer = ! empty( $_REQUEST[ $refer_key ] ) ? esc_url( $_REQUEST[ $refer_key ] ) : false;
86 - } else {
87 -
88 - $values = ! empty( $_REQUEST['values'] ) ? Tools::maybe_recursive_sanitize( $_REQUEST['values'] ) : array();
89 -
90 - foreach ( $values as $data ) {
91 - if ( $data['name'] === $form_key ) {
92 - $this->form_id = $data['value'];
93 - }
94 - }
95 -
96 - }
97 - }
98 -
99 - /**
100 - * Check if current form has configured gateway
101 - *
102 - * @return boolean [description]
103 - */
104 - public function has_gateway() {
105 - return apply_filters( 'jet-form-builder/form-handler/has-gateways', false, $this->form_id );
106 - }
107 -
108 - /**
109 - * Process form with Ajax
110 - *
111 - * @return [type] [description]
112 - */
113 - public function process_ajax_form() {
114 - $this->is_ajax = true;
115 - $this->process_form();
116 - }
117 -
118 - /**
119 - * Process current form
120 - *
121 - * @return [type] [description]
122 - */
123 - public function process_form() {
124 -
125 - $this->setup_form();
126 -
127 - $this->try_set_data();
128 -
129 - do_action( 'jet-form-builder/form-handler/before-send', $this );
130 -
131 - $this->try_to_do_actions();
132 -
133 - do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success );
134 -
135 - if ( true === $this->is_success ) {
136 - $this->send_response( array(
137 - 'status' => 'success',
138 - ) );
139 - } else {
140 - $this->send_response( array(
141 - 'status' => 'failed',
142 - ) );
143 - }
144 - }
145 -
146 - public function try_set_data() {
147 - try {
148 - $request = array(
149 - 'form_id' => $this->form_id,
150 - 'is_ajax' => $this->is_ajax,
151 - 'refer' => $this->refer
152 - );
153 - $this->request_data = ( new Request_Handler( $request ) )->get_form_data();
154 -
155 - } catch ( Handler_Exception $exception ) {
156 - $this->send_response( array(
157 - 'status' => $exception->get_form_status(),
158 - ) );
159 - }
160 - }
161 -
162 -
163 - public function try_to_do_actions() {
164 - try {
165 - $this->action_handler = new Action_Handler( $this->form_id, $this->request_data );
166 -
167 - $this->add_response_data( $this->action_handler->do_actions() );
168 - $this->is_success = true;
169 -
170 - } catch ( Handler_Exception $exception ) {
171 - $this->send_response( array(
172 - 'status' => $exception->get_form_status(),
173 - ) );
174 - }
175 - }
176 -
177 - /**
178 - * Add new properties into response data
179 - *
180 - * @param array $data [description]
181 - */
182 - public function add_response_data( $data = array() ) {
183 - $this->response_data = array_merge( $this->response_data, $data );
184 - }
185 -
186 - public function get_message_builder( $form_id = null ) {
187 -
188 - $form_id = $this->form_id ? $this->form_id : $form_id;
189 -
190 - if ( $this->action_handler && ! empty( $this->action_handler->form_actions ) ) {
191 - $actions = $this->action_handler->form_actions;
192 - } else {
193 - $actions = ( new Action_Handler( $form_id ) )->get_all();
194 - }
195 -
196 - $data = ( object ) array(
197 - 'form_id' => $form_id,
198 - 'actions' => $actions
199 - );
200 -
201 - return new Form_Messages_Builder( $data );
202 - }
203 -
204 - /**
205 - * Redirect back to refer
206 - *
207 - * @param array $args [description]
208 - *
209 - * @return [type] [description]
210 - */
211 - public function send_response( $args = array() ) {
212 -
213 - $args = wp_parse_args( $args, array(
214 - 'status' => 'success',
215 - ) );
216 -
217 - $error_statuses = array( 'validation_failed', 'invalid_email' );
218 -
219 - $this->response_status = $args['status'];
220 -
221 - $query_args = array(
222 - 'status' => $this->response_status,
223 - );
224 -
225 - $query_args = array_merge( $query_args, $this->response_data );
226 -
227 - // Clear form-related arguments
228 - $this->refer = remove_query_arg( array( 'values', 'status', 'fields' ), $this->refer );
229 -
230 - if ( 'validation_failed' === $this->response_status ) {
231 - if ( $this->is_ajax ) {
232 - $query_args['fields'] = $args['errors'];
233 - } else {
234 - $query_args['fields'] = implode( ',', $args['errors'] );
235 - }
236 - }
237 -
238 - $send_values = apply_filters( 'jet-form-builder/form-handler/send-values-on-error', true );
239 -
240 - if ( ! $this->is_ajax && $send_values && in_array( $this->response_status, $error_statuses ) ) {
241 - $query_args['values'] = $this->form_data;
242 - }
243 -
244 - $query_args = apply_filters( 'jet-form-builder/form-handler/query-args', $query_args, $args, $this );
245 -
246 - if ( $this->is_ajax ) {
247 -
248 - $messages = $this->get_message_builder()->set_form_status( $this->response_status );
249 -
250 - ob_start();
251 - $messages->render_messages();
252 - $query_args['message'] = ob_get_clean();
253 -
254 - if ( 'validation_failed' === $this->response_status ) {
255 - ob_start();
256 - $messages->render_empty_field_message();
257 - $query_args['field_message'] = ob_get_clean();
258 - }
259 -
260 - wp_send_json( $query_args );
261 - } else {
262 - $query_args['status'] = urlencode( $query_args['status'] );
263 -
264 - $redirect = add_query_arg( $query_args, $this->refer );
265 - wp_redirect( $redirect );
266 - die();
267 - }
268 - }
269 -
270 -}
271 -
272 -
1 +<?php
2 +
3 +namespace Jet_Form_Builder;
4 +
5 +use Jet_Form_Builder\Actions\Action_Handler;
6 +use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event;
7 +use Jet_Form_Builder\Actions\Events\Default_Required\Default_Required_Event;
8 +use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
9 +use Jet_Form_Builder\Admin\Tabs_Handlers\Options_Handler;
10 +use Jet_Form_Builder\Blocks\Block_Helper;
11 +use Jet_Form_Builder\Classes\Tools;
12 +use Jet_Form_Builder\Exceptions\Action_Exception;
13 +use Jet_Form_Builder\Exceptions\Handler_Exception;
14 +use Jet_Form_Builder\Exceptions\Not_Router_Request;
15 +use Jet_Form_Builder\Exceptions\Repository_Exception;
16 +use Jet_Form_Builder\Exceptions\Request_Exception;
17 +use Jet_Form_Builder\Request\Form_Request_Router;
18 +use Jet_Form_Builder\Request\Request_Handler;
19 +use JFB_Modules\Rich_Content\Macros_Parser;
20 +use JFB_Modules\Rich_Content\Module;
21 +use JFB_Modules\Security\Exceptions\Spam_Exception;
22 +
23 +// If this file is called directly, abort.
24 +if ( ! defined( 'WPINC' ) ) {
25 + die;
26 +}
27 +
28 +/**
29 + * @property Macros_Parser parser
30 + *
31 + * Define Jet_Engine_Booking_Forms_Handler class
32 + */
33 +class Form_Handler {
34 +
35 + public $hook_key = 'jet_form_builder_submit';
36 + public $hook_val = 'submit';
37 + public $form_data = array();
38 + public $response_data = array();
39 + public $is_ajax = false;
40 + public $is_success = false;
41 + public $response_args = array();
42 + public $user_journey_data = array();
43 +
44 + public $form_id;
45 + public $refer;
46 + public $manager;
47 +
48 + /** @var Action_Handler */
49 + public $action_handler;
50 +
51 + public $form_key = '_jet_engine_booking_form_id';
52 + public $refer_key = '_jet_engine_refer';
53 + public $post_id_key = '__queried_post_id';
54 + public $user_journey_key = '_user_journey';
55 + /**
56 + * @var Request_Handler
57 + */
58 + public $request_handler;
59 +
60 +
61 + /**
62 + * Constructor for the class
63 + */
64 + public function __construct() {
65 + $this->set_jfb_request_args();
66 + $this->action_handler = new Action_Handler();
67 + $this->request_handler = new Request_Handler();
68 + }
69 +
70 + public function call_form() {
71 + try {
72 + Form_Request_Router::listen();
73 + } catch ( Not_Router_Request $exception ) {
74 + return;
75 + }
76 +
77 + add_action( 'jet-form-builder/request', array( $this, 'merge_request' ), 1 );
78 +
79 + if ( wp_doing_ajax() ) {
80 + add_action(
81 + 'wp_ajax_' . $this->hook_key,
82 + array( $this, 'process_ajax_form' )
83 + );
84 + add_action(
85 + 'wp_ajax_nopriv_' . $this->hook_key,
86 + array( $this, 'process_ajax_form' )
87 + );
88 + } else {
89 + add_action( 'wp_loaded', array( $this, 'process_form' ), 0 );
90 + }
91 + }
92 +
93 + public function core_fields() {
94 + return apply_filters(
95 + 'jet-form-builder/form-handler/core-fields',
96 + array(
97 + $this->form_key => array(
98 + 'callback' => array( $this, 'set_form_id' ),
99 + ),
100 + $this->refer_key => array(
101 + 'callback' => array( $this, 'set_referrer' ),
102 + ),
103 + $this->post_id_key => array(
104 + 'callback' => array( Tools::class, 'set_current_post' ),
105 + ),
106 + $this->user_journey_key => array(
107 + 'callback' => array( $this, 'set_user_journey_data' ),
108 + ),
109 + )
110 + );
111 + }
112 +
113 + public function merge_request() {
114 + $fields = array(
115 + '__form_id' => $this->get_form_id(),
116 + '__refer' => $this->get_referrer(),
117 + '__is_ajax' => $this->is_ajax(),
118 + );
119 +
120 + foreach ( $fields as $name => $value ) {
121 + jet_fb_context()->set_field_type( 'text-field', $name );
122 + jet_fb_context()->update_request( $value, $name );
123 + jet_fb_context()->make_secure( $name );
124 + }
125 + }
126 +
127 + public function set_user_journey_data( $data ) {
128 + $this->user_journey_data = $data;
129 + }
130 +
131 + public function get_user_journey_data() {
132 + return $this->user_journey_data;
133 + }
134 +
135 +
136 + public function set_referrer( $url ): Form_Handler {
137 + $refer = remove_query_arg(
138 + array( 'values', 'status', 'fields' ),
139 + esc_url_raw( $url )
140 + );
141 +
142 + $this->refer = wp_validate_redirect( $refer );
143 +
144 + return $this;
145 + }
146 +
147 + public function get_referrer() {
148 + return $this->refer;
149 + }
150 +
151 + public function set_form_id( $form_id ) {
152 + $form_id = absint( $form_id );
153 +
154 + $this->form_id = Block_Helper::is_valid_form_post( $form_id, true ) ? $form_id : 0;
155 +
156 + return $this;
157 + }
158 +
159 + public function get_form_id() {
160 + return $this->form_id;
161 + }
162 +
163 + /**
164 + * Is ajax form processing or not
165 + *
166 + * @return boolean [description]
167 + */
168 + public function is_ajax(): bool {
169 + return $this->is_ajax;
170 + }
171 +
172 + /**
173 + * Is form processing or not
174 + *
175 + * @return bool
176 + */
177 + public function is_process(): bool {
178 + return ! empty( $this->refer );
179 + }
180 +
181 + /**
182 + * Setup form data
183 + *
184 + * @return void [description]
185 + */
186 + public function setup_form() {
187 +
188 + if ( $this->form_id ) {
189 + return;
190 + }
191 +
192 + $fields = $this->core_fields();
193 +
194 + foreach ( $fields as $field_name => $options ) {
195 + // phpcs:disable WordPress.Security
196 + if ( ! isset( $_POST[ $field_name ] ) ) {
197 + continue;
198 + }
199 +
200 + Tools::call( $options['callback'] ?? false, $_POST[ $field_name ] );
201 + // phpcs:enable WordPress.Security
202 + }
203 + }
204 +
205 + private function get_response_manager() {
206 + if ( ! $this->form_id ) {
207 + $this->setup_form();
208 + }
209 +
210 + if ( $this->is_ajax ) {
211 + jet_fb_action_handler()->set_form_id( $this->form_id );
212 +
213 + return new Form_Response\Types\Ajax_Response(
214 + array(
215 + 'form_id' => $this->form_id,
216 + 'actions' => jet_fb_action_handler()->get_all(),
217 + )
218 + );
219 + } else {
220 + return new Form_Response\Types\Reload_Response(
221 + array(
222 + 'refer' => $this->refer,
223 + )
224 + );
225 + }
226 + }
227 +
228 + /**
229 + * Process form with Ajax
230 + *
231 + * @return void
232 + */
233 + public function process_ajax_form() {
234 + $this->is_ajax = true;
235 + $this->process_form();
236 + }
237 +
238 + /**
239 + * Process current form
240 + *
241 + * @return void [description]
242 + */
243 + public function process_form() {
244 + $this->setup_form();
245 +
246 + if ( ! $this->form_id || ! $this->refer ) {
247 + $this->add_response_data(
248 + array(
249 + 'reload' => true,
250 + )
251 + );
252 + $this->set_response_args(
253 + array(
254 + 'status' => 'failed',
255 + )
256 + );
257 + $this->send_raw_response();
258 +
259 + return;
260 + }
261 +
262 + $this->try_send();
263 +
264 + if ( true === $this->is_success ) {
265 + $this->send_response(
266 + array(
267 + 'status' => 'success',
268 + )
269 + );
270 + } else {
271 + $this->send_response(
272 + array(
273 + 'status' => 'failed',
274 + )
275 + );
276 + }
277 + }
278 +
279 + public function try_send() {
280 + try {
281 + $this->send_form();
282 + } catch ( Request_Exception $exception ) {
283 + $this->send_response(
284 + array(
285 + 'status' => $exception->get_form_status(),
286 + 'errors' => $exception->get_fields_errors(),
287 + )
288 + );
289 + } catch ( Action_Exception $exception ) {
290 + /**
291 + * @see https://github.com/Crocoblock/jetformbuilder/issues/334
292 + */
293 + $this->is_success = $exception->is_success();
294 +
295 + $this->send_response(
296 + array(
297 + 'status' => $exception->get_form_status(),
298 + )
299 + );
300 + } catch ( Spam_Exception $exception ) {
301 + $this->send_response(
302 + array(
303 + 'status' => $exception->get_form_status(),
304 + )
305 + );
306 + }
307 + }
308 +
309 + /**
310 + * @throws Request_Exception
311 + */
312 + public function send_form() {
313 + if ( ! $this->form_id ) {
314 + throw new Request_Exception( 'failed' );
315 + }
316 +
317 + $this->action_handler->set_form_id( $this->form_id );
318 + $this->request_handler->set_form_data();
319 +
320 + do_action( 'jet-form-builder/form-handler/before-send', $this );
321 +
322 + // execute all actions
323 + jet_fb_events()->execute( Default_Process_Event::class );
324 +
325 + $this->add_response_data( $this->action_handler->response_data );
326 +
327 + $this->is_success = true;
328 + }
329 +
330 +
331 + /**
332 + * Add new properties into response data
333 + *
334 + * @param array $data [description]
335 + */
336 + public function add_response_data( $data = array() ) {
337 + $this->response_data = array_merge( $this->response_data, $data );
338 + }
339 +
340 + public function set_response_args( $args ) {
341 + $this->response_args = array_merge( $this->response_args, $args );
342 +
343 + return $this;
344 + }
345 +
346 + public function get_response_args() {
347 + return $this->response_args;
348 + }
349 +
350 + /**
351 + * Redirect back to refer
352 + *
353 + * @param array $args [description]
354 + *
355 + * @return void [description]
356 + */
357 + public function send_response( $args = array() ) {
358 + try {
359 + $this->set_response_args( $args );
360 +
361 + jet_fb_events()->execute( Default_Required_Event::class );
362 +
363 + do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success );
364 + } catch ( Repository_Exception $exception ) {
365 + $this->send_raw_response();
366 +
367 + return;
368 + } catch ( Handler_Exception $exception ) {
369 + $this->is_success = false;
370 + $this->response_args = array(
371 + 'status' => $exception->get_form_status(),
372 + );
373 + $this->response_data = array();
374 +
375 + $this->send_raw_response();
376 +
377 + return;
378 + }
379 +
380 + $this->send_raw_response();
381 + }
382 +
383 + public function send_raw_response() {
384 + ( new Form_Response\Response(
385 + $this->get_response_manager(),
386 + $this->response_data
387 + ) )->init( $this->response_args )->send();
388 + }
389 +
390 + /**
391 + * Backward compatibility to deprecated properties
392 + *
393 + * @param $name
394 + *
395 + * @return mixed
396 + * @throws Repository_Exception
397 + */
398 + public function __get( $name ) {
399 + switch ( $name ) {
400 + case 'parser':
401 + /** @var Module $rich */
402 + $rich = jet_form_builder()->module( 'rich-content' );
403 +
404 + return $rich->get_parser();
405 + default:
406 + return null;
407 + }
408 + }
409 +
410 + public function set_jfb_request_args() {
411 + $options_handler = new Options_Handler();
412 + $options_handler->set_jfb_request_args();
413 + $options = Tab_Handler_Manager::get_options( 'options-tab' );
414 + if ( isset( $options['gfb_request_args_key'] ) ) {
415 + $this->hook_key = $options['gfb_request_args_key'];
416 + }
417 + if ( isset( $options['gfb_request_args_value'] ) ) {
418 + $this->hook_val = $options['gfb_request_args_value'];
419 + }
420 + }
421 +
422 +}