actions
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
license
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-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
267 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 | * @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( $_POST[ $this->post_id_key ] ) ? get_post( $_POST[ $this->post_id_key ] ) : null; |
| 100 | $this->form_id = ! empty( $_POST[ $this->form_key ] ) ? absint( $_POST[ $this->form_key ] ) : false; |
| 101 | $this->refer = ! empty( $_POST[ $this->refer_key ] ) ? esc_url( $_POST[ $this->refer_key ] ) : false; |
| 102 | } else { |
| 103 | $values = ! empty( $_POST['values'] ) ? Tools::maybe_recursive_sanitize( $_POST['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 | $this->refer = wp_validate_redirect( $this->refer ); |
| 122 | } |
| 123 | |
| 124 | private function get_response_manager() { |
| 125 | if ( ! $this->form_id ) { |
| 126 | $this->setup_form(); |
| 127 | } |
| 128 | |
| 129 | if ( $this->is_ajax ) { |
| 130 | $action_handler = $this->action_handler ? $this->action_handler : new Action_Handler( $this->form_id ); |
| 131 | |
| 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 | if ( ! $this->form_id || ! $this->refer ) { |
| 163 | $this->add_response_data( array( |
| 164 | 'reload' => true, |
| 165 | ) ); |
| 166 | |
| 167 | $this->send_response( array( |
| 168 | 'status' => 'failed', |
| 169 | ) ); |
| 170 | } |
| 171 | |
| 172 | $this->try_set_data(); |
| 173 | |
| 174 | do_action( 'jet-form-builder/form-handler/before-send', $this ); |
| 175 | |
| 176 | $this->try_to_do_actions(); |
| 177 | |
| 178 | do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success ); |
| 179 | |
| 180 | if ( true === $this->is_success ) { |
| 181 | $this->send_response( array( |
| 182 | 'status' => 'success', |
| 183 | ) ); |
| 184 | } else { |
| 185 | $this->send_response( array( |
| 186 | 'status' => 'failed', |
| 187 | ) ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public function try_set_data() { |
| 192 | try { |
| 193 | $request = array( |
| 194 | 'form_id' => $this->form_id, |
| 195 | 'is_ajax' => $this->is_ajax, |
| 196 | 'refer' => $this->refer |
| 197 | ); |
| 198 | $this->action_handler = new Action_Handler( $this->form_id ); |
| 199 | $this->request_handler = new Request_Handler( $request ); |
| 200 | $this->request_data = $this->request_handler->get_form_data(); |
| 201 | |
| 202 | } catch ( Request_Exception $exception ) { |
| 203 | $this->send_response( array( |
| 204 | 'status' => $exception->get_form_status(), |
| 205 | 'errors' => $exception->get_fields_errors() |
| 206 | ) ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | public function try_to_do_actions() { |
| 212 | try { |
| 213 | $this->action_handler->add_request( $this->request_data ); |
| 214 | |
| 215 | $this->add_response_data( $this->action_handler->do_actions() ); |
| 216 | $this->is_success = true; |
| 217 | |
| 218 | } catch ( Handler_Exception $exception ) { |
| 219 | $this->send_response( array( |
| 220 | 'status' => $exception->get_form_status(), |
| 221 | ) ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | public function get_message_builder_data( $form_id ) { |
| 226 | $form_id = $this->form_id ? $this->form_id : $form_id; |
| 227 | |
| 228 | if ( $this->action_handler && ! empty( $this->action_handler->form_actions ) ) { |
| 229 | $actions = $this->action_handler->form_actions; |
| 230 | } else { |
| 231 | $actions = ( new Action_Handler( $form_id ) )->get_all(); |
| 232 | } |
| 233 | |
| 234 | return ( object ) array( |
| 235 | 'form_id' => $form_id, |
| 236 | 'actions' => $actions |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | public function get_message_builder( $form_id ) { |
| 241 | return new Form_Messages\Builder( $this->get_message_builder_data( $form_id ) ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Add new properties into response data |
| 246 | * |
| 247 | * @param array $data [description] |
| 248 | */ |
| 249 | public function add_response_data( $data = array() ) { |
| 250 | $this->response_data = array_merge( $this->response_data, $data ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Redirect back to refer |
| 255 | * |
| 256 | * @param array $args [description] |
| 257 | * |
| 258 | * @return void [description] |
| 259 | */ |
| 260 | public function send_response( $args = array() ) { |
| 261 | ( new Form_Response\Response( $this->get_response_manager(), $this->response_data ) )->init( $args )->send(); |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
| 266 | |
| 267 |