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