PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.0.2
JetFormBuilder — Dynamic Blocks Form Builder v2.0.2
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 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / form-handler.php
jetformbuilder / includes Last commit date
actions 4 years ago addons 4 years ago admin 4 years ago blocks 4 years ago classes 4 years ago db-queries 4 years ago dev-mode 4 years ago exceptions 4 years ago form-actions 4 years ago form-messages 4 years ago form-patterns 4 years ago form-response 4 years ago gateways 4 years ago generators 4 years ago integrations 4 years ago presets 4 years ago request 4 years ago rest-api 4 years ago shortcodes 4 years ago widgets 4 years ago autoloader.php 4 years ago file-upload.php 4 years ago form-break.php 4 years ago form-handler.php 4 years ago form-manager.php 4 years ago live-form.php 4 years ago plugin.php 4 years ago post-type.php 4 years ago
form-handler.php
376 lines
1 <?php
2
3 namespace Jet_Form_Builder;
4
5 use Jet_Form_Builder\Actions\Action_Handler;
6 use Jet_Form_Builder\Actions\Executors\Action_Required_Executor;
7 use Jet_Form_Builder\Classes\Tools;
8 use Jet_Form_Builder\Exceptions\Action_Exception;
9 use Jet_Form_Builder\Exceptions\Handler_Exception;
10 use Jet_Form_Builder\Exceptions\Not_Router_Request;
11 use Jet_Form_Builder\Exceptions\Repository_Exception;
12 use Jet_Form_Builder\Exceptions\Request_Exception;
13 use Jet_Form_Builder\Form_Response;
14 use Jet_Form_Builder\Request\Form_Request_Router;
15 use Jet_Form_Builder\Request\Request_Handler;
16 use Jet_Form_Builder\Request_Router;
17
18 /**
19 * Form builder class
20 */
21
22 // If this file is called directly, abort.
23 if ( ! defined( 'WPINC' ) ) {
24 die;
25 }
26
27 /**
28 * Define Jet_Engine_Booking_Forms_Handler class
29 */
30 class Form_Handler {
31
32 public $hook_key = 'jet_form_builder_submit';
33 public $hook_val = 'submit';
34 public $form_data = array();
35 public $response_data = array();
36 public $is_ajax = false;
37 public $is_success = false;
38 public $response_args = array();
39
40 public $form_id;
41 public $refer;
42 public $manager;
43
44 /** @var Action_Handler */
45 public $action_handler;
46
47 public $form_key = '_jet_engine_booking_form_id';
48 public $refer_key = '_jet_engine_refer';
49 public $post_id_key = '__queried_post_id';
50 /**
51 * @var Request_Handler
52 */
53 public $request_handler;
54
55
56 /**
57 * Constructor for the class
58 */
59 public function __construct() {
60 $this->action_handler = new Action_Handler();
61 $this->request_handler = new Request_Handler();
62 }
63
64 public function call_form() {
65 try {
66 Form_Request_Router::listen();
67 } catch ( Not_Router_Request $exception ) {
68 return;
69 }
70
71 add_filter( 'jet-form-builder/form-handler/form-data', array( $this, 'merge_request' ), 0 );
72
73 if ( wp_doing_ajax() ) {
74 add_action(
75 'wp_ajax_' . $this->hook_key,
76 array( $this, 'process_ajax_form' )
77 );
78 add_action(
79 'wp_ajax_nopriv_' . $this->hook_key,
80 array( $this, 'process_ajax_form' )
81 );
82 } else {
83 add_action( 'wp_loaded', array( $this, 'process_form' ), 0 );
84 }
85 }
86
87 public function core_fields() {
88 return apply_filters(
89 'jet-form-builder/form-handler/core-fields',
90 array(
91 $this->form_key => array(
92 'callback' => array( $this, 'set_form_id' ),
93 ),
94 $this->refer_key => array(
95 'callback' => array( $this, 'set_referrer' ),
96 ),
97 $this->post_id_key => array(
98 'callback' => array( Tools::class, 'set_current_post' ),
99 ),
100 )
101 );
102 }
103
104 /**
105 * Moved to a separate function to define the hidden field when saving the Form Record
106 *
107 * These fields will not be saved to a separate `*_jet_fb_records_fields` table
108 *
109 * @return array
110 */
111 public function hidden_request_fields() {
112 return apply_filters(
113 'jet-form-builder/form-handler/hidden-request-fields',
114 array(
115 '__form_id' => $this->get_form_id(),
116 '__refer' => $this->get_referrer(),
117 '__is_ajax' => $this->is_ajax(),
118 )
119 );
120 }
121
122 public function merge_request( $request ) {
123 $request = array_merge(
124 $this->hidden_request_fields(),
125 $request
126 );
127
128 return $request;
129 }
130
131
132 public function set_referrer( $url ) {
133 $refer = remove_query_arg(
134 array( 'values', 'status', 'fields' ),
135 esc_url_raw( $url )
136 );
137
138 $this->refer = wp_validate_redirect( $refer );
139
140 return $this;
141 }
142
143 public function get_referrer() {
144 return $this->refer;
145 }
146
147 public function set_form_id( $form_id ) {
148 $this->form_id = absint( $form_id );
149
150 return $this;
151 }
152
153 public function get_form_id() {
154 return $this->form_id;
155 }
156
157 /**
158 * Is ajax form processing or not
159 *
160 * @return boolean [description]
161 */
162 public function is_ajax() {
163 return $this->is_ajax;
164 }
165
166 /**
167 * Setup form data
168 *
169 * @return void [description]
170 */
171 public function setup_form() {
172
173 if ( $this->form_id ) {
174 return;
175 }
176
177 $fields = $this->core_fields();
178
179 if ( $this->is_ajax() ) {
180 $values = ! empty( $_POST['values'] ) ? Tools::sanitize_recursive( $_POST['values'] ) : array();
181
182 foreach ( $values as $data ) {
183 if ( ! isset( $fields[ $data['name'] ] ) ) {
184 continue;
185 }
186 $options = $fields[ $data['name'] ] ?? array();
187
188 Tools::call( $options['callback'] ?? false, $data['value'] ?? '' );
189 }
190
191 return;
192 }
193
194 foreach ( $fields as $field_name => $options ) {
195 if ( ! isset( $_POST[ $field_name ] ) ) {
196 continue;
197 }
198
199 // phpcs:disable WordPress
200 Tools::call( $options['callback'] ?? false, $_POST[ $field_name ] );
201 // phpcs:enable WordPress
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
260 $this->try_send();
261
262 if ( true === $this->is_success ) {
263 $this->send_response(
264 array(
265 'status' => 'success',
266 )
267 );
268 } else {
269 $this->send_response(
270 array(
271 'status' => 'failed',
272 )
273 );
274 }
275 }
276
277 public function try_send() {
278 try {
279 $this->send_form();
280 } catch ( Request_Exception $exception ) {
281 $this->send_response(
282 array(
283 'status' => $exception->get_form_status(),
284 'errors' => $exception->get_fields_errors(),
285 )
286 );
287 } catch ( Action_Exception $exception ) {
288 $this->send_response(
289 array(
290 'status' => $exception->get_form_status(),
291 )
292 );
293 }
294 }
295
296 /**
297 * @throws Request_Exception
298 */
299 public function send_form() {
300 $this->action_handler->set_form_id( $this->form_id );
301
302 $this->action_handler->add_request(
303 $this->request_handler->get_form_data()
304 );
305
306 do_action( 'jet-form-builder/form-handler/before-send', $this );
307
308 $this->add_response_data( $this->action_handler->do_actions() );
309
310 $this->is_success = true;
311 }
312
313
314 /**
315 * Add new properties into response data
316 *
317 * @param array $data [description]
318 */
319 public function add_response_data( $data = array() ) {
320 $this->response_data = array_merge( $this->response_data, $data );
321 }
322
323 public function set_response_args( $args ) {
324 $this->response_args = array_merge( $this->response_args, $args );
325
326 return $this;
327 }
328
329 public function get_response_args() {
330 return $this->response_args;
331 }
332
333 /**
334 * Redirect back to refer
335 *
336 * @param array $args [description]
337 *
338 * @return void [description]
339 */
340 public function send_response( $args = array() ) {
341 try {
342 $this->set_response_args( $args );
343
344 ( new Action_Required_Executor() )->soft_run_actions();
345
346 do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success );
347 } catch ( Repository_Exception $exception ) {
348 $this->send_raw_response();
349
350 return;
351 } catch ( Handler_Exception $exception ) {
352 $this->is_success = false;
353 $this->response_args = array(
354 'status' => $exception->get_form_status(),
355 );
356 $this->response_data = array();
357
358 $this->send_raw_response();
359
360 return;
361 }
362
363 $this->send_raw_response();
364 }
365
366 public function send_raw_response() {
367 ( new Form_Response\Response(
368 $this->get_response_manager(),
369 $this->response_data
370 ) )->init( $this->response_args )->send();
371 }
372
373 }
374
375
376