Make WordPress Core

Ticket #19373: wp-19373-20130728-refresh.diff

File wp-19373-20130728-refresh.diff, 3.0 KB (added by alexkingorg, 11 years ago)

Refreshed patch against trunk, handles situation where post_author is not set

  • wp-includes/post.php

     
    26162616 *
    26172617 * @param array $postarr Elements that make up post to insert.
    26182618 * @param bool $wp_error Optional. Allow return of WP_Error on failure.
     2619 * @param bool $sanitize Optional. Run "current user" sanitization routines on $postarr.
    26192620 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
    26202621 */
    2621 function wp_insert_post($postarr, $wp_error = false) {
     2622function wp_insert_post($postarr, $wp_error = false, $sanitize = true) {
    26222623        global $wpdb, $user_ID;
    26232624
    26242625        $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
     
    26272628                'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    26282629                'post_content' => '', 'post_title' => '');
    26292630
    2630         $postarr = wp_parse_args($postarr, $defaults);
     2631        $postarr = wp_parse_args( $postarr, $defaults );
    26312632
    26322633        unset( $postarr[ 'filter' ] );
    26332634
    2634         $postarr = sanitize_post($postarr, 'db');
     2635        if ( $sanitize ) {
     2636                $postarr = sanitize_post( $postarr, 'db' );
     2637        }
    26352638
    26362639        // export array as variables
    26372640        extract($postarr, EXTR_SKIP);
     
    26852688                        $post_category = array();
    26862689        }
    26872690
    2688         if ( empty($post_author) )
    2689                 $post_author = $user_ID;
     2691        // if no post author is set, try to use current logged in user or return error
     2692        if ( empty( $post_author ) ) {
     2693                if ( !empty( $user_ID ) ) {
     2694                        $post_author = $user_ID;
     2695                }
     2696                else {
     2697                        if ( $wp_error )
     2698                                return new WP_Error( 'empty_author', __( 'Post author field was not set and there is no current user context to pull the user ID from.' ) );
     2699                        else
     2700                                return 0;
     2701                }
     2702        }
    26902703
    26912704        // Don't allow contributors to set the post slug for pending review posts
    2692         if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )
     2705        if ( 'pending' == $post_status && $sanitize && !current_user_can( 'publish_posts' ) )
    26932706                $post_name = '';
    26942707
    26952708        // Create a valid post name. Drafts and pending posts are allowed to have an empty
     
    27052718                if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $ID ) == $check_name )
    27062719                        $post_name = $check_name;
    27072720                else // new post, or slug has changed.
    2708                         $post_name = sanitize_title($post_name);
     2721                        $post_name = sanitize_title( $post_name );
    27092722        }
    27102723
    27112724        // If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now
     
    28342847        // new-style support for all custom taxonomies
    28352848        if ( !empty($tax_input) ) {
    28362849                foreach ( $tax_input as $taxonomy => $tags ) {
    2837                         $taxonomy_obj = get_taxonomy($taxonomy);
     2850                        $taxonomy_obj = get_taxonomy( $taxonomy );
    28382851                        if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
    28392852                                $tags = array_filter($tags);
    2840                         if ( current_user_can($taxonomy_obj->cap->assign_terms) )
     2853                        if ( !$sanitize || current_user_can($taxonomy_obj->cap->assign_terms) )
    28412854                                wp_set_post_terms( $post_ID, $tags, $taxonomy );
    28422855                }
    28432856        }