WordPress.org

Make WordPress Core

Ticket #21963: 21963.2.diff

File 21963.2.diff, 16.1 KB (added by ericlewis, 6 years ago)

Freshness for 21963.diff, applies cleanly to trunk now.

  • wp-includes/post.php

     
    26742674                'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    26752675                'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    26762676                'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    2677                 'post_content' => '', 'post_title' => '');
     2677                'post_content' => '', 'post_title' => '', 'context' => '');
    26782678
    26792679        $postarr = wp_parse_args($postarr, $defaults);
    26802680
     
    27092709        if ( empty($post_status) )
    27102710                $post_status = 'draft';
    27112711
     2712        if ( $post_type == 'attachment' && ! in_array( $post_status, array( 'inherit', 'private', 'publish' ) ) )
     2713                $post_status = 'inherit';
     2714
    27122715        if ( !empty($post_category) )
    27132716                $post_category = array_filter($post_category); // Filter out empty terms
    27142717
     
    29092912                update_post_meta($post_ID, '_wp_page_template',  $page_template);
    29102913        }
    29112914
     2915        if ( 'attachment' == $data['post_type'] ) {
     2916                if ( ! empty( $context ) )
     2917                        add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
     2918                if ( ! empty( $file ) )
     2919                        update_attached_file( $post_ID, $file );
     2920        }
     2921
    29122922        wp_transition_post_status($data['post_status'], $previous_status, $post);
    29132923
    29142924        if ( $update ) {
    29152925                do_action('edit_post', $post_ID, $post);
    29162926                $post_after = get_post($post_ID);
    29172927                do_action( 'post_updated', $post_ID, $post_after, $post_before);
     2928                if ( 'attachment' == $post_type )
     2929                        do_action( 'edit_attachment', $post_ID );
     2930        } elseif ( 'attachment' == $post_type ) {
     2931                do_action( 'add_attachment', $post_ID );
    29182932        }
    29192933
    29202934        do_action('save_post', $post_ID, $post);
     
    38293843/**
    38303844 * Insert an attachment.
    38313845 *
    3832  * If you set the 'ID' in the $object parameter, it will mean that you are
    3833  * updating and attempt to update the attachment. You can also set the
    3834  * attachment name or title by setting the key 'post_name' or 'post_title'.
    3835  *
    3836  * You can set the dates for the attachment manually by setting the 'post_date'
    3837  * and 'post_date_gmt' keys' values.
    3838  *
    3839  * By default, the comments will use the default settings for whether the
    3840  * comments are allowed. You can close them manually or keep them open by
    3841  * setting the value for the 'comment_status' key.
    3842  *
    3843  * The $object parameter can have the following:
    3844  *     'post_status'   - Default is 'draft'. Can not be overridden, set the same as parent post.
    3845  *     'post_type'     - Default is 'post', will be set to attachment. Can not override.
    3846  *     'post_author'   - Default is current user ID. The ID of the user, who added the attachment.
    3847  *     'ping_status'   - Default is the value in default ping status option. Whether the attachment
    3848  *                       can accept pings.
    3849  *     'post_parent'   - Default is 0. Can use $parent parameter or set this for the post it belongs
    3850  *                       to, if any.
    3851  *     'menu_order'    - Default is 0. The order it is displayed.
    3852  *     'to_ping'       - Whether to ping.
    3853  *     'pinged'        - Default is empty string.
    3854  *     'post_password' - Default is empty string. The password to access the attachment.
    3855  *     'guid'          - Global Unique ID for referencing the attachment.
    3856  *     'post_content_filtered' - Attachment post content filtered.
    3857  *     'post_excerpt'  - Attachment excerpt.
    3858  *
    38593846 * @since 2.0.0
    3860  * @uses $wpdb
    3861  * @uses $user_ID
    3862  * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update.
    3863  * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update.
     3847 * @uses wp_insert_post()
    38643848 *
    3865  * @param string|array $object Arguments to override defaults.
     3849 * @param array $postarr Arguments to override defaults.
    38663850 * @param string $file Optional filename.
    38673851 * @param int $parent Parent post ID.
    38683852 * @return int Attachment ID.
    38693853 */
    3870 function wp_insert_attachment($object, $file = false, $parent = 0) {
    3871         global $wpdb, $user_ID;
     3854function wp_insert_attachment( $postarr, $file = false, $parent = 0 ) {
     3855        if ( $parent )
     3856                $postarr['post_parent'] = $parent;
    38723857
    3873         $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_ID,
    3874                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    3875                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    3876                 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => '');
    3877 
    3878         $object = wp_parse_args($object, $defaults);
    3879         if ( !empty($parent) )
    3880                 $object['post_parent'] = $parent;
    3881 
    3882         unset( $object[ 'filter' ] );
    3883 
    3884         $object = sanitize_post($object, 'db');
    3885 
    3886         // export array as variables
    3887         extract($object, EXTR_SKIP);
    3888 
    3889         if ( empty($post_author) )
    3890                 $post_author = $user_ID;
    3891 
    3892         $post_type = 'attachment';
    3893 
    3894         if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
    3895                 $post_status = 'inherit';
    3896 
    3897         if ( !empty($post_category) )
    3898                 $post_category = array_filter($post_category); // Filter out empty terms
    3899 
    3900         // Make sure we set a valid category.
    3901         if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
    3902                 $post_category = array();
    3903         }
    3904 
    3905         // Are we updating or creating?
    3906         if ( !empty($ID) ) {
    3907                 $update = true;
    3908                 $post_ID = (int) $ID;
    3909         } else {
    3910                 $update = false;
    3911                 $post_ID = 0;
    3912         }
    3913 
    3914         // Create a valid post name.
    3915         if ( empty($post_name) )
    3916                 $post_name = sanitize_title($post_title);
    3917         else
    3918                 $post_name = sanitize_title($post_name);
    3919 
    3920         // expected_slashed ($post_name)
    3921         $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    3922 
    3923         if ( empty($post_date) )
    3924                 $post_date = current_time('mysql');
    3925         if ( empty($post_date_gmt) )
    3926                 $post_date_gmt = current_time('mysql', 1);
    3927 
    3928         if ( empty($post_modified) )
    3929                 $post_modified = $post_date;
    3930         if ( empty($post_modified_gmt) )
    3931                 $post_modified_gmt = $post_date_gmt;
    3932 
    3933         if ( empty($comment_status) ) {
    3934                 if ( $update )
    3935                         $comment_status = 'closed';
    3936                 else
    3937                         $comment_status = get_option('default_comment_status');
    3938         }
    3939         if ( empty($ping_status) )
    3940                 $ping_status = get_option('default_ping_status');
    3941 
    3942         if ( isset($to_ping) )
    3943                 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
    3944         else
    3945                 $to_ping = '';
    3946 
    3947         if ( isset($post_parent) )
    3948                 $post_parent = (int) $post_parent;
    3949         else
    3950                 $post_parent = 0;
    3951 
    3952         if ( isset($menu_order) )
    3953                 $menu_order = (int) $menu_order;
    3954         else
    3955                 $menu_order = 0;
    3956 
    3957         if ( !isset($post_password) )
    3958                 $post_password = '';
    3959 
    3960         if ( ! isset($pinged) )
    3961                 $pinged = '';
    3962 
    3963         // expected_slashed (everything!)
    3964         $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' ) );
    3965         $data = stripslashes_deep( $data );
    3966 
    3967         if ( $update ) {
    3968                 $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
    3969         } else {
    3970                 // If there is a suggested ID, use it if not already present
    3971                 if ( !empty($import_id) ) {
    3972                         $import_id = (int) $import_id;
    3973                         if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
    3974                                 $data['ID'] = $import_id;
    3975                         }
    3976                 }
    3977 
    3978                 $wpdb->insert( $wpdb->posts, $data );
    3979                 $post_ID = (int) $wpdb->insert_id;
    3980         }
    3981 
    3982         if ( empty($post_name) ) {
    3983                 $post_name = sanitize_title($post_title, $post_ID);
    3984                 $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
    3985         }
    3986 
    3987         if ( is_object_in_taxonomy($post_type, 'category') )
    3988                 wp_set_post_categories( $post_ID, $post_category );
    3989 
    3990         if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
    3991                 wp_set_post_tags( $post_ID, $tags_input );
    3992 
    3993         // support for all custom taxonomies
    3994         if ( !empty($tax_input) ) {
    3995                 foreach ( $tax_input as $taxonomy => $tags ) {
    3996                         $taxonomy_obj = get_taxonomy($taxonomy);
    3997                         if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
    3998                                 $tags = array_filter($tags);
    3999                         if ( current_user_can($taxonomy_obj->cap->assign_terms) )
    4000                                 wp_set_post_terms( $post_ID, $tags, $taxonomy );
    4001                 }
    4002         }
    4003 
    40043858        if ( $file )
    4005                 update_attached_file( $post_ID, $file );
     3859                $postarr['file'] = $file;
    40063860
    4007         clean_post_cache( $post_ID );
    4008 
    4009         if ( ! empty( $context ) )
    4010                 add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
    4011 
    4012         if ( $update) {
    4013                 do_action('edit_attachment', $post_ID);
    4014         } else {
    4015                 do_action('add_attachment', $post_ID);
    4016         }
    4017 
    4018         return $post_ID;
     3861        return wp_insert_post( $postarr );
    40193862}
    40203863
    40213864/**
  • wp-admin/includes/post.php

     
    503503        return 0;
    504504}
    505505
    506 /**
    507  * Creates a new post from the "Write Post" form using $_POST information.
    508  *
    509  * @since 2.1.0
    510  *
    511  * @return unknown
    512  */
    513 function wp_write_post() {
    514         global $user_ID;
    515 
    516         if ( isset($_POST['post_type']) )
    517                 $ptype = get_post_type_object($_POST['post_type']);
    518         else
    519                 $ptype = get_post_type_object('post');
    520 
    521         if ( !current_user_can( $ptype->cap->edit_posts ) ) {
    522                 if ( 'page' == $ptype->name )
    523                         return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
    524                 else
    525                         return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
    526         }
    527 
    528         $_POST['post_mime_type'] = '';
    529 
    530         // Clear out any data in internal vars.
    531         unset( $_POST['filter'] );
    532 
    533         // Edit don't write if we have a post id.
    534         if ( isset( $_POST['post_ID'] ) )
    535                 return edit_post();
    536 
    537         $translated = _wp_translate_postdata( false );
    538         if ( is_wp_error($translated) )
    539                 return $translated;
    540 
    541         if ( isset($_POST['visibility']) ) {
    542                 switch ( $_POST['visibility'] ) {
    543                         case 'public' :
    544                                 $_POST['post_password'] = '';
    545                                 break;
    546                         case 'password' :
    547                                 unset( $_POST['sticky'] );
    548                                 break;
    549                         case 'private' :
    550                                 $_POST['post_status'] = 'private';
    551                                 $_POST['post_password'] = '';
    552                                 unset( $_POST['sticky'] );
    553                                 break;
    554                 }
    555         }
    556 
    557         // Create the post.
    558         $post_ID = wp_insert_post( $_POST );
    559         if ( is_wp_error( $post_ID ) )
    560                 return $post_ID;
    561 
    562         if ( empty($post_ID) )
    563                 return 0;
    564 
    565         add_meta( $post_ID );
    566 
    567         add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    568 
    569         // Now that we have an ID we can fix any attachment anchor hrefs
    570         _fix_attachment_links( $post_ID );
    571 
    572         wp_set_post_lock( $post_ID );
    573 
    574         return $post_ID;
    575 }
    576 
    577 /**
    578  * Calls wp_write_post() and handles the errors.
    579  *
    580  * @since 2.0.0
    581 
    582  * @uses wp_write_post()
    583  * @uses is_wp_error()
    584  * @uses wp_die()
    585  * @return unknown
    586  */
    587 function write_post() {
    588         $result = wp_write_post();
    589         if ( is_wp_error( $result ) )
    590                 wp_die( $result->get_error_message() );
    591         else
    592                 return $result;
    593 }
    594 
    595506//
    596507// Post Meta
    597508//
  • wp-admin/includes/dashboard.php

     
    461461        if ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['action'] ) && 0 === strpos( $_POST['action'], 'post-quickpress' ) && (int) $_POST['post_ID'] ) {
    462462                $view = get_permalink( $_POST['post_ID'] );
    463463                $edit = esc_url( get_edit_post_link( $_POST['post_ID'] ) );
    464                 if ( 'post-quickpress-publish' == $_POST['action'] ) {
     464                if ( ! empty( $_POST['publish'] ) ) {
    465465                        if ( current_user_can('publish_posts') )
    466466                                printf( '<div class="updated"><p>' . __( 'Post published. <a href="%s">View post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( $view ), $edit );
    467467                        else
     
    534534                                <input type="submit" name="publish" id="publish" accesskey="p" class="button-primary" value="<?php current_user_can('publish_posts') ? esc_attr_e('Publish') : esc_attr_e('Submit for Review'); ?>" />
    535535                                <span class="spinner"></span>
    536536                        </span>
    537                         <input type="hidden" name="action" id="quickpost-action" value="post-quickpress-save" />
     537                        <input type="hidden" name="action" id="quickpost-action" value="quickpost" />
    538538                        <input type="hidden" name="post_ID" value="<?php echo $post_ID; ?>" />
    539539                        <input type="hidden" name="post_type" value="post" />
    540                         <?php wp_nonce_field('add-post'); ?>
     540                        <?php wp_nonce_field( 'quick-post_' . $post_ID ); ?>
    541541                        <?php submit_button( __( 'Save Draft' ), 'button', 'save', false, array( 'id' => 'save-post' ) ); ?>
    542542                        <input type="reset" value="<?php esc_attr_e( 'Reset' ); ?>" class="button" />
    543543                        <br class="clear" />
  • wp-admin/includes/deprecated.php

     
    997997 *
    998998 * @since 2.5.0
    999999 * @deprecated 3.5.0
    1000  * @deprecated Use get_default_post_to_edit()
     1000 * @deprecated Use get_default_post_to_edit() 
    10011001 *
    10021002 * @return WP_Post Post object containing all the default post data as attributes
    10031003 */
     
    10091009        return $page;
    10101010}
    10111011
     1012/**
     1013 * Creates a new post.
     1014 *
     1015 * @since 2.1.0
     1016 * @deprecated 3.5.0
     1017 * @deprecated Use wp_insert_post()
     1018 * @see wp_insert_post()
     1019 */
     1020function wp_write_post() {
     1021        _deprecated_function( __FUNCTION__, '3.5', 'wp_insert_post()' );
     1022 
     1023        $ptype = get_post_type_object( isset( $_POST['post_type'] ) ? $_POST['post_type'] : 'post' );
     1024        if ( ! current_user_can( $ptype->cap->edit_posts ) )
     1025                return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
     1026 
     1027        if ( ! isset($_POST['post_ID'] ) )
     1028                $_POST['post_ID'] = get_default_post_to_edit( 'post', true );
     1029        return edit_post();
     1030}
     1031 
     1032/**
     1033 * Calls wp_write_post() and handles the errors.
     1034 *
     1035 * @since 2.0.0
     1036 * @deprecated 3.5.0
     1037 * @deprecated Use wp_insert_post()
     1038 * @see wp_insert_post()
     1039 */
     1040function write_post() {
     1041        _deprecated_function( __FUNCTION__, '3.5', 'wp_insert_post()' );
     1042        $result = wp_write_post();
     1043        if ( is_wp_error( $result ) )
     1044                wp_die( $result->get_error_message() );
     1045        return $result;
     1046}
     1047
    10121048/**
    10131049 * This was once used to create a thumbnail from an Image given a maximum side size.
    10141050 *
  • wp-admin/post.php

     
    9696}
    9797
    9898switch($action) {
    99 case 'postajaxpost':
    100 case 'post':
    101 case 'post-quickpress-publish':
    102 case 'post-quickpress-save':
    103         check_admin_referer('add-' . $post_type);
    104 
    105         if ( 'post-quickpress-publish' == $action )
    106                 $_POST['publish'] = 'publish'; // tell write_post() to publish
    107 
    108         if ( 'post-quickpress-publish' == $action || 'post-quickpress-save' == $action ) {
    109                 $_POST['comment_status'] = get_option('default_comment_status');
    110                 $_POST['ping_status'] = get_option('default_ping_status');
    111                 $post_id = edit_post();
    112         } else {
    113                 $post_id = 'postajaxpost' == $action ? edit_post() : write_post();
    114         }
    115 
    116         if ( 0 === strpos( $action, 'post-quickpress' ) ) {
    117                 $_POST['post_ID'] = $post_id;
    118                 // output the quickpress dashboard widget
    119                 require_once(ABSPATH . 'wp-admin/includes/dashboard.php');
    120                 wp_dashboard_quick_press();
    121                 exit;
    122         }
    123 
    124         redirect_post($post_id);
    125         exit();
    126         break;
    127 
    12899case 'edit':
    129100        $editing = true;
    130101
     
    184155
    185156        break;
    186157
    187 case 'editattachment':
    188         check_admin_referer('update-post_' . $post_id);
     158case 'quickpost':
     159        check_admin_referer( 'quick-post_' . $post_id );
    189160
    190         // Don't let these be changed
    191         unset($_POST['guid']);
    192         $_POST['post_type'] = 'attachment';
     161        $_POST['post_ID'] = edit_post();
     162        // output the quickpress dashboard widget
     163        require_once(ABSPATH . 'wp-admin/includes/dashboard.php');
     164        wp_dashboard_quick_press();
    193165
    194         // Update the thumbnail filename
    195         $newmeta = wp_get_attachment_metadata( $post_id, true );
    196         $newmeta['thumb'] = $_POST['thumb'];
     166        exit;
     167        break;
    197168
    198         wp_update_attachment_metadata( $post_id, $newmeta );
    199 
    200169case 'editpost':
    201170        check_admin_referer('update-post_' . $post_id);
    202171
    203172        $post_id = edit_post();
    204173
    205         redirect_post($post_id); // Send user on their way while we keep working
     174        redirect_post($post_id);
    206175
    207176        exit();
    208177        break;