PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.0
JetFormBuilder — Dynamic Blocks Form Builder v1.2.0
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 1.1.2 1.1.3 1.1.4 1.1.5 All 130 releases
← All changes | includes/form-handler.php +251 -422 trunk1.2.0 View file →
@@ -1,422 +1,251 @@
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 -}
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 +use Jet_Form_Builder\Exceptions\Request_Exception;
10 +use Jet_Form_Builder\Form_Messages;
11 +use Jet_Form_Builder\Form_Response;
12 +use Jet_Form_Builder\Request\Request_Handler;
13 +
14 +/**
15 + * Form builder class
16 + */
17 +
18 +// If this file is called directly, abort.
19 +if ( ! defined( 'WPINC' ) ) {
20 + die;
21 +}
22 +
23 +/**
24 + * Define Jet_Engine_Booking_Forms_Handler class
25 + */
26 +class Form_Handler {
27 +
28 + public $hook_key = 'jet_form_builder_submit';
29 + public $hook_val = 'submit';
30 + public $form_data = array();
31 + public $response_data = array();
32 + public $is_ajax = false;
33 + public $is_success = false;
34 + public $response_status = 'failed';
35 +
36 + public $form_id;
37 + public $refer;
38 + public $manager;
39 + public $action_handler;
40 + public $request_data;
41 +
42 + public $form_key = '_jet_engine_booking_form_id';
43 + public $refer_key = '_jet_engine_refer';
44 + public $post_id_key = '__queried_post_id';
45 +
46 +
47 + /**
48 + * Constructor for the class
49 + */
50 + function __construct() {
51 +
52 + if ( wp_doing_ajax() ) {
53 +
54 + add_action(
55 + 'wp_ajax_' . $this->hook_key,
56 + array( $this, 'process_ajax_form' )
57 + );
58 + add_action(
59 + 'wp_ajax_nopriv_' . $this->hook_key,
60 + array( $this, 'process_ajax_form' )
61 + );
62 +
63 + } else {
64 +
65 + if ( ! isset( $_REQUEST[ $this->hook_key ] ) || $this->hook_val !== $_REQUEST[ $this->hook_key ] ) {
66 + return;
67 + }
68 +
69 + add_action( 'wp_loaded', array( $this, 'process_form' ), 0 );
70 + }
71 + }
72 +
73 + /**
74 + * Is ajax form processing or not
75 + *
76 + * @return boolean [description]
77 + */
78 + public function is_ajax() {
79 + return $this->is_ajax;
80 + }
81 +
82 + /**
83 + * Setup form data
84 + *
85 + * @return [type] [description]
86 + */
87 + public function setup_form() {
88 + global $post;
89 +
90 + if ( $this->form_id ) {
91 + return;
92 + }
93 +
94 + if ( ! $this->is_ajax ) {
95 + $post = ! empty( $_REQUEST[ $this->post_id_key ] ) ? get_post( $_REQUEST[ $this->post_id_key ] ) : null;
96 + $this->form_id = ! empty( $_REQUEST[ $this->form_key ] ) ? absint( $_REQUEST[ $this->form_key ] ) : false;
97 + $this->refer = ! empty( $_REQUEST[ $this->refer_key ] ) ? esc_url( $_REQUEST[ $this->refer_key ] ) : false;
98 + } else {
99 + $values = ! empty( $_REQUEST['values'] ) ? Tools::maybe_recursive_sanitize( $_REQUEST['values'] ) : array();
100 +
101 + foreach ( $values as $data ) {
102 + switch ( $data['name'] ) {
103 + case $this->form_key:
104 + $this->form_id = $data['value'];
105 + break;
106 + case $this->post_id_key:
107 + $post = get_post( $data['value'] );
108 + break;
109 + case $this->refer_key:
110 + $this->refer = esc_attr( $data['value'] );
111 + // Clear form-related arguments
112 + $this->refer = remove_query_arg( array( 'values', 'status', 'fields' ), $this->refer );
113 + break;
114 + }
115 + }
116 +
117 + }
118 + }
119 +
120 + private function get_response_manager() {
121 + if ( ! $this->form_id ) {
122 + $this->setup_form();
123 + }
124 +
125 + $action_handler = $this->action_handler ? $this->action_handler : new Action_Handler( $this->form_id );
126 +
127 + if ( $this->is_ajax ) {
128 + return new Form_Response\Types\Ajax_Response( array(
129 + 'form_id' => $this->form_id,
130 + 'actions' => $action_handler->get_all(),
131 + ) );
132 + } else {
133 + return new Form_Response\Types\Reload_Response( array(
134 + 'refer' => $this->refer,
135 + ) );
136 + }
137 + }
138 +
139 + /**
140 + * Process form with Ajax
141 + *
142 + * @return [type] [description]
143 + */
144 + public function process_ajax_form() {
145 + $this->is_ajax = true;
146 + $this->process_form();
147 + }
148 +
149 + /**
150 + * Process current form
151 + *
152 + * @return [type] [description]
153 + */
154 + public function process_form() {
155 +
156 + $this->setup_form();
157 +
158 + $this->try_set_data();
159 +
160 + do_action( 'jet-form-builder/form-handler/before-send', $this );
161 +
162 + $this->try_to_do_actions();
163 +
164 + do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success );
165 +
166 + if ( true === $this->is_success ) {
167 + $this->send_response( array(
168 + 'status' => 'success',
169 + ) );
170 + } else {
171 + $this->send_response( array(
172 + 'status' => 'failed',
173 + ) );
174 + }
175 + }
176 +
177 + public function try_set_data() {
178 + try {
179 + $request = array(
180 + 'form_id' => $this->form_id,
181 + 'is_ajax' => $this->is_ajax,
182 + 'refer' => $this->refer
183 + );
184 + $this->action_handler = new Action_Handler( $this->form_id );
185 + $this->request_data = ( new Request_Handler( $request ) )->get_form_data();
186 +
187 + } catch ( Request_Exception $exception ) {
188 + $this->send_response( array(
189 + 'status' => $exception->get_form_status(),
190 + 'errors' => $exception->get_fields_errors()
191 + ) );
192 + }
193 + }
194 +
195 +
196 + public function try_to_do_actions() {
197 + try {
198 + $this->action_handler->add_request( $this->request_data );
199 +
200 + $this->add_response_data( $this->action_handler->do_actions() );
201 + $this->is_success = true;
202 +
203 + } catch ( Handler_Exception $exception ) {
204 + $this->send_response( array(
205 + 'status' => $exception->get_form_status(),
206 + ) );
207 + }
208 + }
209 +
210 + public function get_message_builder_data( $form_id ) {
211 + $form_id = $this->form_id ? $this->form_id : $form_id;
212 +
213 + if ( $this->action_handler && ! empty( $this->action_handler->form_actions ) ) {
214 + $actions = $this->action_handler->form_actions;
215 + } else {
216 + $actions = ( new Action_Handler( $form_id ) )->get_all();
217 + }
218 +
219 + return ( object ) array(
220 + 'form_id' => $form_id,
221 + 'actions' => $actions
222 + );
223 + }
224 +
225 + public function get_message_builder( $form_id ) {
226 + return new Form_Messages\Builder( $this->get_message_builder_data( $form_id ) );
227 + }
228 +
229 + /**
230 + * Add new properties into response data
231 + *
232 + * @param array $data [description]
233 + */
234 + public function add_response_data( $data = array() ) {
235 + $this->response_data = array_merge( $this->response_data, $data );
236 + }
237 +
238 + /**
239 + * Redirect back to refer
240 + *
241 + * @param array $args [description]
242 + *
243 + * @return void [description]
244 + */
245 + public function send_response( $args = array() ) {
246 + ( new Form_Response\Response( $this->get_response_manager(), $this->response_data ) )->init( $args )->send();
247 + }
248 +
249 +}
250 +
251 +