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