admin
1 month ago
field-groups
1 month ago
fields
1 month ago
fields-settings
1 month ago
forms
1 month ago
locations
1 month ago
modules
1 month ago
screens
1 month ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
1 month ago
acfe-field-group-functions.php
1 month ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
1 month ago
acfe-helper-array-functions.php
1 month ago
acfe-helper-functions.php
1 month ago
acfe-helper-multi-functions.php
1 month ago
acfe-helper-string-functions.php
1 month ago
acfe-meta-functions.php
1 month ago
acfe-post-functions.php
1 month ago
acfe-screen-functions.php
1 month ago
acfe-template-functions.php
1 month ago
acfe-term-functions.php
1 month ago
acfe-user-functions.php
1 month ago
acfe-wp-functions.php
3 years ago
assets.php
1 month ago
compatibility-acf-5.8.php
2 months ago
compatibility-acf-5.9.php
2 months ago
compatibility-acf-6.5.php
1 month ago
compatibility.php
1 month ago
field-extend.php
2 months ago
field.php
2 months ago
hooks.php
1 month ago
init.php
2 months ago
local-meta.php
3 years ago
media.php
1 month ago
module-acf.php
1 month ago
module-db.php
1 month ago
module-l10n.php
1 month ago
module-legacy.php
3 years ago
module-manager.php
2 months ago
module-post.php
1 month ago
module-posts.php
2 months ago
module-upgrades.php
3 years ago
module.php
1 month ago
multilang.php
1 month ago
revisions.php
2 months ago
screen.php
1 month ago
settings.php
1 month ago
template-tags.php
1 month ago
third-party.php
3 years ago
upgrades.php
1 month ago
hooks.php
564 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_hooks')): |
| 8 | |
| 9 | class acfe_hooks{ |
| 10 | |
| 11 | public $field_group; |
| 12 | |
| 13 | /** |
| 14 | * construct |
| 15 | */ |
| 16 | function __construct(){ |
| 17 | |
| 18 | // save/validate hooks |
| 19 | add_action('acf/save_post', array($this, 'pre_save_post'), 9); |
| 20 | add_action('acf/save_post', array($this, 'save_post'), 15); |
| 21 | add_action('acf/validate_save_post', array($this, 'validate_save_post'), 4); // must be 4 as acf process acf/validate_value on 5 |
| 22 | |
| 23 | // field groups |
| 24 | add_filter('acf/load_field_groups', array($this, 'load_field_groups'), 100); |
| 25 | add_filter('acf/pre_render_fields', array($this, 'pre_render_fields'), 10, 2); |
| 26 | add_action('acf/render_fields', array($this, 'render_fields'), 10, 2); |
| 27 | |
| 28 | // fields |
| 29 | add_filter('acf/field_wrapper_attributes', array($this, 'field_wrapper_attributes'), 10, 2); |
| 30 | add_filter('acf/load_fields', array($this, 'load_fields'), 10, 2); |
| 31 | add_filter('acf/load_field', array($this, 'load_field')); |
| 32 | |
| 33 | // hooks variations |
| 34 | acf_add_filter_variations('acfe/prepare_field_group', array('ID', 'key'), 0); |
| 35 | acf_add_action_variations('acfe/pre_render_field_group', array('ID', 'key'), 0); |
| 36 | acf_add_action_variations('acfe/render_field_group', array('ID', 'key'), 0); |
| 37 | acf_add_filter_variations('acf/field_wrapper_attributes', array('type', 'name', 'key'), 1); |
| 38 | acf_add_filter_variations('acfe/field_wrapper_attributes', array('type', 'name', 'key'), 1); |
| 39 | acf_add_filter_variations('acfe/load_fields', array('type', 'name', 'key'), 1); |
| 40 | acf_add_filter_variations('acfe/load_field', array('type', 'name', 'key'), 0); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * pre_save_post |
| 47 | * |
| 48 | * acf/save_post:9 |
| 49 | * |
| 50 | * @param $post_id |
| 51 | */ |
| 52 | function pre_save_post($post_id = 0){ |
| 53 | $this->do_save_post($post_id, true); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * save_post |
| 59 | * |
| 60 | * acf/save_post:15 |
| 61 | * |
| 62 | * @param $post_id |
| 63 | */ |
| 64 | function save_post($post_id = 0){ |
| 65 | $this->do_save_post($post_id); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * do_save_post |
| 71 | * |
| 72 | * @param $post_id |
| 73 | * @param $pre |
| 74 | */ |
| 75 | function do_save_post($post_id = 0, $pre = false){ |
| 76 | |
| 77 | // validate acf |
| 78 | if(!acf_maybe_get_POST('acf')){ |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // check data |
| 83 | $data = $this->decode_object($post_id); |
| 84 | |
| 85 | if(!$data){ |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // vars |
| 90 | $id = $data['id']; |
| 91 | $type = $data['type']; |
| 92 | $object = $data['object']; |
| 93 | $hooks = $data['hooks']; |
| 94 | $suffix = $pre ? 'pre_' : false; |
| 95 | |
| 96 | // all hooks |
| 97 | $all_hooks = array(); |
| 98 | $all_hooks[] = "acfe/{$suffix}save"; |
| 99 | $all_hooks[] = "acfe/{$suffix}save/id={$post_id}"; |
| 100 | $all_hooks[] = "acfe/{$suffix}save_{$type}"; |
| 101 | foreach($hooks as $hook){ |
| 102 | $all_hooks[] = "acfe/{$suffix}save_{$type}/{$hook}"; |
| 103 | } |
| 104 | $all_hooks[] = "acfe/{$suffix}save_{$type}/id={$post_id}"; |
| 105 | |
| 106 | // check if hooked |
| 107 | $do_action = false; |
| 108 | |
| 109 | foreach($all_hooks as $all_hook){ |
| 110 | if(has_action($all_hook)){ |
| 111 | $do_action = true; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // bail early |
| 117 | if(!$do_action){ |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // setup meta |
| 122 | acfe_setup_meta($_POST['acf'], 'acfe/save', true); |
| 123 | |
| 124 | foreach($all_hooks as $all_hook){ |
| 125 | do_action($all_hook, $post_id, $object); |
| 126 | } |
| 127 | |
| 128 | // reset meta |
| 129 | acfe_reset_meta(); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * validate_save_post |
| 136 | * |
| 137 | * acf/validate_save_post:4 |
| 138 | */ |
| 139 | function validate_save_post(){ |
| 140 | |
| 141 | // vars |
| 142 | $rows = array(); |
| 143 | |
| 144 | // acf |
| 145 | $acf = acf_maybe_get_POST('acf'); |
| 146 | |
| 147 | if(!empty($acf)){ |
| 148 | |
| 149 | $post_id = acf_get_form_data('post_id'); |
| 150 | if($post_id){ |
| 151 | $rows[ $post_id ] = $acf; |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | // menu items |
| 157 | $menu_items = acf_maybe_get_POST('menu-item-acf'); |
| 158 | |
| 159 | if(!empty($menu_items)){ |
| 160 | foreach($menu_items as $post_id => $fields){ |
| 161 | $rows[ $post_id ] = $fields; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // loop rows |
| 166 | foreach($rows as $post_id => $acf){ |
| 167 | |
| 168 | // check data |
| 169 | $data = $this->decode_object($post_id); |
| 170 | |
| 171 | if(!$data){ |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | // vars |
| 176 | $id = $data['id']; |
| 177 | $type = $data['type']; |
| 178 | $object = $data['object']; |
| 179 | $hooks = $data['hooks']; |
| 180 | |
| 181 | // all hooks |
| 182 | $all_hooks = array(); |
| 183 | $all_hooks[] = "acfe/validate_save"; |
| 184 | $all_hooks[] = "acfe/validate_save/id={$post_id}"; |
| 185 | $all_hooks[] = "acfe/validate_save_{$type}"; |
| 186 | foreach($hooks as $hook){ |
| 187 | $all_hooks[] = "acfe/validate_save_{$type}/{$hook}"; |
| 188 | } |
| 189 | $all_hooks[] = "acfe/validate_save_{$type}/id={$post_id}"; |
| 190 | |
| 191 | // check if hooked |
| 192 | $do_action = false; |
| 193 | |
| 194 | foreach($all_hooks as $all_hook){ |
| 195 | if(has_action($all_hook)){ |
| 196 | $do_action = true; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // bail early |
| 202 | if(!$do_action){ |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | // setup meta |
| 207 | acfe_setup_meta($acf, 'acfe/validate_save', true); |
| 208 | |
| 209 | foreach($all_hooks as $all_hook){ |
| 210 | do_action($all_hook, $post_id, $object); |
| 211 | } |
| 212 | |
| 213 | // reset meta |
| 214 | acfe_reset_meta(); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * load_field_groups |
| 223 | * |
| 224 | * acf/load_field_groups:100 |
| 225 | * |
| 226 | * @param $field_groups |
| 227 | * |
| 228 | * @return mixed |
| 229 | */ |
| 230 | function load_field_groups($field_groups){ |
| 231 | |
| 232 | // bail early |
| 233 | if(acfe_is_admin_screen()){ |
| 234 | return $field_groups; |
| 235 | } |
| 236 | |
| 237 | // loop |
| 238 | foreach(array_keys($field_groups) as $i){ |
| 239 | |
| 240 | // get field group |
| 241 | $field_group = $field_groups[ $i ]; |
| 242 | |
| 243 | // apply filters |
| 244 | $field_group = apply_filters('acfe/prepare_field_group', $field_group); |
| 245 | |
| 246 | // hide field group |
| 247 | if($field_group === false){ |
| 248 | unset($field_groups[ $i ]); |
| 249 | |
| 250 | // assign |
| 251 | }else{ |
| 252 | $field_groups[ $i ] = $field_group; |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | return $field_groups; |
| 258 | |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /** |
| 263 | * pre_render_fields |
| 264 | * |
| 265 | * acf/pre_render_fields |
| 266 | * |
| 267 | * @param $fields |
| 268 | * @param $post_id |
| 269 | * |
| 270 | * @return mixed |
| 271 | */ |
| 272 | function pre_render_fields($fields, $post_id){ |
| 273 | |
| 274 | $this->field_group = array(); |
| 275 | |
| 276 | if(!isset($fields[0])){ |
| 277 | return $fields; |
| 278 | } |
| 279 | |
| 280 | if(!acfe_get($fields[0], 'parent')){ |
| 281 | return $fields; |
| 282 | } |
| 283 | |
| 284 | $field_group = acf_get_field_group($fields[0]['parent']); |
| 285 | |
| 286 | if(!$field_group){ |
| 287 | return $fields; |
| 288 | } |
| 289 | |
| 290 | $this->field_group = $field_group; |
| 291 | |
| 292 | // action |
| 293 | do_action('acfe/pre_render_field_group', $field_group, $fields, $post_id); |
| 294 | |
| 295 | return $fields; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * render_fields |
| 302 | * |
| 303 | * acf/render_fields |
| 304 | * |
| 305 | * @param $fields |
| 306 | * @param $post_id |
| 307 | */ |
| 308 | function render_fields($fields, $post_id){ |
| 309 | |
| 310 | if(empty($this->field_group)){ |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | $field_group = $this->field_group; |
| 315 | |
| 316 | // action |
| 317 | do_action('acfe/render_field_group', $field_group, $fields, $post_id); |
| 318 | |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * field_wrapper_attributes |
| 324 | * |
| 325 | * acf/field_wrapper_attributes |
| 326 | * |
| 327 | * @param $wrapper |
| 328 | * @param $field |
| 329 | * |
| 330 | * @return mixed|void |
| 331 | */ |
| 332 | function field_wrapper_attributes($wrapper, $field){ |
| 333 | |
| 334 | return apply_filters('acfe/field_wrapper_attributes', $wrapper, $field); |
| 335 | |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /** |
| 340 | * load_fields |
| 341 | * |
| 342 | * acf/load_fields |
| 343 | * |
| 344 | * @param $fields |
| 345 | * @param $parent |
| 346 | * |
| 347 | * @return mixed|void |
| 348 | */ |
| 349 | function load_fields($fields, $parent){ |
| 350 | |
| 351 | // validate field |
| 352 | // this fitler is also applied on field groups |
| 353 | if(!isset($parent['type'])){ |
| 354 | return $fields; |
| 355 | } |
| 356 | |
| 357 | $fields = apply_filters('acfe/load_fields', $fields, $parent); |
| 358 | |
| 359 | return $fields; |
| 360 | |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /** |
| 365 | * load_field |
| 366 | * |
| 367 | * acf/load_field |
| 368 | * |
| 369 | * @param $field |
| 370 | * |
| 371 | * @return mixed |
| 372 | */ |
| 373 | function load_field($field){ |
| 374 | |
| 375 | // bail early |
| 376 | if(acfe_is_admin_screen()){ |
| 377 | return $field; |
| 378 | } |
| 379 | |
| 380 | // hooks |
| 381 | $field = apply_filters('acfe/load_field', $field); |
| 382 | |
| 383 | // todo: find a solution to add filter variations with deprecated notice |
| 384 | // deprecated: admin |
| 385 | if(acfe_is_admin()){ |
| 386 | |
| 387 | $field = apply_filters_deprecated("acfe/load_field_admin", array($field), '0.8.8', "acfe/load_field"); |
| 388 | $field = apply_filters_deprecated("acfe/load_field_admin/type={$field['type']}", array($field), '0.8.8', "acfe/load_field/type={$field['type']}"); |
| 389 | $field = apply_filters_deprecated("acfe/load_field_admin/name={$field['name']}", array($field), '0.8.8', "acfe/load_field/name={$field['name']}"); |
| 390 | $field = apply_filters_deprecated("acfe/load_field_admin/key={$field['key']}", array($field), '0.8.8', "acfe/load_field/key={$field['key']}"); |
| 391 | |
| 392 | // deprecated: front |
| 393 | }else{ |
| 394 | |
| 395 | $field = apply_filters_deprecated("acfe/load_field_front", array($field), '0.8.8', "acfe/load_field"); |
| 396 | $field = apply_filters_deprecated("acfe/load_field_front/type={$field['type']}", array($field), '0.8.8', "acfe/load_field/type={$field['type']}"); |
| 397 | $field = apply_filters_deprecated("acfe/load_field_front/name={$field['name']}", array($field), '0.8.8', "acfe/load_field/name={$field['name']}"); |
| 398 | $field = apply_filters_deprecated("acfe/load_field_front/key={$field['key']}", array($field), '0.8.8', "acfe/load_field/key={$field['key']}"); |
| 399 | |
| 400 | } |
| 401 | |
| 402 | return $field; |
| 403 | |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /** |
| 408 | * decode_object |
| 409 | * |
| 410 | * @param $post_id |
| 411 | * |
| 412 | * @return array|false |
| 413 | */ |
| 414 | function decode_object($post_id){ |
| 415 | |
| 416 | //data |
| 417 | $data = array( |
| 418 | 'id' => false, |
| 419 | 'type' => false, |
| 420 | 'object' => false, |
| 421 | 'hooks' => array(), |
| 422 | ); |
| 423 | |
| 424 | /** |
| 425 | * @string $post_id 12 | term_46 | user_22 | my-option | comment_89 | widget_56 | menu_74 | menu_item_96 | block_my-block | blog_55 | site_36 | attachment_24 |
| 426 | * @string $id 12 | 46 | 22 | my-option | 89 | widget_56 | 74 | 96 | block_my-block | 55 | 36 | 24 |
| 427 | * @string $type post | term | user | option | comment | option | term | post | block | blog | blog | post |
| 428 | */ |
| 429 | |
| 430 | /** |
| 431 | * @var $type |
| 432 | * @var $id |
| 433 | */ |
| 434 | extract(acf_decode_post_id($post_id)); |
| 435 | |
| 436 | // validate id |
| 437 | if(!$id){ |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | // assign default |
| 442 | $data['id'] = $id; |
| 443 | $data['type'] = $type; |
| 444 | |
| 445 | switch($type){ |
| 446 | |
| 447 | // post |
| 448 | case 'post': { |
| 449 | |
| 450 | $post = get_post($id); |
| 451 | if($post && !is_wp_error($post)){ |
| 452 | |
| 453 | $data['object'] = $post; |
| 454 | |
| 455 | if(isset($post->post_type) && post_type_exists($post->post_type)){ |
| 456 | $data['hooks'][] = "post_type={$post->post_type}"; |
| 457 | } |
| 458 | |
| 459 | } |
| 460 | |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | // term |
| 465 | case 'term': { |
| 466 | |
| 467 | $term = get_term($id); |
| 468 | if($term && !is_wp_error($term)){ |
| 469 | |
| 470 | $data['object'] = $term; |
| 471 | |
| 472 | if(isset($term->taxonomy) && taxonomy_exists($term->taxonomy)){ |
| 473 | $data['hooks'][] = "taxonomy={$term->taxonomy}"; |
| 474 | } |
| 475 | |
| 476 | } |
| 477 | |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | // user |
| 482 | case 'user': { |
| 483 | |
| 484 | $user = get_user_by('id', $id); |
| 485 | if($user && !is_wp_error($user)){ |
| 486 | |
| 487 | $data['object'] = $user; |
| 488 | |
| 489 | if(isset($user->roles) && !empty($user->roles)){ |
| 490 | foreach($user->roles as $role){ |
| 491 | $data['hooks'][] = "role={$role}"; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | } |
| 496 | |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | // option |
| 501 | case 'option': { |
| 502 | |
| 503 | $location = acf_get_form_data('location'); |
| 504 | $options_page = acfe_get($location, 'options_page'); |
| 505 | |
| 506 | if($options_page){ |
| 507 | |
| 508 | $data['object'] = acf_get_options_page($options_page); |
| 509 | $data['hooks'][] = "slug={$options_page}"; |
| 510 | |
| 511 | } |
| 512 | |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | // comment |
| 517 | case 'comment': { |
| 518 | |
| 519 | $comment = get_comment($id); |
| 520 | if($comment && !is_wp_error($comment)){ |
| 521 | $data['object'] = $comment; |
| 522 | } |
| 523 | |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | // block |
| 528 | case 'block': { |
| 529 | |
| 530 | $block = acf_get_block_type("acf/$id"); |
| 531 | if($block){ |
| 532 | $data['object'] = $block; |
| 533 | } |
| 534 | |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | // blog |
| 539 | case 'blog': { |
| 540 | |
| 541 | if(function_exists('get_blog_details')){ |
| 542 | |
| 543 | $blog = get_blog_details($id); |
| 544 | if($blog){ |
| 545 | $data['object'] = $blog; |
| 546 | } |
| 547 | |
| 548 | } |
| 549 | |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | } |
| 554 | |
| 555 | // return |
| 556 | return $data; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | } |
| 561 | |
| 562 | acf_new_instance('acfe_hooks'); |
| 563 | |
| 564 | endif; |