| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 6 |
use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event; |
| 7 |
use Jet_Form_Builder\Actions\Events\Default_Required\Default_Required_Event; |
| 8 |
use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 9 |
use Jet_Form_Builder\Admin\Tabs_Handlers\Options_Handler; |
| 10 |
use Jet_Form_Builder\Blocks\Block_Helper; |
| 11 |
use Jet_Form_Builder\Classes\Tools; |
| 12 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 13 |
use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 14 |
use Jet_Form_Builder\Exceptions\Not_Router_Request; |
| 15 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 16 |
use Jet_Form_Builder\Exceptions\Request_Exception; |
| 17 |
use Jet_Form_Builder\Request\Form_Request_Router; |
| 18 |
use Jet_Form_Builder\Request\Request_Handler; |
| 19 |
use JFB_Modules\Rich_Content\Macros_Parser; |
| 20 |
use JFB_Modules\Rich_Content\Module; |
| 21 |
use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 22 |
|
| 23 |
// If this file is called directly, abort. |
| 24 |
if ( ! defined( 'WPINC' ) ) { |
| 25 |
die; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @property Macros_Parser parser |
| 30 |
* |
| 31 |
* Define Jet_Engine_Booking_Forms_Handler class |
| 32 |
*/ |
| 33 |
class Form_Handler { |
| 34 |
|
| 35 |
public $hook_key = 'jet_form_builder_submit'; |
| 36 |
public $hook_val = 'submit'; |
| 37 |
public $form_data = array(); |
| 38 |
public $response_data = array(); |
| 39 |
public $is_ajax = false; |
| 40 |
public $is_success = false; |
| 41 |
public $response_args = array(); |
| 42 |
public $user_journey_data = array(); |
| 43 |
|
| 44 |
public $form_id; |
| 45 |
public $refer; |
| 46 |
public $manager; |
| 47 |
|
| 48 |
/** @var Action_Handler */ |
| 49 |
public $action_handler; |
| 50 |
|
| 51 |
public $form_key = '_jet_engine_booking_form_id'; |
| 52 |
public $refer_key = '_jet_engine_refer'; |
| 53 |
public $post_id_key = '__queried_post_id'; |
| 54 |
public $user_journey_key = '_user_journey'; |
| 55 |
/** |
| 56 |
* @var Request_Handler |
| 57 |
*/ |
| 58 |
public $request_handler; |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Constructor for the class |
| 63 |
*/ |
| 64 |
public function __construct() { |
| 65 |
$this->set_jfb_request_args(); |
| 66 |
$this->action_handler = new Action_Handler(); |
| 67 |
$this->request_handler = new Request_Handler(); |
| 68 |
} |
| 69 |
|
| 70 |
public function call_form() { |
| 71 |
try { |
| 72 |
Form_Request_Router::listen(); |
| 73 |
} catch ( Not_Router_Request $exception ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
add_action( 'jet-form-builder/request', array( $this, 'merge_request' ), 1 ); |
| 78 |
|
| 79 |
if ( wp_doing_ajax() ) { |
| 80 |
add_action( |
| 81 |
'wp_ajax_' . $this->hook_key, |
| 82 |
array( $this, 'process_ajax_form' ) |
| 83 |
); |
| 84 |
add_action( |
| 85 |
'wp_ajax_nopriv_' . $this->hook_key, |
| 86 |
array( $this, 'process_ajax_form' ) |
| 87 |
); |
| 88 |
} else { |
| 89 |
add_action( 'wp_loaded', array( $this, 'process_form' ), 0 ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function core_fields() { |
| 94 |
return apply_filters( |
| 95 |
'jet-form-builder/form-handler/core-fields', |
| 96 |
array( |
| 97 |
$this->form_key => array( |
| 98 |
'callback' => array( $this, 'set_form_id' ), |
| 99 |
), |
| 100 |
$this->refer_key => array( |
| 101 |
'callback' => array( $this, 'set_referrer' ), |
| 102 |
), |
| 103 |
$this->post_id_key => array( |
| 104 |
'callback' => array( Tools::class, 'set_current_post' ), |
| 105 |
), |
| 106 |
$this->user_journey_key => array( |
| 107 |
'callback' => array( $this, 'set_user_journey_data' ), |
| 108 |
), |
| 109 |
) |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
public function merge_request() { |
| 114 |
$fields = array( |
| 115 |
'__form_id' => $this->get_form_id(), |
| 116 |
'__refer' => $this->get_referrer(), |
| 117 |
'__is_ajax' => $this->is_ajax(), |
| 118 |
); |
| 119 |
|
| 120 |
foreach ( $fields as $name => $value ) { |
| 121 |
jet_fb_context()->set_field_type( 'text-field', $name ); |
| 122 |
jet_fb_context()->update_request( $value, $name ); |
| 123 |
jet_fb_context()->make_secure( $name ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public function set_user_journey_data( $data ) { |
| 128 |
$this->user_journey_data = $data; |
| 129 |
} |
| 130 |
|
| 131 |
public function get_user_journey_data() { |
| 132 |
return $this->user_journey_data; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
public function set_referrer( $url ): Form_Handler { |
| 137 |
$refer = remove_query_arg( |
| 138 |
array( 'values', 'status', 'fields' ), |
| 139 |
esc_url_raw( $url ) |
| 140 |
); |
| 141 |
|
| 142 |
$this->refer = wp_validate_redirect( $refer ); |
| 143 |
|
| 144 |
return $this; |
| 145 |
} |
| 146 |
|
| 147 |
public function get_referrer() { |
| 148 |
return $this->refer; |
| 149 |
} |
| 150 |
|
| 151 |
public function set_form_id( $form_id ) { |
| 152 |
$form_id = absint( $form_id ); |
| 153 |
|
| 154 |
$this->form_id = Block_Helper::is_valid_form_post( $form_id, true ) ? $form_id : 0; |
| 155 |
|
| 156 |
return $this; |
| 157 |
} |
| 158 |
|
| 159 |
public function get_form_id() { |
| 160 |
return $this->form_id; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Is ajax form processing or not |
| 165 |
* |
| 166 |
* @return boolean [description] |
| 167 |
*/ |
| 168 |
public function is_ajax(): bool { |
| 169 |
return $this->is_ajax; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Is form processing or not |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function is_process(): bool { |
| 178 |
return ! empty( $this->refer ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Setup form data |
| 183 |
* |
| 184 |
* @return void [description] |
| 185 |
*/ |
| 186 |
public function setup_form() { |
| 187 |
|
| 188 |
if ( $this->form_id ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
$fields = $this->core_fields(); |
| 193 |
|
| 194 |
foreach ( $fields as $field_name => $options ) { |
| 195 |
// phpcs:disable WordPress.Security |
| 196 |
if ( ! isset( $_POST[ $field_name ] ) ) { |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
Tools::call( $options['callback'] ?? false, $_POST[ $field_name ] ); |
| 201 |
// phpcs:enable WordPress.Security |
| 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 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
$this->try_send(); |
| 263 |
|
| 264 |
if ( true === $this->is_success ) { |
| 265 |
$this->send_response( |
| 266 |
array( |
| 267 |
'status' => 'success', |
| 268 |
) |
| 269 |
); |
| 270 |
} else { |
| 271 |
$this->send_response( |
| 272 |
array( |
| 273 |
'status' => 'failed', |
| 274 |
) |
| 275 |
); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
public function try_send() { |
| 280 |
try { |
| 281 |
$this->send_form(); |
| 282 |
} catch ( Request_Exception $exception ) { |
| 283 |
$this->send_response( |
| 284 |
array( |
| 285 |
'status' => $exception->get_form_status(), |
| 286 |
'errors' => $exception->get_fields_errors(), |
| 287 |
) |
| 288 |
); |
| 289 |
} catch ( Action_Exception $exception ) { |
| 290 |
/** |
| 291 |
* @see https://github.com/Crocoblock/jetformbuilder/issues/334 |
| 292 |
*/ |
| 293 |
$this->is_success = $exception->is_success(); |
| 294 |
|
| 295 |
$this->send_response( |
| 296 |
array( |
| 297 |
'status' => $exception->get_form_status(), |
| 298 |
) |
| 299 |
); |
| 300 |
} catch ( Spam_Exception $exception ) { |
| 301 |
$this->send_response( |
| 302 |
array( |
| 303 |
'status' => $exception->get_form_status(), |
| 304 |
) |
| 305 |
); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* @throws Request_Exception |
| 311 |
*/ |
| 312 |
public function send_form() { |
| 313 |
if ( ! $this->form_id ) { |
| 314 |
throw new Request_Exception( 'failed' ); |
| 315 |
} |
| 316 |
|
| 317 |
$this->action_handler->set_form_id( $this->form_id ); |
| 318 |
$this->request_handler->set_form_data(); |
| 319 |
|
| 320 |
do_action( 'jet-form-builder/form-handler/before-send', $this ); |
| 321 |
|
| 322 |
// execute all actions |
| 323 |
jet_fb_events()->execute( Default_Process_Event::class ); |
| 324 |
|
| 325 |
$this->add_response_data( $this->action_handler->response_data ); |
| 326 |
|
| 327 |
$this->is_success = true; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/** |
| 332 |
* Add new properties into response data |
| 333 |
* |
| 334 |
* @param array $data [description] |
| 335 |
*/ |
| 336 |
public function add_response_data( $data = array() ) { |
| 337 |
$this->response_data = array_merge( $this->response_data, $data ); |
| 338 |
} |
| 339 |
|
| 340 |
public function set_response_args( $args ) { |
| 341 |
$this->response_args = array_merge( $this->response_args, $args ); |
| 342 |
|
| 343 |
return $this; |
| 344 |
} |
| 345 |
|
| 346 |
public function get_response_args() { |
| 347 |
return $this->response_args; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Redirect back to refer |
| 352 |
* |
| 353 |
* @param array $args [description] |
| 354 |
* |
| 355 |
* @return void [description] |
| 356 |
*/ |
| 357 |
public function send_response( $args = array() ) { |
| 358 |
try { |
| 359 |
$this->set_response_args( $args ); |
| 360 |
|
| 361 |
jet_fb_events()->execute( Default_Required_Event::class ); |
| 362 |
|
| 363 |
do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success ); |
| 364 |
} catch ( Repository_Exception $exception ) { |
| 365 |
$this->send_raw_response(); |
| 366 |
|
| 367 |
return; |
| 368 |
} catch ( Handler_Exception $exception ) { |
| 369 |
$this->is_success = false; |
| 370 |
$this->response_args = array( |
| 371 |
'status' => $exception->get_form_status(), |
| 372 |
); |
| 373 |
$this->response_data = array(); |
| 374 |
|
| 375 |
$this->send_raw_response(); |
| 376 |
|
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
$this->send_raw_response(); |
| 381 |
} |
| 382 |
|
| 383 |
public function send_raw_response() { |
| 384 |
( new Form_Response\Response( |
| 385 |
$this->get_response_manager(), |
| 386 |
$this->response_data |
| 387 |
) )->init( $this->response_args )->send(); |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Backward compatibility to deprecated properties |
| 392 |
* |
| 393 |
* @param $name |
| 394 |
* |
| 395 |
* @return mixed |
| 396 |
* @throws Repository_Exception |
| 397 |
*/ |
| 398 |
public function __get( $name ) { |
| 399 |
switch ( $name ) { |
| 400 |
case 'parser': |
| 401 |
/** @var Module $rich */ |
| 402 |
$rich = jet_form_builder()->module( 'rich-content' ); |
| 403 |
|
| 404 |
return $rich->get_parser(); |
| 405 |
default: |
| 406 |
return null; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
public function set_jfb_request_args() { |
| 411 |
$options_handler = new Options_Handler(); |
| 412 |
$options_handler->set_jfb_request_args(); |
| 413 |
$options = Tab_Handler_Manager::get_options( 'options-tab' ); |
| 414 |
if ( isset( $options['gfb_request_args_key'] ) ) { |
| 415 |
$this->hook_key = $options['gfb_request_args_key']; |
| 416 |
} |
| 417 |
if ( isset( $options['gfb_request_args_value'] ) ) { |
| 418 |
$this->hook_val = $options['gfb_request_args_value']; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
} |
| 423 |
|