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