admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
module-acf.php
519 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_acf')): |
| 8 | |
| 9 | class acfe_module_acf{ |
| 10 | |
| 11 | // vars |
| 12 | public $values = array(); |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * construct |
| 17 | */ |
| 18 | function __construct(){ |
| 19 | |
| 20 | add_action('acf/include_fields', array($this, 'include_fields')); |
| 21 | add_action('acf/validate_save_post', array($this, 'validate_save_post'), 1); |
| 22 | add_action('acf/validate_save_post', array($this, 'after_validate_save_post')); |
| 23 | add_action('acf/save_post', array($this, 'save_post'), 1); |
| 24 | add_filter('acf/pre_load_value', array($this, 'pre_load_value'), 10, 3); |
| 25 | add_action('acf/include_admin_tools', array($this, 'include_admin_tools'), 15); |
| 26 | add_action('acf/include_admin_tools', array($this, 'include_admin_tools_sort'), 99); |
| 27 | add_filter('acf/get_post_types', array($this, 'get_post_types'), 10, 2); |
| 28 | //add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10, 2); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * include_fields |
| 34 | * |
| 35 | * acf/include_fields |
| 36 | */ |
| 37 | function include_fields(){ |
| 38 | |
| 39 | // loop modules |
| 40 | foreach(acfe_get_modules() as $module){ |
| 41 | |
| 42 | // trigger include items |
| 43 | $module->do_module_action('acfe/module/include_items'); |
| 44 | |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * validate_save_post |
| 51 | * |
| 52 | * acf/validate_save_post:1 |
| 53 | */ |
| 54 | function validate_save_post(){ |
| 55 | |
| 56 | // validate |
| 57 | if(!acf_current_user_can_admin() || !$this->verify_nonce()){ |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | // get form data |
| 62 | $post_id = acf_get_form_data('post_id'); |
| 63 | |
| 64 | // get module |
| 65 | $module = acfe_get_module_by_item($post_id); |
| 66 | |
| 67 | // validate module |
| 68 | if(!$module){ |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | // register field groups |
| 73 | foreach($module->get_field_groups() as $field_group){ |
| 74 | acf_add_local_field_group($field_group); |
| 75 | } |
| 76 | |
| 77 | // item |
| 78 | $item = $module->get_item($post_id); |
| 79 | |
| 80 | // setup meta |
| 81 | acfe_setup_meta($_POST['acf'], 'acfe/module/validate_save_post', true); |
| 82 | |
| 83 | // validate module values |
| 84 | foreach($module->validate as $name){ |
| 85 | |
| 86 | if(method_exists($module, "validate_{$name}")){ |
| 87 | |
| 88 | // use field_name |
| 89 | $key = "field_{$name}"; |
| 90 | $field = acf_get_field($key); |
| 91 | $value = get_field($key); |
| 92 | $valid = $module->{"validate_{$name}"}($value, $item); |
| 93 | |
| 94 | // empty required |
| 95 | if($field && $field['required'] && empty($value) && !is_numeric($value)){ |
| 96 | $valid = sprintf(__('%s value is required', 'acf'), $field['label']); |
| 97 | } |
| 98 | |
| 99 | // allow $valid to be a custom error message |
| 100 | if(!empty($valid) && is_string($valid)){ |
| 101 | acfe_add_validation_error($key, "acfe:{$valid}"); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | // actions |
| 109 | $module->do_module_action('acfe/module/validate_save_item', $item); |
| 110 | |
| 111 | // reset meta |
| 112 | acfe_reset_meta(); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * after_validate_save_post |
| 119 | * |
| 120 | * acf/validate_save_post |
| 121 | * |
| 122 | * disable errors on module validation to avoid collision with user field validation names |
| 123 | */ |
| 124 | function after_validate_save_post(){ |
| 125 | |
| 126 | // validate |
| 127 | if(!acf_current_user_can_admin() || !$this->verify_nonce()){ |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // get form data |
| 132 | $post_id = acf_get_form_data('post_id'); |
| 133 | |
| 134 | // get module |
| 135 | $module = acfe_get_module_by_item($post_id); |
| 136 | |
| 137 | // validate module |
| 138 | if(!$module){ |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | // get errors |
| 143 | $errors = acfe_as_array(acf()->validation->get_errors()); |
| 144 | |
| 145 | // remove non acfe errors |
| 146 | // this will remove errors set by developers that use field name such as "name" |
| 147 | // note that this will also remove native acf validation message such as "required value" |
| 148 | foreach(array_keys($errors) as $key){ |
| 149 | |
| 150 | if(acfe_starts_with($errors[ $key ]['message'], 'acfe:')){ |
| 151 | $errors[ $key ]['message'] = str_replace('acfe:', '', $errors[ $key ]['message']); |
| 152 | }else{ |
| 153 | unset($errors[ $key ]); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | // add new errors |
| 159 | acf()->validation->errors = $errors; |
| 160 | |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * save_post |
| 166 | * |
| 167 | * acf/save_post:1 |
| 168 | * |
| 169 | * @param $post_id |
| 170 | */ |
| 171 | function save_post($post_id){ |
| 172 | |
| 173 | // validate |
| 174 | if(!acf_current_user_can_admin() || !$this->verify_nonce()){ |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // get module |
| 179 | $module = acfe_get_module_by_item($post_id); |
| 180 | |
| 181 | // validate module |
| 182 | if(!$module){ |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | // setup meta |
| 187 | acfe_setup_meta($_POST['acf'], 'acfe/module/save_post', true); |
| 188 | |
| 189 | // defaults vars |
| 190 | // $item['name'] already set in get_fields() (field is mandatory in field groups) |
| 191 | $item = array( |
| 192 | 'ID' => $post_id, |
| 193 | 'name' => '', |
| 194 | 'label' => acf_maybe_get_POST('post_title'), |
| 195 | ); |
| 196 | |
| 197 | add_filter('acf/pre_format_value', array($this, 'pre_format_value'), 10, 4); |
| 198 | |
| 199 | // alias of get_fields |
| 200 | $fields = get_field_objects(); |
| 201 | |
| 202 | remove_filter('acf/pre_format_value', array($this, 'pre_format_value')); |
| 203 | |
| 204 | $meta = array(); |
| 205 | |
| 206 | // bail early |
| 207 | if($fields){ |
| 208 | foreach($fields as $k => $field){ |
| 209 | |
| 210 | $meta[ $k ] = $field['value']; |
| 211 | |
| 212 | // encode value |
| 213 | if(acfe_get($field, 'encode_value')){ |
| 214 | $with_keys = strpos($field['value'], ' : ') === false; |
| 215 | $meta[ $k ] = acf_decode_choices($field['value'], $with_keys); |
| 216 | } |
| 217 | |
| 218 | // group with |
| 219 | if(acfe_get($field, 'group_with')){ |
| 220 | $meta[ $field['group_with'] ][ $k ] = $field['value']; |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | $item = array_merge($item, $meta); |
| 227 | |
| 228 | // filters |
| 229 | $item = $module->apply_module_filters('acfe/module/prepare_save_item', $item); |
| 230 | |
| 231 | // field exists |
| 232 | if($fields){ |
| 233 | foreach($fields as $k => $field){ |
| 234 | |
| 235 | // cleanup key |
| 236 | if(acfe_get($field, 'cleanup_key')){ |
| 237 | unset($item[ $k ]); |
| 238 | } |
| 239 | |
| 240 | // group with |
| 241 | if(acfe_get($field, 'group_with')){ |
| 242 | unset($item[ $k ]); |
| 243 | } |
| 244 | |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // cleanup empty labels |
| 249 | if(!empty($item['labels'])){ |
| 250 | foreach($item['labels'] as $key => $label){ |
| 251 | |
| 252 | // cleanup label if empty |
| 253 | if(empty($label)){ |
| 254 | unset($item['labels'][ $key ]); |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // reset meta |
| 261 | acfe_reset_meta(); |
| 262 | |
| 263 | // update |
| 264 | $module->update_item($item); |
| 265 | |
| 266 | // bypass acf native values update |
| 267 | $_POST['acf'] = array(); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * pre_load_value |
| 274 | * |
| 275 | * acf/pre_load_value |
| 276 | * |
| 277 | * @param $null |
| 278 | * @param $post_id |
| 279 | * @param $field |
| 280 | * |
| 281 | * @return mixed|null |
| 282 | */ |
| 283 | function pre_load_value($null, $post_id, $field){ |
| 284 | |
| 285 | // validate capability |
| 286 | if(!acf_current_user_can_admin()){ |
| 287 | return $null; |
| 288 | } |
| 289 | |
| 290 | // get module |
| 291 | $module = acfe_get_module_by_item($post_id); |
| 292 | |
| 293 | // validate module |
| 294 | if(!$module){ |
| 295 | return $null; |
| 296 | } |
| 297 | |
| 298 | // load only one time |
| 299 | if(empty($this->values)){ |
| 300 | |
| 301 | // item |
| 302 | $item = $module->get_item($post_id); |
| 303 | |
| 304 | // validate item |
| 305 | if(empty($item)){ |
| 306 | return $null; |
| 307 | } |
| 308 | |
| 309 | // remove unused keys |
| 310 | acf_extract_vars($item, array('ID', '_valid')); |
| 311 | |
| 312 | foreach(array_keys($item) as $k){ |
| 313 | |
| 314 | $v = $item[ $k ]; |
| 315 | $_field = acf_get_field($k); |
| 316 | |
| 317 | if(!$_field){ |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | // encode value |
| 322 | if(acfe_get($_field, 'encode_value')){ |
| 323 | $with_keys = !acf_is_sequential_array($v); |
| 324 | $item[ $k ] = acf_encode_choices($v, $with_keys); |
| 325 | } |
| 326 | |
| 327 | // unparse type |
| 328 | if(acfe_get($_field, 'unparse_type')){ |
| 329 | $item[ $k ] = acfe_unparse_types($v); |
| 330 | } |
| 331 | |
| 332 | } |
| 333 | |
| 334 | // filters |
| 335 | $item = $module->apply_module_filters('acfe/module/prepare_load_item', $item); |
| 336 | |
| 337 | // prefix keys like "name" with "field_name" for acf loading values |
| 338 | $acf = acfe_array_rewrite($item, function($value, $key, $row){ |
| 339 | |
| 340 | // ignore numeric and acf_fc_layout keys |
| 341 | if(is_numeric($key) || $key === 'acf_fc_layout'){ |
| 342 | return array($key => $value); |
| 343 | } |
| 344 | |
| 345 | // return |
| 346 | return array("field_{$key}" => $value); |
| 347 | |
| 348 | }, true); |
| 349 | |
| 350 | // set values |
| 351 | $this->values = $acf; |
| 352 | |
| 353 | } |
| 354 | |
| 355 | return acfe_get($this->values, $field['key'], $null); |
| 356 | |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /** |
| 361 | * pre_format_value |
| 362 | * |
| 363 | * acf/pre_format_value:10 |
| 364 | * |
| 365 | * @param $null |
| 366 | * @param $value |
| 367 | * @param $post_id |
| 368 | * @param $field |
| 369 | * |
| 370 | * @return false|mixed |
| 371 | */ |
| 372 | function pre_format_value($null, $value, $post_id, $field){ |
| 373 | |
| 374 | // do not format value for wysiwyg fields |
| 375 | if($field['type'] === 'wysiwyg'){ |
| 376 | return $value; |
| 377 | } |
| 378 | |
| 379 | return $null; |
| 380 | |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /** |
| 385 | * include_admin_tools |
| 386 | * |
| 387 | * acf/include_admin_tools:15 |
| 388 | */ |
| 389 | function include_admin_tools(){ |
| 390 | |
| 391 | foreach(acfe_get_modules() as $module){ |
| 392 | |
| 393 | // get tool names |
| 394 | $export_tool = $module->get_export_tool(); |
| 395 | $import_tool = $module->get_import_tool(); |
| 396 | |
| 397 | // reigster acf tools |
| 398 | acf()->admin_tools->tools[ $export_tool ] = new acfe_module_export($module); |
| 399 | acf()->admin_tools->tools[ $import_tool ] = new acfe_module_import($module); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * include_admin_tools_sort |
| 408 | * |
| 409 | * acf/include_admin_tools:99 |
| 410 | * |
| 411 | * Sort ACF tools |
| 412 | */ |
| 413 | function include_admin_tools_sort(){ |
| 414 | |
| 415 | $sort = array( |
| 416 | 'export', |
| 417 | 'import', |
| 418 | 'acfe_module_post_type_export', |
| 419 | 'acfe_module_post_type_import', |
| 420 | 'acfe_module_taxonomy_export', |
| 421 | 'acfe_module_taxonomy_import', |
| 422 | 'acfe_module_block_type_export', |
| 423 | 'acfe_module_block_type_import', |
| 424 | 'acfe_module_options_page_export', |
| 425 | 'acfe_module_options_page_import', |
| 426 | 'acfe_module_template_export', |
| 427 | 'acfe_module_template_import', |
| 428 | ); |
| 429 | |
| 430 | uksort(acf()->admin_tools->tools, function($a, $b) use($sort){ |
| 431 | foreach($sort as $value){ |
| 432 | if($a === $value){return 0;} |
| 433 | if($b === $value){return 1;} |
| 434 | } |
| 435 | }); |
| 436 | |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
| 441 | * get_post_types |
| 442 | * |
| 443 | * acf/get_post_types |
| 444 | * |
| 445 | * remove reserved post types |
| 446 | * |
| 447 | * @param $post_types |
| 448 | * @param $args |
| 449 | * |
| 450 | * @return mixed |
| 451 | */ |
| 452 | function get_post_types($post_types, $args){ |
| 453 | |
| 454 | foreach($post_types as $k => $post_type){ |
| 455 | if(acfe_is_post_type_reserved($post_type)){ |
| 456 | unset($post_types[ $k ]); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return $post_types; |
| 461 | |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /** |
| 466 | * wp_insert_post_data |
| 467 | * |
| 468 | * force field_name as post_name. This has been disabled as it cause problem when updating names and generating sync files |
| 469 | * |
| 470 | * @param $args |
| 471 | * @param $post_array |
| 472 | * |
| 473 | * @return mixed |
| 474 | */ |
| 475 | function wp_insert_post_data($args, $post_array){ |
| 476 | |
| 477 | // get post id |
| 478 | $post_id = acfe_get($post_array, 'ID'); |
| 479 | |
| 480 | // get module |
| 481 | $module = acfe_get_module_by_item($post_id); |
| 482 | |
| 483 | // validate module |
| 484 | if(!$module){ |
| 485 | return $args; |
| 486 | } |
| 487 | |
| 488 | if(!isset($post_array['acf'])){ |
| 489 | return $args; |
| 490 | } |
| 491 | |
| 492 | $name = acfe_get($post_array['acf'], 'field_name'); |
| 493 | $args['post_name'] = sanitize_title($name); |
| 494 | |
| 495 | return $args; |
| 496 | |
| 497 | } |
| 498 | |
| 499 | |
| 500 | /** |
| 501 | * verify_nonce |
| 502 | * |
| 503 | * @return bool |
| 504 | */ |
| 505 | function verify_nonce(){ |
| 506 | |
| 507 | // get nonce |
| 508 | $nonce = acf_maybe_get_POST('_acfe_module_nonce'); |
| 509 | |
| 510 | // verify nonce |
| 511 | return $nonce && wp_verify_nonce($nonce, 'acfe_module'); |
| 512 | |
| 513 | } |
| 514 | |
| 515 | } |
| 516 | |
| 517 | acf_new_instance('acfe_module_acf'); |
| 518 | |
| 519 | endif; |