actions
5 years ago
admin
5 years ago
blocks
5 years ago
classes
5 years ago
compatibility
5 years ago
exceptions
5 years ago
gateways
5 years ago
generators
5 years ago
integrations
5 years ago
shortcodes
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
273 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 | |
| 10 | /** |
| 11 | * Form builder class |
| 12 | */ |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Define Jet_Engine_Booking_Forms_Handler class |
| 21 | */ |
| 22 | class Form_Handler { |
| 23 | |
| 24 | public $hook_key = 'jet_form_builder_submit'; |
| 25 | public $hook_val = 'submit'; |
| 26 | public $form_data = array(); |
| 27 | public $response_data = array(); |
| 28 | public $is_ajax = false; |
| 29 | public $is_success = false; |
| 30 | public $response_status = 'failed'; |
| 31 | |
| 32 | public $form_id; |
| 33 | public $refer; |
| 34 | public $manager; |
| 35 | public $action_handler; |
| 36 | public $request_data; |
| 37 | |
| 38 | /** |
| 39 | * Constructor for the class |
| 40 | */ |
| 41 | function __construct() { |
| 42 | |
| 43 | if ( wp_doing_ajax() ) { |
| 44 | |
| 45 | add_action( |
| 46 | 'wp_ajax_' . $this->hook_key, |
| 47 | array( $this, 'process_ajax_form' ) |
| 48 | ); |
| 49 | add_action( |
| 50 | 'wp_ajax_nopriv_' . $this->hook_key, |
| 51 | array( $this, 'process_ajax_form' ) |
| 52 | ); |
| 53 | |
| 54 | } else { |
| 55 | |
| 56 | if ( ! isset( $_REQUEST[ $this->hook_key ] ) || $this->hook_val !== $_REQUEST[ $this->hook_key ] ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | add_action( 'wp_loaded', array( $this, 'process_form' ), 0 ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Is ajax form processing or not |
| 66 | * |
| 67 | * @return boolean [description] |
| 68 | */ |
| 69 | public function is_ajax() { |
| 70 | return $this->is_ajax; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Setup form data |
| 75 | * |
| 76 | * @return [type] [description] |
| 77 | */ |
| 78 | public function setup_form() { |
| 79 | |
| 80 | $form_key = '_jet_engine_booking_form_id'; |
| 81 | $refer_key = '_jet_engine_refer'; |
| 82 | |
| 83 | if ( ! $this->is_ajax ) { |
| 84 | $this->form_id = ! empty( $_REQUEST[ $form_key ] ) ? absint( $_REQUEST[ $form_key ] ) : false; |
| 85 | $this->refer = ! empty( $_REQUEST[ $refer_key ] ) ? esc_url( $_REQUEST[ $refer_key ] ) : false; |
| 86 | } else { |
| 87 | |
| 88 | $values = ! empty( $_REQUEST['values'] ) ? Tools::maybe_recursive_sanitize( $_REQUEST['values'] ) : array(); |
| 89 | |
| 90 | foreach ( $values as $data ) { |
| 91 | if ( $data['name'] === $form_key ) { |
| 92 | $this->form_id = $data['value']; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check if current form has configured gateway |
| 101 | * |
| 102 | * @return boolean [description] |
| 103 | */ |
| 104 | public function has_gateway() { |
| 105 | return apply_filters( 'jet-form-builder/form-handler/has-gateways', false, $this->form_id ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Process form with Ajax |
| 110 | * |
| 111 | * @return [type] [description] |
| 112 | */ |
| 113 | public function process_ajax_form() { |
| 114 | $this->is_ajax = true; |
| 115 | $this->process_form(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Process current form |
| 120 | * |
| 121 | * @return [type] [description] |
| 122 | */ |
| 123 | public function process_form() { |
| 124 | |
| 125 | $this->setup_form(); |
| 126 | |
| 127 | $this->try_set_data(); |
| 128 | |
| 129 | do_action( 'jet-form-builder/form-handler/before-send', $this ); |
| 130 | |
| 131 | $this->try_to_do_actions(); |
| 132 | |
| 133 | do_action( 'jet-form-builder/form-handler/after-send', $this, $this->is_success ); |
| 134 | |
| 135 | if ( true === $this->is_success ) { |
| 136 | $this->send_response( array( |
| 137 | 'status' => 'success', |
| 138 | ) ); |
| 139 | } else { |
| 140 | $this->send_response( array( |
| 141 | 'status' => 'failed', |
| 142 | ) ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public function try_set_data() { |
| 147 | try { |
| 148 | $request = array( |
| 149 | 'form_id' => $this->form_id, |
| 150 | 'is_ajax' => $this->is_ajax, |
| 151 | 'refer' => $this->refer |
| 152 | ); |
| 153 | $this->request_data = ( new Request_Handler( $request ) )->get_form_data(); |
| 154 | |
| 155 | } catch ( Handler_Exception $exception ) { |
| 156 | $this->send_response( array( |
| 157 | 'status' => $exception->get_form_status(), |
| 158 | ) ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | public function try_to_do_actions() { |
| 164 | try { |
| 165 | $this->action_handler = new Action_Handler( $this->form_id, $this->request_data ); |
| 166 | |
| 167 | $this->add_response_data( $this->action_handler->do_actions() ); |
| 168 | $this->is_success = true; |
| 169 | |
| 170 | } catch ( Handler_Exception $exception ) { |
| 171 | $this->send_response( array( |
| 172 | 'status' => $exception->get_form_status(), |
| 173 | ) ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Add new properties into response data |
| 179 | * |
| 180 | * @param array $data [description] |
| 181 | */ |
| 182 | public function add_response_data( $data = array() ) { |
| 183 | $this->response_data = array_merge( $this->response_data, $data ); |
| 184 | } |
| 185 | |
| 186 | public function get_message_builder( $form_id = null ) { |
| 187 | |
| 188 | $form_id = $this->form_id ? $this->form_id : $form_id; |
| 189 | |
| 190 | if ( $this->action_handler && ! empty( $this->action_handler->form_actions ) ) { |
| 191 | $actions = $this->action_handler->form_actions; |
| 192 | } else { |
| 193 | $actions = ( new Action_Handler( $form_id ) )->get_all(); |
| 194 | } |
| 195 | |
| 196 | $data = ( object ) array( |
| 197 | 'form_id' => $form_id, |
| 198 | 'actions' => $actions |
| 199 | ); |
| 200 | |
| 201 | return new Form_Messages_Builder( $data ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Redirect back to refer |
| 206 | * |
| 207 | * @param array $args [description] |
| 208 | * |
| 209 | * @return [type] [description] |
| 210 | */ |
| 211 | public function send_response( $args = array() ) { |
| 212 | |
| 213 | $args = wp_parse_args( $args, array( |
| 214 | 'status' => 'success', |
| 215 | ) ); |
| 216 | |
| 217 | $error_statuses = array( 'validation_failed', 'invalid_email' ); |
| 218 | |
| 219 | $this->response_status = $args['status']; |
| 220 | |
| 221 | $query_args = array( |
| 222 | 'status' => $this->response_status, |
| 223 | ); |
| 224 | |
| 225 | $query_args = array_merge( $query_args, $this->response_data ); |
| 226 | |
| 227 | // Clear form-related arguments |
| 228 | $this->refer = remove_query_arg( array( 'values', 'status', 'fields' ), $this->refer ); |
| 229 | |
| 230 | if ( 'validation_failed' === $this->response_status ) { |
| 231 | if ( $this->is_ajax ) { |
| 232 | $query_args['fields'] = $args['errors']; |
| 233 | } else { |
| 234 | $query_args['fields'] = implode( ',', $args['errors'] ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | $send_values = apply_filters( 'jet-form-builder/form-handler/send-values-on-error', true ); |
| 239 | |
| 240 | if ( ! $this->is_ajax && $send_values && in_array( $this->response_status, $error_statuses ) ) { |
| 241 | $query_args['values'] = $this->form_data; |
| 242 | } |
| 243 | |
| 244 | $query_args = apply_filters( 'jet-form-builder/form-handler/query-args', $query_args, $args, $this ); |
| 245 | |
| 246 | if ( $this->is_ajax ) { |
| 247 | |
| 248 | $messages = $this->get_message_builder()->set_form_status( $this->response_status ); |
| 249 | |
| 250 | ob_start(); |
| 251 | $messages->render_messages(); |
| 252 | $query_args['message'] = ob_get_clean(); |
| 253 | |
| 254 | if ( 'validation_failed' === $this->response_status ) { |
| 255 | ob_start(); |
| 256 | $messages->render_empty_field_message(); |
| 257 | $query_args['field_message'] = ob_get_clean(); |
| 258 | } |
| 259 | |
| 260 | wp_send_json( $query_args ); |
| 261 | } else { |
| 262 | $query_args['status'] = urlencode( $query_args['status'] ); |
| 263 | |
| 264 | $redirect = add_query_arg( $query_args, $this->refer ); |
| 265 | wp_redirect( $redirect ); |
| 266 | die(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | |
| 273 |