hooks.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_hooks')): |
| 7 | |
| 8 | class acfe_hooks{ |
| 9 | |
| 10 | public $field_group; |
| 11 | |
| 12 | function __construct(){ |
| 13 | |
| 14 | // General |
| 15 | add_action('acf/save_post', array($this, 'pre_save_post'), 9); |
| 16 | add_action('acf/save_post', array($this, 'save_post'), 15); |
| 17 | add_action('acf/validate_save_post', array($this, 'validate_save_post')); |
| 18 | |
| 19 | // Field Groups |
| 20 | add_filter('acf/load_field_groups', array($this, 'load_field_groups'), 100); |
| 21 | add_filter('acf/pre_render_fields', array($this, 'pre_render_fields'), 10, 2); |
| 22 | add_action('acf/render_fields', array($this, 'render_fields'), 10, 2); |
| 23 | |
| 24 | // Fields |
| 25 | add_filter('acf/field_wrapper_attributes', array($this, 'field_wrapper_attributes'), 10, 2); |
| 26 | add_filter('acf/load_fields', array($this, 'load_fields'), 10, 2); |
| 27 | add_filter('acf/load_field', array($this, 'load_field')); |
| 28 | |
| 29 | // Options Page |
| 30 | add_action('acf/input/form_data', array($this, 'form_data')); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | function pre_save_post($post_id = 0){ |
| 35 | |
| 36 | $this->do_save_post($post_id, true); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | function save_post($post_id = 0){ |
| 41 | |
| 42 | $this->do_save_post($post_id); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | function do_save_post($post_id = 0, $pre = false){ |
| 47 | |
| 48 | // Validate acf |
| 49 | if(!acf_maybe_get_POST('acf')) |
| 50 | return; |
| 51 | |
| 52 | // Check data |
| 53 | $data = $this->decode_object($post_id); |
| 54 | |
| 55 | if(!$data) |
| 56 | return; |
| 57 | |
| 58 | // Vars |
| 59 | $id = $data['id']; |
| 60 | $type = $data['type']; |
| 61 | $object = $data['object']; |
| 62 | $hooks = $data['hooks']; |
| 63 | $suffix = $pre ? 'pre_' : false; |
| 64 | |
| 65 | // All hooks |
| 66 | $all_hooks = array(); |
| 67 | $all_hooks[] = "acfe/{$suffix}save"; |
| 68 | $all_hooks[] = "acfe/{$suffix}save/id={$post_id}"; |
| 69 | $all_hooks[] = "acfe/{$suffix}save_{$type}"; |
| 70 | foreach($hooks as $hook){ |
| 71 | $all_hooks[] = "acfe/{$suffix}save_{$type}/{$hook}"; |
| 72 | } |
| 73 | $all_hooks[] = "acfe/{$suffix}save_{$type}/id={$post_id}"; |
| 74 | |
| 75 | // Check if hooked |
| 76 | $do_action = false; |
| 77 | |
| 78 | foreach($all_hooks as $all_hook){ |
| 79 | |
| 80 | if(!has_action($all_hook)) continue; |
| 81 | |
| 82 | $do_action = true; |
| 83 | break; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | // Bail early |
| 88 | if(!$do_action) |
| 89 | return; |
| 90 | |
| 91 | // Setup Meta |
| 92 | acfe_setup_meta($_POST['acf'], 'acfe/save', true); |
| 93 | |
| 94 | foreach($all_hooks as $all_hook){ |
| 95 | |
| 96 | do_action($all_hook, $post_id, $object); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | acfe_reset_meta(); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | function validate_save_post(){ |
| 105 | |
| 106 | // vars |
| 107 | $rows = array(); |
| 108 | |
| 109 | // General |
| 110 | $acf = acf_maybe_get_POST('acf'); |
| 111 | |
| 112 | if(!empty($acf)){ |
| 113 | |
| 114 | $post_id = acf_maybe_get_POST('_acf_post_id'); |
| 115 | |
| 116 | if($post_id){ |
| 117 | $rows[$post_id] = $acf; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | // Menu Items |
| 123 | $menu_items = acf_maybe_get_POST('menu-item-acf'); |
| 124 | |
| 125 | if(!empty($menu_items)){ |
| 126 | |
| 127 | foreach($menu_items as $post_id => $fields){ |
| 128 | $rows[$post_id] = $fields; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | foreach($rows as $post_id => $acf){ |
| 134 | |
| 135 | // Check data |
| 136 | $data = $this->decode_object($post_id); |
| 137 | |
| 138 | if(!$data) |
| 139 | continue; |
| 140 | |
| 141 | // Vars |
| 142 | $id = $data['id']; |
| 143 | $type = $data['type']; |
| 144 | $object = $data['object']; |
| 145 | $hooks = $data['hooks']; |
| 146 | |
| 147 | // All hooks |
| 148 | $all_hooks = array(); |
| 149 | $all_hooks[] = "acfe/validate_save"; |
| 150 | $all_hooks[] = "acfe/validate_save/id={$post_id}"; |
| 151 | $all_hooks[] = "acfe/validate_save_{$type}"; |
| 152 | foreach($hooks as $hook){ |
| 153 | $all_hooks[] = "acfe/validate_save_{$type}/{$hook}"; |
| 154 | } |
| 155 | $all_hooks[] = "acfe/validate_save_{$type}/id={$post_id}"; |
| 156 | |
| 157 | // Check if hooked |
| 158 | $do_action = false; |
| 159 | |
| 160 | foreach($all_hooks as $all_hook){ |
| 161 | |
| 162 | if(!has_action($all_hook)) continue; |
| 163 | |
| 164 | $do_action = true; |
| 165 | break; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | // Bail early |
| 170 | if(!$do_action) |
| 171 | continue; |
| 172 | |
| 173 | // Setup Meta |
| 174 | acfe_setup_meta($acf, 'acfe/validate_save', true); |
| 175 | |
| 176 | foreach($all_hooks as $all_hook){ |
| 177 | |
| 178 | do_action($all_hook, $post_id, $object); |
| 179 | |
| 180 | } |
| 181 | |
| 182 | // Reset meta |
| 183 | acfe_reset_meta(); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | function decode_object($post_id){ |
| 190 | |
| 191 | //vars |
| 192 | $id = false; |
| 193 | $type = false; |
| 194 | $data = array( |
| 195 | 'id' => false, |
| 196 | 'type' => false, |
| 197 | 'object' => false, |
| 198 | 'hooks' => array(), |
| 199 | ); |
| 200 | |
| 201 | /* |
| 202 | * @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 |
| 203 | * @string $id 12 | 46 | 22 | my-option | 89 | widget_56 | 74 | 96 | block_my-block | 55 | 36 | 24 |
| 204 | * @string $type post | term | user | option | comment | option | term | post | block | blog | blog | post |
| 205 | */ |
| 206 | extract(acf_decode_post_id($post_id)); |
| 207 | |
| 208 | // Validate ID |
| 209 | if(!$id) |
| 210 | return false; |
| 211 | |
| 212 | $data['id'] = $id; |
| 213 | $data['type'] = $type; |
| 214 | |
| 215 | // Post |
| 216 | if($type === 'post'){ |
| 217 | |
| 218 | $post = get_post($id); |
| 219 | |
| 220 | if($post && !is_wp_error($post)){ |
| 221 | |
| 222 | $data['object'] = $post; |
| 223 | |
| 224 | if(isset($post->post_type) && post_type_exists($post->post_type)){ |
| 225 | $data['hooks'][] = "post_type={$post->post_type}"; |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | // Term |
| 231 | }elseif($type === 'term'){ |
| 232 | |
| 233 | $term = get_term($id); |
| 234 | |
| 235 | if($term && !is_wp_error($term)){ |
| 236 | |
| 237 | $data['object'] = $term; |
| 238 | |
| 239 | if(isset($term->taxonomy) && taxonomy_exists($term->taxonomy)){ |
| 240 | $data['hooks'][] = "taxonomy={$term->taxonomy}"; |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | |
| 245 | // User |
| 246 | }elseif($type === 'user'){ |
| 247 | |
| 248 | $user = get_user_by('id', $id); |
| 249 | |
| 250 | if($user && !is_wp_error($user)){ |
| 251 | |
| 252 | $data['object'] = $user; |
| 253 | |
| 254 | if(isset($user->roles) && !empty($user->roles)){ |
| 255 | |
| 256 | foreach($user->roles as $role){ |
| 257 | $data['hooks'][] = "role={$role}"; |
| 258 | } |
| 259 | |
| 260 | } |
| 261 | |
| 262 | } |
| 263 | |
| 264 | // Option |
| 265 | }elseif($type === 'option'){ |
| 266 | |
| 267 | $options_page = acf_maybe_get_POST('_acf_options_page'); |
| 268 | |
| 269 | if($options_page){ |
| 270 | |
| 271 | $data['object'] = acf_get_options_page($options_page); |
| 272 | |
| 273 | $data['hooks'][] = "slug={$options_page}"; |
| 274 | |
| 275 | } |
| 276 | |
| 277 | // Comment |
| 278 | }elseif($type === 'comment'){ |
| 279 | |
| 280 | $comment = get_comment($id); |
| 281 | |
| 282 | if($comment && !is_wp_error($comment)){ |
| 283 | |
| 284 | $data['object'] = $comment; |
| 285 | |
| 286 | } |
| 287 | |
| 288 | // Block |
| 289 | }elseif($type === 'block'){ |
| 290 | |
| 291 | $block = acf_get_block_type("acf/$id"); |
| 292 | |
| 293 | if($block){ |
| 294 | |
| 295 | $data['object'] = $block; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | // Blog |
| 300 | }elseif($type === 'blog'){ |
| 301 | |
| 302 | if(function_exists('get_blog_details')){ |
| 303 | |
| 304 | $blog = get_blog_details($id); |
| 305 | |
| 306 | if($blog){ |
| 307 | |
| 308 | $data['object'] = $blog; |
| 309 | |
| 310 | } |
| 311 | |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | return $data; |
| 317 | |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Load Field Groups |
| 322 | */ |
| 323 | function load_field_groups($field_groups){ |
| 324 | |
| 325 | // Do not execute in ACF Field Group UI |
| 326 | if(acfe_is_admin_screen()) |
| 327 | return $field_groups; |
| 328 | |
| 329 | foreach($field_groups as $i => &$field_group){ |
| 330 | |
| 331 | $field_group = apply_filters("acfe/prepare_field_group", $field_group); |
| 332 | |
| 333 | if(isset($field_group['ID'])) |
| 334 | $field_group = apply_filters("acfe/prepare_field_group/ID={$field_group['ID']}", $field_group); |
| 335 | |
| 336 | if(isset($field_group['key'])) |
| 337 | $field_group = apply_filters("acfe/prepare_field_group/key={$field_group['key']}", $field_group); |
| 338 | |
| 339 | // Do not render if false |
| 340 | if($field_group === false) |
| 341 | unset($field_groups[$i]); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | return $field_groups; |
| 346 | |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Pre Render Fields |
| 351 | */ |
| 352 | function pre_render_fields($fields, $post_id){ |
| 353 | |
| 354 | $this->field_group = array(); |
| 355 | |
| 356 | if(!isset($fields[0])) |
| 357 | return $fields; |
| 358 | |
| 359 | if(!acf_maybe_get($fields[0], 'parent')) |
| 360 | return $fields; |
| 361 | |
| 362 | $field_group = acf_get_field_group($fields[0]['parent']); |
| 363 | |
| 364 | if(!$field_group) |
| 365 | return $fields; |
| 366 | |
| 367 | $this->field_group = $field_group; |
| 368 | |
| 369 | do_action("acfe/pre_render_field_group", $field_group, $fields, $post_id); |
| 370 | do_action("acfe/pre_render_field_group/ID={$field_group['ID']}", $field_group, $fields, $post_id); |
| 371 | do_action("acfe/pre_render_field_group/key={$field_group['key']}", $field_group, $fields, $post_id); |
| 372 | |
| 373 | return $fields; |
| 374 | |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Pre Render Fields |
| 379 | */ |
| 380 | function render_fields($fields, $post_id){ |
| 381 | |
| 382 | if(empty($this->field_group)) |
| 383 | return; |
| 384 | |
| 385 | $field_group = $this->field_group; |
| 386 | |
| 387 | do_action("acfe/render_field_group", $field_group, $fields, $post_id); |
| 388 | do_action("acfe/render_field_group/ID={$field_group['ID']}", $field_group, $fields, $post_id); |
| 389 | do_action("acfe/render_field_group/key={$field_group['key']}", $field_group, $fields, $post_id); |
| 390 | |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Field Wrapper Attributes |
| 395 | */ |
| 396 | function field_wrapper_attributes($wrapper, $field){ |
| 397 | |
| 398 | $wrapper = apply_filters("acfe/field_wrapper_attributes", $wrapper, $field); |
| 399 | $wrapper = apply_filters("acfe/field_wrapper_attributes/type={$field['type']}", $wrapper, $field); |
| 400 | $wrapper = apply_filters("acfe/field_wrapper_attributes/name={$field['name']}", $wrapper, $field); |
| 401 | $wrapper = apply_filters("acfe/field_wrapper_attributes/key={$field['key']}", $wrapper, $field); |
| 402 | |
| 403 | return $wrapper; |
| 404 | |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Load Fields |
| 409 | */ |
| 410 | function load_fields($fields, $parent){ |
| 411 | |
| 412 | // check if field (fitler is also called on field groups) |
| 413 | if(!acf_maybe_get($parent, 'type')) |
| 414 | return $fields; |
| 415 | |
| 416 | $fields = apply_filters("acfe/load_fields", $fields, $parent); |
| 417 | $fields = apply_filters("acfe/load_fields/type={$parent['type']}", $fields, $parent); |
| 418 | $fields = apply_filters("acfe/load_fields/name={$parent['name']}", $fields, $parent); |
| 419 | $fields = apply_filters("acfe/load_fields/key={$parent['key']}", $fields, $parent); |
| 420 | |
| 421 | return $fields; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Load Field |
| 427 | */ |
| 428 | function load_field($field){ |
| 429 | |
| 430 | // Do not execute in ACF Field Group UI |
| 431 | if(acfe_is_admin_screen()) |
| 432 | return $field; |
| 433 | |
| 434 | // Hooks |
| 435 | $field = apply_filters("acfe/load_field", $field); |
| 436 | $field = apply_filters("acfe/load_field/type={$field['type']}", $field); |
| 437 | $field = apply_filters("acfe/load_field/name={$field['name']}", $field); |
| 438 | $field = apply_filters("acfe/load_field/key={$field['key']}", $field); |
| 439 | |
| 440 | |
| 441 | // Deprecated: Admin |
| 442 | if(acfe_is_admin()){ |
| 443 | |
| 444 | $field = apply_filters_deprecated("acfe/load_field_admin", array($field), '0.8.8', "acfe/load_field"); |
| 445 | $field = apply_filters_deprecated("acfe/load_field_admin/type={$field['type']}", array($field), '0.8.8', "acfe/load_field/type={$field['type']}"); |
| 446 | $field = apply_filters_deprecated("acfe/load_field_admin/name={$field['name']}", array($field), '0.8.8', "acfe/load_field/name={$field['name']}"); |
| 447 | $field = apply_filters_deprecated("acfe/load_field_admin/key={$field['key']}", array($field), '0.8.8', "acfe/load_field/key={$field['key']}"); |
| 448 | |
| 449 | } |
| 450 | |
| 451 | // Deprecated: Front |
| 452 | else{ |
| 453 | |
| 454 | $field = apply_filters_deprecated("acfe/load_field_front", array($field), '0.8.8', "acfe/load_field"); |
| 455 | $field = apply_filters_deprecated("acfe/load_field_front/type={$field['type']}", array($field), '0.8.8', "acfe/load_field/type={$field['type']}"); |
| 456 | $field = apply_filters_deprecated("acfe/load_field_front/name={$field['name']}", array($field), '0.8.8', "acfe/load_field/name={$field['name']}"); |
| 457 | $field = apply_filters_deprecated("acfe/load_field_front/key={$field['key']}", array($field), '0.8.8', "acfe/load_field/key={$field['key']}"); |
| 458 | |
| 459 | } |
| 460 | |
| 461 | return $field; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Form Data for Options Page |
| 467 | */ |
| 468 | function form_data($data){ |
| 469 | |
| 470 | if(acf_maybe_get($data, 'screen') !== 'options') |
| 471 | return; |
| 472 | |
| 473 | global $plugin_page; |
| 474 | |
| 475 | if(!$plugin_page) |
| 476 | return; |
| 477 | |
| 478 | acf_hidden_input(array( |
| 479 | 'id' => '_acf_options_page', |
| 480 | 'name' => '_acf_options_page', |
| 481 | 'value' => $plugin_page |
| 482 | )); |
| 483 | |
| 484 | } |
| 485 | |
| 486 | } |
| 487 | |
| 488 | new acfe_hooks(); |
| 489 | |
| 490 | endif; |