post.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_form_post')): |
| 7 | |
| 8 | class acfe_form_post{ |
| 9 | |
| 10 | function __construct(){ |
| 11 | |
| 12 | /* |
| 13 | * Form |
| 14 | */ |
| 15 | add_filter('acfe/form/load/post', array($this, 'load'), 10, 3); |
| 16 | add_action('acfe/form/make/post', array($this, 'make'), 10, 3); |
| 17 | add_action('acfe/form/submit/post', array($this, 'submit'), 10, 5); |
| 18 | |
| 19 | /* |
| 20 | * Admin |
| 21 | */ |
| 22 | add_filter('acf/prepare_field/name=acfe_form_post_save_meta', array(acfe()->acfe_form, 'map_fields')); |
| 23 | add_filter('acf/prepare_field/name=acfe_form_post_load_meta', array(acfe()->acfe_form, 'map_fields')); |
| 24 | |
| 25 | add_filter('acf/prepare_field/name=acfe_form_post_save_target', array(acfe()->acfe_form, 'map_fields_deep')); |
| 26 | add_filter('acf/prepare_field/name=acfe_form_post_load_source', array(acfe()->acfe_form, 'map_fields_deep')); |
| 27 | |
| 28 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_type', array(acfe()->acfe_form, 'map_fields_deep')); |
| 29 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_status', array(acfe()->acfe_form, 'map_fields_deep')); |
| 30 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_title', array(acfe()->acfe_form, 'map_fields_deep')); |
| 31 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_name', array(acfe()->acfe_form, 'map_fields_deep')); |
| 32 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_content', array(acfe()->acfe_form, 'map_fields_deep')); |
| 33 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_author', array(acfe()->acfe_form, 'map_fields_deep')); |
| 34 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_parent', array(acfe()->acfe_form, 'map_fields_deep')); |
| 35 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_terms', array(acfe()->acfe_form, 'map_fields_deep')); |
| 36 | |
| 37 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_type', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 38 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_status', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 39 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_title', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 40 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 41 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_content', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 42 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_author', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 43 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_parent', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 44 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_terms', array(acfe()->acfe_form, 'map_fields_deep_no_custom')); |
| 45 | |
| 46 | add_filter('acf/prepare_field/name=acfe_form_post_save_target', array($this, 'prepare_choices'), 5); |
| 47 | add_filter('acf/prepare_field/name=acfe_form_post_load_source', array($this, 'prepare_choices'), 5); |
| 48 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_parent', array($this, 'prepare_choices'), 5); |
| 49 | add_filter('acf/prepare_field/name=acfe_form_post_save_post_author', array($this, 'prepare_choices_users'), 5); |
| 50 | |
| 51 | |
| 52 | add_action('acf/render_field/name=acfe_form_post_advanced_load', array($this, 'advanced_load')); |
| 53 | add_action('acf/render_field/name=acfe_form_post_advanced_save_args', array($this, 'advanced_save_args')); |
| 54 | add_action('acf/render_field/name=acfe_form_post_advanced_save', array($this, 'advanced_save')); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | function load($form, $current_post_id, $action){ |
| 59 | |
| 60 | // Form |
| 61 | $form_name = acf_maybe_get($form, 'name'); |
| 62 | $form_id = acf_maybe_get($form, 'ID'); |
| 63 | |
| 64 | // Action |
| 65 | $post_action = get_sub_field('acfe_form_post_action'); |
| 66 | |
| 67 | // Load values |
| 68 | $load_values = get_sub_field('acfe_form_post_load_values'); |
| 69 | $load_meta = get_sub_field('acfe_form_post_load_meta'); |
| 70 | |
| 71 | // Load values |
| 72 | if(!$load_values) |
| 73 | return $form; |
| 74 | |
| 75 | $_post_id = get_sub_field('acfe_form_post_load_source'); |
| 76 | $_post_type = get_sub_field('acfe_form_post_map_post_type'); |
| 77 | $_post_status = get_sub_field('acfe_form_post_map_post_status'); |
| 78 | $_post_title = get_sub_field('acfe_form_post_map_post_title'); |
| 79 | $_post_name = get_sub_field('acfe_form_post_map_post_name'); |
| 80 | $_post_content = get_sub_field('acfe_form_post_map_post_content'); |
| 81 | $_post_author = get_sub_field('acfe_form_post_map_post_author'); |
| 82 | $_post_parent = get_sub_field('acfe_form_post_map_post_parent'); |
| 83 | $_post_terms = get_sub_field('acfe_form_post_map_post_terms'); |
| 84 | |
| 85 | // Map {field:name} {get_field:name} {query_var:name} |
| 86 | $_post_id = acfe_form_map_field_value_load($_post_id, $current_post_id, $form); |
| 87 | $_post_type = acfe_form_map_field_value_load($_post_type, $current_post_id, $form); |
| 88 | $_post_status = acfe_form_map_field_value_load($_post_status, $current_post_id, $form); |
| 89 | $_post_title = acfe_form_map_field_value_load($_post_title, $current_post_id, $form); |
| 90 | $_post_name = acfe_form_map_field_value_load($_post_name, $current_post_id, $form); |
| 91 | $_post_content = acfe_form_map_field_value_load($_post_content, $current_post_id, $form); |
| 92 | $_post_author = acfe_form_map_field_value_load($_post_author, $current_post_id, $form); |
| 93 | $_post_parent = acfe_form_map_field_value_load($_post_parent, $current_post_id, $form); |
| 94 | $_post_terms = acfe_form_map_field_value_load($_post_terms, $current_post_id, $form); |
| 95 | |
| 96 | // Filters |
| 97 | $_post_id = apply_filters('acfe/form/load/post_id', $_post_id, $form, $action); |
| 98 | $_post_id = apply_filters('acfe/form/load/post_id/form=' . $form_name, $_post_id, $form, $action); |
| 99 | |
| 100 | if(!empty($action)) |
| 101 | $_post_id = apply_filters('acfe/form/load/post_id/action=' . $action, $_post_id, $form, $action); |
| 102 | |
| 103 | // Invalid Post ID |
| 104 | if(!$_post_id) |
| 105 | return $form; |
| 106 | |
| 107 | // Post type |
| 108 | if(acf_is_field_key($_post_type)){ |
| 109 | |
| 110 | $key = array_search($_post_type, $load_meta); |
| 111 | |
| 112 | if($key !== false) |
| 113 | unset($load_meta[$key]); |
| 114 | |
| 115 | $form['map'][$_post_type]['value'] = get_post_field('post_type', $_post_id); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | // Post status |
| 120 | if(acf_is_field_key($_post_status)){ |
| 121 | |
| 122 | $key = array_search($_post_status, $load_meta); |
| 123 | |
| 124 | if($key !== false) |
| 125 | unset($load_meta[$key]); |
| 126 | |
| 127 | $form['map'][$_post_status]['value'] = get_post_field('post_status', $_post_id); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | // Post title |
| 132 | if(acf_is_field_key($_post_title)){ |
| 133 | |
| 134 | $key = array_search($_post_title, $load_meta); |
| 135 | |
| 136 | if($key !== false) |
| 137 | unset($load_meta[$key]); |
| 138 | |
| 139 | $form['map'][$_post_title]['value'] = get_post_field('post_title', $_post_id); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | // Post name |
| 144 | if(acf_is_field_key($_post_name)){ |
| 145 | |
| 146 | $key = array_search($_post_name, $load_meta); |
| 147 | |
| 148 | if($key !== false) |
| 149 | unset($load_meta[$key]); |
| 150 | |
| 151 | $form['map'][$_post_name]['value'] = get_post_field('post_name', $_post_id); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | // Post content |
| 156 | if(acf_is_field_key($_post_content)){ |
| 157 | |
| 158 | $key = array_search($_post_content, $load_meta); |
| 159 | |
| 160 | if($key !== false) |
| 161 | unset($load_meta[$key]); |
| 162 | |
| 163 | $form['map'][$_post_content]['value'] = get_post_field('post_content', $_post_id); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | // Post author |
| 168 | if(acf_is_field_key($_post_author)){ |
| 169 | |
| 170 | $key = array_search($_post_author, $load_meta); |
| 171 | |
| 172 | if($key !== false) |
| 173 | unset($load_meta[$key]); |
| 174 | |
| 175 | $form['map'][$_post_author]['value'] = get_post_field('post_author', $_post_id); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | // Post parent |
| 180 | if(acf_is_field_key($_post_parent)){ |
| 181 | |
| 182 | $key = array_search($_post_parent, $load_meta); |
| 183 | |
| 184 | if($key !== false) |
| 185 | unset($load_meta[$key]); |
| 186 | |
| 187 | $form['map'][$_post_parent]['value'] = get_post_field('post_parent', $_post_id); |
| 188 | |
| 189 | } |
| 190 | |
| 191 | // Post terms |
| 192 | if(acf_is_field_key($_post_terms)){ |
| 193 | |
| 194 | $key = array_search($_post_terms, $load_meta); |
| 195 | |
| 196 | if($key !== false) |
| 197 | unset($load_meta[$key]); |
| 198 | |
| 199 | $taxonomies = acf_get_taxonomies(array( |
| 200 | 'post_type' => get_post_type($_post_id) |
| 201 | )); |
| 202 | |
| 203 | if(!empty($taxonomies)){ |
| 204 | |
| 205 | $terms = array(); |
| 206 | |
| 207 | foreach($taxonomies as $taxonomy){ |
| 208 | |
| 209 | $get_the_terms = get_the_terms($_post_id, $taxonomy); |
| 210 | if(!$get_the_terms || is_wp_error($get_the_terms)) |
| 211 | continue; |
| 212 | |
| 213 | $terms = array_merge($terms, $get_the_terms); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | $return = wp_list_pluck($terms, 'term_id'); |
| 218 | |
| 219 | $form['map'][$_post_terms]['value'] = $return; |
| 220 | |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | |
| 225 | // Load others values |
| 226 | if(!empty($load_meta)){ |
| 227 | |
| 228 | foreach($load_meta as $field_key){ |
| 229 | |
| 230 | $field = acf_get_field($field_key); |
| 231 | |
| 232 | $form['map'][$field_key]['value'] = acf_get_value($_post_id, $field); |
| 233 | |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | |
| 238 | return $form; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | function make($form, $current_post_id, $action){ |
| 243 | |
| 244 | // Form |
| 245 | $form_name = acf_maybe_get($form, 'name'); |
| 246 | $form_id = acf_maybe_get($form, 'ID'); |
| 247 | |
| 248 | // Prepare |
| 249 | $prepare = true; |
| 250 | $prepare = apply_filters('acfe/form/prepare/post', $prepare, $form, $current_post_id, $action); |
| 251 | $prepare = apply_filters('acfe/form/prepare/post/form=' . $form_name, $prepare, $form, $current_post_id, $action); |
| 252 | |
| 253 | if(!empty($action)) |
| 254 | $prepare = apply_filters('acfe/form/prepare/post/action=' . $action, $prepare, $form, $current_post_id, $action); |
| 255 | |
| 256 | if($prepare === false) |
| 257 | return; |
| 258 | |
| 259 | // Action |
| 260 | $post_action = get_sub_field('acfe_form_post_action'); |
| 261 | |
| 262 | // Load values |
| 263 | $load_values = get_sub_field('acfe_form_post_load_values'); |
| 264 | |
| 265 | // Pre-process |
| 266 | $_post_content_group = get_sub_field('acfe_form_post_save_post_content_group'); |
| 267 | $_post_content = $_post_content_group['acfe_form_post_save_post_content']; |
| 268 | $_post_content_custom = $_post_content_group['acfe_form_post_save_post_content_custom']; |
| 269 | |
| 270 | if($_post_content === 'custom') |
| 271 | $_post_content = $_post_content_custom; |
| 272 | |
| 273 | $map = array(); |
| 274 | |
| 275 | if($load_values){ |
| 276 | |
| 277 | // Mapping |
| 278 | $map = array( |
| 279 | 'post_type' => get_sub_field('acfe_form_post_map_post_type'), |
| 280 | 'post_status' => get_sub_field('acfe_form_post_map_post_status'), |
| 281 | 'post_title' => get_sub_field('acfe_form_post_map_post_title'), |
| 282 | 'post_name' => get_sub_field('acfe_form_post_map_post_name'), |
| 283 | 'post_content' => get_sub_field('acfe_form_post_map_post_content'), |
| 284 | 'post_author' => get_sub_field('acfe_form_post_map_post_author'), |
| 285 | 'post_parent' => get_sub_field('acfe_form_post_map_post_parent'), |
| 286 | 'post_terms' => get_sub_field('acfe_form_post_map_post_terms'), |
| 287 | ); |
| 288 | |
| 289 | } |
| 290 | |
| 291 | // Fields |
| 292 | $fields = array( |
| 293 | 'target' => get_sub_field('acfe_form_post_save_target'), |
| 294 | 'post_type' => get_sub_field('acfe_form_post_save_post_type'), |
| 295 | 'post_status' => get_sub_field('acfe_form_post_save_post_status'), |
| 296 | 'post_title' => get_sub_field('acfe_form_post_save_post_title'), |
| 297 | 'post_name' => get_sub_field('acfe_form_post_save_post_name'), |
| 298 | 'post_content' => $_post_content, |
| 299 | 'post_author' => get_sub_field('acfe_form_post_save_post_author'), |
| 300 | 'post_parent' => get_sub_field('acfe_form_post_save_post_parent'), |
| 301 | 'post_terms' => get_sub_field('acfe_form_post_save_post_terms'), |
| 302 | ); |
| 303 | |
| 304 | $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form); |
| 305 | |
| 306 | $_post_id = 0; |
| 307 | |
| 308 | // Insert Post |
| 309 | if($post_action === 'insert_post'){ |
| 310 | |
| 311 | $_post_id = wp_insert_post(array( |
| 312 | 'post_title' => 'Post' |
| 313 | )); |
| 314 | |
| 315 | } |
| 316 | |
| 317 | // Update Post |
| 318 | elseif($post_action === 'update_post'){ |
| 319 | |
| 320 | $_post_id = $data['target']; |
| 321 | |
| 322 | } |
| 323 | |
| 324 | // Invalid Post ID |
| 325 | if(!$_post_id) |
| 326 | return; |
| 327 | |
| 328 | $args = array(); |
| 329 | |
| 330 | // ID |
| 331 | $args['ID'] = $_post_id; |
| 332 | |
| 333 | // Post type |
| 334 | if(!empty($data['post_type'])){ |
| 335 | |
| 336 | if(is_array($data['post_type'])) |
| 337 | $data['post_type'] = acfe_array_to_string($data['post_type']); |
| 338 | |
| 339 | $args['post_type'] = $data['post_type']; |
| 340 | |
| 341 | } |
| 342 | |
| 343 | // Post status |
| 344 | if(!empty($data['post_status'])){ |
| 345 | |
| 346 | if(is_array($data['post_status'])) |
| 347 | $data['post_status'] = acfe_array_to_string($data['post_status']); |
| 348 | |
| 349 | $args['post_status'] = $data['post_status']; |
| 350 | |
| 351 | } |
| 352 | |
| 353 | // Post title |
| 354 | if(!empty($data['post_title'])){ |
| 355 | |
| 356 | if(is_array($data['post_title'])) |
| 357 | $data['post_title'] = acfe_array_to_string($data['post_title']); |
| 358 | |
| 359 | $args['post_title'] = $data['post_title']; |
| 360 | |
| 361 | if($data['post_title'] === 'generated_id') |
| 362 | $args['post_title'] = $_post_id; |
| 363 | |
| 364 | } |
| 365 | |
| 366 | // Post name |
| 367 | if(!empty($data['post_name'])){ |
| 368 | |
| 369 | if(is_array($data['post_name'])) |
| 370 | $data['post_name'] = acfe_array_to_string($data['post_name']); |
| 371 | |
| 372 | $args['post_name'] = $data['post_name']; |
| 373 | |
| 374 | if($data['post_name'] === 'generated_id') |
| 375 | $args['post_name'] = $_post_id; |
| 376 | |
| 377 | } |
| 378 | |
| 379 | // Post content |
| 380 | if(!empty($data['post_content'])){ |
| 381 | |
| 382 | if(is_array($data['post_content'])) |
| 383 | $data['post_content'] = acfe_array_to_string($data['post_content']); |
| 384 | |
| 385 | $args['post_content'] = $data['post_content']; |
| 386 | |
| 387 | } |
| 388 | |
| 389 | // Post author |
| 390 | if(!empty($data['post_author'])){ |
| 391 | |
| 392 | if(is_array($data['post_author'])) |
| 393 | $data['post_author'] = acfe_array_to_string($data['post_author']); |
| 394 | |
| 395 | $args['post_author'] = $data['post_author']; |
| 396 | |
| 397 | } |
| 398 | |
| 399 | // Post parent |
| 400 | if(!empty($data['post_parent'])){ |
| 401 | |
| 402 | if(is_array($data['post_author'])) |
| 403 | $data['post_author'] = acfe_array_to_string($data['post_author']); |
| 404 | |
| 405 | $args['post_parent'] = $data['post_parent']; |
| 406 | |
| 407 | } |
| 408 | |
| 409 | // Post terms |
| 410 | if(!empty($data['post_terms'])){ |
| 411 | |
| 412 | $terms = acf_array($data['post_terms']); |
| 413 | |
| 414 | // Tax input |
| 415 | if(!empty($terms)){ |
| 416 | |
| 417 | foreach($terms as $term){ |
| 418 | |
| 419 | if(is_string($term) || is_numeric($term)){ |
| 420 | |
| 421 | $args['acfe_form_terms'][] = $term; |
| 422 | |
| 423 | }elseif(is_array($term)){ |
| 424 | |
| 425 | foreach($term as $sub_term){ |
| 426 | |
| 427 | // String || Numeric |
| 428 | if(is_string($sub_term) || is_numeric($sub_term)){ |
| 429 | |
| 430 | $args['acfe_form_terms'][] = $sub_term; |
| 431 | |
| 432 | // Array |
| 433 | }elseif(is_array($sub_term)){ |
| 434 | |
| 435 | if(!acf_maybe_get($sub_term, 'term_id')) |
| 436 | continue; |
| 437 | |
| 438 | $args['acfe_form_terms'][] = $sub_term['term_id']; |
| 439 | |
| 440 | // Object |
| 441 | }elseif(is_object($sub_term) && is_a($sub_term, 'WP_Term')){ |
| 442 | |
| 443 | if(!isset($sub_term->term_id) || empty($sub_term->term_id)) |
| 444 | continue; |
| 445 | |
| 446 | $args['acfe_form_terms'][] = $sub_term->term_id; |
| 447 | |
| 448 | } |
| 449 | |
| 450 | |
| 451 | } |
| 452 | |
| 453 | }elseif(is_object($term) && is_a($term, 'WP_Term')){ |
| 454 | |
| 455 | if(!isset($term->term_id) || empty($term->term_id)) |
| 456 | continue; |
| 457 | |
| 458 | $args['acfe_form_terms'][] = $term->term_id; |
| 459 | |
| 460 | } |
| 461 | |
| 462 | |
| 463 | |
| 464 | } |
| 465 | |
| 466 | } |
| 467 | |
| 468 | } |
| 469 | |
| 470 | // Args |
| 471 | $args = apply_filters('acfe/form/submit/post_args', $args, $post_action, $form, $action); |
| 472 | $args = apply_filters('acfe/form/submit/post_args/form=' . $form_name, $args, $post_action, $form, $action); |
| 473 | |
| 474 | if(!empty($action)) |
| 475 | $args = apply_filters('acfe/form/submit/post_args/action=' . $action, $args, $post_action, $form, $action); |
| 476 | |
| 477 | // Bail early if false |
| 478 | if($args === false) |
| 479 | return; |
| 480 | |
| 481 | // Post terms pre-process (let post update first, for post type) |
| 482 | $terms = array(); |
| 483 | |
| 484 | if(acf_maybe_get($args, 'acfe_form_terms')){ |
| 485 | |
| 486 | $terms = acf_extract_var($args, 'acfe_form_terms'); |
| 487 | |
| 488 | } |
| 489 | |
| 490 | // Update Post |
| 491 | $_post_id = wp_update_post($args); |
| 492 | |
| 493 | // Post terms process |
| 494 | if(!empty($terms)){ |
| 495 | |
| 496 | $term_objects = array(); |
| 497 | $term_create = array(); |
| 498 | |
| 499 | foreach($terms as $term){ |
| 500 | |
| 501 | if(is_numeric($term)){ |
| 502 | |
| 503 | $get_term = get_term($term); |
| 504 | |
| 505 | if(empty($get_term) || is_wp_error($get_term)) |
| 506 | continue; |
| 507 | |
| 508 | $term_objects[$get_term->taxonomy][] = $get_term->term_id; |
| 509 | |
| 510 | }elseif(is_string($term)){ |
| 511 | |
| 512 | $explode = explode('|', $term); |
| 513 | |
| 514 | // No taxonomy found in input |
| 515 | if(isset($explode[1])){ |
| 516 | |
| 517 | $term_create[$explode[1]][] = $explode[0]; |
| 518 | |
| 519 | }else{ |
| 520 | |
| 521 | // Get post type |
| 522 | $post_type = 'post'; |
| 523 | |
| 524 | if(isset($args['post_type'])) |
| 525 | $post_type = $args['post_type']; |
| 526 | |
| 527 | $taxonomies = get_object_taxonomies($post_type); |
| 528 | |
| 529 | if(!empty($taxonomies)){ |
| 530 | |
| 531 | $taxonomy = $taxonomies[0]; |
| 532 | |
| 533 | $term_create[$taxonomy] = $explode[0]; |
| 534 | |
| 535 | } |
| 536 | |
| 537 | } |
| 538 | |
| 539 | } |
| 540 | |
| 541 | } |
| 542 | |
| 543 | // Term Objects |
| 544 | if(!empty($term_objects)){ |
| 545 | |
| 546 | foreach($term_objects as $term_taxonomy => $term_ids){ |
| 547 | |
| 548 | wp_set_object_terms($args['ID'], $term_ids, $term_taxonomy, true); |
| 549 | |
| 550 | } |
| 551 | |
| 552 | } |
| 553 | |
| 554 | // Create Terms (with slugs) |
| 555 | if(!empty($term_create)){ |
| 556 | |
| 557 | foreach($term_create as $term_taxonomy => $term_slugs){ |
| 558 | |
| 559 | wp_set_object_terms($args['ID'], $term_slugs, $term_taxonomy, true); |
| 560 | |
| 561 | } |
| 562 | |
| 563 | } |
| 564 | |
| 565 | } |
| 566 | |
| 567 | // Submit |
| 568 | do_action('acfe/form/submit/post', $_post_id, $post_action, $args, $form, $action); |
| 569 | do_action('acfe/form/submit/post/form=' . $form_name, $_post_id, $post_action, $args, $form, $action); |
| 570 | |
| 571 | if(!empty($action)) |
| 572 | do_action('acfe/form/submit/post/action=' . $action, $_post_id, $post_action, $args, $form, $action); |
| 573 | |
| 574 | } |
| 575 | |
| 576 | function submit($_post_id, $post_action, $args, $form, $action){ |
| 577 | |
| 578 | // Form name |
| 579 | $form_name = acf_maybe_get($form, 'name'); |
| 580 | |
| 581 | // Get post array |
| 582 | $post_object = get_post($_post_id, 'ARRAY_A'); |
| 583 | |
| 584 | $post_object['permalink'] = get_permalink($_post_id); |
| 585 | $post_object['admin_url'] = admin_url('post.php?post=' . $_post_id . '&action=edit'); |
| 586 | |
| 587 | // Retrieve Post Author data |
| 588 | $post_author = $post_object['post_author']; |
| 589 | $user_object = get_user_by('ID', $post_author); |
| 590 | |
| 591 | if(isset($user_object->data)){ |
| 592 | |
| 593 | $user = json_decode(json_encode($user_object->data), true); |
| 594 | |
| 595 | $user_object_meta = get_user_meta($user['ID']); |
| 596 | |
| 597 | $user_meta = array(); |
| 598 | |
| 599 | foreach($user_object_meta as $k => $v){ |
| 600 | |
| 601 | if(!isset($v[0])) |
| 602 | continue; |
| 603 | |
| 604 | $user_meta[$k] = $v[0]; |
| 605 | |
| 606 | } |
| 607 | |
| 608 | $user_array = array_merge($user, $user_meta); |
| 609 | |
| 610 | $user_array['permalink'] = get_author_posts_url($post_author); |
| 611 | $user_array['admin_url'] = admin_url('user-edit.php?user_id=' . $post_author); |
| 612 | |
| 613 | $post_object['post_author_data'] = $user_array; |
| 614 | |
| 615 | } |
| 616 | |
| 617 | $post_object = apply_filters('acfe/form/query_var/post', $post_object, $_post_id, $post_action, $args, $form, $action); |
| 618 | $post_object = apply_filters('acfe/form/query_var/post/form=' . $form_name, $post_object, $_post_id, $post_action, $args, $form, $action); |
| 619 | $post_object = apply_filters('acfe/form/query_var/post/action=' . $action, $post_object, $_post_id, $post_action, $args, $form, $action); |
| 620 | |
| 621 | // Query var |
| 622 | $query_var = acfe_form_unique_action_id($form, 'post'); |
| 623 | |
| 624 | if(!empty($action)) |
| 625 | $query_var = $action; |
| 626 | |
| 627 | // Set Query Var |
| 628 | set_query_var($query_var, $post_object); |
| 629 | |
| 630 | // Meta save |
| 631 | $save_meta = get_sub_field('acfe_form_post_save_meta'); |
| 632 | |
| 633 | if(!empty($save_meta)){ |
| 634 | |
| 635 | $meta = acfe_form_filter_meta($save_meta, $_POST['acf']); |
| 636 | |
| 637 | if(!empty($meta)){ |
| 638 | |
| 639 | // Backup original acf post data |
| 640 | $acf = $_POST['acf']; |
| 641 | |
| 642 | // Save meta fields |
| 643 | acf_save_post($_post_id, $meta); |
| 644 | |
| 645 | // Restore original acf post data |
| 646 | $_POST['acf'] = $acf; |
| 647 | |
| 648 | } |
| 649 | |
| 650 | } |
| 651 | |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Post: Select2 Choices |
| 656 | */ |
| 657 | function prepare_choices($field){ |
| 658 | |
| 659 | $field['choices']['current_post'] = 'Current: Post'; |
| 660 | $field['choices']['current_post_parent'] = 'Current: Post Parent'; |
| 661 | |
| 662 | if(acf_maybe_get($field, 'value')){ |
| 663 | |
| 664 | $field_type = acf_get_field_type('post_object'); |
| 665 | $field['post_type'] = acf_get_post_types(); |
| 666 | |
| 667 | // load posts |
| 668 | $posts = $field_type->get_posts($field['value'], $field); |
| 669 | |
| 670 | if($posts){ |
| 671 | |
| 672 | foreach(array_keys($posts) as $i){ |
| 673 | |
| 674 | // vars |
| 675 | $post = acf_extract_var($posts, $i); |
| 676 | |
| 677 | // append to choices |
| 678 | $field['choices'][$post->ID] = $field_type->get_post_title($post, $field); |
| 679 | |
| 680 | } |
| 681 | |
| 682 | } |
| 683 | |
| 684 | } |
| 685 | |
| 686 | return $field; |
| 687 | |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * User: Select2 Choices |
| 692 | */ |
| 693 | function prepare_choices_users($field){ |
| 694 | |
| 695 | $field['choices']['current_user'] = 'Current User'; |
| 696 | $field['choices']['current_post_author'] = 'Current Post Author'; |
| 697 | |
| 698 | if(acf_maybe_get($field, 'value')){ |
| 699 | |
| 700 | $field_type = acf_get_field_type('user'); |
| 701 | |
| 702 | // Clean value into an array of IDs. |
| 703 | $user_ids = array_map('intval', acf_array($field['value'])); |
| 704 | |
| 705 | // Find users in database (ensures all results are real). |
| 706 | $users = acf_get_users(array( |
| 707 | 'include' => $user_ids |
| 708 | )); |
| 709 | |
| 710 | // Append. |
| 711 | if($users){ |
| 712 | |
| 713 | foreach($users as $user){ |
| 714 | $field['choices'][$user->ID] = $field_type->get_result($user, $field); |
| 715 | } |
| 716 | |
| 717 | } |
| 718 | |
| 719 | } |
| 720 | |
| 721 | return $field; |
| 722 | |
| 723 | } |
| 724 | |
| 725 | function advanced_load($field){ |
| 726 | |
| 727 | $form_name = 'my_form'; |
| 728 | |
| 729 | if(acf_maybe_get($field, 'value')) |
| 730 | $form_name = get_field('acfe_form_name', $field['value']); |
| 731 | |
| 732 | ?>You may use the following hooks:<br /><br /> |
| 733 | <pre data-codemirror="php-plain"> |
| 734 | add_filter('acfe/form/load/post_id', 'my_form_post_values_source', 10, 3); |
| 735 | add_filter('acfe/form/load/post_id/form=<?php echo $form_name; ?>', 'my_form_post_values_source', 10, 3); |
| 736 | add_filter('acfe/form/load/post_id/action=my-post-action', 'my_form_post_values_source', 10, 3);</pre> |
| 737 | <br /> |
| 738 | <pre data-codemirror="php-plain"> |
| 739 | /** |
| 740 | * @int $post_id Post ID used as source |
| 741 | * @array $form The form settings |
| 742 | * @string $action The action alias name |
| 743 | */ |
| 744 | add_filter('acfe/form/load/post_id/form=<?php echo $form_name; ?>', 'my_form_post_values_source', 10, 3); |
| 745 | function my_form_post_values_source($post_id, $form, $action){ |
| 746 | |
| 747 | /** |
| 748 | * Force to load values from the post ID 145 |
| 749 | */ |
| 750 | $post_id = 145; |
| 751 | |
| 752 | |
| 753 | /** |
| 754 | * Return |
| 755 | */ |
| 756 | return $post_id; |
| 757 | |
| 758 | }</pre><?php |
| 759 | |
| 760 | } |
| 761 | |
| 762 | function advanced_save_args($field){ |
| 763 | |
| 764 | $form_name = 'my_form'; |
| 765 | |
| 766 | if(acf_maybe_get($field, 'value')) |
| 767 | |
| 768 | $form_name = get_field('acfe_form_name', $field['value']); |
| 769 | |
| 770 | ?>You may use the following hooks:<br /><br /> |
| 771 | <pre data-codemirror="php-plain"> |
| 772 | add_filter('acfe/form/submit/post_args', 'my_form_post_args', 10, 4); |
| 773 | add_filter('acfe/form/submit/post_args/form=<?php echo $form_name; ?>', 'my_form_post_args', 10, 4); |
| 774 | add_filter('acfe/form/submit/post_args/action=my-post-action', 'my_form_post_args', 10, 4);</pre> |
| 775 | <br /> |
| 776 | <pre data-codemirror="php-plain"> |
| 777 | /** |
| 778 | * @array $args The generated post arguments |
| 779 | * @string $type Action type: 'insert_post' or 'update_post' |
| 780 | * @array $form The form settings |
| 781 | * @string $action The action alias name |
| 782 | */ |
| 783 | add_filter('acfe/form/submit/post_args/form=<?php echo $form_name; ?>', 'my_form_post_args', 10, 4); |
| 784 | function my_form_post_args($args, $type, $form, $action){ |
| 785 | |
| 786 | /** |
| 787 | * Force specific post title if the action type is 'insert_post' |
| 788 | */ |
| 789 | if($type === 'insert_post'){ |
| 790 | |
| 791 | $args['post_title'] = 'My title'; |
| 792 | |
| 793 | } |
| 794 | |
| 795 | |
| 796 | /** |
| 797 | * Get the form input value named 'my_field' |
| 798 | * This is the value entered by the user during the form submission |
| 799 | */ |
| 800 | $my_field = get_field('my_field'); |
| 801 | |
| 802 | |
| 803 | /** |
| 804 | * Get the field value 'my_field' from the post ID 145 |
| 805 | */ |
| 806 | $my_post_field = get_field('my_field', 145); |
| 807 | |
| 808 | |
| 809 | /** |
| 810 | * Return arguments |
| 811 | * Note: Return false will stop post & meta insert/update |
| 812 | */ |
| 813 | return $args; |
| 814 | |
| 815 | }</pre><?php |
| 816 | |
| 817 | } |
| 818 | |
| 819 | function advanced_save($field){ |
| 820 | |
| 821 | $form_name = 'my_form'; |
| 822 | |
| 823 | if(acf_maybe_get($field, 'value')) |
| 824 | $form_name = get_field('acfe_form_name', $field['value']); |
| 825 | |
| 826 | ?>You may use the following hooks:<br /><br /> |
| 827 | <pre data-codemirror="php-plain"> |
| 828 | add_action('acfe/form/submit/post', 'my_form_post_save', 10, 5); |
| 829 | add_action('acfe/form/submit/post/form=<?php echo $form_name; ?>', 'my_form_post_save', 10, 5); |
| 830 | add_action('acfe/form/submit/post/action=my-post-action', 'my_form_post_save', 10, 5);</pre> |
| 831 | <br /> |
| 832 | <pre data-codemirror="php-plain"> |
| 833 | /** |
| 834 | * @int $post_id The targeted post ID |
| 835 | * @string $type Action type: 'insert_post' or 'update_post' |
| 836 | * @array $args The generated post arguments |
| 837 | * @array $form The form settings |
| 838 | * @string $action The action alias name |
| 839 | * |
| 840 | * Note: At this point the post & meta fields are already saved in the database |
| 841 | */ |
| 842 | add_action('acfe/form/submit/post/form=<?php echo $form_name; ?>', 'my_form_post_save', 10, 5); |
| 843 | function my_form_post_save($post_id, $type, $args, $form, $action){ |
| 844 | |
| 845 | /** |
| 846 | * Get the value from the form input named 'my_field' |
| 847 | * This is the value entered by the user during the form submission |
| 848 | */ |
| 849 | $my_field = get_field('my_field'); |
| 850 | |
| 851 | |
| 852 | /** |
| 853 | * Get the field value 'my_field' from the currently saved post |
| 854 | */ |
| 855 | $my_post_field = get_field('my_field', $post_id); |
| 856 | |
| 857 | }</pre><?php |
| 858 | |
| 859 | } |
| 860 | |
| 861 | } |
| 862 | |
| 863 | new acfe_form_post(); |
| 864 | |
| 865 | endif; |