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