PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.1.7
JetFormBuilder — Dynamic Blocks Form Builder v1.1.7
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-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 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 form-messages-builder.php 5 years ago form-messages-manager.php 5 years ago form-preset.php 5 years ago live-form.php 5 years ago plugin.php 5 years ago post-type.php 5 years ago request-handler.php 5 years ago
form-handler.php
242 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->is_ajax ) {
91 $post = ! empty( $_REQUEST[ $this->post_id_key ] ) ? get_post( $_REQUEST[ $this->post_id_key ] ) : null;
92 $this->form_id = ! empty( $_REQUEST[ $this->form_key ] ) ? absint( $_REQUEST[ $this->form_key ] ) : false;
93 $this->refer = ! empty( $_REQUEST[ $this->refer_key ] ) ? esc_url( $_REQUEST[ $this->refer_key ] ) : false;
94 } else {
95 $values = ! empty( $_REQUEST['values'] ) ? Tools::maybe_recursive_sanitize( $_REQUEST['values'] ) : array();
96
97 foreach ( $values as $data ) {
98 switch ( $data['name'] ) {
99 case $this->form_key:
100 $this->form_id = $data['value'];
101 break;
102 case $this->post_id_key:
103 $post = get_post( $data['value'] );
104 break;
105 case $this->refer_key:
106 $this->refer = esc_attr( $data['value'] );
107 // Clear form-related arguments
108 $this->refer = remove_query_arg( array( 'values', 'status', 'fields' ), $this->refer );
109 break;
110 }
111 }
112
113 }
114 }
115
116 private function get_response_manager() {
117 if ( $this->is_ajax ) {
118 return new Form_Response\Types\Ajax_Response( array(
119 'form_id' => $this->form_id,
120 'actions' => $this->action_handler->get_all(),
121 ) );
122 } else {
123 return new Form_Response\Types\Reload_Response( array(
124 'refer' => $this->refer,
125 ) );
126 }
127 }
128
129 /**
130 * Process form with Ajax
131 *
132 * @return [type] [description]
133 */
134 public function process_ajax_form() {
135 $this->is_ajax = true;
136 $this->process_form();
137 }
138
139 /**
140 * Process current form
141 *
142 * @return [type] [description]
143 */
144 public function process_form() {
145
146 $this->setup_form();
147
148 $this->try_set_data();
149
150 do_action( 'jet-form-builder/form-handler/before-send', $this );
151
152 $this->try_to_do_actions();
153
154 do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success );
155
156 if ( true === $this->is_success ) {
157 $this->send_response( array(
158 'status' => 'success',
159 ) );
160 } else {
161 $this->send_response( array(
162 'status' => 'failed',
163 ) );
164 }
165 }
166
167 public function try_set_data() {
168 try {
169 $request = array(
170 'form_id' => $this->form_id,
171 'is_ajax' => $this->is_ajax,
172 'refer' => $this->refer
173 );
174 $this->action_handler = new Action_Handler( $this->form_id );
175 $this->request_data = ( new Request_Handler( $request ) )->get_form_data();
176
177 } catch ( Request_Exception $exception ) {
178 $this->send_response( array(
179 'status' => $exception->get_form_status(),
180 'errors' => $exception->get_fields_errors()
181 ) );
182 }
183 }
184
185
186 public function try_to_do_actions() {
187 try {
188 $this->action_handler->add_request( $this->request_data );
189
190 $this->add_response_data( $this->action_handler->do_actions() );
191 $this->is_success = true;
192
193 } catch ( Handler_Exception $exception ) {
194 $this->send_response( array(
195 'status' => $exception->get_form_status(),
196 ) );
197 }
198 }
199
200 public function get_message_builder_data( $form_id ) {
201 $form_id = $this->form_id ? $this->form_id : $form_id;
202
203 if ( $this->action_handler && ! empty( $this->action_handler->form_actions ) ) {
204 $actions = $this->action_handler->form_actions;
205 } else {
206 $actions = ( new Action_Handler( $form_id ) )->get_all();
207 }
208
209 return ( object ) array(
210 'form_id' => $form_id,
211 'actions' => $actions
212 );
213 }
214
215 public function get_message_builder( $form_id ) {
216 return new Form_Messages\Builder( $this->get_message_builder_data( $form_id ) );
217 }
218
219 /**
220 * Add new properties into response data
221 *
222 * @param array $data [description]
223 */
224 public function add_response_data( $data = array() ) {
225 $this->response_data = array_merge( $this->response_data, $data );
226 }
227
228 /**
229 * Redirect back to refer
230 *
231 * @param array $args [description]
232 *
233 * @return void [description]
234 */
235 public function send_response( $args = array() ) {
236 ( new Form_Response\Response( $this->get_response_manager(), $this->response_data ) )->init( $args )->send();
237 }
238
239 }
240
241
242