settings.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_field_settings')): |
| 7 | |
| 8 | class acfe_field_settings{ |
| 9 | |
| 10 | function __construct(){ |
| 11 | |
| 12 | // Actions |
| 13 | add_action('load-post.php', array($this, 'load')); |
| 14 | add_action('wp_ajax_acf/field_group/render_field_settings', array($this, 'field_types_action'), 5); |
| 15 | |
| 16 | // Filters |
| 17 | add_filter('acfe/load_field', array($this, 'load_field')); |
| 18 | add_filter('acf/prepare_field', array($this, 'prepare_field')); |
| 19 | |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Load |
| 24 | */ |
| 25 | function load(){ |
| 26 | |
| 27 | if(!acf_is_screen('acf-field-group')) |
| 28 | return; |
| 29 | |
| 30 | $this->field_types_action(); |
| 31 | |
| 32 | //add_action('acf/render_field_settings', array($this, 'quick_settings'), 998); |
| 33 | |
| 34 | // Fix: Repeater |
| 35 | add_filter('acf/prepare_field/name=acfe_settings', array($this, 'fix_repeater')); |
| 36 | add_filter('acf/prepare_field/name=acfe_settings_location', array($this, 'fix_repeater')); |
| 37 | add_filter('acf/prepare_field/name=acfe_settings_settings', array($this, 'fix_repeater')); |
| 38 | add_filter('acf/prepare_field/name=acfe_settings_setting_type', array($this, 'fix_repeater')); |
| 39 | add_filter('acf/prepare_field/name=acfe_settings_setting_name', array($this, 'fix_repeater')); |
| 40 | add_filter('acf/prepare_field/name=acfe_settings_setting_operator', array($this, 'fix_repeater')); |
| 41 | add_filter('acf/prepare_field/name=acfe_settings_setting_value', array($this, 'fix_repeater')); |
| 42 | |
| 43 | // Fix: Clone |
| 44 | add_filter('acf/update_field', array($this, 'fix_clone')); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | function quick_settings($field){ |
| 49 | |
| 50 | $valid = false; |
| 51 | |
| 52 | $field_group = acfe_get_field_group_from_field($field); |
| 53 | |
| 54 | if(acf_maybe_get($field_group, 'acfe_form')) |
| 55 | $valid = true; |
| 56 | |
| 57 | if(!$valid && acf_maybe_get($field, '_name') === 'new_field'){ |
| 58 | |
| 59 | $field_group_id = get_the_ID(); |
| 60 | |
| 61 | if($field_group_id){ |
| 62 | |
| 63 | $field_group = acf_get_field_group($field_group_id); |
| 64 | |
| 65 | if(acf_maybe_get($field_group, 'acfe_form')) |
| 66 | $valid = true; |
| 67 | |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | if(!$valid) |
| 73 | return; |
| 74 | |
| 75 | // Hide Field |
| 76 | acf_render_field_setting($field, array( |
| 77 | 'label' => __('Hide Field', 'acfe'), |
| 78 | 'instructions' => __('Hide field on specific location'), |
| 79 | 'prepend' => '', |
| 80 | 'append' => '', |
| 81 | 'type' => 'group', |
| 82 | 'name' => 'acfe_hide', |
| 83 | 'sub_fields' => array( |
| 84 | array( |
| 85 | 'label' => '', |
| 86 | 'name' => 'hide_front', |
| 87 | 'key' => 'hide_front', |
| 88 | 'type' => 'true_false', |
| 89 | 'ui' => true, |
| 90 | 'message' => 'Hide on front', |
| 91 | 'default_value' => false, |
| 92 | 'required' => false, |
| 93 | 'wrapper' => array( |
| 94 | 'width' => 33, |
| 95 | 'class' => 'acfe_width_auto', |
| 96 | 'id' => '', |
| 97 | ), |
| 98 | ), |
| 99 | array( |
| 100 | 'label' => '', |
| 101 | 'name' => 'hide_admin', |
| 102 | 'key' => 'hide_admin', |
| 103 | 'type' => 'true_false', |
| 104 | 'ui' => true, |
| 105 | 'message' => 'Hide on admin', |
| 106 | 'default_value' => false, |
| 107 | 'required' => false, |
| 108 | 'wrapper' => array( |
| 109 | 'width' => 33, |
| 110 | 'class' => 'acfe_width_auto', |
| 111 | 'id' => '', |
| 112 | ), |
| 113 | ), |
| 114 | ) |
| 115 | )); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get field types |
| 121 | */ |
| 122 | function field_types_action(){ |
| 123 | |
| 124 | // Get Fields Types |
| 125 | foreach(acf_get_field_types_info() as $field){ |
| 126 | |
| 127 | // Field type |
| 128 | $field_type = $field['name']; |
| 129 | |
| 130 | // Exclude |
| 131 | if(in_array($field_type, array('message', 'accordion', 'tab', 'acfe_column', 'acfe_dynamic_message'))) |
| 132 | continue; |
| 133 | |
| 134 | add_action('acf/render_field_settings/type=' . $field_type, array($this, 'render_field_settings'), 990); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Add Setting |
| 142 | */ |
| 143 | function render_field_settings($field){ |
| 144 | |
| 145 | $valid = false; |
| 146 | |
| 147 | // Ajax |
| 148 | if(acf_verify_ajax()){ |
| 149 | |
| 150 | $field_group = acfe_get_field_group_from_field($field); |
| 151 | |
| 152 | if(acf_maybe_get($field_group, 'acfe_form')) |
| 153 | $valid = true; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | // Display |
| 158 | else{ |
| 159 | |
| 160 | if(acf_maybe_get($field, 'acfe_form')) |
| 161 | $valid = true; |
| 162 | |
| 163 | if(!$valid && acf_maybe_get($field, '_name') === 'new_field'){ |
| 164 | |
| 165 | $field_group_id = get_the_ID(); |
| 166 | |
| 167 | if($field_group_id){ |
| 168 | |
| 169 | $field_group = acf_get_field_group($field_group_id); |
| 170 | |
| 171 | if(acf_maybe_get($field_group, 'acfe_form')) |
| 172 | $valid = true; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | if(!$valid) |
| 181 | return; |
| 182 | |
| 183 | // Settings |
| 184 | acf_render_field_setting($field, array( |
| 185 | 'label' => __('Advanced settings', 'acf'), |
| 186 | 'name' => 'acfe_settings', |
| 187 | 'key' => 'acfe_settings', |
| 188 | 'instructions' => __('Change field settings based on location'), |
| 189 | 'type' => 'repeater', |
| 190 | 'button_label' => __('Add settings'), |
| 191 | 'required' => false, |
| 192 | 'layout' => 'row', |
| 193 | 'sub_fields' => array( |
| 194 | array( |
| 195 | 'label' => 'Location', |
| 196 | 'name' => 'acfe_settings_location', |
| 197 | 'key' => 'acfe_settings_location', |
| 198 | 'type' => 'select', |
| 199 | 'instructions' => '', |
| 200 | 'required' => 0, |
| 201 | 'conditional_logic' => 0, |
| 202 | 'wrapper' => array( |
| 203 | 'width' => '', |
| 204 | 'class' => '', |
| 205 | 'id' => '', |
| 206 | ), |
| 207 | 'choices' => array( |
| 208 | 'admin' => 'Administration', |
| 209 | 'front' => 'Front-end', |
| 210 | ), |
| 211 | 'allow_null' => true, |
| 212 | 'multiple' => 0, |
| 213 | 'ui' => 0, |
| 214 | 'return_format' => 'value', |
| 215 | 'ajax' => 0, |
| 216 | 'placeholder' => 'Everywhere', |
| 217 | ), |
| 218 | array( |
| 219 | 'label' => __('Settings'), |
| 220 | 'name' => 'acfe_settings_settings', |
| 221 | 'key' => 'acfe_settings_settings', |
| 222 | 'instructions' => '', |
| 223 | 'type' => 'repeater', |
| 224 | 'button_label' => __('+'), |
| 225 | 'required' => false, |
| 226 | 'layout' => 'table', |
| 227 | 'sub_fields' => array( |
| 228 | array( |
| 229 | 'ID' => false, |
| 230 | 'label' => 'Setting', |
| 231 | 'name' => 'acfe_settings_setting_type', |
| 232 | 'key' => 'acfe_settings_setting_type', |
| 233 | 'prefix' => '', |
| 234 | '_name' => '', |
| 235 | '_prepare' => '', |
| 236 | 'type' => 'select', |
| 237 | 'instructions' => false, |
| 238 | 'required' => false, |
| 239 | 'wrapper' => array( |
| 240 | 'width' => '', |
| 241 | 'class' => '', |
| 242 | 'id' => '', |
| 243 | ), |
| 244 | 'choices' => array( |
| 245 | 'required' => 'Required', |
| 246 | 'hide_field' => 'Hide field', |
| 247 | 'hide_label' => 'Hide label', |
| 248 | 'default_value' => 'Default value', |
| 249 | 'placeholder' => 'Placeholder', |
| 250 | 'instructions' => 'Instructions', |
| 251 | 'custom' => 'Custom setting', |
| 252 | ) |
| 253 | ), |
| 254 | array( |
| 255 | 'ID' => false, |
| 256 | 'label' => 'Setting name', |
| 257 | 'name' => 'acfe_settings_setting_name', |
| 258 | 'key' => 'acfe_settings_setting_name', |
| 259 | 'prefix' => '', |
| 260 | '_name' => '', |
| 261 | '_prepare' => '', |
| 262 | 'type' => 'text', |
| 263 | 'instructions' => false, |
| 264 | 'required' => false, |
| 265 | 'wrapper' => array( |
| 266 | 'width' => '', |
| 267 | 'class' => '', |
| 268 | 'id' => '', |
| 269 | ), |
| 270 | 'conditional_logic' => array( |
| 271 | array( |
| 272 | array( |
| 273 | 'field' => 'acfe_settings_setting_type', |
| 274 | 'operator' => '==', |
| 275 | 'value' => 'custom', |
| 276 | ) |
| 277 | ) |
| 278 | ) |
| 279 | ), |
| 280 | array( |
| 281 | 'ID' => false, |
| 282 | 'label' => 'Operator / Value', |
| 283 | 'name' => 'acfe_settings_setting_operator', |
| 284 | 'key' => 'acfe_settings_setting_operator', |
| 285 | 'prefix' => '', |
| 286 | '_name' => '', |
| 287 | '_prepare' => '', |
| 288 | 'type' => 'select', |
| 289 | 'choices' => array( |
| 290 | 'Values' => array( |
| 291 | 'true' => '= true', |
| 292 | 'false' => '= false', |
| 293 | 'empty' => '= (empty)', |
| 294 | ), |
| 295 | 'Operators' => array( |
| 296 | '=' => '=', |
| 297 | ), |
| 298 | ), |
| 299 | 'instructions' => false, |
| 300 | 'required' => false, |
| 301 | 'wrapper' => array( |
| 302 | 'width' => '', |
| 303 | 'class' => '', |
| 304 | 'id' => '', |
| 305 | ), |
| 306 | ), |
| 307 | array( |
| 308 | 'ID' => false, |
| 309 | 'label' => 'Value', |
| 310 | 'name' => 'acfe_settings_setting_value', |
| 311 | 'key' => 'acfe_settings_setting_value', |
| 312 | 'prefix' => '', |
| 313 | '_name' => '', |
| 314 | '_prepare' => '', |
| 315 | 'type' => 'text', |
| 316 | 'instructions' => false, |
| 317 | 'placeholder' => '', |
| 318 | 'required' => false, |
| 319 | 'wrapper' => array( |
| 320 | 'width' => '', |
| 321 | 'class' => '', |
| 322 | 'id' => '', |
| 323 | ), |
| 324 | 'conditional_logic' => array( |
| 325 | array( |
| 326 | array( |
| 327 | 'field' => 'acfe_settings_setting_operator', |
| 328 | 'operator' => '==', |
| 329 | 'value' => '=', |
| 330 | ) |
| 331 | ), |
| 332 | ) |
| 333 | ), |
| 334 | ) |
| 335 | ), |
| 336 | ) |
| 337 | ), false); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Load field |
| 343 | */ |
| 344 | function load_field($field){ |
| 345 | |
| 346 | if(!acf_maybe_get($field, 'acfe_settings')) |
| 347 | return $field; |
| 348 | |
| 349 | $exclude = apply_filters('acfe/settings/exclude', false, $field); |
| 350 | if($exclude) |
| 351 | return $field; |
| 352 | |
| 353 | foreach($field['acfe_settings'] as $k => $rule){ |
| 354 | |
| 355 | // Fix possible ACF Clone Index |
| 356 | if($k === 'acfcloneindex') |
| 357 | continue; |
| 358 | |
| 359 | // Screen |
| 360 | $screen = isset($rule['acfe_settings_location']) ? $rule['acfe_settings_location'] : ''; |
| 361 | $screen_allow = false; |
| 362 | |
| 363 | // Screen: All |
| 364 | if(empty($screen)){ |
| 365 | |
| 366 | $screen_allow = true; |
| 367 | |
| 368 | } |
| 369 | |
| 370 | // Screen: Admin |
| 371 | elseif($screen === 'admin' && acfe_form_is_admin()){ |
| 372 | |
| 373 | $screen_allow = true; |
| 374 | |
| 375 | } |
| 376 | |
| 377 | // Screen: Front |
| 378 | elseif($screen === 'front' && acfe_form_is_front()){ |
| 379 | |
| 380 | $screen_allow = true; |
| 381 | |
| 382 | } |
| 383 | |
| 384 | if(!$screen_allow) |
| 385 | continue; |
| 386 | |
| 387 | if(!acf_maybe_get($rule, 'acfe_settings_settings')) |
| 388 | continue; |
| 389 | |
| 390 | // Properties |
| 391 | $properties = $rule['acfe_settings_settings']; |
| 392 | |
| 393 | foreach($properties as $property){ |
| 394 | |
| 395 | // Required / Hide field / Hide label / Default value / Placeholder / Instructions |
| 396 | $property_name = $property['acfe_settings_setting_type']; |
| 397 | |
| 398 | // Custom |
| 399 | if($property['acfe_settings_setting_type'] === 'custom'){ |
| 400 | |
| 401 | if(!isset($property['acfe_settings_setting_name']) || empty($property['acfe_settings_setting_name'])) |
| 402 | continue; |
| 403 | |
| 404 | $property_name = $property['acfe_settings_setting_name']; |
| 405 | |
| 406 | } |
| 407 | |
| 408 | // = value |
| 409 | if($property['acfe_settings_setting_operator'] === '='){ |
| 410 | |
| 411 | $field[$property_name] = $property['acfe_settings_setting_value']; |
| 412 | |
| 413 | } |
| 414 | |
| 415 | // = true |
| 416 | elseif($property['acfe_settings_setting_operator'] === 'true'){ |
| 417 | |
| 418 | $field[$property_name] = true; |
| 419 | |
| 420 | } |
| 421 | |
| 422 | // = false |
| 423 | elseif($property['acfe_settings_setting_operator'] === 'false'){ |
| 424 | |
| 425 | $field[$property_name] = false; |
| 426 | |
| 427 | } |
| 428 | |
| 429 | // = empty |
| 430 | elseif($property['acfe_settings_setting_operator'] === 'empty'){ |
| 431 | |
| 432 | $field[$property_name] = ''; |
| 433 | |
| 434 | } |
| 435 | |
| 436 | } |
| 437 | |
| 438 | } |
| 439 | |
| 440 | return $field; |
| 441 | |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Additional settings |
| 446 | */ |
| 447 | function prepare_field($field){ |
| 448 | |
| 449 | if(isset($field['hide_field']) && !empty($field['hide_field'])){ |
| 450 | |
| 451 | return false; |
| 452 | |
| 453 | } |
| 454 | |
| 455 | if(isset($field['hide_label']) && !empty($field['hide_label'])){ |
| 456 | |
| 457 | $field['label'] = ''; |
| 458 | |
| 459 | } |
| 460 | |
| 461 | return $field; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Process Setting |
| 467 | */ |
| 468 | function fix_repeater($field){ |
| 469 | |
| 470 | $field['prefix'] = str_replace('row-', '', $field['prefix']); |
| 471 | $field['name'] = str_replace('row-', '', $field['name']); |
| 472 | |
| 473 | return $field; |
| 474 | |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Setting: ACF Clone Index fix for flexible duplicate |
| 479 | */ |
| 480 | function fix_clone($field){ |
| 481 | |
| 482 | if(isset($field['acfe_settings']['acfcloneindex'])) |
| 483 | $field['acfe_settings'] = false; |
| 484 | |
| 485 | return $field; |
| 486 | |
| 487 | } |
| 488 | |
| 489 | } |
| 490 | |
| 491 | // initialize |
| 492 | new acfe_field_settings(); |
| 493 | |
| 494 | endif; |