custom.php
6 years ago
email.php
6 years ago
option.php
6 years ago
post.php
6 years ago
term.php
6 years ago
user.php
6 years ago
post.php
522 lines
| 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/action/post', array($this, 'load'), 1, 2); |
| 16 | add_action('acfe/form/submit/action/post', array($this, 'submit'), 1, 2); |
| 17 | |
| 18 | /* |
| 19 | * Admin |
| 20 | */ |
| 21 | add_filter('acf/prepare_field/name=acfe_form_post_save_meta', array(acfe()->acfe_form, 'map_fields')); |
| 22 | add_filter('acf/prepare_field/name=acfe_form_post_load_meta', array(acfe()->acfe_form, 'map_fields_deep')); |
| 23 | |
| 24 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_type', array(acfe()->acfe_form, 'map_fields_deep')); |
| 25 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_status', array(acfe()->acfe_form, 'map_fields_deep')); |
| 26 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_title', array(acfe()->acfe_form, 'map_fields_deep')); |
| 27 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_name', array(acfe()->acfe_form, 'map_fields_deep')); |
| 28 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_content', array(acfe()->acfe_form, 'map_fields_deep')); |
| 29 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_author', array(acfe()->acfe_form, 'map_fields_deep')); |
| 30 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_parent', array(acfe()->acfe_form, 'map_fields_deep')); |
| 31 | add_filter('acf/prepare_field/name=acfe_form_post_map_post_terms', array(acfe()->acfe_form, 'map_fields_deep')); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function load($form, $post_id){ |
| 36 | |
| 37 | // Form |
| 38 | $form_name = acf_maybe_get($form, 'form_name'); |
| 39 | $form_id = acf_maybe_get($form, 'form_id'); |
| 40 | $post_info = acf_get_post_id_info($post_id); |
| 41 | |
| 42 | // Action |
| 43 | $post_action = get_sub_field('acfe_form_post_action'); |
| 44 | |
| 45 | // Load values |
| 46 | $load_values = get_sub_field('acfe_form_post_load_values'); |
| 47 | $load_source = get_sub_field('acfe_form_post_load_source'); |
| 48 | $load_meta = get_sub_field('acfe_form_post_load_meta'); |
| 49 | |
| 50 | // Load values |
| 51 | if(!$load_values) |
| 52 | return $form; |
| 53 | |
| 54 | $_post_type = get_sub_field('acfe_form_post_map_post_type'); |
| 55 | $_post_status = get_sub_field('acfe_form_post_map_post_status'); |
| 56 | $_post_title = get_sub_field('acfe_form_post_map_post_title'); |
| 57 | $_post_name = get_sub_field('acfe_form_post_map_post_name'); |
| 58 | $_post_content = get_sub_field('acfe_form_post_map_post_content'); |
| 59 | $_post_author = get_sub_field('acfe_form_post_map_post_author'); |
| 60 | $_post_parent = get_sub_field('acfe_form_post_map_post_parent'); |
| 61 | $_post_terms = get_sub_field('acfe_form_post_map_post_terms'); |
| 62 | |
| 63 | $_post_id = 0; |
| 64 | |
| 65 | // Custom Post ID |
| 66 | if($load_source !== 'current_post'){ |
| 67 | |
| 68 | $_post_id = $load_source; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | // Current Post |
| 73 | elseif($load_source === 'current_post'){ |
| 74 | |
| 75 | if($post_info['type'] === 'post') |
| 76 | $_post_id = $post_info['id']; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | $_post_id = apply_filters('acfe/form/load/action/post/' . $post_action . '_id', $_post_id, $form); |
| 81 | $_post_id = apply_filters('acfe/form/load/action/post/' . $post_action . '_id/name=' . $form_name, $_post_id, $form); |
| 82 | $_post_id = apply_filters('acfe/form/load/action/post/' . $post_action . '_id/id=' . $form_id, $_post_id, $form); |
| 83 | |
| 84 | // Invalid Post ID |
| 85 | if(!$_post_id) |
| 86 | return $form; |
| 87 | |
| 88 | // Post type |
| 89 | if(acf_is_field_key($_post_type)){ |
| 90 | |
| 91 | $key = array_search($_post_type, $load_meta); |
| 92 | |
| 93 | if($key !== false){ |
| 94 | |
| 95 | unset($load_meta[$key]); |
| 96 | $form['map'][$_post_type]['value'] = get_post_field('post_type', $_post_id); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | // Post status |
| 103 | if(acf_is_field_key($_post_status)){ |
| 104 | |
| 105 | $key = array_search($_post_type, $load_meta); |
| 106 | |
| 107 | if($key !== false){ |
| 108 | |
| 109 | unset($load_meta[$key]); |
| 110 | $form['map'][$_post_status]['value'] = get_post_field('post_status', $_post_id); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | // Post title |
| 117 | if(acf_is_field_key($_post_title)){ |
| 118 | |
| 119 | $key = array_search($_post_title, $load_meta); |
| 120 | |
| 121 | if($key !== false){ |
| 122 | |
| 123 | unset($load_meta[$key]); |
| 124 | $form['map'][$_post_title]['value'] = get_post_field('post_title', $_post_id); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | // Post name |
| 131 | if(acf_is_field_key($_post_name)){ |
| 132 | |
| 133 | $key = array_search($_post_name, $load_meta); |
| 134 | |
| 135 | if($key !== false){ |
| 136 | |
| 137 | unset($load_meta[$key]); |
| 138 | $form['map'][$_post_name]['value'] = get_post_field('post_name', $_post_id); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | // Post content |
| 145 | if(acf_is_field_key($_post_content)){ |
| 146 | |
| 147 | $key = array_search($_post_content, $load_meta); |
| 148 | |
| 149 | if($key !== false){ |
| 150 | |
| 151 | unset($load_meta[$key]); |
| 152 | $form['map'][$_post_content]['value'] = get_post_field('post_content', $_post_id); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | // Post author |
| 159 | if(acf_is_field_key($_post_author)){ |
| 160 | |
| 161 | $key = array_search($_post_author, $load_meta); |
| 162 | |
| 163 | if($key !== false){ |
| 164 | |
| 165 | unset($load_meta[$key]); |
| 166 | $form['map'][$_post_author]['value'] = get_post_field('post_author', $_post_id); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |
| 172 | // Post parent |
| 173 | if(acf_is_field_key($_post_parent)){ |
| 174 | |
| 175 | $key = array_search($_post_parent, $load_meta); |
| 176 | |
| 177 | if($key !== false){ |
| 178 | |
| 179 | unset($load_meta[$key]); |
| 180 | $form['map'][$_post_parent]['value'] = get_post_field('post_parent', $_post_id); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | // Post terms |
| 187 | if(acf_is_field_key($_post_terms)){ |
| 188 | |
| 189 | $key = array_search($_post_terms, $load_meta); |
| 190 | |
| 191 | if($key !== false){ |
| 192 | |
| 193 | unset($load_meta[$key]); |
| 194 | |
| 195 | $taxonomies = acf_get_taxonomies(array( |
| 196 | 'post_type' => get_post_type($_post_id) |
| 197 | )); |
| 198 | |
| 199 | if(!empty($taxonomies)){ |
| 200 | |
| 201 | $terms = array(); |
| 202 | |
| 203 | foreach($taxonomies as $taxonomy){ |
| 204 | |
| 205 | $get_the_terms = get_the_terms($_post_id, $taxonomy); |
| 206 | if(!$get_the_terms || is_wp_error($get_the_terms)) |
| 207 | continue; |
| 208 | |
| 209 | $terms = array_merge($terms, $get_the_terms); |
| 210 | |
| 211 | } |
| 212 | |
| 213 | $return = wp_list_pluck($terms, 'term_id'); |
| 214 | |
| 215 | $form['map'][$_post_terms]['value'] = $return; |
| 216 | |
| 217 | } |
| 218 | |
| 219 | |
| 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 submit($form, $post_id){ |
| 243 | |
| 244 | $form_name = acf_maybe_get($form, 'form_name'); |
| 245 | $form_id = acf_maybe_get($form, 'form_id'); |
| 246 | $post_info = acf_get_post_id_info($post_id); |
| 247 | |
| 248 | // Action |
| 249 | $post_action = get_sub_field('acfe_form_post_action'); |
| 250 | |
| 251 | // Mapping |
| 252 | $map = array( |
| 253 | 'post_type' => get_sub_field('acfe_form_post_map_post_type'), |
| 254 | 'post_status' => get_sub_field('acfe_form_post_map_post_status'), |
| 255 | 'post_title' => get_sub_field('acfe_form_post_map_post_title'), |
| 256 | 'post_name' => get_sub_field('acfe_form_post_map_post_name'), |
| 257 | 'post_content' => get_sub_field('acfe_form_post_map_post_content'), |
| 258 | 'post_author' => get_sub_field('acfe_form_post_map_post_author'), |
| 259 | 'post_parent' => get_sub_field('acfe_form_post_map_post_parent'), |
| 260 | 'post_terms' => get_sub_field('acfe_form_post_map_post_terms'), |
| 261 | ); |
| 262 | |
| 263 | // Fields |
| 264 | $_target = get_sub_field('acfe_form_post_save_target'); |
| 265 | |
| 266 | $_post_type = get_sub_field('acfe_form_post_save_post_type'); |
| 267 | $_post_status = get_sub_field('acfe_form_post_save_post_status'); |
| 268 | |
| 269 | $_post_title_group = get_sub_field('acfe_form_post_save_post_title_group'); |
| 270 | $_post_title = $_post_title_group['acfe_form_post_save_post_title']; |
| 271 | $_post_title_custom = $_post_title_group['acfe_form_post_save_post_title_custom']; |
| 272 | |
| 273 | $_post_name_group = get_sub_field('acfe_form_post_save_post_name_group'); |
| 274 | $_post_name = $_post_name_group['acfe_form_post_save_post_name']; |
| 275 | $_post_name_custom = $_post_name_group['acfe_form_post_save_post_name_custom']; |
| 276 | |
| 277 | $_post_content_group = get_sub_field('acfe_form_post_save_post_content_group'); |
| 278 | $_post_content = $_post_content_group['acfe_form_post_save_post_content']; |
| 279 | $_post_content_custom = $_post_content_group['acfe_form_post_save_post_content_custom']; |
| 280 | |
| 281 | $_post_author = get_sub_field('acfe_form_post_save_post_author'); |
| 282 | $_post_parent = get_sub_field('acfe_form_post_save_post_parent'); |
| 283 | $_post_terms = get_sub_field('acfe_form_post_save_post_terms'); |
| 284 | |
| 285 | $_post_id = 0; |
| 286 | |
| 287 | // Insert Post |
| 288 | if($post_action === 'insert_post'){ |
| 289 | |
| 290 | $temp_title = false; |
| 291 | |
| 292 | // Post title |
| 293 | if(!empty($map['post_title'])){ |
| 294 | |
| 295 | $temp_title = acfe_form_map_field_value($map['post_title'], $_POST['acf'], $_post_id); |
| 296 | |
| 297 | }elseif($_post_title === 'generated_id'){ |
| 298 | |
| 299 | $temp_title = true; |
| 300 | |
| 301 | }elseif($_post_title === 'custom'){ |
| 302 | |
| 303 | $temp_title = acfe_form_map_field_value($_post_title_custom, $_POST['acf'], $_post_id); |
| 304 | |
| 305 | } |
| 306 | |
| 307 | if(!empty($temp_title)){ |
| 308 | |
| 309 | $_post_id = wp_insert_post(array( |
| 310 | 'post_title' => 'Post' |
| 311 | )); |
| 312 | |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
| 317 | // Update Post |
| 318 | elseif($post_action === 'update_post'){ |
| 319 | |
| 320 | // Custom Post ID |
| 321 | if($_target !== 'current_post'){ |
| 322 | |
| 323 | $_post_id = $_target; |
| 324 | |
| 325 | } |
| 326 | |
| 327 | // Current Post |
| 328 | elseif($_target === 'current_post'){ |
| 329 | |
| 330 | if($post_info['type'] === 'post') |
| 331 | $_post_id = $post_info['id']; |
| 332 | |
| 333 | } |
| 334 | |
| 335 | } |
| 336 | |
| 337 | // Invalid Post ID |
| 338 | if(!$_post_id) |
| 339 | return; |
| 340 | |
| 341 | $args = array(); |
| 342 | |
| 343 | // ID |
| 344 | $args['ID'] = $_post_id; |
| 345 | |
| 346 | // Post type |
| 347 | if(!empty($map['post_type'])){ |
| 348 | |
| 349 | $args['post_type'] = acfe_form_map_field_value($map['post_type'], $_POST['acf'], $_post_id); |
| 350 | |
| 351 | }elseif(!empty($_post_type)){ |
| 352 | |
| 353 | $args['post_type'] = $_post_type; |
| 354 | |
| 355 | } |
| 356 | |
| 357 | // Post status |
| 358 | if(!empty($map['post_status'])){ |
| 359 | |
| 360 | $args['post_status'] = acfe_form_map_field_value($map['post_status'], $_POST['acf'], $_post_id); |
| 361 | |
| 362 | }elseif(!empty($_post_status)){ |
| 363 | |
| 364 | $args['post_status'] = $_post_status; |
| 365 | |
| 366 | } |
| 367 | |
| 368 | // Post title |
| 369 | if(!empty($map['post_title'])){ |
| 370 | |
| 371 | $args['post_title'] = acfe_form_map_field_value($map['post_title'], $_POST['acf'], $_post_id); |
| 372 | |
| 373 | }elseif($_post_title === 'generated_id'){ |
| 374 | |
| 375 | $args['post_title'] = $_post_id; |
| 376 | |
| 377 | }elseif($_post_title === 'custom'){ |
| 378 | |
| 379 | $args['post_title'] = acfe_form_map_field_value($_post_title_custom, $_POST['acf'], $_post_id); |
| 380 | |
| 381 | } |
| 382 | |
| 383 | // Post name |
| 384 | if(!empty($map['post_name'])){ |
| 385 | |
| 386 | $args['post_name'] = acfe_form_map_field_value($map['post_name'], $_POST['acf'], $_post_id); |
| 387 | |
| 388 | }elseif($_post_name === 'generated_id'){ |
| 389 | |
| 390 | $args['post_name'] = $_post_id; |
| 391 | |
| 392 | }elseif($_post_name === 'custom'){ |
| 393 | |
| 394 | $args['post_name'] = acfe_form_map_field_value($_post_name_custom, $_POST['acf'], $_post_id); |
| 395 | |
| 396 | } |
| 397 | |
| 398 | // Post content |
| 399 | if(!empty($map['post_content'])){ |
| 400 | |
| 401 | $args['post_content'] = acfe_form_map_field_value($map['post_content'], $_POST['acf'], $_post_id); |
| 402 | |
| 403 | }elseif($_post_content === 'custom'){ |
| 404 | |
| 405 | $args['post_content'] = acfe_form_map_field_value($_post_content_custom, $_POST['acf'], $_post_id); |
| 406 | |
| 407 | } |
| 408 | |
| 409 | // Post author |
| 410 | if(!empty($map['post_author'])){ |
| 411 | |
| 412 | $args['post_author'] = acfe_form_map_field_value($map['post_author'], $_POST['acf'], $_post_id); |
| 413 | |
| 414 | }elseif(!empty($_post_author)){ |
| 415 | |
| 416 | // Custom user ID |
| 417 | $args['post_author'] = $_post_author; |
| 418 | |
| 419 | // Current User |
| 420 | if($_post_author === 'current_user'){ |
| 421 | |
| 422 | $args['post_author'] = get_current_user_id(); |
| 423 | |
| 424 | // Current Post Author |
| 425 | }elseif($_post_author === 'current_post_author'){ |
| 426 | |
| 427 | if($post_info['type'] === 'post') |
| 428 | $args['post_author'] = get_post_field('post_author', $post_info['id']); |
| 429 | |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | // Post parent |
| 435 | if(!empty($map['post_parent'])){ |
| 436 | |
| 437 | $args['post_parent'] = acfe_form_map_field_value($map['post_parent'], $_POST['acf'], $_post_id); |
| 438 | |
| 439 | }elseif(!empty($_post_parent)){ |
| 440 | |
| 441 | // Custom Post ID |
| 442 | $args['post_parent'] = $_post_parent; |
| 443 | |
| 444 | // Current Post |
| 445 | if($_post_parent === 'current_post'){ |
| 446 | |
| 447 | if($post_info['type'] === 'post') |
| 448 | $args['post_parent'] = $post_info['id']; |
| 449 | |
| 450 | } |
| 451 | |
| 452 | } |
| 453 | |
| 454 | $terms = array(); |
| 455 | |
| 456 | // Post terms |
| 457 | if(!empty($map['post_terms'])){ |
| 458 | |
| 459 | $terms = acf_array(acfe_form_map_field_value($map['post_terms'], $_POST['acf'], $_post_id)); |
| 460 | |
| 461 | }elseif(!empty($_post_terms)){ |
| 462 | |
| 463 | $terms = acf_array($_post_terms); |
| 464 | |
| 465 | } |
| 466 | |
| 467 | // Tax input |
| 468 | if(!empty($terms)){ |
| 469 | |
| 470 | foreach($terms as $term){ |
| 471 | |
| 472 | $args['tax_input'][$term->taxonomy][] = $term->term_id; |
| 473 | |
| 474 | } |
| 475 | |
| 476 | } |
| 477 | |
| 478 | $args = apply_filters('acfe/form/submit/action/post/' . $post_action . '_args', $args, $form, $_post_id); |
| 479 | $args = apply_filters('acfe/form/submit/action/post/' . $post_action . '_args/name=' . $form_name, $args, $form, $_post_id); |
| 480 | $args = apply_filters('acfe/form/submit/action/post/' . $post_action . '_args/id=' . $form_id, $args, $form, $_post_id); |
| 481 | |
| 482 | if($args === false) |
| 483 | return; |
| 484 | |
| 485 | // Update Post |
| 486 | $_post_id = wp_update_post($args); |
| 487 | |
| 488 | do_action('acfe/form/submit/action/post/' . $post_action, $form, $_post_id, $args); |
| 489 | do_action('acfe/form/submit/action/post/' . $post_action . '/name=' . $form_name, $form, $_post_id, $args); |
| 490 | do_action('acfe/form/submit/action/post/' . $post_action . '/id=' . $form_id, $form, $_post_id, $args); |
| 491 | |
| 492 | // Meta save |
| 493 | $save_meta = get_sub_field('acfe_form_post_save_meta'); |
| 494 | |
| 495 | if(!empty($save_meta)){ |
| 496 | |
| 497 | $data = acfe_form_filter_meta($save_meta, $_POST['acf']); |
| 498 | |
| 499 | if(!empty($data)){ |
| 500 | |
| 501 | // Backup original acf post data |
| 502 | $acf = $_POST['acf']; |
| 503 | |
| 504 | // Save meta fields |
| 505 | acf_save_post($_post_id, $data); |
| 506 | |
| 507 | // Restore original acf post data |
| 508 | $_POST['acf'] = $acf; |
| 509 | |
| 510 | } |
| 511 | |
| 512 | } |
| 513 | |
| 514 | |
| 515 | |
| 516 | } |
| 517 | |
| 518 | } |
| 519 | |
| 520 | new acfe_form_post(); |
| 521 | |
| 522 | endif; |