PluginProbe
Advanced Custom Fields: Extended / 0.8.6.9
Advanced Custom Fields: Extended v0.8.6.9
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 All 92 releases
acf-extended / includes / modules / form / form-front.php
form-front.php
725 lines 23.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // vars
13 $this->fields = array(
14
15 '_validate_email' => array(
16 'prefix' => 'acf',
17 'name' => '_validate_email',
18 'key' => '_validate_email',
19 'label' => __('Validate Email', 'acf'),
20 'type' => 'text',
21 'value' => '',
22 'wrapper' => array('style' => 'display:none !important;')
23 )
24
25 );
26
27 // Validation
28 add_action('acf/validate_save_post', array($this, 'validate_save_post'), 1);
29
30 // Submit
31 add_action('wp', array($this, 'check_submit_form'));
32
33 add_shortcode('acfe_form', array($this, 'add_shortcode'));
34
35 }
36
37 function validate_save_post(){
38
39 if(!acfe_form_is_front())
40 return;
41
42 if(acf_maybe_get_POST('_acf_screen') !== 'acfe_form')
43 return;
44
45 $form = acfe_form_decrypt_args();
46
47 if(!$form)
48 return;
49
50 $post_id = acf_maybe_get($form, 'post_id', false);
51 $form_name = acf_maybe_get($form, 'form_name');
52 $form_id = acf_maybe_get($form, 'form_id');
53
54 if(!$form_name || !$form_id)
55 return;
56
57 foreach($this->fields as $k => $field){
58
59 // bail early if no in $_POST
60 if(!isset($_POST['acf'][ $k ]))
61 continue;
62
63 // register
64 acf_add_local_field($field);
65
66 }
67
68 // Honeypot
69 if(!empty($acf['_validate_email'])){
70
71 acf_add_validation_error('', __('Spam Detected', 'acf'));
72
73 }
74
75 // Validation
76 acf_setup_meta($_POST['acf'], 'acfe_form_validation', true);
77
78 // Actions
79 if(have_rows('acfe_form_actions', $form_id)):
80
81 while(have_rows('acfe_form_actions', $form_id)): the_row();
82
83 $action = get_row_layout();
84
85 $alias = get_sub_field('acfe_form_custom_alias');
86
87 // Custom Action
88 if($action === 'custom'){
89
90 $action = get_sub_field('acfe_form_custom_action');
91 $alias = '';
92
93 }
94
95 do_action('acfe/form/validation/' . $action, $form, $post_id, $alias);
96 do_action('acfe/form/validation/' . $action . '/form=' . $form_name, $form, $post_id, $alias);
97
98 if(!empty($alias))
99 do_action('acfe/form/validation/' . $action . '/action=' . $alias, $form, $post_id, $alias);
100
101 endwhile;
102 endif;
103
104 do_action('acfe/form/validation', $form, $post_id);
105 do_action('acfe/form/validation/form=' . $form_name, $form, $post_id);
106
107 acf_reset_meta('acfe_form_validation');
108
109 }
110
111 function check_submit_form(){
112
113 // Verify nonce.
114 if(!acf_verify_nonce('acfe_form'))
115 return;
116
117 $form = acfe_form_decrypt_args();
118
119 if(!$form)
120 return;
121
122 // ACF
123 $_POST['acf'] = isset($_POST['acf']) ? $_POST['acf'] : array();
124
125 // Run kses on all $_POST data.
126 if($form['kses'] && isset($_POST['acf'])){
127
128 $_POST['acf'] = wp_kses_post_deep($_POST['acf']);
129
130 }
131
132 // Validate data and show errors.
133 acf_validate_save_post(true);
134
135 // Submit form.
136 $this->submit_form($form);
137
138 }
139
140 function submit_form($form){
141
142 // vars
143 $post_id = acf_maybe_get($form, 'post_id', false);
144 $form_name = acf_maybe_get($form, 'form_name');
145 $form_id = acf_maybe_get($form, 'form_id');
146
147 acf_save_post(false);
148
149 unset($_FILES);
150
151 acf_setup_meta($_POST['acf'], 'acfe_form_submit', true);
152
153 // Actions
154 if(have_rows('acfe_form_actions', $form_id)):
155
156 while(have_rows('acfe_form_actions', $form_id)): the_row();
157
158 $action = get_row_layout();
159
160 $alias = get_sub_field('acfe_form_custom_alias');
161
162 do_action('acfe/form/make/' . $action, $form, $post_id, $alias);
163
164 endwhile;
165 endif;
166
167 do_action('acfe/form/submit', $form, $post_id);
168 do_action('acfe/form/submit/form=' . $form_name, $form, $post_id);
169
170 acf_reset_meta('acfe_form_submit');
171
172 // vars
173 $return = acf_maybe_get($form, 'return', '');
174
175 // redirect
176 if($return){
177
178 $return = acfe_form_map_field_value($return, $post_id, $form);
179
180 // redirect
181 wp_redirect($return);
182 exit;
183
184 }
185
186 }
187
188 function validate_form($param){
189
190 $form_name = false;
191 $form_id = false;
192
193 // Name
194 if(!is_numeric($param)){
195
196 $form = get_page_by_path($param, OBJECT, 'acfe-form');
197 if(!$form)
198 return false;
199
200 // Form
201 $form_id = $form->ID;
202 $form_name = get_field('acfe_form_name', $form_id);
203
204 }
205
206 // ID
207 else{
208
209 if(get_post_type($param) !== 'acfe-form')
210 return false;
211
212 // Form
213 $form_id = $param;
214 $form_name = get_field('acfe_form_name', $form_id);
215
216 }
217
218 if(!$form_name || !$form_id)
219 return false;
220
221 // Defaults
222 $defaults = array(
223
224 // General
225 'form_name' => $form_name,
226 'form_id' => $form_id,
227 'post_id' => acf_get_valid_post_id(),
228 'field_groups' => false,
229 'field_groups_rules' => false,
230 'post_field_groups' => false,
231 'form' => true,
232 'form_attributes' => array(),
233 'fields_attributes' => array(),
234 'html_before_fields' => '',
235 'custom_html_enabled' => '',
236 'custom_html' => '',
237 'html_after_fields' => '',
238 'form_submit' => true,
239 'submit_value' => __('Update', 'acf'),
240 'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />',
241 'html_submit_spinner' => '<span class="acf-spinner"></span>',
242
243 // Submission
244 'hide_error' => '',
245 'hide_unload' => '',
246 'hide_revalidation' => '',
247 'errors_position' => 'above',
248 'errors_class' => '',
249 'updated_message' => __('Post updated', 'acf'),
250 'html_updated_message' => '<div id="message" class="updated">%s</div>',
251 'updated_hide_form' => false,
252 'return' => '',
253
254 // Mapping
255 'map' => array(),
256
257 // Advanced
258 'honeypot' => true,
259 'kses' => true,
260 'uploader' => 'basic',
261 'field_el' => 'div',
262 'label_placement' => 'top',
263 'instruction_placement' => 'label'
264
265 );
266
267 $defaults['form_attributes'] = wp_parse_args($defaults['form_attributes'], array(
268 'id' => '',
269 'class' => 'acfe-form',
270 'action' => '',
271 'method' => 'post',
272 'data-fields-class' => '',
273 'data-hide-unload' => '',
274 'data-hide-error' => '',
275 'data-hide-revalidation'=> '',
276 'data-errors-position' => '',
277 'data-errors-class' => '',
278 ));
279
280 $defaults['fields_attributes'] = wp_parse_args($defaults['fields_attributes'], array(
281 'wrapper_class' => '',
282 'class' => '',
283 ));
284
285 // Field Groups
286 $defaults['field_groups'] = get_field('acfe_form_field_groups', $form_id);
287 $defaults['field_groups_rules'] = get_field('acfe_form_field_groups_rules', $form_id);
288 $defaults['post_field_groups'] = get_field('acfe_form_post_field_groups', $form_id);
289
290 // General
291 $defaults['form'] = get_field('acfe_form_form_element', $form_id);
292
293 $form_attributes = get_field('acfe_form_attributes', $form_id);
294
295 if(!empty($form_attributes['acfe_form_attributes_class']))
296 $defaults['form_attributes']['class'] .= ' ' . $form_attributes['acfe_form_attributes_class'];
297
298 if(!empty($form_attributes['acfe_form_attributes_id']))
299 $defaults['form_attributes']['id'] = $form_attributes['acfe_form_attributes_id'];
300
301 $acfe_form_fields_attributes = get_field('acfe_form_fields_attributes', $form_id);
302
303 if(isset($acfe_form_fields_attributes['acfe_form_fields_wrapper_class']))
304 $defaults['fields_attributes']['wrapper_class'] = $acfe_form_fields_attributes['acfe_form_fields_wrapper_class'];
305
306 if(isset($acfe_form_fields_attributes['acfe_form_fields_class']))
307 $defaults['fields_attributes']['class'] = $acfe_form_fields_attributes['acfe_form_fields_class'];
308
309 $defaults['html_before_fields'] = get_field('acfe_form_html_before_fields', $form_id);
310 $defaults['custom_html_enabled'] = get_field('acfe_form_custom_html_enable', $form_id);
311 $defaults['custom_html'] = get_field('acfe_form_custom_html', $form_id);
312 $defaults['html_after_fields'] = get_field('acfe_form_html_after_fields', $form_id);
313 $defaults['form_submit'] = get_field('acfe_form_form_submit', $form_id);
314 $defaults['submit_value'] = get_field('acfe_form_submit_value', $form_id);
315 $defaults['html_submit_button'] = get_field('acfe_form_html_submit_button', $form_id);
316 $defaults['html_submit_spinner'] = get_field('acfe_form_html_submit_spinner', $form_id);
317
318 // Validation
319 $defaults['errors_position'] = get_field('acfe_form_errors_position', $form_id);
320 $defaults['errors_class'] = get_field('acfe_form_errors_class', $form_id);
321 $defaults['hide_error'] = get_field('acfe_form_hide_error', $form_id);
322 $defaults['hide_unload'] = get_field('acfe_form_hide_unload', $form_id);
323 $defaults['hide_revalidation'] = get_field('acfe_form_hide_revalidation', $form_id);
324
325 // Submission
326 $defaults['updated_message'] = get_field('acfe_form_updated_message', $form_id);
327 $defaults['html_updated_message'] = get_field('acfe_form_html_updated_message', $form_id);
328 $defaults['updated_hide_form'] = get_field('acfe_form_updated_hide_form', $form_id);
329 $defaults['return'] = get_field('acfe_form_return', $form_id);
330
331 // Advanced
332 $defaults['honeypot'] = get_field('acfe_form_honeypot', $form_id);
333 $defaults['kses'] = get_field('acfe_form_kses', $form_id);
334 $defaults['uploader'] = get_field('acfe_form_uploader', $form_id);
335 $defaults['form_field_el'] = get_field('acfe_form_form_field_el', $form_id);
336 $defaults['label_placement'] = get_field('acfe_form_label_placement', $form_id);
337 $defaults['field_el'] = get_field('acf-field_acfe_form_form_field_el', $form_id);
338 $defaults['instruction_placement'] = get_field('acfe_form_instruction_placement', $form_id);
339
340 //$args = wp_parse_args($param, $defaults);
341 $args = $defaults;
342
343 // Override
344 if(!empty($args['fields_attributes']['class']))
345 $args['form_attributes']['data-fields-class'] = $args['fields_attributes']['class'];
346
347 if(!empty($args['hide_error']))
348 $args['form_attributes']['data-hide-error'] = $args['hide_error'];
349
350 if(!empty($args['hide_unload']))
351 $args['form_attributes']['data-hide-unload'] = $args['hide_unload'];
352
353 if(!empty($args['hide_revalidation']))
354 $args['form_attributes']['data-hide-revalidation'] = $args['hide_revalidation'];
355
356 if(!empty($args['errors_position']))
357 $args['form_attributes']['data-errors-position'] = $args['errors_position'];
358
359 if(!empty($args['errors_class']))
360 $args['form_attributes']['data-errors-class'] = $args['errors_class'];
361
362 if(acf_maybe_get_POST('acf')){
363
364 acf_setup_meta($_POST['acf'], 'acfe_form_load', true);
365
366 }
367
368 // Args
369 $args = apply_filters('acfe/form/load', $args, $args['post_id']);
370 $args = apply_filters('acfe/form/load/form=' . $form_name, $args, $args['post_id']);
371
372 // Load
373 if(have_rows('acfe_form_actions', $form_id)):
374 while(have_rows('acfe_form_actions', $form_id)): the_row();
375
376 $action = get_row_layout();
377
378 $alias = get_sub_field('acfe_form_custom_alias');
379
380 // Custom Action
381 if($action === 'custom'){
382
383 $action = get_sub_field('acfe_form_custom_action');
384 $alias = '';
385
386 }
387
388 $args = apply_filters('acfe/form/load/' . $action, $args, $args['post_id'], $alias);
389 $args = apply_filters('acfe/form/load/' . $action . '/form=' . $form_name, $args, $args['post_id'], $alias);
390
391 if(!empty($alias))
392 $args = apply_filters('acfe/form/load/' . $action . '/action=' . $alias, $args, $args['post_id']);
393
394 endwhile;
395 endif;
396
397 if(acf_maybe_get_POST('acf')){
398
399 acf_reset_meta('acfe_form_load');
400
401 }
402
403 return $args;
404
405 }
406
407 /*
408 * ACFE Form: render_form
409 *
410 */
411 function render_form($args = array()){
412
413 $args = $this->validate_form($args);
414
415 // bail early if no args
416 if(!$args)
417 return false;
418
419 // load acf scripts
420 acf_enqueue_scripts();
421
422 // Register local fields.
423 foreach($this->fields as $k => $field){
424
425 acf_add_local_field($field);
426
427 }
428
429 // honeypot
430 if($args['honeypot']){
431
432 $fields[] = acf_get_field('_validate_email');
433
434 }
435
436 // Updated message
437 if(acfe_form_is_submitted($args['form_name'])){
438
439 // Trigger Success JS
440 echo '<div class="acfe-form-success" data-form-name="' . $args['form_name'] . '" data-form-id="' . $args['form_id'] . '"></div>';
441
442 if(!empty($args['updated_message'])){
443
444 $message = $args['updated_message'];
445
446 if(acf_maybe_get_POST('acf')){
447
448 $message = acfe_form_map_field_value($args['updated_message'], $args['post_id'], $args);
449
450 }
451
452 if(!empty($args['html_updated_message'])){
453
454 printf($args['html_updated_message'], wp_unslash($message));
455
456 }else{
457
458 echo $message;
459
460 }
461
462 }
463
464 // Hide form
465 if($args['updated_hide_form']){
466
467 return false;
468
469 }
470
471 }
472
473 if(!empty($args['fields_attributes']['wrapper_class']) || !empty($args['fields_attributes']['class']) || $args['label_placement'] === 'hidden'){
474
475 add_filter('acf/prepare_field', function($field) use($args){
476
477 if(!$field)
478 return $field;
479
480 if(!empty($args['fields_attributes']['wrapper_class']))
481 $field['wrapper']['class'] .= ' ' . $args['fields_attributes']['wrapper_class'];
482
483 if(!empty($args['fields_attributes']['class']))
484 $field['class'] .= ' ' . $args['fields_attributes']['class'];
485
486 if($args['label_placement'] === 'hidden')
487 $field['label'] = false;
488
489 return $field;
490
491 });
492
493 }
494
495
496 if(!empty($args['map'])){
497
498 foreach($args['map'] as $field_key => $array){
499
500 add_filter('acf/prepare_field/key=' . $field_key, function($field) use($array){
501
502 if(!$field)
503 return $field;
504
505 $field = array_merge($field, $array);
506
507 return $field;
508
509 });
510
511 }
512
513 }
514
515 // uploader (always set incase of multiple forms on the page)
516 acf_disable_filter('acfe/form/uploader');
517
518 if($args['uploader'] !== 'default'){
519
520 acf_enable_filter('acfe/form/uploader');
521
522 acf_update_setting('uploader', $args['uploader']);
523
524 }
525
526 $wrapper = 'div';
527
528 if($args['form'])
529 $wrapper = 'form';
530
531 ?>
532
533 <<?php echo $wrapper; ?> <?php acf_esc_attr_e($args['form_attributes']); ?>>
534
535 <?php
536
537 // render post data
538 acf_form_data(array(
539 'screen' => 'acfe_form',
540 'post_id' => $args['post_id'],
541 'form' => acf_encrypt(json_encode($args))
542 ));
543
544 $label_placement = false;
545 if($args['label_placement'] !== 'hidden')
546 $label_placement = '-' . $args['label_placement'];
547
548 ?>
549 <div class="acf-fields acf-form-fields <?php echo $label_placement; ?>">
550
551 <?php
552
553 // html before fields
554 echo $args['html_before_fields'];
555
556 // Custom HTML
557 if(!empty($args['custom_html_enabled']) && !empty($args['custom_html'])){
558
559 echo acfe_form_render_fields($args['custom_html'], $args['post_id'], $args);
560
561 }
562
563 // Normal Render
564 else{
565
566 // vars
567 $field_groups = array();
568 $fields = array();
569
570 // Post Field groups (Deprecated)
571 if($args['post_field_groups']){
572
573 // Override Field Groups
574 $post_field_groups = acf_get_field_groups(array(
575 'post_id' => $args['post_field_groups']
576 ));
577
578 $args['field_groups'] = wp_list_pluck($post_field_groups, 'key');
579
580 }
581
582 // Field groups
583 if($args['field_groups']){
584
585 foreach($args['field_groups'] as $selector){
586
587 // Bypass Author Module
588 if($selector === 'group_acfe_author')
589 continue;
590
591 $field_groups[] = acf_get_field_group($selector);
592
593 }
594
595 }
596
597 // Apply Field Groups Rules
598 if($args['field_groups_rules']){
599
600 if(!empty($field_groups)){
601
602 $post_id = get_the_ID();
603
604 $filter = array(
605 'post_id' => $post_id,
606 'post_type' => get_post_type($post_id),
607 );
608
609 $filtered = array();
610
611 foreach($field_groups as $field_group){
612
613 // Deleted field group
614 if(!isset($field_group['location']))
615 continue;
616
617 // Force active
618 $field_group['active'] = true;
619
620 if(acf_get_field_group_visibility($field_group, $filter)){
621
622 $filtered[] = $field_group;
623
624 }
625
626 }
627
628 $field_groups = $filtered;
629
630 }
631
632 }
633
634 //load fields based on field groups
635 if(!empty($field_groups)){
636
637 foreach($field_groups as $field_group){
638
639 $field_group_fields = acf_get_fields($field_group);
640
641 if(!empty($field_group_fields)){
642
643 foreach(array_keys($field_group_fields) as $i){
644
645 $fields[] = acf_extract_var($field_group_fields, $i);
646
647 }
648
649 }
650
651 }
652
653 }
654
655 acf_render_fields($fields, acf_uniqid('acfe_form'), $args['field_el'], $args['instruction_placement']);
656
657 }
658
659 // html after fields
660 echo $args['html_after_fields'];
661
662 ?>
663
664 </div>
665
666 <?php if($args['form_submit']): ?>
667
668 <div class="acf-form-submit">
669
670 <?php printf($args['html_submit_button'], $args['submit_value']); ?>
671 <?php echo $args['html_submit_spinner']; ?>
672
673 </div>
674
675 <?php endif; ?>
676
677 </<?php echo $wrapper; ?>>
678
679 <?php
680
681 }
682
683 function add_shortcode($atts){
684
685 $atts = shortcode_atts(array(
686 'name' => false,
687 'id' => false
688 ), $atts, 'acfe_form');
689
690 if(is_admin())
691 return;
692
693 if(!empty($atts['name'])){
694
695 ob_start();
696
697 acfe_form($atts['name']);
698
699 return ob_get_clean();
700
701 }
702
703 if(!empty($atts['id'])){
704
705 ob_start();
706
707 acfe_form($atts['id']);
708
709 return ob_get_clean();
710
711 }
712
713 }
714
715 }
716
717 acfe()->form_front = new acfe_form_front();
718
719 endif;
720
721 function acfe_form($args = array()){
722
723 acfe()->form_front->render_form($args);
724
725 }