PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.2
JetFormBuilder — Dynamic Blocks Form Builder v1.3.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 compatibility 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 shortcodes 4 years ago widgets 4 years ago autoloader.php 4 years ago file-upload.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
287 lines
1 <?php
2
3 namespace Jet_Form_Builder;
4
5 use Jet_Form_Builder\Actions\Action_Handler;
6 use Jet_Form_Builder\Classes\Tools;
7 use Jet_Form_Builder\Exceptions\Action_Exception;
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 * @var Request_Handler
47 */
48 public $request_handler;
49
50
51 /**
52 * Constructor for the class
53 */
54 public function __construct() {
55
56 if ( wp_doing_ajax() ) {
57
58 add_action(
59 'wp_ajax_' . $this->hook_key,
60 array( $this, 'process_ajax_form' )
61 );
62 add_action(
63 'wp_ajax_nopriv_' . $this->hook_key,
64 array( $this, 'process_ajax_form' )
65 );
66
67 } else {
68
69 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
70 if ( ! isset( $_REQUEST[ $this->hook_key ] ) || $this->hook_val !== $_REQUEST[ $this->hook_key ] ) {
71 return;
72 }
73
74 add_action( 'wp_loaded', array( $this, 'process_form' ), 0 );
75 }
76 }
77
78 /**
79 * Is ajax form processing or not
80 *
81 * @return boolean [description]
82 */
83 public function is_ajax() {
84 return $this->is_ajax;
85 }
86
87 /**
88 * Setup form data
89 *
90 * @return void [description]
91 */
92 public function setup_form() {
93 global $post;
94
95 if ( $this->form_id ) {
96 return;
97 }
98
99 // phpcs:disable WordPress
100 if ( ! $this->is_ajax ) {
101 $post = ! empty( $_POST[ $this->post_id_key ] ) ? get_post( absint( $_POST[ $this->post_id_key ] ) ) : null;
102 $this->form_id = ! empty( $_POST[ $this->form_key ] ) ? absint( $_POST[ $this->form_key ] ) : false;
103 $this->refer = ! empty( $_POST[ $this->refer_key ] ) ? esc_url_raw( $_POST[ $this->refer_key ] ) : false;
104 } else {
105 $values = ! empty( $_POST['values'] ) ? Tools::sanitize_recursive( $_POST['values'] ) : array();
106
107 foreach ( $values as $data ) {
108 switch ( $data['name'] ) {
109 case $this->form_key:
110 $this->form_id = absint( $data['value'] );
111 break;
112 case $this->post_id_key:
113 $post = get_post( absint( $data['value'] ) );
114 break;
115 case $this->refer_key:
116 $this->refer = esc_url_raw( $data['value'] );
117 // Clear form-related arguments.
118 $this->refer = remove_query_arg( array( 'values', 'status', 'fields' ), $this->refer );
119 break;
120 }
121 }
122 }
123 // phpcs:enable WordPress
124
125 $this->refer = wp_validate_redirect( $this->refer );
126 }
127
128 private function get_response_manager() {
129 if ( ! $this->form_id ) {
130 $this->setup_form();
131 }
132
133 if ( $this->is_ajax ) {
134 $action_handler = $this->action_handler ? $this->action_handler : new Action_Handler( $this->form_id );
135
136 return new Form_Response\Types\Ajax_Response(
137 array(
138 'form_id' => $this->form_id,
139 'actions' => $action_handler->get_all(),
140 )
141 );
142 } else {
143 return new Form_Response\Types\Reload_Response(
144 array(
145 'refer' => $this->refer,
146 )
147 );
148 }
149 }
150
151 /**
152 * Process form with Ajax
153 *
154 * @return [type] [description]
155 */
156 public function process_ajax_form() {
157 $this->is_ajax = true;
158 $this->process_form();
159 }
160
161 /**
162 * Process current form
163 *
164 * @return [type] [description]
165 */
166 public function process_form() {
167
168 $this->setup_form();
169
170 if ( ! $this->form_id || ! $this->refer ) {
171 $this->add_response_data(
172 array(
173 'reload' => true,
174 )
175 );
176
177 $this->send_response(
178 array(
179 'status' => 'failed',
180 )
181 );
182 }
183
184 $this->try_set_data();
185
186 do_action( 'jet-form-builder/form-handler/before-send', $this );
187
188 $this->try_to_do_actions();
189
190 do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success );
191
192 if ( true === $this->is_success ) {
193 $this->send_response(
194 array(
195 'status' => 'success',
196 )
197 );
198 } else {
199 $this->send_response(
200 array(
201 'status' => 'failed',
202 )
203 );
204 }
205 }
206
207 public function try_set_data() {
208 try {
209 $request = array(
210 'form_id' => $this->form_id,
211 'is_ajax' => $this->is_ajax,
212 'refer' => $this->refer,
213 );
214 $this->action_handler = new Action_Handler( $this->form_id );
215 $this->request_handler = new Request_Handler( $request );
216 $this->request_data = $this->request_handler->get_form_data();
217
218 } catch ( Request_Exception $exception ) {
219 $this->send_response(
220 array(
221 'status' => $exception->get_form_status(),
222 'errors' => $exception->get_fields_errors(),
223 )
224 );
225 }
226 }
227
228
229 public function try_to_do_actions() {
230 try {
231 $this->action_handler->add_request( $this->request_data );
232
233 $this->add_response_data( $this->action_handler->do_actions() );
234 $this->is_success = true;
235
236 } catch ( Handler_Exception $exception ) {
237 $this->send_response(
238 array(
239 'status' => $exception->get_form_status(),
240 )
241 );
242 }
243 }
244
245 public function get_message_builder_data( $form_id ) {
246 $form_id = $this->form_id ? $this->form_id : $form_id;
247
248 if ( $this->action_handler && ! empty( $this->action_handler->form_actions ) ) {
249 $actions = $this->action_handler->form_actions;
250 } else {
251 $actions = ( new Action_Handler( $form_id ) )->get_all();
252 }
253
254 return (object) array(
255 'form_id' => $form_id,
256 'actions' => $actions,
257 );
258 }
259
260 public function get_message_builder( $form_id ) {
261 return new Form_Messages\Builder( $this->get_message_builder_data( $form_id ) );
262 }
263
264 /**
265 * Add new properties into response data
266 *
267 * @param array $data [description]
268 */
269 public function add_response_data( $data = array() ) {
270 $this->response_data = array_merge( $this->response_data, $data );
271 }
272
273 /**
274 * Redirect back to refer
275 *
276 * @param array $args [description]
277 *
278 * @return void [description]
279 */
280 public function send_response( $args = array() ) {
281 ( new Form_Response\Response( $this->get_response_manager(), $this->response_data ) )->init( $args )->send();
282 }
283
284 }
285
286
287