module-form-action-custom.php
9 months ago
module-form-action-email.php
1 week ago
module-form-action-post.php
3 months ago
module-form-action-redirect.php
9 months ago
module-form-action-term.php
3 months ago
module-form-action-user.php
1 week ago
module-form-action.php
3 months ago
module-form-compatibility.php
3 months ago
module-form-deprecated.php
1 year ago
module-form-fields.php
3 months ago
module-form-front-hooks.php
3 months ago
module-form-front-render.php
1 week ago
module-form-front.php
3 months ago
module-form-shortcode.php
1 week ago
module-form-upgrades.php
3 months ago
module-form.php
3 months ago
module-form-front-hooks.php
464 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_form_front_render_hooks')): |
| 8 | |
| 9 | class acfe_module_form_front_render_hooks{ |
| 10 | |
| 11 | /** |
| 12 | * __construct |
| 13 | */ |
| 14 | function __construct(){ |
| 15 | |
| 16 | add_filter('acfe/form/prepare_form', array($this, 'prepare_form'), 9); |
| 17 | add_action('acfe/form/render_success', array($this, 'render_success'), 9); |
| 18 | |
| 19 | add_action('acfe/form/render_before_form', array($this, 'render_before_form'), 9); |
| 20 | add_action('acfe/form/render_before_fields', array($this, 'render_before_fields'), 9); |
| 21 | add_action('acfe/form/render_fields', array($this, 'render_fields'), 9); |
| 22 | add_action('acfe/form/render_after_fields', array($this, 'render_after_fields'), 9); |
| 23 | add_action('acfe/form/render_submit', array($this, 'render_submit'), 9); |
| 24 | add_action('acfe/form/render_after_form', array($this, 'render_after_form'), 9); |
| 25 | |
| 26 | add_action('acfe/form/validate_form', array($this, 'validate_form'), 9); |
| 27 | add_action('acfe/form/submit_form', array($this, 'submit_form'), 9); |
| 28 | add_filter('acfe/form/load_form', array($this, 'load_form'), 19); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * prepare_form |
| 35 | * |
| 36 | * @param $form |
| 37 | */ |
| 38 | function prepare_form($form){ |
| 39 | |
| 40 | if(!$form){ |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | add_filter("acfe/form/prepare_form/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'prepare_form'), 9); |
| 45 | return apply_filters("acfe/form/prepare_form/form={$form['name']}", $form); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * render_success |
| 52 | * |
| 53 | * @param $form |
| 54 | */ |
| 55 | function render_success($form){ |
| 56 | |
| 57 | add_action("acfe/form/render_success/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_success'), 9); |
| 58 | do_action("acfe/form/render_success/form={$form['name']}", $form); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * render_before_form |
| 65 | * |
| 66 | * @param $form |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | function render_before_form($form){ |
| 71 | |
| 72 | add_action("acfe/form/render_before_form/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_before_form'), 9); |
| 73 | do_action("acfe/form/render_before_form/form={$form['name']}", $form); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * render_before_fields |
| 80 | * |
| 81 | * @param $form |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | function render_before_fields($form){ |
| 86 | |
| 87 | add_action("acfe/form/render_before_fields/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_before_fields'), 9); |
| 88 | do_action("acfe/form/render_before_fields/form={$form['name']}", $form); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * render_fields |
| 95 | * |
| 96 | * @param $form |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | function render_fields($form){ |
| 101 | |
| 102 | add_action("acfe/form/render_fields/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_fields'), 9); |
| 103 | do_action("acfe/form/render_fields/form={$form['name']}", $form); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * render_after_fields |
| 110 | * |
| 111 | * @param $form |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | function render_after_fields($form){ |
| 116 | |
| 117 | add_action("acfe/form/render_after_fields/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_after_fields'), 9); |
| 118 | do_action("acfe/form/render_after_fields/form={$form['name']}", $form); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * render_submit |
| 125 | * |
| 126 | * @param $form |
| 127 | * |
| 128 | * @return void |
| 129 | */ |
| 130 | function render_submit($form){ |
| 131 | |
| 132 | add_action("acfe/form/render_submit/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_submit'), 9); |
| 133 | do_action("acfe/form/render_submit/form={$form['name']}", $form); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * render_after_form |
| 140 | * |
| 141 | * @param $form |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | function render_after_form($form){ |
| 146 | |
| 147 | add_action("acfe/form/render_after_form/form={$form['name']}", array(acf_get_instance('acfe_module_form_front_render'), 'render_after_form'), 9); |
| 148 | do_action("acfe/form/render_after_form/form={$form['name']}", $form); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * validate_form |
| 155 | * |
| 156 | * @param $form |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | function validate_form($form){ |
| 161 | |
| 162 | // validate recaptcha |
| 163 | $this->validate_recaptcha($form); |
| 164 | |
| 165 | // validate form |
| 166 | add_action("acfe/form/validate_form/form={$form['name']}", array($this, 'validate_actions'), 9); |
| 167 | do_action("acfe/form/validate_form/form={$form['name']}", $form); |
| 168 | |
| 169 | // validate value |
| 170 | add_filter('acf/validate_value', array($this, 'validate_value'), 10, 4); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * validate_recaptcha |
| 177 | * |
| 178 | * @param $form |
| 179 | * |
| 180 | * @return void |
| 181 | */ |
| 182 | function validate_recaptcha($form){ |
| 183 | |
| 184 | // vars |
| 185 | $has_recaptcha = false; |
| 186 | |
| 187 | // get form mapped field groups |
| 188 | $field_groups = acfe_as_array($form['field_groups']); |
| 189 | if(empty($field_groups)){ |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | // loop field groups |
| 194 | foreach($field_groups as $field_group_key){ |
| 195 | |
| 196 | // get field group fields |
| 197 | $fields = acf_get_fields($field_group_key); |
| 198 | if(empty($fields)){ |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | // get field details |
| 203 | $fields_details = acfe_get_fields_details_recursive($fields); |
| 204 | if(empty($fields_details)){ |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | // loop fields details |
| 209 | foreach($fields_details as $details){ |
| 210 | if($details['field']['type'] === 'acfe_recaptcha'){ |
| 211 | $has_recaptcha = true; |
| 212 | break 2; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | // has recaptcha: check recaptcha flag |
| 219 | if($has_recaptcha && empty(acf_get_form_data('acfe/form/recaptcha'))){ |
| 220 | acfe_add_validation_error('', __('Invalid reCaptcha, please try again', 'acfe')); |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * validate_actions |
| 228 | * |
| 229 | * @param $form |
| 230 | * |
| 231 | * @return void |
| 232 | */ |
| 233 | function validate_actions($form){ |
| 234 | |
| 235 | // force array |
| 236 | $form['actions'] = acfe_as_array($form['actions']); |
| 237 | |
| 238 | // validate actions |
| 239 | foreach($form['actions'] as $action){ |
| 240 | |
| 241 | // tags context |
| 242 | acfe_add_context('action', $action); |
| 243 | |
| 244 | // validate action |
| 245 | do_action("acfe/form/validate_{$action['action']}", $form, $action); |
| 246 | do_action("acfe/form/validate_{$action['action']}/form={$form['name']}", $form, $action); |
| 247 | do_action("acfe/form/validate_{$action['action']}/action={$action['name']}", $form, $action); |
| 248 | |
| 249 | // tags context |
| 250 | acfe_delete_context('action'); |
| 251 | |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * validate_value |
| 259 | * |
| 260 | * @param $valid |
| 261 | * @param $value |
| 262 | * @param $field |
| 263 | * @param $input |
| 264 | * |
| 265 | * @return void |
| 266 | */ |
| 267 | function validate_value($valid, $value, $field, $input){ |
| 268 | |
| 269 | // get form |
| 270 | $form = acf_get_form_data('acfe/form'); |
| 271 | |
| 272 | // filter |
| 273 | add_filter('acfe/form/validate_value', array($this, 'validate_field_value'), 9, 5); |
| 274 | add_filter('acfe/form/validate_value', array($this, 'validate_field_value_after'), 999, 5); |
| 275 | $valid = apply_filters('acfe/form/validate_value', $valid, $value, $field, $input, $form); |
| 276 | |
| 277 | return $valid; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * validate_field_value |
| 284 | * |
| 285 | * @param $valid |
| 286 | * @param $value |
| 287 | * @param $field |
| 288 | * @param $input |
| 289 | * @param $form |
| 290 | * |
| 291 | * @return mixed|null |
| 292 | */ |
| 293 | function validate_field_value($valid, $value, $field, $input, $form){ |
| 294 | |
| 295 | // variations |
| 296 | $valid = apply_filters("acfe/form/validate_value/form={$form['name']}", $valid, $value, $field, $input, $form); |
| 297 | $valid = apply_filters("acfe/form/validate_value/type={$field['type']}", $valid, $value, $field, $input, $form); |
| 298 | $valid = apply_filters("acfe/form/validate_value/name={$field['_name']}", $valid, $value, $field, $input, $form); |
| 299 | $valid = apply_filters("acfe/form/validate_value/key={$field['key']}", $valid, $value, $field, $input, $form); |
| 300 | |
| 301 | return $valid; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | /** |
| 306 | * validate_field_value_after |
| 307 | * |
| 308 | * @param $valid |
| 309 | * @param $value |
| 310 | * @param $field |
| 311 | * @param $input |
| 312 | * @param $form |
| 313 | * |
| 314 | * @return mixed|null |
| 315 | */ |
| 316 | function validate_field_value_after($valid, $value, $field, $input, $form){ |
| 317 | |
| 318 | // field is invalid and not required |
| 319 | if(!$valid && empty($field['required'])){ |
| 320 | $valid = __('Validation failed', 'acf'); // add generic message |
| 321 | } |
| 322 | |
| 323 | return $valid; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /** |
| 328 | * submit_form |
| 329 | * |
| 330 | * @param $form |
| 331 | * |
| 332 | * @return void |
| 333 | */ |
| 334 | function submit_form($form){ |
| 335 | |
| 336 | add_action("acfe/form/submit_form/form={$form['name']}", array($this, 'submit_actions'), 9); |
| 337 | do_action("acfe/form/submit_form/form={$form['name']}", $form); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /** |
| 343 | * submit_actions |
| 344 | * |
| 345 | * @param $form |
| 346 | * |
| 347 | * @return void |
| 348 | */ |
| 349 | function submit_actions($form){ |
| 350 | |
| 351 | // force array |
| 352 | $form['actions'] = acfe_as_array($form['actions']); |
| 353 | |
| 354 | // submit actions |
| 355 | foreach($form['actions'] as $action){ |
| 356 | |
| 357 | // tags context |
| 358 | acfe_add_context('action', $action); |
| 359 | |
| 360 | // prepare action |
| 361 | $action = apply_filters("acfe/form/prepare_{$action['action']}", $action, $form); |
| 362 | if($action){$action = apply_filters("acfe/form/prepare_{$action['action']}/form={$form['name']}", $action, $form);} |
| 363 | if($action){$action = apply_filters("acfe/form/prepare_{$action['action']}/action={$action['name']}", $action, $form);} |
| 364 | |
| 365 | if($action === false){ |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | // make action |
| 370 | do_action("acfe/form/make_{$action['action']}", $form, $action); |
| 371 | |
| 372 | // tags context |
| 373 | acfe_delete_context('action'); |
| 374 | |
| 375 | } |
| 376 | |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /** |
| 381 | * load_form |
| 382 | * |
| 383 | * @param $form |
| 384 | * |
| 385 | * @return false|mixed|null |
| 386 | */ |
| 387 | function load_form($form){ |
| 388 | |
| 389 | if(!$form){ |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | // load actions |
| 394 | add_filter("acfe/form/load_form/form={$form['name']}", array($this, 'load_actions'), 19); |
| 395 | return apply_filters("acfe/form/load_form/form={$form['name']}", $form); |
| 396 | |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /** |
| 401 | * load_actions |
| 402 | * |
| 403 | * @param $form |
| 404 | * |
| 405 | * @return mixed|null |
| 406 | */ |
| 407 | function load_actions($form){ |
| 408 | |
| 409 | if(!$form){ |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | // backup map without actions injecting values |
| 414 | $form['map_default'] = $form['map']; |
| 415 | |
| 416 | // update context |
| 417 | acfe_add_context('form', $form); |
| 418 | |
| 419 | // tags context |
| 420 | $opt = array('context' => 'display'); |
| 421 | |
| 422 | // apply tags |
| 423 | acfe_apply_tags($form['attributes']['form']['class'], $opt); |
| 424 | acfe_apply_tags($form['attributes']['form']['id'], $opt); |
| 425 | acfe_apply_tags($form['attributes']['fields']['wrapper_class'], $opt); |
| 426 | acfe_apply_tags($form['attributes']['fields']['class'], $opt); |
| 427 | acfe_apply_tags($form['attributes']['submit']['value'], $opt); |
| 428 | acfe_apply_tags($form['attributes']['submit']['button'], $opt); |
| 429 | acfe_apply_tags($form['attributes']['submit']['spinner'], $opt); |
| 430 | acfe_apply_tags($form['validation']['errors_class'], $opt); |
| 431 | |
| 432 | // deprecated |
| 433 | if(isset($form['return'])){ |
| 434 | acfe_apply_tags($form['return'], $opt); |
| 435 | } |
| 436 | |
| 437 | // load form per action |
| 438 | foreach($form['actions'] as $action){ |
| 439 | |
| 440 | // tags context |
| 441 | acfe_add_context('action', $action); |
| 442 | |
| 443 | // load action |
| 444 | $form = apply_filters("acfe/form/load_{$action['action']}", $form, $action); |
| 445 | $form = apply_filters("acfe/form/load_{$action['action']}/form={$form['name']}", $form, $action); |
| 446 | $form = apply_filters("acfe/form/load_{$action['action']}/action={$action['name']}", $form, $action); |
| 447 | |
| 448 | // tags context |
| 449 | acfe_delete_context('action'); |
| 450 | |
| 451 | } |
| 452 | |
| 453 | // update context |
| 454 | acfe_add_context('form', $form); |
| 455 | |
| 456 | return $form; |
| 457 | |
| 458 | } |
| 459 | |
| 460 | } |
| 461 | |
| 462 | acf_new_instance('acfe_module_form_front_render_hooks'); |
| 463 | |
| 464 | endif; |