| 4485 | | function wp_insert_attachment($object, $file = false, $parent = 0) { |
| 4486 | | global $wpdb; |
| 4487 | | |
| 4488 | | $user_id = get_current_user_id(); |
| 4489 | | |
| 4490 | | $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id, |
| 4491 | | 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, |
| 4492 | | 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', |
| 4493 | | 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, |
| 4494 | | 'post_title' => '', 'post_content' => '', 'context' => ''); |
| 4495 | | |
| 4496 | | $object = wp_parse_args($object, $defaults); |
| 4497 | | if ( ! empty( $parent ) ) { |
| 4498 | | $object['post_parent'] = $parent; |
| 4499 | | } |
| 4500 | | unset( $object[ 'filter' ] ); |
| 4501 | | |
| 4502 | | $object = sanitize_post($object, 'db'); |
| 4503 | | |
| 4504 | | $post_ID = 0; |
| 4505 | | $update = false; |
| 4506 | | $guid = $object['guid']; |
| 4507 | | |
| 4508 | | // Are we updating or creating? |
| 4509 | | if ( ! empty( $object['ID'] ) ) { |
| 4510 | | $update = true; |
| 4511 | | $post_ID = (int) $object['ID']; |
| 4512 | | |
| 4513 | | // wp_insert_post() checks for the existence of this post.... |
| 4514 | | } |
| 4515 | | |
| 4516 | | $post_type = 'attachment'; |
| 4517 | | |
| 4518 | | $post_title = $object['post_title']; |
| 4519 | | $post_content = $object['post_content']; |
| 4520 | | $post_excerpt = $object['post_excerpt']; |
| 4521 | | if ( isset( $object['post_name'] ) ) { |
| 4522 | | $post_name = $object['post_name']; |
| 4523 | | } |
| 4524 | | |
| 4525 | | // wp_insert_post() checks $maybe_empty |
| 4526 | | |
| 4527 | | $post_status = $object['post_status']; |
| 4528 | | if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) { |
| 4529 | | $post_status = 'inherit'; |
| 4530 | | } |
| 4531 | | |
| 4532 | | if ( ! empty( $object['post_category'] ) ) { |
| 4533 | | $post_category = array_filter( $object['post_category'] ); // Filter out empty terms |
| 4534 | | } |
| 4535 | | |
| 4536 | | // Make sure we set a valid category. |
| 4537 | | if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) { |
| 4538 | | // ironically, the default post_type for this function is 'post' |
| 4539 | | // as such, this should be probably have the same checks as wp_insert_post(), |
| 4540 | | // since all 'post's require a category. BUT, you can't override the post_type, because the |
| 4541 | | // variable is explicitly set. |
| 4542 | | $post_category = array(); |
| 4543 | | } |
| 4544 | | |
| 4545 | | // Create a valid post name. |
| 4546 | | if ( empty( $post_name ) ) { |
| 4547 | | $post_name = sanitize_title($post_title); |
| 4548 | | } else { |
| 4549 | | // missing check from wp_insert_post on update: |
| 4550 | | // "On updates, we need to check to see if it's using the old, fixed sanitization context." |
| 4551 | | $post_name = sanitize_title( $post_name ); |
| 4552 | | } |
| 4553 | | |
| 4554 | | if ( isset( $object['post_parent'] ) ) { |
| 4555 | | $post_parent = (int) $object['post_parent']; |
| 4556 | | } else { |
| 4557 | | $post_parent = 0; |
| 4558 | | } |
| 4559 | | |
| 4560 | | if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) { |
| 4561 | | $post_date = current_time( 'mysql' ); |
| 4562 | | } else { |
| 4563 | | $post_date = $object['post_date']; |
| 4564 | | } |
| 4565 | | |
| 4566 | | // wp_insert_post() validates the date here |
| 4567 | | |
| 4568 | | if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) { |
| 4569 | | $post_date_gmt = get_gmt_from_date( $post_date ); |
| 4570 | | } else { |
| 4571 | | $post_date_gmt = $object['post_date_gmt']; |
| 4572 | | } |
| 4573 | | |
| 4574 | | if ( $update || '0000-00-00 00:00:00' == $post_date ) { |
| 4575 | | $post_modified = current_time( 'mysql' ); |
| 4576 | | $post_modified_gmt = current_time( 'mysql', 1 ); |
| 4577 | | } else { |
| 4578 | | $post_modified = $post_date; |
| 4579 | | $post_modified_gmt = $post_date_gmt; |
| 4580 | | } |
| 4581 | | |
| 4582 | | // wp_insert_post() does "future" checks |
| 4583 | | |
| 4584 | | if ( empty( $object['comment_status'] ) ) { |
| 4585 | | if ( $update ) { |
| 4586 | | $comment_status = 'closed'; |
| 4587 | | } else { |
| 4588 | | $comment_status = get_option('default_comment_status'); |
| 4589 | | } |
| 4590 | | } else { |
| 4591 | | $comment_status = $object['comment_status']; |
| 4592 | | } |
| 4593 | | |
| 4594 | | // these variables are needed by compact() later |
| 4595 | | $post_content_filtered = $object['post_content_filtered']; |
| 4596 | | $post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author']; |
| 4597 | | $ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status']; |
| 4598 | | $to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : ''; |
| 4599 | | $pinged = isset( $object['pinged'] ) ? $object['pinged'] : ''; |
| 4600 | | $import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0; |
| 4601 | | |
| 4602 | | if ( isset( $object['menu_order'] ) ) { |
| 4603 | | $menu_order = (int) $object['menu_order']; |
| 4604 | | } else { |
| 4605 | | $menu_order = 0; |
| 4606 | | } |
| 4607 | | |
| 4608 | | $post_password = isset( $object['post_password'] ) ? $object['post_password'] : ''; |
| 4609 | | |
| 4610 | | // skips the 'wp_insert_post_parent' filter, present in wp_insert_post() |
| 4611 | | |
| 4612 | | // expected_slashed ($post_name) |
| 4613 | | $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); |
| 4614 | | |
| 4615 | | // don't unslash |
| 4616 | | $post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : ''; |
| 4617 | | |
| 4618 | | // expected_slashed (everything!) |
| 4619 | | $data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ); |
| 4620 | | |
| 4621 | | /** |
| 4622 | | * Filter attachment post data before it is updated in or added |
| 4623 | | * to the database. |
| 4624 | | * |
| 4625 | | * @since 3.9.0 |
| 4626 | | * |
| 4627 | | * @param array $data Array of sanitized attachment post data. |
| 4628 | | * @param array $object Array of un-sanitized attachment post data. |
| 4629 | | */ |
| 4630 | | $data = apply_filters( 'wp_insert_attachment_data', $data, $object ); |
| 4631 | | $data = wp_unslash( $data ); |
| 4632 | | $where = array( 'ID' => $post_ID ); |
| 4633 | | |
| 4634 | | if ( $update ) { |
| 4635 | | // skips 'pre_post_update' action |
| 4636 | | $wpdb->update( $wpdb->posts, $data, $where ); |
| 4637 | | } else { |
| 4638 | | // If there is a suggested ID, use it if not already present |
| 4639 | | if ( ! empty( $import_id ) ) { |
| 4640 | | $import_id = (int) $import_id; |
| 4641 | | if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { |
| 4642 | | $data['ID'] = $import_id; |
| 4643 | | } |
| 4644 | | } |
| 4645 | | |
| 4646 | | $wpdb->insert( $wpdb->posts, $data ); |
| 4647 | | $post_ID = (int) $wpdb->insert_id; |
| 4648 | | $where = array( 'ID' => $post_ID ); |
| 4649 | | } |
| 4650 | | |
| 4651 | | if ( empty( $data['post_name'] ) ) { |
| 4652 | | $data['post_name'] = sanitize_title( $data['post_title'], $post_ID ); |
| 4653 | | $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
| 4654 | | } |
| 4655 | | |
| 4656 | | if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) { |
| 4657 | | wp_set_post_categories( $post_ID, $post_category ); |
| 4658 | | } |
| 4659 | | |
| 4660 | | if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) { |
| 4661 | | wp_set_post_tags( $post_ID, $object['tags_input'] ); |
| 4662 | | } |
| 4663 | | |
| 4664 | | // new-style support for all custom taxonomies |
| 4665 | | if ( ! empty( $object['tax_input'] ) ) { |
| 4666 | | foreach ( $object['tax_input'] as $taxonomy => $tags ) { |
| 4667 | | $taxonomy_obj = get_taxonomy($taxonomy); |
| 4668 | | if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical. |
| 4669 | | $tags = array_filter($tags); |
| 4670 | | } |
| 4671 | | if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
| 4672 | | wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
| 4673 | | } |
| 4674 | | } |
| 4675 | | } |
| 4676 | | |
| 4677 | | if ( $file ) { |
| 4678 | | update_attached_file( $post_ID, $file ); |
| 4679 | | } |
| 4680 | | |
| 4681 | | // wp_insert_post() fills in guid if it is empty |
| 4682 | | |
| 4683 | | clean_post_cache( $post_ID ); |
| 4684 | | |
| 4685 | | if ( ! empty( $object['context'] ) ) { |
| 4686 | | add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true ); |
| 4687 | | } |
| 4688 | | |
| 4689 | | // skips wp_transition_post_status |
| 4690 | | |
| 4691 | | // the actions completely diverge from wp_insert_post() |
| 4692 | | |
| 4693 | | if ( $update ) { |
| 4694 | | /** |
| 4695 | | * Fires once an existing attachment has been updated. |
| 4696 | | * |
| 4697 | | * @since 2.0.0 |
| 4698 | | * |
| 4699 | | * @param int $post_ID Attachment ID. |
| 4700 | | */ |
| 4701 | | do_action( 'edit_attachment', $post_ID ); |
| 4702 | | } else { |
| 4703 | | |
| 4704 | | /** |
| 4705 | | * Fires once an attachment has been added. |
| 4706 | | * |
| 4707 | | * @since 2.0.0 |
| 4708 | | * |
| 4709 | | * @param int $post_ID Attachment ID. |
| 4710 | | */ |
| 4711 | | do_action( 'add_attachment', $post_ID ); |
| 4712 | | } |
| 4713 | | |
| 4714 | | return $post_ID; |
| | 4533 | function wp_insert_attachment( $object, $file = false, $parent = 0 ) { |
| | 4534 | $defaults = array( 'file' => $file, 'post_parent' => $parent ); |
| | 4535 | $data = wp_parse_args( $object, $defaults ); |
| | 4536 | $data['post_type'] = 'attachment'; |
| | 4537 | return wp_insert_post( $data ); |