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