Make WordPress Core

Ticket #20547: 20547.2.diff

File 20547.2.diff, 8.2 KB (added by wonderboymusic, 12 years ago)
  • src/wp-includes/post.php

     
    27332733                'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    27342734                'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    27352735                'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    2736                 'post_content' => '', 'post_title' => '');
     2736                'post_content' => '', 'post_title' => '', 'context' => '', 'file' => '' );
    27372737
    27382738        $postarr = wp_parse_args($postarr, $defaults);
    27392739
     
    28932893        $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    28942894
    28952895        // expected_slashed (everything!)
    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' ) );
    28972897        $data = apply_filters('wp_insert_post_data', $data, $postarr);
    28982898        $data = wp_unslash( $data );
    28992899        $where = array( 'ID' => $post_ID );
     
    29532953        $current_guid = get_post_field( 'guid', $post_ID );
    29542954
    29552955        // Set GUID
    2956         if ( !$update && '' == $current_guid )
     2956        if ( ! $update && 'attachment' !== $post_type && '' == $current_guid )
    29572957                $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
    29582958
     2959        if ( 'attachment' === $post_type && ! empty( $file ) ) {
     2960                update_attached_file( $post_ID, $file );
     2961        }
     2962
    29592963        clean_post_cache( $post_ID );
    29602964
    29612965        $post = get_post($post_ID);
     
    29762980
    29772981        if ( $update ) {
    29782982                do_action('edit_post', $post_ID, $post);
     2983                if ( 'attachment' === $post_type ) {
     2984                        do_action( 'edit_attachment', $post_ID );
     2985                }
    29792986                $post_after = get_post($post_ID);
    29802987                do_action( 'post_updated', $post_ID, $post_after, $post_before);
     2988        } elseif ( 'attachment' === $post_type ) {
     2989                do_action( 'add_attachment', $post_ID );
    29812990        }
    29822991
     2992        if ( 'attachment' === $post_type && ! empty( $context ) ) {
     2993                add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
     2994        }
     2995
    29832996        do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );
    29842997        do_action( 'save_post', $post_ID, $post, $update );
    29852998        do_action( 'wp_insert_post', $post_ID, $post, $update );
     
    39964009 * @param int $parent Parent post ID.
    39974010 * @return int Attachment ID.
    39984011 */
    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) )
     4012function 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 ) ) {
    40114016                $object['post_parent'] = $parent;
    4012 
    4013         unset( $object[ 'filter' ] );
    4014 
    4015         $object = sanitize_post($object, 'db');
    4016 
    4017         // export array as variables
    4018         extract($object, EXTR_SKIP);
    4019 
    4020         if ( empty($post_author) )
    4021                 $post_author = $user_id;
    4022 
    4023         $post_type = 'attachment';
    4024 
    4025         if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
    4026                 $post_status = 'inherit';
    4027 
    4028         if ( !empty($post_category) )
    4029                 $post_category = array_filter($post_category); // Filter out empty terms
    4030 
    4031         // Make sure we set a valid category.
    4032         if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
    4033                 $post_category = array();
    40344017        }
    4035 
    4036         // Are we updating or creating?
    4037         if ( !empty($ID) ) {
    4038                 $update = true;
    4039                 $post_ID = (int) $ID;
    4040         } else {
    4041                 $update = false;
    4042                 $post_ID = 0;
     4018        $object['post_type'] = 'attachment';
     4019        if ( ! in_array( $object['post_status'], array( 'inherit', 'private' ) ) ) {
     4020                $object['post_status'] = 'inherit';
    40434021        }
    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 );
    41614023}
    4162 
    41634024/**
    41644025 * Trashes or deletes an attachment.
    41654026 *