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