forms-hooks.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_dynamic_forms_hooks')): |
| 7 | |
| 8 | class acfe_dynamic_forms_hooks{ |
| 9 | |
| 10 | /* |
| 11 | * Construct |
| 12 | */ |
| 13 | function __construct(){ |
| 14 | |
| 15 | // Fields |
| 16 | add_filter('acf/load_value/name=acfe_form_custom_html_enable', array($this, 'prepare_custom_html'), 10, 3); |
| 17 | add_filter('acf/prepare_field/name=acfe_form_actions', array($this, 'prepare_actions')); |
| 18 | add_filter('acf/prepare_field/name=acfe_form_field_groups', array($this, 'field_groups_choices')); |
| 19 | add_filter('acf/prepare_field/name=acfe_form_return', array($this, 'form_return_deprecated')); |
| 20 | |
| 21 | // Format values |
| 22 | add_filter('acfe/form/format_value/type=post_object', array($this, 'format_value_post_object'), 5, 4); |
| 23 | add_filter('acfe/form/format_value/type=relationship', array($this, 'format_value_post_object'), 5, 4); |
| 24 | add_filter('acfe/form/format_value/type=user', array($this, 'format_value_user'), 5, 4); |
| 25 | add_filter('acfe/form/format_value/type=taxonomy', array($this, 'format_value_taxonomy'), 5, 4); |
| 26 | add_filter('acfe/form/format_value/type=image', array($this, 'format_value_file'), 5, 4); |
| 27 | add_filter('acfe/form/format_value/type=file', array($this, 'format_value_file'), 5, 4); |
| 28 | add_filter('acfe/form/format_value/type=select', array($this, 'format_value_select'), 5, 4); |
| 29 | add_filter('acfe/form/format_value/type=checkbox', array($this, 'format_value_select'), 5, 4); |
| 30 | add_filter('acfe/form/format_value/type=radio', array($this, 'format_value_select'), 5, 4); |
| 31 | add_filter('acfe/form/format_value/type=google_map', array($this, 'format_value_google_map'), 5, 4); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function prepare_custom_html($value, $post_id, $field){ |
| 36 | |
| 37 | $custom_html = trim(get_field('acfe_form_custom_html', $post_id)); |
| 38 | |
| 39 | if($value === false && !empty($custom_html)) |
| 40 | $value = true; |
| 41 | |
| 42 | return $value; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | function prepare_actions($field){ |
| 47 | |
| 48 | if(empty(acf_get_instance('acfe_dynamic_forms_helpers')->get_field_groups())){ |
| 49 | |
| 50 | $field['instructions'] .= '<br /><u>No field groups are currently mapped</u>'; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | return $field; |
| 55 | |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Field Groups Choices |
| 60 | */ |
| 61 | function field_groups_choices($field){ |
| 62 | |
| 63 | // Vars |
| 64 | $field_groups = acf_get_field_groups(); |
| 65 | $hidden = acfe_get_setting('reserved_field_groups', array()); |
| 66 | |
| 67 | foreach($field_groups as $field_group){ |
| 68 | |
| 69 | if(in_array($field_group['key'], $hidden)) |
| 70 | continue; |
| 71 | |
| 72 | $field['choices'][$field_group['key']] = $field_group['title']; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | return $field; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | function form_return_deprecated($field){ |
| 81 | |
| 82 | if(empty($field['value'])) |
| 83 | return false; |
| 84 | |
| 85 | return $field; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | // Post Object & Relationship |
| 90 | function format_value_post_object($value, $_value, $post_id, $field){ |
| 91 | |
| 92 | $value = acf_get_array($_value); |
| 93 | $array = array(); |
| 94 | |
| 95 | foreach($value as $p_id){ |
| 96 | |
| 97 | $array[] = get_the_title($p_id); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | return implode(', ', $array); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | // User |
| 106 | function format_value_user($value, $_value, $post_id, $field){ |
| 107 | |
| 108 | $value = acf_get_array($_value); |
| 109 | $array = array(); |
| 110 | |
| 111 | foreach($value as $user_id){ |
| 112 | |
| 113 | $user_data = get_userdata($user_id); |
| 114 | $array[] = $user_data->user_nicename; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | return implode(', ', $array); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | // Taxonomy |
| 123 | function format_value_taxonomy($value, $_value, $post_id, $field){ |
| 124 | |
| 125 | $value = acf_get_array($_value); |
| 126 | $array = array(); |
| 127 | |
| 128 | foreach($value as $term_id){ |
| 129 | |
| 130 | $term = get_term($term_id); |
| 131 | $array[] = $term->name; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | return implode(', ', $array); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | // Image / File |
| 140 | function format_value_file($value, $_value, $post_id, $field){ |
| 141 | |
| 142 | $value = acf_get_array($_value); |
| 143 | $array = array(); |
| 144 | |
| 145 | foreach($value as $v){ |
| 146 | |
| 147 | $array[] = get_the_title($v); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | return implode(', ', $array); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | // Select / Checkbox / Radio |
| 156 | function format_value_select($value, $_value, $post_id, $field){ |
| 157 | |
| 158 | $value = acf_get_array($_value); |
| 159 | $array = array(); |
| 160 | |
| 161 | foreach($value as $v){ |
| 162 | |
| 163 | $array[] = acf_maybe_get($field['choices'], $v, $v); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | return implode(', ', $array); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | // Google Map |
| 172 | function format_value_google_map($value, $_value, $post_id, $field){ |
| 173 | |
| 174 | if(is_string($value)){ |
| 175 | |
| 176 | $value = json_decode(wp_unslash($value), true); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | $value = acf_get_array($value); |
| 181 | |
| 182 | $address = acf_maybe_get($value, 'address'); |
| 183 | |
| 184 | return $address; |
| 185 | |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | acf_new_instance('acfe_dynamic_forms_hooks'); |
| 191 | |
| 192 | endif; |