WordPress.org

Make WordPress Core

Ticket #21963: 21963.diff

File 21963.diff, 15.7 KB (added by nacin, 6 years ago)
  • wp-includes/post.php

     
    25532553                'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    25542554                'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    25552555                'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    2556                 'post_content' => '', 'post_title' => '');
     2556                'post_content' => '', 'post_title' => '', 'context' => '');
    25572557
    25582558        $postarr = wp_parse_args($postarr, $defaults);
    25592559
     
    25882588        if ( empty($post_status) )
    25892589                $post_status = 'draft';
    25902590
     2591        if ( $post_type == 'attachment' && ! in_array( $post_status, array( 'inherit', 'private', 'publish' ) ) )
     2592                $post_status = 'inherit';
     2593
    25912594        if ( !empty($post_category) )
    25922595                $post_category = array_filter($post_category); // Filter out empty terms
    25932596
     
    27882791                update_post_meta($post_ID, '_wp_page_template',  $page_template);
    27892792        }
    27902793
     2794        if ( 'attachment' == $data['post_type'] ) {
     2795                if ( ! empty( $context ) )
     2796                        add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
     2797                if ( ! empty( $file ) )
     2798                        update_attached_file( $post_ID, $file );
     2799        }
     2800
    27912801        wp_transition_post_status($data['post_status'], $previous_status, $post);
    27922802
    27932803        if ( $update ) {
    27942804                do_action('edit_post', $post_ID, $post);
    27952805                $post_after = get_post($post_ID);
    27962806                do_action( 'post_updated', $post_ID, $post_after, $post_before);
     2807                if ( 'attachment' == $post_type )
     2808                        do_action( 'edit_attachment', $post_ID );
     2809        } elseif ( 'attachment' == $post_type ) {
     2810                do_action( 'add_attachment', $post_ID );
    27972811        }
    27982812
    27992813        do_action('save_post', $post_ID, $post);
     
    37063720/**
    37073721 * Insert an attachment.
    37083722 *
    3709  * If you set the 'ID' in the $object parameter, it will mean that you are
    3710  * updating and attempt to update the attachment. You can also set the
    3711  * attachment name or title by setting the key 'post_name' or 'post_title'.
    3712  *
    3713  * You can set the dates for the attachment manually by setting the 'post_date'
    3714  * and 'post_date_gmt' keys' values.
    3715  *
    3716  * By default, the comments will use the default settings for whether the
    3717  * comments are allowed. You can close them manually or keep them open by
    3718  * setting the value for the 'comment_status' key.
    3719  *
    3720  * The $object parameter can have the following:
    3721  *     'post_status'   - Default is 'draft'. Can not be overridden, set the same as parent post.
    3722  *     'post_type'     - Default is 'post', will be set to attachment. Can not override.
    3723  *     'post_author'   - Default is current user ID. The ID of the user, who added the attachment.
    3724  *     'ping_status'   - Default is the value in default ping status option. Whether the attachment
    3725  *                       can accept pings.
    3726  *     'post_parent'   - Default is 0. Can use $parent parameter or set this for the post it belongs
    3727  *                       to, if any.
    3728  *     'menu_order'    - Default is 0. The order it is displayed.
    3729  *     'to_ping'       - Whether to ping.
    3730  *     'pinged'        - Default is empty string.
    3731  *     'post_password' - Default is empty string. The password to access the attachment.
    3732  *     'guid'          - Global Unique ID for referencing the attachment.
    3733  *     'post_content_filtered' - Attachment post content filtered.
    3734  *     'post_excerpt'  - Attachment excerpt.
    3735  *
    37363723 * @since 2.0.0
    3737  * @uses $wpdb
    3738  * @uses $user_ID
    3739  * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update.
    3740  * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update.
     3724 * @uses wp_insert_post()
    37413725 *
    3742  * @param string|array $object Arguments to override defaults.
     3726 * @param array $postarr Arguments to override defaults.
    37433727 * @param string $file Optional filename.
    37443728 * @param int $parent Parent post ID.
    37453729 * @return int Attachment ID.
    37463730 */
    3747 function wp_insert_attachment($object, $file = false, $parent = 0) {
    3748         global $wpdb, $user_ID;
     3731function wp_insert_attachment( $postarr, $file = false, $parent = 0 ) {
     3732        if ( $parent )
     3733                $postarr['post_parent'] = $parent;
    37493734
    3750         $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_ID,
    3751                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    3752                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    3753                 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => '');
    3754 
    3755         $object = wp_parse_args($object, $defaults);
    3756         if ( !empty($parent) )
    3757                 $object['post_parent'] = $parent;
    3758 
    3759         unset( $object[ 'filter' ] );
    3760 
    3761         $object = sanitize_post($object, 'db');
    3762 
    3763         // export array as variables
    3764         extract($object, EXTR_SKIP);
    3765 
    3766         if ( empty($post_author) )
    3767                 $post_author = $user_ID;
    3768 
    3769         $post_type = 'attachment';
    3770 
    3771         if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
    3772                 $post_status = 'inherit';
    3773 
    3774         if ( !empty($post_category) )
    3775                 $post_category = array_filter($post_category); // Filter out empty terms
    3776 
    3777         // Make sure we set a valid category.
    3778         if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
    3779                 $post_category = array();
    3780         }
    3781 
    3782         // Are we updating or creating?
    3783         if ( !empty($ID) ) {
    3784                 $update = true;
    3785                 $post_ID = (int) $ID;
    3786         } else {
    3787                 $update = false;
    3788                 $post_ID = 0;
    3789         }
    3790 
    3791         // Create a valid post name.
    3792         if ( empty($post_name) )
    3793                 $post_name = sanitize_title($post_title);
    3794         else
    3795                 $post_name = sanitize_title($post_name);
    3796 
    3797         // expected_slashed ($post_name)
    3798         $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    3799 
    3800         if ( empty($post_date) )
    3801                 $post_date = current_time('mysql');
    3802         if ( empty($post_date_gmt) )
    3803                 $post_date_gmt = current_time('mysql', 1);
    3804 
    3805         if ( empty($post_modified) )
    3806                 $post_modified = $post_date;
    3807         if ( empty($post_modified_gmt) )
    3808                 $post_modified_gmt = $post_date_gmt;
    3809 
    3810         if ( empty($comment_status) ) {
    3811                 if ( $update )
    3812                         $comment_status = 'closed';
    3813                 else
    3814                         $comment_status = get_option('default_comment_status');
    3815         }
    3816         if ( empty($ping_status) )
    3817                 $ping_status = get_option('default_ping_status');
    3818 
    3819         if ( isset($to_ping) )
    3820                 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
    3821         else
    3822                 $to_ping = '';
    3823 
    3824         if ( isset($post_parent) )
    3825                 $post_parent = (int) $post_parent;
    3826         else
    3827                 $post_parent = 0;
    3828 
    3829         if ( isset($menu_order) )
    3830                 $menu_order = (int) $menu_order;
    3831         else
    3832                 $menu_order = 0;
    3833 
    3834         if ( !isset($post_password) )
    3835                 $post_password = '';
    3836 
    3837         if ( ! isset($pinged) )
    3838                 $pinged = '';
    3839 
    3840         // expected_slashed (everything!)
    3841         $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' ) );
    3842         $data = stripslashes_deep( $data );
    3843 
    3844         if ( $update ) {
    3845                 $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
    3846         } else {
    3847                 // If there is a suggested ID, use it if not already present
    3848                 if ( !empty($import_id) ) {
    3849                         $import_id = (int) $import_id;
    3850                         if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
    3851                                 $data['ID'] = $import_id;
    3852                         }
    3853                 }
    3854 
    3855                 $wpdb->insert( $wpdb->posts, $data );
    3856                 $post_ID = (int) $wpdb->insert_id;
    3857         }
    3858 
    3859         if ( empty($post_name) ) {
    3860                 $post_name = sanitize_title($post_title, $post_ID);
    3861                 $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
    3862         }
    3863 
    3864         if ( is_object_in_taxonomy($post_type, 'category') )
    3865                 wp_set_post_categories( $post_ID, $post_category );
    3866 
    3867         if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
    3868                 wp_set_post_tags( $post_ID, $tags_input );
    3869 
    3870         // support for all custom taxonomies
    3871         if ( !empty($tax_input) ) {
    3872                 foreach ( $tax_input as $taxonomy => $tags ) {
    3873                         $taxonomy_obj = get_taxonomy($taxonomy);
    3874                         if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
    3875                                 $tags = array_filter($tags);
    3876                         if ( current_user_can($taxonomy_obj->cap->assign_terms) )
    3877                                 wp_set_post_terms( $post_ID, $tags, $taxonomy );
    3878                 }
    3879         }
    3880 
    38813735        if ( $file )
    3882                 update_attached_file( $post_ID, $file );
     3736                $postarr['file'] = $file;
    38833737
    3884         clean_post_cache( $post_ID );
    3885 
    3886         if ( ! empty( $context ) )
    3887                 add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
    3888 
    3889         if ( $update) {
    3890                 do_action('edit_attachment', $post_ID);
    3891         } else {
    3892                 do_action('add_attachment', $post_ID);
    3893         }
    3894 
    3895         return $post_ID;
     3738        return wp_insert_post( $postarr );
    38963739}
    38973740
    38983741/**
  • 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
     
    527527                </div>
    528528
    529529                <p class="submit">
    530                         <input type="hidden" name="action" id="quickpost-action" value="post-quickpress-save" />
     530                        <input type="hidden" name="action" id="quickpost-action" value="quickpost" />
    531531                        <input type="hidden" name="post_ID" value="<?php echo $post_ID; ?>" />
    532532                        <input type="hidden" name="post_type" value="post" />
    533                         <?php wp_nonce_field('add-post'); ?>
     533                        <?php wp_nonce_field( 'quick-post_' . $post_ID ); ?>
    534534                        <?php submit_button( __( 'Save Draft' ), 'button', 'save', false, array( 'id' => 'save-post' ) ); ?>
    535535                        <input type="reset" value="<?php esc_attr_e( 'Reset' ); ?>" class="button" />
    536536                        <span id="publishing-action">
  • 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}
  • 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;