| 2896 | | $data = compact( array( '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', 'guid' ) ); |
| | 2896 | $data = compact( array( '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' ) ); |
| 3999 | | function wp_insert_attachment($object, $file = false, $parent = 0) { |
| 4000 | | global $wpdb; |
| 4001 | | |
| 4002 | | $user_id = get_current_user_id(); |
| 4003 | | |
| 4004 | | $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id, |
| 4005 | | 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'post_title' => '', |
| 4006 | | 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'post_content' => '', |
| 4007 | | 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => ''); |
| 4008 | | |
| 4009 | | $object = wp_parse_args($object, $defaults); |
| 4010 | | if ( !empty($parent) ) |
| | 4012 | function wp_insert_attachment( $object, $file = false, $parent = 0 ) { |
| | 4013 | $defaults = array( 'post_status' => 'inherit', 'post_type' => 'attachment', 'context' => '', 'file' => $file ); |
| | 4014 | $object = wp_parse_args( $object, $defaults ); |
| | 4015 | if ( ! empty( $parent ) ) { |
| 4044 | | |
| 4045 | | // Create a valid post name. |
| 4046 | | if ( empty($post_name) ) |
| 4047 | | $post_name = sanitize_title($post_title); |
| 4048 | | else |
| 4049 | | $post_name = sanitize_title($post_name); |
| 4050 | | |
| 4051 | | // expected_slashed ($post_name) |
| 4052 | | $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent); |
| 4053 | | |
| 4054 | | if ( empty($post_date) ) |
| 4055 | | $post_date = current_time('mysql'); |
| 4056 | | if ( empty($post_date_gmt) ) |
| 4057 | | $post_date_gmt = current_time('mysql', 1); |
| 4058 | | |
| 4059 | | if ( empty($post_modified) ) |
| 4060 | | $post_modified = $post_date; |
| 4061 | | if ( empty($post_modified_gmt) ) |
| 4062 | | $post_modified_gmt = $post_date_gmt; |
| 4063 | | |
| 4064 | | if ( empty($comment_status) ) { |
| 4065 | | if ( $update ) |
| 4066 | | $comment_status = 'closed'; |
| 4067 | | else |
| 4068 | | $comment_status = get_option('default_comment_status'); |
| 4069 | | } |
| 4070 | | if ( empty($ping_status) ) |
| 4071 | | $ping_status = get_option('default_ping_status'); |
| 4072 | | |
| 4073 | | if ( isset($to_ping) ) |
| 4074 | | $to_ping = preg_replace('|\s+|', "\n", $to_ping); |
| 4075 | | else |
| 4076 | | $to_ping = ''; |
| 4077 | | |
| 4078 | | if ( isset($post_parent) ) |
| 4079 | | $post_parent = (int) $post_parent; |
| 4080 | | else |
| 4081 | | $post_parent = 0; |
| 4082 | | |
| 4083 | | if ( isset($menu_order) ) |
| 4084 | | $menu_order = (int) $menu_order; |
| 4085 | | else |
| 4086 | | $menu_order = 0; |
| 4087 | | |
| 4088 | | if ( !isset($post_password) ) |
| 4089 | | $post_password = ''; |
| 4090 | | |
| 4091 | | if ( ! isset($pinged) ) |
| 4092 | | $pinged = ''; |
| 4093 | | |
| 4094 | | // expected_slashed (everything!) |
| 4095 | | $data = compact( array( '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' ) ); |
| 4096 | | |
| 4097 | | /** |
| 4098 | | * Filter attachment post data before it is updated in or added |
| 4099 | | * to the database. |
| 4100 | | * |
| 4101 | | * @since 3.9.0 |
| 4102 | | * |
| 4103 | | * @param array $data Array of sanitized attachment post data. |
| 4104 | | * @param array $object Array of un-sanitized attachment post data. |
| 4105 | | */ |
| 4106 | | $data = apply_filters( 'wp_insert_attachment_data', $data, $object ); |
| 4107 | | $data = wp_unslash( $data ); |
| 4108 | | |
| 4109 | | if ( $update ) { |
| 4110 | | $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) ); |
| 4111 | | } else { |
| 4112 | | // If there is a suggested ID, use it if not already present |
| 4113 | | if ( !empty($import_id) ) { |
| 4114 | | $import_id = (int) $import_id; |
| 4115 | | if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) { |
| 4116 | | $data['ID'] = $import_id; |
| 4117 | | } |
| 4118 | | } |
| 4119 | | |
| 4120 | | $wpdb->insert( $wpdb->posts, $data ); |
| 4121 | | $post_ID = (int) $wpdb->insert_id; |
| 4122 | | } |
| 4123 | | |
| 4124 | | if ( empty($post_name) ) { |
| 4125 | | $post_name = sanitize_title($post_title, $post_ID); |
| 4126 | | $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) ); |
| 4127 | | } |
| 4128 | | |
| 4129 | | if ( is_object_in_taxonomy($post_type, 'category') ) |
| 4130 | | wp_set_post_categories( $post_ID, $post_category ); |
| 4131 | | |
| 4132 | | if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') ) |
| 4133 | | wp_set_post_tags( $post_ID, $tags_input ); |
| 4134 | | |
| 4135 | | // support for all custom taxonomies |
| 4136 | | if ( !empty($tax_input) ) { |
| 4137 | | foreach ( $tax_input as $taxonomy => $tags ) { |
| 4138 | | $taxonomy_obj = get_taxonomy($taxonomy); |
| 4139 | | if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical. |
| 4140 | | $tags = array_filter($tags); |
| 4141 | | if ( current_user_can($taxonomy_obj->cap->assign_terms) ) |
| 4142 | | wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
| 4143 | | } |
| 4144 | | } |
| 4145 | | |
| 4146 | | if ( $file ) |
| 4147 | | update_attached_file( $post_ID, $file ); |
| 4148 | | |
| 4149 | | clean_post_cache( $post_ID ); |
| 4150 | | |
| 4151 | | if ( ! empty( $context ) ) |
| 4152 | | add_post_meta( $post_ID, '_wp_attachment_context', $context, true ); |
| 4153 | | |
| 4154 | | if ( $update) { |
| 4155 | | do_action('edit_attachment', $post_ID); |
| 4156 | | } else { |
| 4157 | | do_action('add_attachment', $post_ID); |
| 4158 | | } |
| 4159 | | |
| 4160 | | return $post_ID; |
| | 4022 | return wp_insert_post( $object ); |