form-front.php
570 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_form_front')): |
| 7 | |
| 8 | class acfe_form_front{ |
| 9 | |
| 10 | function __construct(){ |
| 11 | |
| 12 | // Validation |
| 13 | add_action('acf/validate_save_post', array($this, 'validate_save_post'), 1); |
| 14 | |
| 15 | // Submit |
| 16 | add_action('wp', array($this, 'check_submit_form')); |
| 17 | |
| 18 | add_shortcode('acfe_form', array($this, 'add_shortcode')); |
| 19 | |
| 20 | } |
| 21 | |
| 22 | function validate_save_post(){ |
| 23 | |
| 24 | if(!acfe_form_is_front()) |
| 25 | return; |
| 26 | |
| 27 | if(!acf_maybe_get_POST('_acf_form')) |
| 28 | return; |
| 29 | |
| 30 | $form = json_decode(acf_decrypt($_POST['_acf_form']), true); |
| 31 | |
| 32 | if(empty($form)) |
| 33 | return; |
| 34 | |
| 35 | $form_name = acf_maybe_get($form, 'form_name'); |
| 36 | $form_id = acf_maybe_get($form, 'form_id'); |
| 37 | $post_id = acf_maybe_get($form, '_acf_post_id'); |
| 38 | |
| 39 | if(!$form_name || !$form_id) |
| 40 | return; |
| 41 | |
| 42 | // Honeypot |
| 43 | if(!empty($acf['_validate_email'])){ |
| 44 | |
| 45 | acf_add_validation_error('', __('Spam Detected', 'acf')); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | // Validation |
| 50 | acf_setup_meta($_POST['acf'], 'acfe_form_validation', true); |
| 51 | |
| 52 | do_action('acfe/form/validation', $form, $post_id); |
| 53 | do_action('acfe/form/validation/name=' . $form_name, $form, $post_id); |
| 54 | do_action('acfe/form/validation/id=' . $form_id, $form, $post_id); |
| 55 | |
| 56 | acf_reset_meta('acfe_form_validation'); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | function check_submit_form(){ |
| 61 | |
| 62 | // Verify nonce. |
| 63 | if(is_admin() || !acf_verify_nonce('acf_form')) |
| 64 | return false; |
| 65 | |
| 66 | // Confirm form has been submitted. |
| 67 | if(!acf_maybe_get_POST('_acf_form')) |
| 68 | return; |
| 69 | |
| 70 | $form = json_decode(acf_decrypt($_POST['_acf_form']), true); |
| 71 | |
| 72 | if(!$form) |
| 73 | return false; |
| 74 | |
| 75 | // ACF |
| 76 | $_POST['acf'] = isset($_POST['acf']) ? $_POST['acf'] : array(); |
| 77 | |
| 78 | // Run kses on all $_POST data. |
| 79 | if($form['kses'] && isset($_POST['acf'])){ |
| 80 | |
| 81 | $_POST['acf'] = wp_kses_post_deep($_POST['acf']); |
| 82 | |
| 83 | } |
| 84 | |
| 85 | // Validate data and show errors. |
| 86 | acf_validate_save_post(true); |
| 87 | |
| 88 | // Submit form. |
| 89 | $this->submit_form($form); |
| 90 | |
| 91 | } |
| 92 | |
| 93 | function submit_form($form){ |
| 94 | |
| 95 | // vars |
| 96 | $post_id = acf_maybe_get($form, 'post_id', 0); |
| 97 | $form_name = acf_maybe_get($form, 'form_name'); |
| 98 | $form_id = acf_maybe_get($form, 'form_id'); |
| 99 | |
| 100 | acf_save_post(false); |
| 101 | |
| 102 | unset($_FILES); |
| 103 | |
| 104 | acf_setup_meta($_POST['acf'], 'acfe_form_submit', true); |
| 105 | |
| 106 | // Actions |
| 107 | if(have_rows('acfe_form_actions', $form_id)): |
| 108 | |
| 109 | while(have_rows('acfe_form_actions', $form_id)): the_row(); |
| 110 | |
| 111 | $action = get_row_layout(); |
| 112 | |
| 113 | // Custom Action |
| 114 | if($action === 'custom'){ |
| 115 | |
| 116 | $action = get_sub_field('acfe_form_custom_action'); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | do_action('acfe/form/submit/action/' . $action, $form, $post_id); |
| 121 | do_action('acfe/form/submit/action/' . $action . '/name=' . $form_name, $form, $post_id); |
| 122 | do_action('acfe/form/submit/action/' . $action . '/id=' . $form_id, $form, $post_id); |
| 123 | |
| 124 | endwhile; |
| 125 | endif; |
| 126 | |
| 127 | do_action('acfe/form/submit', $form, $post_id); |
| 128 | do_action('acfe/form/submit/name=' . $form_name, $form, $post_id); |
| 129 | do_action('acfe/form/submit/id=' . $form_id, $form, $post_id); |
| 130 | |
| 131 | acf_reset_meta('acfe_form_submit'); |
| 132 | |
| 133 | // vars |
| 134 | $return = acf_maybe_get($form, 'return', ''); |
| 135 | |
| 136 | // redirect |
| 137 | if($return){ |
| 138 | |
| 139 | // update %placeholders% |
| 140 | $return = str_replace('%post_id%', $post_id, $return); |
| 141 | $return = str_replace('%post_url%', get_permalink($post_id), $return); |
| 142 | |
| 143 | // redirect |
| 144 | wp_redirect($return); |
| 145 | exit; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | function validate_form($args){ |
| 152 | |
| 153 | $form_name = false; |
| 154 | $form_id = false; |
| 155 | |
| 156 | // String |
| 157 | if(is_string($args)){ |
| 158 | |
| 159 | $form = get_page_by_path($args, OBJECT, 'acfe-form'); |
| 160 | if(!$form) |
| 161 | return false; |
| 162 | |
| 163 | // Form |
| 164 | $form_id = $form->ID; |
| 165 | $form_name = get_field('acfe_form_name', $form_id); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | // Int |
| 170 | elseif(is_int($args)){ |
| 171 | |
| 172 | if(get_post_type($args) !== 'acfe-form') |
| 173 | return false; |
| 174 | |
| 175 | // Form |
| 176 | $form_id = $args; |
| 177 | $form_name = get_field('acfe_form_name', $form_id); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | if(!$form_name || !$form_id) |
| 182 | return false; |
| 183 | |
| 184 | // Defaults |
| 185 | $defaults = array( |
| 186 | |
| 187 | // General |
| 188 | 'form_name' => $form_name, |
| 189 | 'form_id' => $form_id, |
| 190 | 'post_id' => acf_get_valid_post_id(), |
| 191 | 'field_groups' => false, |
| 192 | 'form' => true, |
| 193 | 'form_attributes' => array(), |
| 194 | 'fields_attributes' => array(), |
| 195 | 'html_before_fields' => '', |
| 196 | 'custom_html' => '', |
| 197 | 'html_after_fields' => '', |
| 198 | 'form_submit' => true, |
| 199 | 'submit_value' => __('Update', 'acf'), |
| 200 | 'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />', |
| 201 | 'html_submit_spinner' => '<span class="acf-spinner"></span>', |
| 202 | |
| 203 | // Submission |
| 204 | 'hide_error' => '', |
| 205 | 'hide_unload' => '', |
| 206 | 'errors_position' => 'above', |
| 207 | 'errors_class' => '', |
| 208 | 'updated_message' => __('Post updated', 'acf'), |
| 209 | 'html_updated_message' => '<div id="message" class="updated">%s</div>', |
| 210 | 'updated_hide_form' => false, |
| 211 | 'return' => '', |
| 212 | |
| 213 | // Mapping |
| 214 | 'map' => array(), |
| 215 | |
| 216 | // Advanced |
| 217 | 'honeypot' => true, |
| 218 | 'kses' => true, |
| 219 | 'uploader' => 'basic', |
| 220 | 'field_el' => 'div', |
| 221 | 'label_placement' => 'top', |
| 222 | 'instruction_placement' => 'label' |
| 223 | |
| 224 | ); |
| 225 | |
| 226 | $defaults['form_attributes'] = wp_parse_args($defaults['form_attributes'], array( |
| 227 | 'id' => '', |
| 228 | 'class' => 'acfe-form', |
| 229 | 'action' => '', |
| 230 | 'method' => 'post', |
| 231 | 'data-fields-class' => '', |
| 232 | 'data-hide-error' => '', |
| 233 | 'data-errors-position' => '', |
| 234 | 'data-errors-class' => '', |
| 235 | )); |
| 236 | |
| 237 | $defaults['fields_attributes'] = wp_parse_args($defaults['fields_attributes'], array( |
| 238 | 'wrapper_class' => '', |
| 239 | 'class' => '', |
| 240 | )); |
| 241 | |
| 242 | // Field Groups |
| 243 | $defaults['field_groups'] = get_field('acfe_form_field_groups', $form_id); |
| 244 | |
| 245 | // General |
| 246 | $defaults['form'] = get_field('acfe_form_form_element', $form_id); |
| 247 | |
| 248 | $form_attributes = get_field('acfe_form_attributes', $form_id); |
| 249 | |
| 250 | if(!empty($form_attributes['acfe_form_attributes_class'])) |
| 251 | $defaults['form_attributes']['class'] .= ' ' . $form_attributes['acfe_form_attributes_class']; |
| 252 | |
| 253 | if(!empty($form_attributes['acfe_form_attributes_id'])) |
| 254 | $defaults['form_attributes']['id'] = $form_attributes['acfe_form_attributes_id']; |
| 255 | |
| 256 | $acfe_form_fields_attributes = get_field('acfe_form_fields_attributes', $form_id); |
| 257 | |
| 258 | if(isset($acfe_form_fields_attributes['acfe_form_fields_wrapper_class'])) |
| 259 | $defaults['fields_attributes']['wrapper_class'] = $acfe_form_fields_attributes['acfe_form_fields_wrapper_class']; |
| 260 | |
| 261 | if(isset($acfe_form_fields_attributes['acfe_form_fields_class'])) |
| 262 | $defaults['fields_attributes']['class'] = $acfe_form_fields_attributes['acfe_form_fields_class']; |
| 263 | |
| 264 | $defaults['html_before_fields'] = get_field('acfe_form_html_before_fields', $form_id); |
| 265 | $defaults['custom_html'] = get_field('acfe_form_custom_html', $form_id); |
| 266 | $defaults['html_after_fields'] = get_field('acfe_form_html_after_fields', $form_id); |
| 267 | $defaults['form_submit'] = get_field('acfe_form_form_submit', $form_id); |
| 268 | $defaults['submit_value'] = get_field('acfe_form_submit_value', $form_id); |
| 269 | $defaults['html_submit_button'] = get_field('acfe_form_html_submit_button', $form_id); |
| 270 | $defaults['html_submit_spinner'] = get_field('acfe_form_html_submit_spinner', $form_id); |
| 271 | |
| 272 | // Validation |
| 273 | $defaults['errors_position'] = get_field('acfe_form_errors_position', $form_id); |
| 274 | $defaults['errors_class'] = get_field('acfe_form_errors_class', $form_id); |
| 275 | $defaults['hide_error'] = get_field('acfe_form_hide_error', $form_id); |
| 276 | $defaults['hide_unload'] = get_field('acfe_form_hide_unload', $form_id); |
| 277 | |
| 278 | // Submission |
| 279 | $defaults['updated_message'] = get_field('acfe_form_updated_message', $form_id); |
| 280 | $defaults['html_updated_message'] = get_field('acfe_form_html_updated_message', $form_id); |
| 281 | $defaults['updated_hide_form'] = get_field('acfe_form_updated_hide_form', $form_id); |
| 282 | $defaults['return'] = get_field('acfe_form_return', $form_id); |
| 283 | |
| 284 | // Advanced |
| 285 | $defaults['honeypot'] = get_field('acfe_form_honeypot', $form_id); |
| 286 | $defaults['kses'] = get_field('acfe_form_kses', $form_id); |
| 287 | $defaults['uploader'] = get_field('acfe_form_uploader', $form_id); |
| 288 | $defaults['form_field_el'] = get_field('acfe_form_form_field_el', $form_id); |
| 289 | $defaults['label_placement'] = get_field('acfe_form_label_placement', $form_id); |
| 290 | $defaults['field_el'] = get_field('acf-field_acfe_form_form_field_el', $form_id); |
| 291 | $defaults['instruction_placement'] = get_field('acfe_form_instruction_placement', $form_id); |
| 292 | |
| 293 | $args = wp_parse_args($args, $defaults); |
| 294 | |
| 295 | // Override |
| 296 | if(!empty($args['fields_attributes']['class'])) |
| 297 | $args['form_attributes']['data-fields-class'] = $args['fields_attributes']['class']; |
| 298 | |
| 299 | if(!empty($args['hide_error'])) |
| 300 | $args['form_attributes']['data-hide-error'] = $args['hide_error']; |
| 301 | |
| 302 | if(!empty($args['hide_unload'])) |
| 303 | $args['form_attributes']['data-hide-unload'] = $args['hide_unload']; |
| 304 | |
| 305 | if(!empty($args['errors_position'])) |
| 306 | $args['form_attributes']['data-errors-position'] = $args['errors_position']; |
| 307 | |
| 308 | if(!empty($args['errors_class'])) |
| 309 | $args['form_attributes']['data-errors-class'] = $args['errors_class']; |
| 310 | |
| 311 | // Load |
| 312 | if(have_rows('acfe_form_actions', $form_id)): |
| 313 | while(have_rows('acfe_form_actions', $form_id)): the_row(); |
| 314 | |
| 315 | $action = get_row_layout(); |
| 316 | |
| 317 | // Custom Action |
| 318 | if($action === 'custom'){ |
| 319 | |
| 320 | $action = get_sub_field('acfe_form_custom_action'); |
| 321 | |
| 322 | } |
| 323 | |
| 324 | $args = apply_filters('acfe/form/load/action/' . $action, $args, $args['post_id']); |
| 325 | $args = apply_filters('acfe/form/load/action/' . $action . '/name=' . $form_name, $args, $args['post_id']); |
| 326 | $args = apply_filters('acfe/form/load/action/' . $action . '/id=' . $form_id, $args, $args['post_id']); |
| 327 | |
| 328 | endwhile; |
| 329 | endif; |
| 330 | |
| 331 | // Args |
| 332 | $args = apply_filters('acfe/form/load', $args, $args['post_id']); |
| 333 | $args = apply_filters('acfe/form/load/name=' . $form_name, $args, $args['post_id']); |
| 334 | $args = apply_filters('acfe/form/load/id=' . $form_id, $args, $args['post_id']); |
| 335 | |
| 336 | return $args; |
| 337 | |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * ACFE Form: render_form |
| 342 | * |
| 343 | */ |
| 344 | function render_form($args = array()){ |
| 345 | |
| 346 | $args = $this->validate_form($args); |
| 347 | |
| 348 | // bail early if no args |
| 349 | if(!$args) |
| 350 | return false; |
| 351 | |
| 352 | // load acf scripts |
| 353 | acf_enqueue_scripts(); |
| 354 | |
| 355 | // vars |
| 356 | $field_groups = array(); |
| 357 | $fields = array(); |
| 358 | |
| 359 | // Field groups |
| 360 | if($args['field_groups']){ |
| 361 | |
| 362 | foreach($args['field_groups'] as $selector){ |
| 363 | |
| 364 | $field_groups[] = acf_get_field_group($selector); |
| 365 | |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | |
| 371 | //load fields based on field groups |
| 372 | if(!empty($field_groups)){ |
| 373 | |
| 374 | foreach($field_groups as $field_group){ |
| 375 | |
| 376 | $field_group_fields = acf_get_fields($field_group); |
| 377 | |
| 378 | if(!empty($field_group_fields)){ |
| 379 | |
| 380 | foreach(array_keys($field_group_fields) as $i){ |
| 381 | |
| 382 | $fields[] = acf_extract_var($field_group_fields, $i); |
| 383 | |
| 384 | } |
| 385 | |
| 386 | } |
| 387 | |
| 388 | } |
| 389 | |
| 390 | } |
| 391 | |
| 392 | // honeypot |
| 393 | if($args['honeypot']){ |
| 394 | |
| 395 | $fields[] = acf_get_field('_validate_email'); |
| 396 | |
| 397 | } |
| 398 | |
| 399 | // updated message |
| 400 | if(acf_maybe_get_POST('_acf_form')){ |
| 401 | |
| 402 | $form = json_decode(acf_decrypt($_POST['_acf_form']), true); |
| 403 | |
| 404 | if(acf_maybe_get($form, 'form_name') === $args['form_name']){ |
| 405 | |
| 406 | if(!empty($args['updated_message'])){ |
| 407 | |
| 408 | if(!empty($args['html_updated_message'])){ |
| 409 | |
| 410 | printf($args['html_updated_message'], $args['updated_message']); |
| 411 | |
| 412 | }else{ |
| 413 | |
| 414 | echo $args['updated_message']; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | } |
| 419 | |
| 420 | if($args['updated_hide_form']) |
| 421 | return; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | } |
| 426 | |
| 427 | if(!empty($args['fields_attributes']['wrapper_class']) || !empty($args['fields_attributes']['class'])){ |
| 428 | |
| 429 | add_filter('acf/prepare_field', function($field) use($args){ |
| 430 | |
| 431 | if(!empty($args['fields_attributes']['wrapper_class'])) |
| 432 | $field['wrapper']['class'] .= ' ' . $args['fields_attributes']['wrapper_class']; |
| 433 | |
| 434 | if(!empty($args['fields_attributes']['class'])) |
| 435 | $field['class'] .= ' ' . $args['fields_attributes']['class']; |
| 436 | |
| 437 | return $field; |
| 438 | |
| 439 | }); |
| 440 | |
| 441 | } |
| 442 | |
| 443 | |
| 444 | if(!empty($args['map'])){ |
| 445 | |
| 446 | foreach($args['map'] as $field_key => $array){ |
| 447 | |
| 448 | add_filter('acf/prepare_field/key=' . $field_key, function($field) use($array){ |
| 449 | |
| 450 | $field = array_merge($field, $array); |
| 451 | |
| 452 | return $field; |
| 453 | |
| 454 | }); |
| 455 | |
| 456 | } |
| 457 | |
| 458 | } |
| 459 | |
| 460 | // uploader (always set incase of multiple forms on the page) |
| 461 | acf_update_setting('uploader', $args['uploader']); |
| 462 | |
| 463 | // display form |
| 464 | if($args['form']): ?> |
| 465 | |
| 466 | <form <?php acf_esc_attr_e($args['form_attributes']); ?>> |
| 467 | |
| 468 | <?php endif; |
| 469 | |
| 470 | // render post data |
| 471 | acf_form_data(array( |
| 472 | 'screen' => 'acf_form', |
| 473 | 'post_id' => $args['post_id'], |
| 474 | 'form' => acf_encrypt(json_encode($args)) |
| 475 | )); |
| 476 | |
| 477 | ?> |
| 478 | <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?>"> |
| 479 | |
| 480 | <?php |
| 481 | |
| 482 | // html before fields |
| 483 | echo $args['html_before_fields']; |
| 484 | |
| 485 | // Custom HTML |
| 486 | if(!empty($args['custom_html'])){ |
| 487 | |
| 488 | echo acfe_form_render_fields($args['custom_html'], false, $args); |
| 489 | |
| 490 | } |
| 491 | |
| 492 | // Normal Render |
| 493 | else{ |
| 494 | |
| 495 | acf_render_fields($fields, false, $args['field_el'], $args['instruction_placement']); |
| 496 | |
| 497 | } |
| 498 | |
| 499 | // html after fields |
| 500 | echo $args['html_after_fields']; |
| 501 | |
| 502 | ?> |
| 503 | |
| 504 | </div> |
| 505 | |
| 506 | <?php if($args['form'] || $args['form_submit']): ?> |
| 507 | |
| 508 | <div class="acf-form-submit"> |
| 509 | |
| 510 | <?php printf($args['html_submit_button'], $args['submit_value']); ?> |
| 511 | <?php echo $args['html_submit_spinner']; ?> |
| 512 | |
| 513 | </div> |
| 514 | |
| 515 | <?php endif; ?> |
| 516 | |
| 517 | <?php if($args['form']): ?> |
| 518 | </form> |
| 519 | <?php endif; ?> |
| 520 | <script> |
| 521 | if(window.history.replaceState){ |
| 522 | window.history.replaceState( null, null, window.location.href ); |
| 523 | } |
| 524 | </script> |
| 525 | <?php |
| 526 | |
| 527 | } |
| 528 | |
| 529 | function add_shortcode($atts){ |
| 530 | |
| 531 | $atts = shortcode_atts(array( |
| 532 | 'name' => false, |
| 533 | 'ID' => false |
| 534 | ), $atts, 'acfe_form'); |
| 535 | |
| 536 | if(!empty($atts['name'])){ |
| 537 | |
| 538 | ob_start(); |
| 539 | |
| 540 | acfe_form($atts['name']); |
| 541 | |
| 542 | return ob_get_clean(); |
| 543 | |
| 544 | } |
| 545 | |
| 546 | if(!empty($atts['ID'])){ |
| 547 | |
| 548 | ob_start(); |
| 549 | |
| 550 | acfe_form($atts['ID']); |
| 551 | |
| 552 | return ob_get_clean(); |
| 553 | |
| 554 | } |
| 555 | |
| 556 | return; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | } |
| 561 | |
| 562 | acfe()->form_front = new acfe_form_front(); |
| 563 | |
| 564 | endif; |
| 565 | |
| 566 | function acfe_form($args = array()){ |
| 567 | |
| 568 | acfe()->form_front->render_form($args); |
| 569 | |
| 570 | } |