PluginProbe ʕ •ᴥ•ʔ
Advanced Custom Fields: Extended / 0.8
Advanced Custom Fields: Extended v0.8
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 0.8.8.5 0.8.8.6 0.8.8.7 0.8.8.8 0.8.8.9 0.8.9 0.8.9.1 0.8.9.2 0.8.9.3 0.8.9.4 0.8.9.5 0.9 0.9.0.1 0.9.0.2 0.9.0.3 0.9.0.4 0.9.0.5 0.9.0.6 0.9.0.7 0.9.0.8 0.9.0.9 0.9.1 0.9.1.1 0.9.2 0.9.2.1 0.9.2.2 0.9.2.3 0.9.2.4 trunk 0.5 0.5.1 0.5.2 0.5.2.1 0.5.2.3 0.5.5 0.5.5.1 0.5.8 0.5.8.1 0.6 0.6.0.1 0.6.0.2 0.6.1 0.6.3 0.6.5 0.6.7 0.6.7.2 0.7 0.7.0.3 0.7.5 0.7.5.5 0.7.8 0.7.9 0.7.9.3 0.7.9.4 0.7.9.9.8 0.7.9.9.9 0.8 0.8.1 0.8.2 0.8.3 0.8.3.1 0.8.4 0.8.4.1 0.8.4.5 0.8.4.6 0.8.5 0.8.5.5
acf-extended / includes / modules / form / admin.php
acf-extended / includes / modules / form Last commit date
actions 6 years ago admin.php 6 years ago field-group.php 6 years ago form-front.php 6 years ago
admin.php
1921 lines
1 <?php
2
3 if(!defined('ABSPATH'))
4 exit;
5
6 if(!class_exists('acfe_form')):
7
8 class acfe_form{
9
10 public $post_type = '';
11 public $fields_groups = array();
12
13 public $posts = array();
14 public $users = array();
15
16 function __construct(){
17
18 // Post Type
19 $this->post_type = 'acfe-form';
20
21 // Admin
22 add_action('init', array($this, 'init'));
23 add_action('admin_menu', array($this, 'admin_menu'));
24 add_action('current_screen', array($this, 'current_screen'));
25
26 // ACF
27 add_filter('acf/get_post_types', array($this, 'filter_post_type'), 10, 2);
28 add_filter('acf/pre_load_post_id', array($this, 'validate_post_id'), 10, 2);
29
30 // Fields
31 add_filter('acf/prepare_field/name=acfe_form_actions', array($this, 'prepare_actions'));
32 add_filter('acf/prepare_field/name=acfe_form_field_groups', array($this, 'field_groups_choices'));
33 add_filter('acf/prepare_field/name=acfe_form_email_files', array($this, 'prepare_email_files'));
34
35 // Posts
36 $this->posts = array(
37 'acfe_form_post_save_target',
38 'acfe_form_post_save_post_parent',
39 'acfe_form_post_load_source',
40 );
41
42 foreach($this->posts as $tag){
43
44 add_filter('acf/prepare_field/name=' . $tag, array($this, 'prepare_value_post'));
45
46 }
47
48 // Users
49 $this->users = array(
50 'acfe_form_post_save_post_author',
51 'acfe_form_user_save_target',
52 'acfe_form_user_load_source',
53 );
54
55 foreach($this->users as $tag){
56
57 add_filter('acf/prepare_field/name=' . $tag, array($this, 'prepare_value_user'));
58
59 }
60
61 // Terms
62 $this->terms = array(
63 'acfe_form_term_save_target',
64 'acfe_form_term_save_parent',
65 'acfe_form_term_load_source',
66 );
67
68 foreach($this->terms as $tag){
69
70 add_filter('acf/prepare_field/name=' . $tag, array($this, 'prepare_value_term'));
71
72 }
73
74 // Ajax
75 add_action('wp_ajax_acf/fields/select/query', array($this, 'ajax_query_post'), 5);
76 add_action('wp_ajax_nopriv_acf/fields/select/query', array($this, 'ajax_query_post'), 5);
77
78 add_action('wp_ajax_acf/fields/select/query', array($this, 'ajax_query_user'), 5);
79 add_action('wp_ajax_nopriv_acf/fields/select/query', array($this, 'ajax_query_user'), 5);
80
81 add_action('wp_ajax_acf/fields/select/query', array($this, 'ajax_query_term'), 5);
82 add_action('wp_ajax_nopriv_acf/fields/select/query', array($this, 'ajax_query_term'), 5);
83
84 }
85
86 function init(){
87
88 // Post Type
89 register_post_type($this->post_type, array(
90 'label' => __('Forms', 'acf'),
91 'description' => __('Forms', 'acf'),
92 'labels' => array(
93 'name' => __('Forms', 'acf'),
94 'singular_name' => __('Form', 'acf'),
95 'menu_name' => __('Forms', 'acf'),
96 'edit_item' => 'Edit Form',
97 'add_new_item' => 'New Form',
98 ),
99 'supports' => array('title'),
100 'hierarchical' => false,
101 'public' => false,
102 'show_ui' => true,
103 'show_in_menu' => false,
104 'menu_icon' => 'dashicons-feedback',
105 'show_in_admin_bar' => false,
106 'show_in_nav_menus' => false,
107 'can_export' => false,
108 'has_archive' => false,
109 'rewrite' => false,
110 'exclude_from_search' => true,
111 'publicly_queryable' => false,
112 'capabilities' => array(
113 'publish_posts' => acf_get_setting('capability'),
114 'edit_posts' => acf_get_setting('capability'),
115 'edit_others_posts' => acf_get_setting('capability'),
116 'delete_posts' => acf_get_setting('capability'),
117 'delete_others_posts' => acf_get_setting('capability'),
118 'read_private_posts' => acf_get_setting('capability'),
119 'edit_post' => acf_get_setting('capability'),
120 'delete_post' => acf_get_setting('capability'),
121 'read_post' => acf_get_setting('capability'),
122 )
123 ));
124
125 }
126
127 function admin_menu(){
128
129 if(!acf_get_setting('show_admin'))
130 return;
131
132 add_submenu_page('edit.php?post_type=acf-field-group', __('Forms', 'acf'), __('Forms', 'acf'), acf_get_setting('capability'), 'edit.php?post_type=' . $this->post_type);
133
134 }
135
136 function current_screen(){
137
138 global $typenow;
139
140 if($typenow !== $this->post_type)
141 return;
142
143 add_action('load-edit.php', array($this, 'load_list'));
144 add_action('load-post.php', array($this, 'load_post'));
145 add_action('load-post-new.php', array($this, 'load_post_new'));
146
147 add_action('load-post.php', array($this, 'load'));
148 add_action('load-post-new.php', array($this, 'load'));
149
150 }
151
152 function load_list(){
153
154 // Posts per page
155 add_filter('edit_posts_per_page', function(){
156 return 999;
157 });
158
159 // Order
160 add_action('pre_get_posts', function($query){
161
162 if(!$query->is_main_query())
163 return;
164
165 if(!acf_maybe_get($_REQUEST,'orderby'))
166 $query->set('orderby', 'name');
167
168 if(!acf_maybe_get($_REQUEST,'order'))
169 $query->set('order', 'ASC');
170
171 });
172
173 // Columns
174 add_filter('manage_edit-' . $this->post_type . '_columns', array($this, 'admin_columns'));
175 add_action('manage_' . $this->post_type . '_posts_custom_column', array($this, 'admin_columns_html'), 10, 2);
176
177 }
178
179 function load_post(){
180
181 // vars
182 $this->fields_groups = $this->get_fields_groups();
183
184 add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
185
186 add_filter('acf/pre_render_fields', array($this, 'render_integration'), 10, 2);
187
188 }
189
190 function load_post_new(){
191
192 // ...
193
194 }
195
196 function load(){
197
198 // Save Post
199 add_action('acf/save_post', array($this, 'save_form'), 20);
200
201 // Metaboxes
202 remove_meta_box('slugdiv', $this->post_type, 'normal');
203
204 // Menu
205 add_filter('parent_file', function(){
206 return 'edit.php?post_type=acf-field-group';
207 });
208
209 // Submenu
210 add_filter('submenu_file', function(){
211 return 'edit.php?post_type=' . $this->post_type;
212 });
213
214 // Footer
215 add_action('admin_footer', array($this, 'load_footer'));
216
217 }
218
219 function load_footer(){
220 ?>
221 <script type="text/javascript">
222 (function($){
223
224 if(typeof acf === 'undefined')
225 return;
226
227 $('#post').submit(function(e){
228
229 // vars
230 var $title = $('#titlewrap #title');
231
232 // empty
233 if(!$title.val()){
234
235 // prevent default
236 e.preventDefault();
237
238 // alert
239 alert('Form title is required.');
240
241 // focus
242 $title.focus();
243
244 }
245
246 });
247
248 })(jQuery);
249 </script>
250 <?php
251 }
252
253 function filter_post_type($post_types, $args){
254
255 if(empty($post_types))
256 return $post_types;
257
258 foreach($post_types as $k => $post_type){
259
260 if($post_type !== $this->post_type)
261 continue;
262
263 unset($post_types[$k]);
264
265 }
266
267 return $post_types;
268
269 }
270
271 function get_fields_groups($form_id = false){
272
273 if(!$form_id)
274 $form_id = get_the_ID();
275
276 if(!$form_id && isset($_REQUEST['post']))
277 $form_id = $_REQUEST['post'];
278
279 if(!$form_id)
280 return false;
281
282 $return = array();
283
284 // Field Groups
285 $field_groups = get_field('acfe_form_field_groups', $form_id);
286
287 if(!empty($field_groups)){
288
289 foreach($field_groups as $field_group_key){
290
291 $field_group = acf_get_field_group($field_group_key);
292 if(!$field_group)
293 continue;
294
295 $field_group['fields'] = acf_get_fields($field_group);
296
297 $return[] = $field_group;
298
299 }
300
301 }
302
303 // return
304 return $return;
305
306 }
307
308 function save_form($post_id){
309
310 // Get Post
311 $name = get_field('acfe_form_name', $post_id);
312
313 // Update post
314 wp_update_post(array(
315 'ID' => $post_id,
316 'post_name' => $name,
317 ));
318
319 $new_name = get_post_field('post_name', $post_id);
320
321 if($new_name !== $name)
322 update_field('acfe_form_name', $new_name, $post_id);
323
324 }
325
326 function add_meta_boxes(){
327
328 $data = $this->fields_groups;
329
330 if(empty($data))
331 return;
332
333 add_meta_box(
334
335 // ID
336 'acfe-form-details',
337
338 // Title
339 __('Fields', 'acf'),
340
341 // Render
342 array($this, 'render_meta_boxes'),
343
344 // Screen
345 $this->post_type,
346
347 // Position
348 'normal',
349
350 // Priority
351 'default'
352
353 );
354
355 }
356
357 function render_meta_boxes($array, $data){
358
359 foreach($this->fields_groups as $field_group){ ?>
360
361 <?php
362 acf_disable_filters();
363
364 $field_group_db = acf_get_field_group($field_group['key']);
365
366 acf_enable_filters();
367 ?>
368
369 <div class="acf-field">
370
371 <div class="acf-label">
372 <label><a href="<?php echo admin_url('post.php?post=' . $field_group_db['ID'] . '&action=edit'); ?>"><?php echo $field_group['title']; ?></a></label>
373 <p class="description"><?php echo $field_group['key']; ?></p>
374 </div>
375
376 <div class="acf-input">
377
378 <?php if(!empty($field_group['fields'])){ ?>
379
380 <table class="acf-table">
381 <thead>
382 <th class="acf-th" width="25%"><strong>Label</strong></th>
383 <th class="acf-th" width="25%"><strong>Name</strong></th>
384 <th class="acf-th" width="25%"><strong>Key</strong></th>
385 <th class="acf-th" width="25%"><strong>Type</strong></th>
386 </thead>
387
388 <tbody>
389 <?php
390
391 $array = array();
392 foreach($field_group['fields'] as $field){
393
394 $this->get_fields_labels_recursive($array, $field);
395
396 }
397
398 foreach($array as $field_key => $field_label){
399
400 $field = acf_get_field($field_key);
401 $type = acf_get_field_type($field['type']);
402 $type_label = '-';
403 if(isset($type->label))
404 $type_label = $type->label;
405 ?>
406
407 <tr class="acf-row">
408 <td width="25%"><?php echo $field_label; ?></td>
409 <td width="25%"><?php echo $field['name']; ?></td>
410 <td width="25%"><code><?php echo $field_key; ?></code></td>
411 <td width="25%"><?php echo $type_label; ?></td>
412 </tr>
413
414 <?php } ?>
415 </tbody>
416 </table>
417
418 <?php } ?>
419 </div>
420
421 </div>
422
423 <?php } ?>
424
425 <script type="text/javascript">
426 if(typeof acf !== 'undefined'){
427
428 acf.newPostbox(<?php echo wp_json_encode(array(
429 'id' => 'acfe-form-details',
430 'key' => '',
431 'style' => 'default',
432 'label' => 'left',
433 'edit' => false
434 )); ?>);
435
436 }
437 </script>
438 <?php
439
440 }
441
442 function get_fields_labels_recursive(&$array, $field){
443
444 $label = '';
445
446 $ancestors = isset($field['ancestors']) ? $field['ancestors'] : count(acf_get_field_ancestors($field));
447 $label = str_repeat('- ', $ancestors) . $label;
448
449 $label .= !empty($field['label']) ? $field['label'] : '(' . __('no label', 'acf') . ')';
450 $label .= $field['required'] ? ' <span class="acf-required">*</span>' : '';
451
452 $array[$field['key']] = $label;
453
454 if(isset($field['sub_fields']) && !empty($field['sub_fields'])){
455
456 foreach($field['sub_fields'] as $s_field){
457
458 $this->get_fields_labels_recursive($array, $s_field);
459
460 }
461
462 }
463
464 }
465
466 // Field groups choices
467 function field_groups_choices($field){
468
469 acf_disable_filters();
470
471 $field_groups = acf_get_field_groups();
472 if(empty($field_groups))
473 return $field;
474
475 foreach($field_groups as $field_group){
476
477 $field['choices'][$field_group['key']] = $field_group['title'];
478
479 }
480
481 acf_enable_filters();
482
483 return $field;
484
485 }
486
487 function map_field_groups($field){
488
489 $post_id = get_the_ID();
490
491 $field_groups = get_field('acfe_form_field_groups', $post_id);
492
493 if(empty($field_groups))
494 return false;
495
496 foreach($field_groups as $field_group_key){
497
498 $field_group = acf_get_field_group($field_group_key);
499 if(!$field_group)
500 continue;
501
502 $field['choices'][$field_group_key] = $field_group['title'];
503
504 }
505
506 return $field;
507
508 }
509
510 function prepare_actions($field){
511
512 $field['instructions'] = 'Add actions on form submission';
513
514 $data = $this->fields_groups;
515
516 if(empty($data)){
517
518 $field['instructions'] .= '<br /><u>No field groups are currently mapped</u>';
519
520 }
521
522 return $field;
523
524 }
525
526 function prepare_email_files($field){
527
528 $data = $this->fields_groups;
529
530 if(empty($data))
531 return false;
532
533 return $field;
534
535 }
536
537
538 function map_fields_deep($field){
539
540 $choices = array();
541
542 if(!empty($field['choices']))
543 $choices['Generic'] = $field['choices'];
544
545 $fields_choices = $this->get_fields_choices(true);
546
547 if(!empty($fields_choices)){
548
549 $field['choices'] = array_merge($choices, $fields_choices);
550
551 }
552
553 return $field;
554
555 }
556
557 function map_fields($field){
558
559 $fields_choices = $this->get_fields_choices();
560
561 if(empty($fields_choices))
562 return false;
563
564 $field['choices'] = $fields_choices;
565
566 return $field;
567
568 }
569
570 function get_fields_choices($deep = false){
571
572 $data = $this->fields_groups;
573 $choices = array();
574
575 if(empty($data))
576 return false;
577
578 $field_groups = array();
579
580 foreach($data as $field_group){
581
582 if(empty($field_group['fields']))
583 continue;
584
585 foreach($field_group['fields'] as $s_field){
586
587 $field_groups[$field_group['title']][] = $s_field;
588
589 }
590
591 }
592
593 if(!empty($field_groups)){
594
595 foreach($field_groups as $field_group_title => $fields){
596
597 foreach($fields as $field){
598
599 if(isset($choices[$field_group_title][$field['key']]))
600 continue;
601
602 // First level
603 if(!$deep){
604
605 $label = !empty($field['label']) ? $field['label'] : '(' . __('no label', 'acf') . ')';
606 $label .= $field['required'] ? ' *' : '';
607
608 $choices[$field_group_title][$field['key']] = $label. ' (' . $field['key'] . ')';
609
610 // Deep
611 }else{
612
613 $this->get_fields_choices_recursive($choices[$field_group_title], $field);
614
615 }
616
617 }
618
619 }
620
621 }
622
623 return $choices;
624
625 }
626
627 function get_fields_choices_recursive(&$choices, $field){
628
629 $label = '';
630
631 $ancestors = isset($field['ancestors']) ? $field['ancestors'] : count(acf_get_field_ancestors($field));
632 $label = str_repeat('- ', $ancestors) . $label;
633
634 $label .= !empty($field['label']) ? $field['label'] : '(' . __('no label', 'acf') . ')';
635 $label .= $field['required'] ? ' *' : '';
636
637 $choices[$field['key']] = $label. ' (' . $field['key'] . ')';
638
639 if(isset($field['sub_fields']) && !empty($field['sub_fields'])){
640
641 foreach($field['sub_fields'] as $s_field){
642
643 $this->get_fields_choices_recursive($choices, $s_field);
644
645 }
646
647 }
648
649 }
650
651 function render_fields($content, $post_id, $args){
652
653 // Mapping
654 $form_id = $args['form_id'];
655 $form_name = $args['form_name'];
656
657 $mapped_field_groups = $this->get_fields_groups($form_id);
658 $mapped_fields = array();
659
660 if(!empty($mapped_field_groups)){
661
662 foreach($mapped_field_groups as $field_group){
663
664 if(empty($field_group['fields']))
665 continue;
666
667 foreach($field_group['fields'] as $field){
668
669 $mapped_fields[] = $field;
670
671 }
672
673 }
674
675 }
676
677 // Match {field:key}
678 if(preg_match_all('/{field:(.*?)}/', $content, $matches)){
679
680 foreach($matches[1] as $i => $field_key){
681
682 $field = false;
683
684 // Field key
685 if(strpos($field_key, 'field_') === 0){
686
687 $field = acf_get_field($field_key);
688
689 // Field name
690 }else{
691
692 if(!empty($mapped_fields)){
693
694 foreach($mapped_fields as $mapped_field){
695
696 if($mapped_field['name'] !== $field_key)
697 continue;
698
699 $field = $mapped_field;
700 break;
701
702 }
703
704 }
705
706 }
707
708 if(!$field){
709
710 $content = str_replace('{field:' . $field_key . '}', '', $content);
711 continue;
712
713 }
714
715 $fields = array();
716 $fields[] = $field;
717
718 ob_start();
719
720 acf_render_fields($fields, $post_id, $args['field_el'], $args['instruction_placement']);
721
722 $render_field = ob_get_clean();
723
724 $content = str_replace('{field:' . $field_key . '}', $render_field, $content);
725
726 }
727
728 }
729
730 // Match {field_group:key}
731 if(preg_match_all('/{field_group:(.*?)}/', $content, $matches)){
732
733 $field_groups = acf_get_field_groups();
734
735 foreach($matches[1] as $i => $field_group_key){
736
737 $fields = false;
738
739 // Field group key
740 if(strpos($field_group_key, 'group_') === 0){
741
742 $fields = acf_get_fields($field_group_key);
743
744 // Field group title
745 }else{
746
747 if(!empty($field_groups)){
748
749 foreach($field_groups as $field_group){
750
751 if($field_group['title'] !== $field_group_key)
752 continue;
753
754 $fields = acf_get_fields($field_group['key']);
755 break;
756
757 }
758
759 }
760
761 }
762
763 if(!$fields){
764
765 $content = str_replace('{field_group:' . $field_group_key . '}', '', $content);
766 continue;
767
768 }
769
770 ob_start();
771
772 acf_render_fields($fields, $post_id, $args['field_el'], $args['instruction_placement']);
773
774 $render_fields = ob_get_clean();
775
776 $content = str_replace('{field_group:' . $field_group_key . '}', $render_fields, $content);
777
778 }
779
780 }
781
782 return $content;
783
784 }
785
786 function format_value($value, $post_id = 0, $field){
787
788 $return = acf_format_value($value, $post_id, $field);
789
790 // Post Object & Relationship
791 if($field['type'] === 'post_object' || $field['type'] === 'relationship'){
792
793 $value = acf_get_array($value);
794 $array = array();
795
796 foreach($value as $p_id){
797
798 $array[] = get_the_title($p_id);
799
800 }
801
802 $return = implode(', ', $array);
803
804 }
805
806 // User
807 elseif($field['type'] === 'user'){
808
809 $value = acf_get_array($value);
810 $array = array();
811
812 foreach($value as $user_id){
813
814 $user_data = get_userdata($user_id);
815 $array[] = $user_data->user_nicename;
816
817 }
818
819 $return = implode(', ', $array);
820
821 }
822
823 // Taxonomy
824 elseif($field['type'] === 'taxonomy'){
825
826 $value = acf_get_array($value);
827 $array = array();
828
829 foreach($value as $term_id){
830
831 $term = get_term($term_id);
832 $array[] = $term->name;
833
834 }
835
836 $return = implode(', ', $array);
837
838 }
839
840 // Image / File
841 elseif($field['type'] === 'image' || $field['type'] === 'file'){
842
843 $return = $return['title'];
844
845 }
846
847 return $return;
848
849 }
850
851 function map_fields_values($array, &$data = array()){
852
853 if(empty($array))
854 return false;
855
856 foreach($array as $field_key => $value){
857
858 $field = acf_get_field($field_key);
859
860 // bypass _validate_email (honeypot)
861 if(!$field || !isset($field['name']) || $field['name'] === '_validate_email')
862 continue;
863
864 $data[] = array(
865 'label' => $field['label'],
866 'name' => $field['name'],
867 'key' => $field['key'],
868 'field' => $field,
869 'value' => $value,
870 );
871
872 if(is_array($value)){
873
874 $this->map_fields_values($value, $data);
875
876 }
877
878 }
879
880 return $data;
881
882 }
883
884 function map_field_value($content, $acf = false, $post_id = 0){
885
886 if(!$acf)
887 $acf = $_POST['acf'];
888
889 if(!$acf)
890 return false;
891
892 $data = $this->map_fields_values($acf);
893
894 // Field key
895 if(acf_is_field_key($content)){
896
897 if(empty($data))
898 return false;
899
900 foreach($data as $field){
901
902 if($field['key'] !== $content)
903 continue;
904
905 return $field['value'];
906
907 }
908
909 }
910
911 // Content
912 else{
913
914 // Match {field:key}
915 if(preg_match_all('/{field:(.*?)}/', $content, $matches)){
916
917 foreach($matches[1] as $i => $field_key){
918
919 if(!empty($data)){
920
921 foreach($data as $field){
922
923 if($field['name'] !== $field_key && $field['key'] !== $field_key)
924 continue;
925
926 $content = str_replace('{field:' . $field_key . '}', $this->format_value($field['value'], $post_id, $field['field']), $content);
927 break;
928
929 }
930
931 }
932
933 $content = str_replace('{field:' . $field_key . '}', '', $content);
934
935 }
936
937 }
938
939 // Match {fields}
940 if(preg_match('/{fields}/', $content, $matches)){
941
942 $content_html = '';
943
944 if(!empty($data)){
945
946 foreach($data as $field){
947
948 $label = !empty($field['label']) ? $field['label'] : $field['name'];
949
950 $content_html .= $label . ': ' . $this->format_value($field['value'], $post_id, $field['field']) . "<br/>\n";
951
952 }
953
954 }
955
956 $content = str_replace('{fields}', $content_html, $content);
957
958 }
959
960 return $content;
961
962 }
963
964 }
965
966 function map_field_get_value($content){
967
968 // Match {field:key}
969 if(preg_match_all('/{field:(.*?)}/', $content, $matches)){
970
971 foreach($matches[1] as $i => $field_key){
972
973 $value = get_field($field_key);
974 $content = str_replace('{field:' . $field_key . '}', $value, $content);
975
976 }
977
978 }
979
980 return $content;
981
982 }
983
984 function filter_meta($meta, $acf){
985
986 if(empty($meta) || empty($acf))
987 return false;
988
989 foreach($acf as $field_key => $value){
990
991 if(in_array($field_key, $meta))
992 continue;
993
994 unset($acf[$field_key]);
995
996 }
997
998 return $acf;
999
1000 }
1001
1002 // Allow form post_id null
1003 function validate_post_id($null, $post_id){
1004
1005 if($post_id === null)
1006 return false;
1007
1008 return $null;
1009
1010 }
1011
1012 function render_integration($fields, $post_id){
1013
1014 $_fields = $fields;
1015 $last_field = end($_fields);
1016
1017 if(!$last_field || $last_field['name'] !== 'acfe_form_instruction_placement')
1018 return $fields;
1019
1020 $form_id = $post_id;
1021 $form_name = get_field('acfe_form_name', $form_id);
1022
1023 $fields[] = array(
1024 'type' => 'tab',
1025 'name' => '',
1026 'label' => 'Integration',
1027 'value' => '',
1028 );
1029
1030 $fields[] = array(
1031 'type' => 'message',
1032 'name' => '',
1033 'label' => 'Shortcode',
1034 'value' => '',
1035 'message' => '<code>[acfe_form name="' . $form_name . '"]</code> or <code>[acfe_form ID="' . $form_id . '"]</code>',
1036 'new_lines' => false,
1037 );
1038
1039 ob_start();
1040 ?>
1041 <pre>&lt;?php get_header(); ?&gt;
1042
1043 &lt;!-- <?php echo get_the_title($form_id); ?> --&gt;
1044 &lt;?php acfe_form(&apos;<?php echo $form_name; ?>&apos;); ?&gt;
1045
1046 &lt;?php get_footer(); ?&gt;</pre>
1047 <?php $html = ob_get_clean();
1048
1049 $fields[] = array(
1050 'type' => 'message',
1051 'name' => '',
1052 'label' => 'PHP Form Integration',
1053 'value' => '',
1054 'message' => $html,
1055 'new_lines' => false,
1056 );
1057
1058 if(!empty($this->fields_groups)){
1059
1060 foreach($this->fields_groups as $field_group){
1061
1062 if(empty($field_group['fields']))
1063 continue;
1064
1065 $_fields = $field_group['fields'];
1066 break;
1067
1068 }
1069
1070 if(!empty($_fields)){
1071
1072 $field = $_fields[0];
1073
1074 $_form_name = str_replace('-', '_', $form_name);
1075 $_field_name = str_replace('-', '_', sanitize_title($field['name']));
1076
1077 // Field Validation
1078
1079 ob_start();
1080 ?>
1081 <pre>&lt;?php
1082
1083 add_filter(&apos;acf/validate_value/name=<?php echo $field['name']; ?>&apos;, &apos;my_<?php echo $_field_name; ?>_validation&apos;, 10, 4);
1084 function my_<?php echo $_field_name; ?>_validation($valid, $value, $field, $input){
1085
1086 if(!$valid)
1087 return $valid;
1088
1089 if($value === &apos;Hello&apos;)
1090 $valid = &apos;Hello is not allowed&apos;;
1091
1092 return $valid;
1093
1094 }</pre>
1095 <?php $html = ob_get_clean();
1096
1097 $fields[] = array(
1098 'type' => 'message',
1099 'name' => '',
1100 'label' => 'PHP Field Validation',
1101 'value' => '',
1102 'message' => $html,
1103 'new_lines' => false,
1104 );
1105
1106
1107 // Form Validation
1108
1109 ob_start();
1110 ?>
1111 <pre>&lt;?php
1112
1113 add_action(&apos;acfe/form/validation/name=<?php echo $form_name; ?>&apos;, &apos;my_<?php echo $_form_name; ?>_validation&apos;, 10, 2);
1114 function my_<?php echo $_form_name; ?>_validation($form, $target_post_id){
1115
1116 /**
1117 * @array $form Form arguments
1118 * @bool/string $target_post_id Targeted post id
1119 */
1120
1121 $<?php echo $_field_name; ?> = get_field(&apos;<?php echo $field['name']; ?>&apos;);
1122 $<?php echo $_field_name; ?>_unformatted = get_field(&apos;<?php echo $field['name']; ?>&apos;, false, false);
1123
1124 if($<?php echo $_field_name; ?> === &apos;Hello&apos;){
1125
1126 acfe_add_validation_error(&apos;<?php echo $field['name']; ?>&apos;, &apos;Hello is not allowed&apos;);
1127
1128 }
1129
1130 }</pre>
1131 <?php $html = ob_get_clean();
1132
1133 $fields[] = array(
1134 'type' => 'message',
1135 'name' => '',
1136 'label' => 'PHP Form Validation',
1137 'value' => '',
1138 'message' => $html,
1139 'new_lines' => false,
1140 );
1141
1142
1143 // Form Submission
1144
1145 ob_start();
1146 ?>
1147 <pre>&lt;?php
1148
1149 add_action(&apos;acfe/form/submit/name=<?php echo $form_name; ?>&apos;, &apos;my_<?php echo $_form_name; ?>_submit&apos;, 10, 2);
1150 function my_<?php echo $_form_name; ?>_submit($form, $target_post_id){
1151
1152 /**
1153 * @array $form Form arguments
1154 * @bool/string $target_post_id Targeted post id
1155 */
1156
1157 $<?php echo $_field_name; ?> = get_field(&apos;<?php echo $field['name']; ?>&apos;);
1158 $<?php echo $_field_name; ?>_unformatted = get_field(&apos;<?php echo $field['name']; ?>&apos;, false, false);
1159
1160 if($<?php echo $_field_name; ?> === &apos;do_something&apos;){
1161
1162 // Do something
1163
1164 }
1165
1166 }</pre>
1167 <?php $html = ob_get_clean();
1168
1169 $fields[] = array(
1170 'type' => 'message',
1171 'name' => '',
1172 'label' => 'PHP Form Submit: Custom Action',
1173 'value' => '',
1174 'message' => $html,
1175 'new_lines' => false,
1176 );
1177
1178 }
1179
1180 }
1181
1182 return $fields;
1183
1184 }
1185
1186 function admin_columns($columns){
1187
1188 if(isset($columns['date']))
1189 unset($columns['date']);
1190
1191 $columns['name'] = __('Name');
1192 $columns['field_groups'] = __('Field groups', 'acf');
1193 $columns['actions'] = __('Actions');
1194 $columns['shortcode'] = __('Shortcode');
1195
1196 return $columns;
1197
1198 }
1199
1200 function admin_columns_html($column, $post_id){
1201
1202 // Name
1203 if($column == 'name'){
1204
1205 echo '<code style="-webkit-user-select: all;-moz-user-select: all;-ms-user-select: all;user-select: all;font-size: 12px;">' . get_field('acfe_form_name', $post_id) . '</code>';
1206
1207 }
1208
1209 // Field groups
1210 elseif($column == 'field_groups'){
1211
1212 $field_groups = get_field('acfe_form_field_groups', $post_id);
1213
1214 if(empty($field_groups)){
1215
1216 echo '';
1217 return;
1218
1219 }
1220
1221 $fg = array();
1222
1223 foreach($field_groups as $field_group_key){
1224
1225 acf_disable_filters();
1226
1227 $field_group = acf_get_field_group($field_group_key);
1228
1229 acf_enable_filters();
1230
1231 $fg[] = '<a href="' . admin_url('post.php?post=' . $field_group['ID'] . '&action=edit') . '">' . $field_group['title'] . '</a>';
1232
1233 }
1234
1235 echo implode(', ', $fg);
1236
1237 }
1238
1239 // Actions
1240 elseif($column == 'actions'){
1241
1242 $customs = $emails = $posts = $terms = $users = array();
1243 $found = false;
1244
1245 if(have_rows('acfe_form_actions', $post_id)):
1246 while(have_rows('acfe_form_actions', $post_id)): the_row();
1247
1248 // Custom
1249 if(get_row_layout() === 'custom'){
1250
1251 $action_name = get_sub_field('acfe_form_custom_action');
1252
1253 $customs[] = '<span class="acf-js-tooltip dashicons dashicons-editor-code" title="Custom action: ' . $action_name . '"></span>';
1254 $found = true;
1255
1256 }
1257
1258 // E-mail
1259 elseif(get_row_layout() === 'email'){
1260
1261 $emails[] = '<span class="acf-js-tooltip dashicons dashicons-email" title="E-mail"></span>';
1262 $found = true;
1263
1264 }
1265
1266 // Post
1267 elseif(get_row_layout() === 'post'){
1268
1269 $action = get_sub_field('acfe_form_post_action');
1270
1271 // Insert
1272 if($action === 'insert_post'){
1273
1274 $posts[] = '<span class="acf-js-tooltip dashicons dashicons-edit" title="Create post"></span>';
1275 $found = true;
1276
1277 }
1278
1279 // Update
1280 elseif($action === 'update_post'){
1281
1282 $posts[] = '<span class="acf-js-tooltip dashicons dashicons-update" title="Update post"></span>';
1283 $found = true;
1284
1285 }
1286
1287 }
1288
1289 // Term
1290 elseif(get_row_layout() === 'term'){
1291
1292 $action = get_sub_field('acfe_form_term_action');
1293
1294 // Insert
1295 if($action === 'insert_term'){
1296
1297 $terms[] = '<span class="acf-js-tooltip dashicons dashicons-category" title="Create term"></span>';
1298 $found = true;
1299
1300 }
1301
1302 // Update
1303 elseif($action === 'update_term'){
1304
1305 $terms[] = '<span class="acf-js-tooltip dashicons dashicons-category" title="Update term"></span>';
1306 $found = true;
1307
1308 }
1309
1310 }
1311
1312 // User
1313 elseif(get_row_layout() === 'user'){
1314
1315 $action = get_sub_field('acfe_form_user_action');
1316
1317 // Insert
1318 if($action === 'insert_user'){
1319
1320 $users[] = '<span class="acf-js-tooltip dashicons dashicons-admin-users" title="Create user"></span>';
1321 $found = true;
1322
1323 }
1324
1325 // Update
1326 elseif($action === 'update_user'){
1327
1328 $users[] = '<span class="acf-js-tooltip dashicons dashicons-admin-users" title="Update user"></span>';
1329 $found = true;
1330
1331 }
1332
1333 }
1334
1335 endwhile;
1336 endif;
1337
1338 if(!empty($customs))
1339 echo implode('', $customs);
1340
1341 if(!empty($emails))
1342 echo implode('', $emails);
1343
1344 if(!empty($posts))
1345 echo implode('', $posts);
1346
1347 if(!empty($terms))
1348 echo implode('', $terms);
1349
1350 if(!empty($users))
1351 echo implode('', $users);
1352
1353 if(!$found)
1354 echo '';
1355
1356 }
1357
1358 // Field groups
1359 elseif($column == 'shortcode'){
1360
1361 echo '<code style="-webkit-user-select: all;-moz-user-select: all;-ms-user-select: all;user-select: all;font-size: 12px;">[acfe_form name="' . get_field('acfe_form_name', $post_id) . '"]</code>';
1362
1363 }
1364
1365 }
1366
1367 function ajax_query_post(){
1368
1369 if(!acf_verify_ajax())
1370 die();
1371
1372 // get choices
1373 $response = $this->get_ajax_query_post($_POST);
1374
1375 // return
1376 if(!$response)
1377 return;
1378
1379 // return ajax
1380 acf_send_ajax_results($response);
1381
1382 }
1383
1384 function get_ajax_query_post($options = array()){
1385
1386 // defaults
1387 $options = acf_parse_args($options, array(
1388 'post_id' => 0,
1389 's' => '',
1390 'field_key' => '',
1391 'paged' => 1
1392 ));
1393
1394 // load field
1395 $field = acf_get_field($options['field_key']);
1396 if(!$field)
1397 return false;
1398
1399 // Target field name
1400 if(!in_array($field['name'], $this->posts))
1401 return false;
1402
1403 $field_type = acf_get_field_type('post_object');
1404
1405 // vars
1406 $results = array();
1407 $args = array();
1408 $s = false;
1409 $is_search = false;
1410
1411 // paged
1412 $args['posts_per_page'] = 20;
1413 $args['paged'] = $options['paged'];
1414
1415 // search
1416 if($options['s'] !== ''){
1417
1418 // strip slashes (search may be integer)
1419 $s = wp_unslash( strval($options['s']) );
1420
1421 // update vars
1422 $args['s'] = $s;
1423 $is_search = true;
1424
1425 }
1426
1427 // post_type
1428 $args['post_type'] = acf_get_post_types();
1429
1430 // get posts grouped by post type
1431 $groups = acf_get_grouped_posts($args);
1432
1433 // bail early if no posts
1434 if(empty($groups))
1435 return false;
1436
1437 if(!$is_search && $args['paged'] === 1){
1438
1439 $results[] = array(
1440 'text' => 'Generic',
1441 'children' => array(
1442 array(
1443 'id' => 'current_post',
1444 'text' => 'Current Post',
1445 )
1446 )
1447 );
1448
1449 }
1450
1451 // loop
1452 foreach(array_keys($groups) as $group_title){
1453
1454 // vars
1455 $posts = acf_extract_var($groups, $group_title);
1456
1457 // data
1458 $data = array(
1459 'text' => $group_title,
1460 'children' => array()
1461 );
1462
1463 // convert post objects to post titles
1464 foreach(array_keys($posts) as $post_id){
1465
1466 $posts[$post_id] = $field_type->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search);
1467
1468 }
1469
1470 // order posts by search
1471 if($is_search && empty($args['orderby'])){
1472
1473 $posts = acf_order_by_search($posts, $args['s']);
1474
1475 }
1476
1477 // append to $data
1478 foreach(array_keys($posts) as $post_id){
1479
1480 $data['children'][] = $field_type->get_post_result($post_id, $posts[$post_id]);
1481
1482 }
1483
1484 // append to $results
1485 $results[] = $data;
1486
1487 }
1488
1489
1490 // optgroup or single
1491 $post_type = acf_get_array($args['post_type']);
1492
1493 if(count($post_type) == 1){
1494
1495 $results = $results[0]['children'];
1496
1497 }
1498
1499 // vars
1500 $response = array(
1501 'results' => $results,
1502 'limit' => $args['posts_per_page']
1503 );
1504
1505 // return
1506 return $response;
1507
1508 }
1509
1510 function prepare_value_post($field){
1511
1512 if(!acf_maybe_get($field, 'value'))
1513 return $field;
1514
1515 $field['choices'] = array();
1516 $field['choices']['current_post'] = 'Current Post';
1517
1518 $field_type = acf_get_field_type('post_object');
1519 $field['post_type'] = acf_get_post_types();
1520
1521 // load posts
1522 $posts = $field_type->get_posts($field['value'], $field);
1523
1524 if($posts){
1525
1526 foreach(array_keys($posts) as $i){
1527
1528 // vars
1529 $post = acf_extract_var($posts, $i);
1530
1531 // append to choices
1532 $field['choices'][$post->ID] = $field_type->get_post_title( $post, $field );
1533
1534 }
1535
1536 }
1537
1538 return $field;
1539
1540 }
1541
1542 function ajax_query_user(){
1543
1544 if(!acf_verify_ajax())
1545 die();
1546
1547 // get choices
1548 $response = $this->get_ajax_query_user($_POST);
1549
1550 // return
1551 if(!$response)
1552 return;
1553
1554 // return ajax
1555 acf_send_ajax_results($response);
1556
1557 }
1558
1559 function get_ajax_query_user($options = array()){
1560
1561 // defaults
1562 $options = acf_parse_args($options, array(
1563 'post_id' => 0,
1564 's' => '',
1565 'field_key' => '',
1566 'paged' => 1
1567 ));
1568
1569
1570 // load field
1571 $field = acf_get_field($options['field_key']);
1572 if(!$field)
1573 return false;
1574
1575 // Target field name
1576 if(!in_array($field['name'], $this->users))
1577 return false;
1578
1579 $field_type = acf_get_field_type('user');
1580
1581 // vars
1582 $results = array();
1583 $args = array();
1584 $s = false;
1585 $is_search = false;
1586
1587 // paged
1588 $args['users_per_page'] = 20;
1589 $args['paged'] = $options['paged'];
1590
1591 // search
1592 if($options['s'] !== ''){
1593
1594 // strip slashes (search may be integer)
1595 $s = wp_unslash( strval($options['s']) );
1596
1597 // update vars
1598 $args['s'] = $s;
1599 $is_search = true;
1600
1601 }
1602
1603 // role
1604 if(!empty($field['role'])){
1605
1606 $args['role'] = acf_get_array( $field['role'] );
1607
1608 }
1609
1610 // search
1611 if($is_search){
1612
1613 // append to $args
1614 $args['search'] = '*' . $options['s'] . '*';
1615
1616 // add reference
1617 $field_type->field = $field;
1618
1619 // add filter to modify search colums
1620 add_filter('user_search_columns', array($field_type, 'user_search_columns'), 10, 3);
1621
1622 }
1623
1624 // get users
1625 $groups = acf_get_grouped_users($args);
1626
1627 // Current user
1628 if(!$is_search && $args['paged'] === 1){
1629
1630 $results[] = array(
1631 'text' => 'Generic',
1632 'children' => array(
1633 array(
1634 'id' => 'current_user',
1635 'text' => 'Current User',
1636 ),
1637 array(
1638 'id' => 'current_post_author',
1639 'text' => 'Current Post Author',
1640 ),
1641 )
1642 );
1643
1644 }
1645
1646 // loop
1647 if(!empty($groups)){
1648
1649 foreach(array_keys($groups) as $group_title){
1650
1651 // vars
1652 $users = acf_extract_var( $groups, $group_title );
1653 $data = array(
1654 'text' => $group_title,
1655 'children' => array()
1656 );
1657
1658 // append users
1659 foreach( array_keys($users) as $user_id ) {
1660
1661 $users[ $user_id ] = $field_type->get_result( $users[ $user_id ], $field, $options['post_id'] );
1662
1663 };
1664
1665 // order by search
1666 if( $is_search && empty($args['orderby']) ) {
1667
1668 $users = acf_order_by_search( $users, $args['s'] );
1669
1670 }
1671
1672 // append to $data
1673 foreach( $users as $id => $title ) {
1674
1675 $data['children'][] = array(
1676 'id' => $id,
1677 'text' => $title
1678 );
1679
1680 }
1681
1682 // append to $r
1683 $results[] = $data;
1684
1685 }
1686
1687 // optgroup or single
1688 if(!empty($args['role']) && count($args['role']) == 1){
1689
1690 $results = $results[0]['children'];
1691
1692 }
1693 }
1694
1695 // vars
1696 $response = array(
1697 'results' => $results,
1698 'limit' => $args['users_per_page']
1699 );
1700
1701 // return
1702 return $response;
1703
1704 }
1705
1706 function prepare_value_user($field){
1707
1708 if(!acf_maybe_get($field, 'value'))
1709 return $field;
1710
1711 $field['choices'] = array();
1712 $field['choices']['current_user'] = 'Current User';
1713 $field['choices']['current_post_author'] = 'Current Post Author';
1714
1715 $field_type = acf_get_field_type('user');
1716
1717 // Clean value into an array of IDs.
1718 $user_ids = array_map('intval', acf_array($field['value']));
1719
1720 // Find users in database (ensures all results are real).
1721 $users = acf_get_users(array(
1722 'include' => $user_ids
1723 ));
1724
1725 // Append.
1726 if($users){
1727
1728 foreach($users as $user){
1729 $field['choices'][$user->ID] = $field_type->get_result($user, $field);
1730 }
1731
1732 }
1733
1734 return $field;
1735
1736 }
1737
1738 function ajax_query_term(){
1739
1740 if(!acf_verify_ajax())
1741 die();
1742
1743 // get choices
1744 $response = $this->get_ajax_query_term($_POST);
1745
1746 // return
1747 if(!$response)
1748 return;
1749
1750 // return ajax
1751 acf_send_ajax_results($response);
1752
1753 }
1754
1755 function get_ajax_query_term($options = array()){
1756
1757 // defaults
1758 $options = acf_parse_args($options, array(
1759 'post_id' => 0,
1760 's' => '',
1761 'field_key' => '',
1762 'paged' => 1
1763 ));
1764
1765 // load field
1766 $field = acf_get_field($options['field_key']);
1767 if(!$field)
1768 return false;
1769
1770 // Target field name
1771 if(!in_array($field['name'], $this->terms))
1772 return false;
1773
1774 // vars
1775 $results = array();
1776 $args = array();
1777 $s = false;
1778 $is_search = false;
1779
1780 // paged
1781 $args['posts_per_page'] = 20;
1782 $args['paged'] = $options['paged'];
1783
1784 // search
1785 if($options['s'] !== ''){
1786
1787 // strip slashes (search may be integer)
1788 $s = wp_unslash( strval($options['s']) );
1789
1790 // update vars
1791 $args['s'] = $s;
1792 $is_search = true;
1793
1794 }
1795
1796 $terms_args = array(
1797 'number' => $args['posts_per_page'],
1798 'offset' => ($args['paged'] - 1) * $args['posts_per_page'],
1799 );
1800
1801 // get grouped terms
1802 $terms = acf_get_grouped_terms($terms_args);
1803 $groups = acf_get_choices_from_grouped_terms($terms, 'name');
1804
1805 // bail early if no posts
1806 if(empty($groups))
1807 return false;
1808
1809 if(!$is_search && $args['paged'] === 1){
1810
1811 $results[] = array(
1812 'text' => 'Generic',
1813 'children' => array(
1814 array(
1815 'id' => 'current_term',
1816 'text' => 'Current Term',
1817 )
1818 )
1819 );
1820
1821 }
1822
1823 // loop
1824 foreach(array_keys($groups) as $group_title){
1825
1826 // vars
1827 $terms = acf_extract_var($groups, $group_title);
1828
1829 // data
1830 $data = array(
1831 'text' => $group_title,
1832 'children' => array()
1833 );
1834
1835 if($is_search && empty($args['orderby'])){
1836
1837 $terms = acf_order_by_search($terms, $args['s']);
1838
1839 }
1840
1841 // append to $data
1842 foreach($terms as $term_id => $name){
1843
1844 $data['children'][] = array(
1845 'id' => $term_id,
1846 'text' => $name
1847 );
1848
1849 }
1850
1851 // append to $results
1852 $results[] = $data;
1853
1854 }
1855
1856 // vars
1857 $response = array(
1858 'results' => $results,
1859 'limit' => $args['posts_per_page']
1860 );
1861
1862 // return
1863 return $response;
1864
1865 }
1866
1867 function prepare_value_term($field){
1868
1869 if(!acf_maybe_get($field, 'value'))
1870 return $field;
1871
1872 $value = $field['value'];
1873
1874 $field['choices'] = array();
1875 $field['choices']['current_term'] = 'Current Term';
1876
1877 if(is_array($value))
1878 $value = $value[0];
1879
1880 $term = get_term($value);
1881
1882 if($term){
1883
1884 $field['choices'][$term->term_id] = $term->name;
1885
1886 }
1887
1888 return $field;
1889
1890 }
1891
1892 }
1893
1894 // initialize
1895 acfe()->acfe_form = new acfe_form();
1896
1897 endif;
1898
1899 function acfe_form_render_fields($content, $post_id, $args){
1900
1901 return acfe()->acfe_form->render_fields($content, $post_id, $args);
1902
1903 }
1904
1905 function acfe_form_map_field_value($field, $acf, $post_id = 0){
1906
1907 return acfe()->acfe_form->map_field_value($field, $acf, $post_id);
1908
1909 }
1910
1911 function acfe_form_map_field_get_value($field){
1912
1913 return acfe()->acfe_form->map_field_get_value($field);
1914
1915 }
1916
1917 function acfe_form_filter_meta($meta, $acf){
1918
1919 return acfe()->acfe_form->filter_meta($meta, $acf);
1920
1921 }