module-acf.php
| 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_filter('acf/pre_load_value', array($this, 'pre_load_value'), 10, 3); |
| 24 | add_action('acf/save_post', array($this, 'save_post'), 1); |
| 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 | // get form data |
| 57 | $post_id = acf_get_form_data('post_id'); |
| 58 | |
| 59 | // get module |
| 60 | $module = acfe_get_module_by_item($post_id); |
| 61 | |
| 62 | // validate module |
| 63 | if(!$module){ |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // register field groups |
| 68 | foreach($module->get_field_groups() as $field_group){ |
| 69 | acf_add_local_field_group($field_group); |
| 70 | } |
| 71 | |
| 72 | // item |
| 73 | $item = $module->get_item($post_id); |
| 74 | |
| 75 | // setup meta |
| 76 | acfe_setup_meta($_POST['acf'], 'acfe/module/validate_save_post', true); |
| 77 | |
| 78 | // validate module values |
| 79 | foreach($module->validate as $name){ |
| 80 | |
| 81 | if(method_exists($module, "validate_{$name}")){ |
| 82 | |
| 83 | // use field_name |
| 84 | $key = "field_{$name}"; |
| 85 | $field = acf_get_field($key); |
| 86 | $value = get_field($key); |
| 87 | $valid = $module->{"validate_{$name}"}($value, $item); |
| 88 | |
| 89 | // empty required |
| 90 | if($field && $field['required'] && empty($value) && !is_numeric($value)){ |
| 91 | $valid = sprintf(__('%s value is required', 'acf'), $field['label']); |
| 92 | } |
| 93 | |
| 94 | // allow $valid to be a custom error message |
| 95 | if(!empty($valid) && is_string($valid)){ |
| 96 | acfe_add_validation_error($key, "acfe:{$valid}"); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | // actions |
| 104 | $module->do_module_action('acfe/module/validate_save_item', $item); |
| 105 | |
| 106 | // reset meta |
| 107 | acfe_reset_meta(); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * after_validate_save_post |
| 114 | * |
| 115 | * acf/validate_save_post |
| 116 | * |
| 117 | * disable errors on module validation to avoid collision with user field validation names |
| 118 | */ |
| 119 | function after_validate_save_post(){ |
| 120 | |
| 121 | // get form data |
| 122 | $post_id = acf_get_form_data('post_id'); |
| 123 | |
| 124 | // get module |
| 125 | $module = acfe_get_module_by_item($post_id); |
| 126 | |
| 127 | // validate module |
| 128 | if(!$module){ |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // get errors |
| 133 | $errors = acf_get_array(acf()->validation->get_errors()); |
| 134 | |
| 135 | // remove non acfe errors |
| 136 | // this will remove errors set by developers that use field name such as "name" |
| 137 | // note that this will also remove native acf validation message such as "required value" |
| 138 | foreach(array_keys($errors) as $key){ |
| 139 | |
| 140 | if(!acfe_starts_with($errors[ $key ]['message'], 'acfe:')){ |
| 141 | unset($errors[ $key ]); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | // cleanup acfe error messages |
| 147 | foreach(array_keys($errors) as $key){ |
| 148 | $errors[ $key ]['message'] = str_replace('acfe:', '', $errors[ $key ]['message']); |
| 149 | } |
| 150 | |
| 151 | // add new errors |
| 152 | acf()->validation->errors = $errors; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * pre_load_value |
| 158 | * |
| 159 | * acf/pre_load_value |
| 160 | * |
| 161 | * @param $null |
| 162 | * @param $post_id |
| 163 | * @param $field |
| 164 | * |
| 165 | * @return mixed|null |
| 166 | */ |
| 167 | function pre_load_value($null, $post_id, $field){ |
| 168 | |
| 169 | // get module |
| 170 | $module = acfe_get_module_by_item($post_id); |
| 171 | |
| 172 | // validate module |
| 173 | if(!$module){ |
| 174 | return $null; |
| 175 | } |
| 176 | |
| 177 | // load only one time |
| 178 | if(empty($this->values)){ |
| 179 | |
| 180 | // item |
| 181 | $item = $module->get_item($post_id); |
| 182 | |
| 183 | // validate item |
| 184 | if(empty($item)){ |
| 185 | return $null; |
| 186 | } |
| 187 | |
| 188 | // remove unused keys |
| 189 | acf_extract_vars($item, array('ID', '_valid')); |
| 190 | |
| 191 | foreach(array_keys($item) as $k){ |
| 192 | |
| 193 | $v = $item[ $k ]; |
| 194 | $_field = acf_get_field($k); |
| 195 | |
| 196 | if(!$_field){ |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | // encode value |
| 201 | if(acf_maybe_get($_field, 'encode_value')){ |
| 202 | $with_keys = !acf_is_sequential_array($v); |
| 203 | $item[ $k ] = acf_encode_choices($v, $with_keys); |
| 204 | } |
| 205 | |
| 206 | // unparse type |
| 207 | if(acf_maybe_get($_field, 'unparse_type')){ |
| 208 | $item[ $k ] = acfe_unparse_types($v); |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | |
| 213 | // filters |
| 214 | $item = $module->apply_module_filters('acfe/module/prepare_load_item', $item); |
| 215 | |
| 216 | // prefix keys like "name" with "field_name" for acf loading values |
| 217 | $acf = acfe_prefix_array_keys($item, 'field_', array('acf_fc_layout')); |
| 218 | |
| 219 | // set values |
| 220 | $this->values = $acf; |
| 221 | |
| 222 | } |
| 223 | |
| 224 | return acf_maybe_get($this->values, $field['key'], $null); |
| 225 | |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * pre_format_value |
| 231 | * |
| 232 | * acf/pre_format_value:10 |
| 233 | * |
| 234 | * @param $null |
| 235 | * @param $value |
| 236 | * @param $post_id |
| 237 | * @param $field |
| 238 | * |
| 239 | * @return false|mixed |
| 240 | */ |
| 241 | function pre_format_value($null, $value, $post_id, $field){ |
| 242 | |
| 243 | // do not format value for wysiwyg fields |
| 244 | if($field['type'] === 'wysiwyg'){ |
| 245 | return $value; |
| 246 | } |
| 247 | |
| 248 | return $null; |
| 249 | |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * save_post |
| 255 | * |
| 256 | * acf/save_post:1 |
| 257 | * |
| 258 | * @param $post_id |
| 259 | */ |
| 260 | function save_post($post_id){ |
| 261 | |
| 262 | // get module |
| 263 | $module = acfe_get_module_by_item($post_id); |
| 264 | |
| 265 | // validate module |
| 266 | if(!$module){ |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // setup meta |
| 271 | acfe_setup_meta($_POST['acf'], 'acfe/module/save_post', true); |
| 272 | |
| 273 | // defaults vars |
| 274 | // $item['name'] already set in get_fields() (field is mandatory in field groups) |
| 275 | $item = array( |
| 276 | 'ID' => $post_id, |
| 277 | 'name' => '', |
| 278 | 'label' => acf_maybe_get_POST('post_title'), |
| 279 | ); |
| 280 | |
| 281 | add_filter('acf/pre_format_value', array($this, 'pre_format_value'), 10, 4); |
| 282 | |
| 283 | // alias of get_fields |
| 284 | $fields = get_field_objects(); |
| 285 | |
| 286 | remove_filter('acf/pre_format_value', array($this, 'pre_format_value')); |
| 287 | |
| 288 | $meta = array(); |
| 289 | |
| 290 | // bail early |
| 291 | if($fields){ |
| 292 | foreach($fields as $k => $field){ |
| 293 | |
| 294 | $meta[ $k ] = $field['value']; |
| 295 | |
| 296 | // encode value |
| 297 | if(acf_maybe_get($field, 'encode_value')){ |
| 298 | $with_keys = strpos($field['value'], ' : ') === false; |
| 299 | $meta[ $k ] = acf_decode_choices($field['value'], $with_keys); |
| 300 | } |
| 301 | |
| 302 | // group with |
| 303 | if(acf_maybe_get($field, 'group_with')){ |
| 304 | $meta[ $field['group_with'] ][ $k ] = $field['value']; |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | $item = array_merge($item, $meta); |
| 311 | |
| 312 | // filters |
| 313 | $item = $module->apply_module_filters('acfe/module/prepare_save_item', $item); |
| 314 | |
| 315 | // field exists |
| 316 | if($fields){ |
| 317 | foreach($fields as $k => $field){ |
| 318 | |
| 319 | // cleanup key |
| 320 | if(acf_maybe_get($field, 'cleanup_key')){ |
| 321 | unset($item[ $k ]); |
| 322 | } |
| 323 | |
| 324 | // group with |
| 325 | if(acf_maybe_get($field, 'group_with')){ |
| 326 | unset($item[ $k ]); |
| 327 | } |
| 328 | |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // cleanup empty labels |
| 333 | if(!empty($item['labels'])){ |
| 334 | foreach($item['labels'] as $key => $label){ |
| 335 | |
| 336 | // cleanup label if empty |
| 337 | if(empty($label)){ |
| 338 | unset($item['labels'][ $key ]); |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // reset meta |
| 345 | acfe_reset_meta(); |
| 346 | |
| 347 | // update |
| 348 | $module->update_item($item); |
| 349 | |
| 350 | // bypass acf native values update |
| 351 | $_POST['acf'] = array(); |
| 352 | |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /** |
| 357 | * include_admin_tools |
| 358 | * |
| 359 | * acf/include_admin_tools:15 |
| 360 | */ |
| 361 | function include_admin_tools(){ |
| 362 | |
| 363 | foreach(acfe_get_modules() as $module){ |
| 364 | |
| 365 | // get tool names |
| 366 | $export_tool = $module->get_export_tool(); |
| 367 | $import_tool = $module->get_import_tool(); |
| 368 | |
| 369 | // reigster acf tools |
| 370 | acf()->admin_tools->tools[ $export_tool ] = new acfe_module_export($module); |
| 371 | acf()->admin_tools->tools[ $import_tool ] = new acfe_module_import($module); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /** |
| 379 | * include_admin_tools_sort |
| 380 | * |
| 381 | * acf/include_admin_tools:99 |
| 382 | * |
| 383 | * Sort ACF tools |
| 384 | */ |
| 385 | function include_admin_tools_sort(){ |
| 386 | |
| 387 | $sort = array( |
| 388 | 'export', |
| 389 | 'import', |
| 390 | 'acfe_module_post_type_export', |
| 391 | 'acfe_module_post_type_import', |
| 392 | 'acfe_module_taxonomy_export', |
| 393 | 'acfe_module_taxonomy_import', |
| 394 | 'acfe_module_block_type_export', |
| 395 | 'acfe_module_block_type_import', |
| 396 | 'acfe_module_options_page_export', |
| 397 | 'acfe_module_options_page_import', |
| 398 | 'acfe_module_template_export', |
| 399 | 'acfe_module_template_import', |
| 400 | ); |
| 401 | |
| 402 | uksort(acf()->admin_tools->tools, function($a, $b) use($sort){ |
| 403 | foreach($sort as $value){ |
| 404 | if($a === $value){return 0;} |
| 405 | if($b === $value){return 1;} |
| 406 | } |
| 407 | }); |
| 408 | |
| 409 | } |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * get_post_types |
| 414 | * |
| 415 | * acf/get_post_types |
| 416 | * |
| 417 | * remove reserved post types |
| 418 | * |
| 419 | * @param $post_types |
| 420 | * @param $args |
| 421 | * |
| 422 | * @return mixed |
| 423 | */ |
| 424 | function get_post_types($post_types, $args){ |
| 425 | |
| 426 | foreach($post_types as $k => $post_type){ |
| 427 | if(acfe_is_post_type_reserved($post_type)){ |
| 428 | unset($post_types[ $k ]); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return $post_types; |
| 433 | |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /** |
| 438 | * wp_insert_post_data |
| 439 | * |
| 440 | * force field_name as post_name. This has been disabled as it cause problem when updating names and generating sync files |
| 441 | * |
| 442 | * @param $args |
| 443 | * @param $post_array |
| 444 | * |
| 445 | * @return mixed |
| 446 | */ |
| 447 | function wp_insert_post_data($args, $post_array){ |
| 448 | |
| 449 | // get post id |
| 450 | $post_id = acf_maybe_get($post_array, 'ID'); |
| 451 | |
| 452 | // get module |
| 453 | $module = acfe_get_module_by_item($post_id); |
| 454 | |
| 455 | // validate module |
| 456 | if(!$module){ |
| 457 | return $args; |
| 458 | } |
| 459 | |
| 460 | if(!isset($post_array['acf'])){ |
| 461 | return $args; |
| 462 | } |
| 463 | |
| 464 | $name = acf_maybe_get($post_array['acf'], 'field_name'); |
| 465 | $args['post_name'] = sanitize_title($name); |
| 466 | |
| 467 | return $args; |
| 468 | |
| 469 | } |
| 470 | |
| 471 | } |
| 472 | |
| 473 | acf_new_instance('acfe_module_acf'); |
| 474 | |
| 475 | endif; |