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