field-forms.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_field_forms')): |
| 7 | |
| 8 | class acfe_field_forms extends acf_field{ |
| 9 | |
| 10 | function __construct(){ |
| 11 | |
| 12 | $this->name = 'acfe_forms'; |
| 13 | $this->label = __('Forms', 'acfe'); |
| 14 | $this->category = apply_filters('acfe/form_field_type_category', 'relational'); |
| 15 | $this->defaults = array( |
| 16 | 'post_type' => array(), |
| 17 | 'field_type' => 'checkbox', |
| 18 | 'multiple' => 0, |
| 19 | 'allow_null' => 0, |
| 20 | 'choices' => array(), |
| 21 | 'default_value' => '', |
| 22 | 'ui' => 0, |
| 23 | 'ajax' => 0, |
| 24 | 'placeholder' => '', |
| 25 | 'layout' => '', |
| 26 | 'toggle' => 0, |
| 27 | 'allow_custom' => 0, |
| 28 | 'return_format' => 'name', |
| 29 | ); |
| 30 | |
| 31 | parent::__construct(); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function prepare_field($field){ |
| 36 | |
| 37 | $field['choices'] = acfe_get_pretty_forms($field['forms']); |
| 38 | |
| 39 | // Set Field Type |
| 40 | $field['type'] = $field['field_type']; |
| 41 | |
| 42 | return $field; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | function render_field_settings($field){ |
| 47 | |
| 48 | if(isset($field['default_value'])) |
| 49 | $field['default_value'] = acf_encode_choices($field['default_value'], false); |
| 50 | |
| 51 | // Allow Form |
| 52 | acf_render_field_setting($field, array( |
| 53 | 'label' => __('Allow Forms','acf'), |
| 54 | 'instructions' => '', |
| 55 | 'type' => 'select', |
| 56 | 'name' => 'forms', |
| 57 | 'choices' => acfe_get_pretty_forms(), |
| 58 | 'multiple' => 1, |
| 59 | 'ui' => 1, |
| 60 | 'allow_null' => 1, |
| 61 | 'placeholder' => __("All forms",'acf'), |
| 62 | )); |
| 63 | |
| 64 | // field_type |
| 65 | acf_render_field_setting($field, array( |
| 66 | 'label' => __('Appearance','acf'), |
| 67 | 'instructions' => __('Select the appearance of this field', 'acf'), |
| 68 | 'type' => 'select', |
| 69 | 'name' => 'field_type', |
| 70 | 'optgroup' => true, |
| 71 | 'choices' => array( |
| 72 | 'checkbox' => __('Checkbox', 'acf'), |
| 73 | 'radio' => __('Radio Buttons', 'acf'), |
| 74 | 'select' => _x('Select', 'noun', 'acf') |
| 75 | ) |
| 76 | )); |
| 77 | |
| 78 | // default_value |
| 79 | acf_render_field_setting($field, array( |
| 80 | 'label' => __('Default Value','acf'), |
| 81 | 'instructions' => __('Enter each default value on a new line','acf'), |
| 82 | 'name' => 'default_value', |
| 83 | 'type' => 'textarea', |
| 84 | )); |
| 85 | |
| 86 | // return_format |
| 87 | acf_render_field_setting($field, array( |
| 88 | 'label' => __('Return Value', 'acf'), |
| 89 | 'instructions' => '', |
| 90 | 'type' => 'radio', |
| 91 | 'name' => 'return_format', |
| 92 | 'choices' => array( |
| 93 | 'id' => __('Form ID', 'acfe'), |
| 94 | 'name' => __('Form name', 'acfe') |
| 95 | ), |
| 96 | 'layout' => 'horizontal', |
| 97 | )); |
| 98 | |
| 99 | // Select + Radio: allow_null |
| 100 | acf_render_field_setting($field, array( |
| 101 | 'label' => __('Allow Null?','acf'), |
| 102 | 'instructions' => '', |
| 103 | 'name' => 'allow_null', |
| 104 | 'type' => 'true_false', |
| 105 | 'ui' => 1, |
| 106 | 'conditions' => array( |
| 107 | array( |
| 108 | array( |
| 109 | 'field' => 'field_type', |
| 110 | 'operator' => '==', |
| 111 | 'value' => 'select', |
| 112 | ), |
| 113 | ), |
| 114 | array( |
| 115 | array( |
| 116 | 'field' => 'field_type', |
| 117 | 'operator' => '==', |
| 118 | 'value' => 'radio', |
| 119 | ), |
| 120 | ), |
| 121 | ) |
| 122 | )); |
| 123 | |
| 124 | // placeholder |
| 125 | acf_render_field_setting($field, array( |
| 126 | 'label' => __('Placeholder Text','acf'), |
| 127 | 'instructions' => __('Appears within the input','acf'), |
| 128 | 'type' => 'text', |
| 129 | 'name' => 'placeholder', |
| 130 | 'placeholder' => _x('Select', 'verb', 'acf'), |
| 131 | 'conditional_logic' => array( |
| 132 | array( |
| 133 | array( |
| 134 | 'field' => 'field_type', |
| 135 | 'operator' => '==', |
| 136 | 'value' => 'select', |
| 137 | ), |
| 138 | array( |
| 139 | 'field' => 'allow_null', |
| 140 | 'operator' => '==', |
| 141 | 'value' => '1', |
| 142 | ), |
| 143 | |
| 144 | ), |
| 145 | array( |
| 146 | array( |
| 147 | 'field' => 'field_type', |
| 148 | 'operator' => '==', |
| 149 | 'value' => 'select', |
| 150 | ), |
| 151 | array( |
| 152 | 'field' => 'ui', |
| 153 | 'operator' => '==', |
| 154 | 'value' => '1', |
| 155 | ), |
| 156 | |
| 157 | ), |
| 158 | ) |
| 159 | )); |
| 160 | |
| 161 | // Select: multiple |
| 162 | acf_render_field_setting($field, array( |
| 163 | 'label' => __('Select multiple values?','acf'), |
| 164 | 'instructions' => '', |
| 165 | 'name' => 'multiple', |
| 166 | 'type' => 'true_false', |
| 167 | 'ui' => 1, |
| 168 | 'conditions' => array( |
| 169 | array( |
| 170 | array( |
| 171 | 'field' => 'field_type', |
| 172 | 'operator' => '==', |
| 173 | 'value' => 'select', |
| 174 | ), |
| 175 | ), |
| 176 | ) |
| 177 | )); |
| 178 | |
| 179 | // Select: ui |
| 180 | acf_render_field_setting($field, array( |
| 181 | 'label' => __('Stylised UI','acf'), |
| 182 | 'instructions' => '', |
| 183 | 'name' => 'ui', |
| 184 | 'type' => 'true_false', |
| 185 | 'ui' => 1, |
| 186 | 'conditions' => array( |
| 187 | array( |
| 188 | array( |
| 189 | 'field' => 'field_type', |
| 190 | 'operator' => '==', |
| 191 | 'value' => 'select', |
| 192 | ), |
| 193 | ), |
| 194 | ) |
| 195 | )); |
| 196 | |
| 197 | |
| 198 | // Select: ajax |
| 199 | acf_render_field_setting($field, array( |
| 200 | 'label' => __('Use AJAX to lazy load choices?','acf'), |
| 201 | 'instructions' => '', |
| 202 | 'name' => 'ajax', |
| 203 | 'type' => 'true_false', |
| 204 | 'ui' => 1, |
| 205 | 'conditions' => array( |
| 206 | array( |
| 207 | array( |
| 208 | 'field' => 'field_type', |
| 209 | 'operator' => '==', |
| 210 | 'value' => 'select', |
| 211 | ), |
| 212 | array( |
| 213 | 'field' => 'ui', |
| 214 | 'operator' => '==', |
| 215 | 'value' => 1, |
| 216 | ), |
| 217 | ), |
| 218 | ) |
| 219 | )); |
| 220 | |
| 221 | // Radio: other_choice |
| 222 | acf_render_field_setting($field, array( |
| 223 | 'label' => __('Other','acf'), |
| 224 | 'instructions' => '', |
| 225 | 'name' => 'other_choice', |
| 226 | 'type' => 'true_false', |
| 227 | 'ui' => 1, |
| 228 | 'message' => __("Add 'other' choice to allow for custom values", 'acf'), |
| 229 | 'conditions' => array( |
| 230 | array( |
| 231 | array( |
| 232 | 'field' => 'field_type', |
| 233 | 'operator' => '==', |
| 234 | 'value' => 'radio', |
| 235 | ), |
| 236 | ), |
| 237 | ) |
| 238 | )); |
| 239 | |
| 240 | |
| 241 | // Radio: save_other_choice |
| 242 | acf_render_field_setting($field, array( |
| 243 | 'label' => __('Save Other','acf'), |
| 244 | 'instructions' => '', |
| 245 | 'name' => 'save_other_choice', |
| 246 | 'type' => 'true_false', |
| 247 | 'ui' => 1, |
| 248 | 'message' => __("Save 'other' values to the field's choices", 'acf'), |
| 249 | 'conditions' => array( |
| 250 | array( |
| 251 | array( |
| 252 | 'field' => 'field_type', |
| 253 | 'operator' => '==', |
| 254 | 'value' => 'radio', |
| 255 | ), |
| 256 | array( |
| 257 | 'field' => 'other_choice', |
| 258 | 'operator' => '==', |
| 259 | 'value' => 1, |
| 260 | ), |
| 261 | ), |
| 262 | ) |
| 263 | )); |
| 264 | |
| 265 | // Checkbox: layout |
| 266 | acf_render_field_setting($field, array( |
| 267 | 'label' => __('Layout','acf'), |
| 268 | 'instructions' => '', |
| 269 | 'type' => 'radio', |
| 270 | 'name' => 'layout', |
| 271 | 'layout' => 'horizontal', |
| 272 | 'choices' => array( |
| 273 | 'vertical' => __("Vertical",'acf'), |
| 274 | 'horizontal' => __("Horizontal",'acf') |
| 275 | ), |
| 276 | 'conditions' => array( |
| 277 | array( |
| 278 | array( |
| 279 | 'field' => 'field_type', |
| 280 | 'operator' => '==', |
| 281 | 'value' => 'checkbox', |
| 282 | ), |
| 283 | ), |
| 284 | array( |
| 285 | array( |
| 286 | 'field' => 'field_type', |
| 287 | 'operator' => '==', |
| 288 | 'value' => 'radio', |
| 289 | ), |
| 290 | ), |
| 291 | ) |
| 292 | )); |
| 293 | |
| 294 | // Checkbox: toggle |
| 295 | acf_render_field_setting($field, array( |
| 296 | 'label' => __('Toggle','acf'), |
| 297 | 'instructions' => __('Prepend an extra checkbox to toggle all choices','acf'), |
| 298 | 'name' => 'toggle', |
| 299 | 'type' => 'true_false', |
| 300 | 'ui' => 1, |
| 301 | 'conditions' => array( |
| 302 | array( |
| 303 | array( |
| 304 | 'field' => 'field_type', |
| 305 | 'operator' => '==', |
| 306 | 'value' => 'checkbox', |
| 307 | ), |
| 308 | ), |
| 309 | ) |
| 310 | )); |
| 311 | |
| 312 | // Checkbox: other_choice |
| 313 | acf_render_field_setting($field, array( |
| 314 | 'label' => __('Allow Custom','acf'), |
| 315 | 'instructions' => '', |
| 316 | 'name' => 'allow_custom', |
| 317 | 'type' => 'true_false', |
| 318 | 'ui' => 1, |
| 319 | 'message' => __("Allow 'custom' values to be added", 'acf'), |
| 320 | 'conditions' => array( |
| 321 | array( |
| 322 | array( |
| 323 | 'field' => 'field_type', |
| 324 | 'operator' => '==', |
| 325 | 'value' => 'checkbox', |
| 326 | ), |
| 327 | ), |
| 328 | ) |
| 329 | )); |
| 330 | |
| 331 | |
| 332 | // Checkbox: save_other_choice |
| 333 | acf_render_field_setting($field, array( |
| 334 | 'label' => __('Save Custom','acf'), |
| 335 | 'instructions' => '', |
| 336 | 'name' => 'save_custom', |
| 337 | 'type' => 'true_false', |
| 338 | 'ui' => 1, |
| 339 | 'message' => __("Save 'custom' values to the field's choices", 'acf'), |
| 340 | 'conditions' => array( |
| 341 | array( |
| 342 | array( |
| 343 | 'field' => 'field_type', |
| 344 | 'operator' => '==', |
| 345 | 'value' => 'checkbox', |
| 346 | ), |
| 347 | array( |
| 348 | 'field' => 'allow_custom', |
| 349 | 'operator' => '==', |
| 350 | 'value' => 1, |
| 351 | ), |
| 352 | ), |
| 353 | ) |
| 354 | )); |
| 355 | |
| 356 | } |
| 357 | |
| 358 | function update_field($field){ |
| 359 | |
| 360 | $field['default_value'] = acf_decode_choices($field['default_value'], true); |
| 361 | |
| 362 | if($field['field_type'] === 'radio') |
| 363 | $field['default_value'] = acfe_unarray($field['default_value']); |
| 364 | |
| 365 | return $field; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | function format_value($value, $post_id, $field){ |
| 370 | |
| 371 | // Return: name |
| 372 | if($field['return_format'] === 'name'){ |
| 373 | |
| 374 | // array |
| 375 | if(acf_is_array($value)){ |
| 376 | |
| 377 | foreach($value as $i => $v){ |
| 378 | |
| 379 | $form_name = get_field('acfe_form_name', $v); |
| 380 | |
| 381 | $value[$i] = $form_name; |
| 382 | |
| 383 | } |
| 384 | |
| 385 | // string |
| 386 | }else{ |
| 387 | |
| 388 | $value = get_field('acfe_form_name', $value); |
| 389 | |
| 390 | } |
| 391 | |
| 392 | } |
| 393 | |
| 394 | // return |
| 395 | return $value; |
| 396 | |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | |
| 401 | // initialize |
| 402 | acf_register_field_type('acfe_field_forms'); |
| 403 | |
| 404 | endif; |