Make WordPress Core

Changeset 28601


Ignore:
Timestamp:
05/28/2014 06:13:55 PM (9 years ago)
Author:
nacin
Message:

Revert [28579] and [28582]. see #21963.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r28582 r28601  
    29262926        'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    29272927        'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    2928         'post_content' => '', 'post_title' => '', 'context' => '');
     2928        'post_content' => '', 'post_title' => '');
    29292929
    29302930    $postarr = wp_parse_args($postarr, $defaults);
     
    29672967    }
    29682968
    2969     $maybe_empty = 'attachment' !== $post_type
    2970         && ! $post_content && ! $post_title && ! $post_excerpt
    2971         && post_type_supports( $post_type, 'editor' )
    2972         && post_type_supports( $post_type, 'title' )
    2973         && post_type_supports( $post_type, 'excerpt' );
     2969    $maybe_empty = ! $post_content && ! $post_title && ! $post_excerpt && post_type_supports( $post_type, 'editor' )
     2970        && post_type_supports( $post_type, 'title' ) && post_type_supports( $post_type, 'excerpt' );
    29742971
    29752972    /**
     
    29982995
    29992996    $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
    3000     if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
    3001         $post_status = 'inherit';
    3002     }
    30032997
    30042998    if ( ! empty( $postarr['post_category'] ) ) {
     
    30773071    }
    30783072
    3079     if ( 'attachment' !== $post_type ) {
    3080         if ( 'publish' == $post_status ) {
    3081             $now = gmdate('Y-m-d H:i:59');
    3082             if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
    3083                 $post_status = 'future';
    3084             }
    3085         } elseif( 'future' == $post_status ) {
    3086             $now = gmdate('Y-m-d H:i:59');
    3087             if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
    3088                 $post_status = 'publish';
    3089             }
     3073    if ( 'publish' == $post_status ) {
     3074        $now = gmdate('Y-m-d H:i:59');
     3075        if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
     3076            $post_status = 'future';
     3077        }
     3078    } elseif( 'future' == $post_status ) {
     3079        $now = gmdate('Y-m-d H:i:59');
     3080        if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
     3081            $post_status = 'publish';
    30903082        }
    30913083    }
     
    31423134    $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
    31433135
    3144     // don't unslash
    3145     $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
    3146 
    31473136    // expected_slashed (everything!)
    3148     $data = compact( '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' );
    3149 
    3150     if ( 'attachment' === $post_type ) {
    3151         /**
    3152          * Filter attachment post data before it is updated in or added to the database.
    3153          *
    3154          * @since 3.9.0
    3155          *
    3156          * @param array $data    An array of sanitized attachment post data.
    3157          * @param array $postarr An array of unsanitized attachment post data.
    3158          */
    3159         $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
    3160     } else {
    3161         /**
    3162          * Filter slashed post data just before it is inserted into the database.
    3163          *
    3164          * @since 2.7.0
    3165          *
    3166          * @param array $data    An array of slashed post data.
    3167          * @param array $postarr An array of sanitized, but otherwise unmodified post data.
    3168          */
    3169         $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
    3170     }
     3137    $data = compact( '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' );
     3138
     3139    /**
     3140     * Filter slashed post data just before it is inserted into the database.
     3141     *
     3142     * @since 2.7.0
     3143     *
     3144     * @param array $data    Array of slashed post data.
     3145     * @param array $postarr Array of sanitized, but otherwise unmodified post data.
     3146     */
     3147    $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
    31713148    $data = wp_unslash( $data );
    31723149    $where = array( 'ID' => $post_ID );
     
    31903167        }
    31913168    } else {
     3169        if ( isset( $postarr['post_mime_type'] ) ) {
     3170            $data['post_mime_type'] = wp_unslash( $postarr['post_mime_type'] ); // This isn't in the update
     3171        }
    31923172        // If there is a suggested ID, use it if not already present
    31933173        if ( ! empty( $import_id ) ) {
     
    32103190    }
    32113191
    3212     if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3213         $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
     3192    if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3193        $data['post_name'] = sanitize_title($data['post_title'], $post_ID);
    32143194        $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    32153195    }
     
    32363216    }
    32373217
    3238     if ( 'attachment' !== $postarr['post_type'] ) {
    3239         $current_guid = get_post_field( 'guid', $post_ID );
    3240 
    3241         // Set GUID
    3242         if ( ! $update && '' == $current_guid ) {
    3243             $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
    3244         }
    3245     } else {
    3246         if ( isset( $postarr['file'] ) ) {
    3247             update_attached_file( $post_ID, $postarr['file'] );
    3248         }
    3249 
    3250         if ( ! empty( $postarr['context'] ) ) {
    3251             add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
    3252         }
    3253     }
    3254 
     3218    $current_guid = get_post_field( 'guid', $post_ID );
     3219
     3220    // Set GUID
     3221    if ( ! $update && '' == $current_guid ) {
     3222        $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
     3223    }
    32553224    clean_post_cache( $post_ID );
    32563225
     
    32703239    }
    32713240
    3272     if ( 'attachment' !== $postarr['post_type'] ) {
    3273         wp_transition_post_status( $data['post_status'], $previous_status, $post );
    3274     } else {
    3275         if ( $update ) {
    3276             /**
    3277              * Fires once an existing attachment has been updated.
    3278              *
    3279              * @since 2.0.0
    3280              *
    3281              * @param int $post_ID Attachment ID.
    3282              */
    3283             do_action( 'edit_attachment', $post_ID );
    3284         } else {
    3285 
    3286             /**
    3287              * Fires once an attachment has been added.
    3288              *
    3289              * @since 2.0.0
    3290              *
    3291              * @param int $post_ID Attachment ID.
    3292              */
    3293             do_action( 'add_attachment', $post_ID );
    3294         }
    3295 
    3296         return $post_ID;
    3297     }
     3241    wp_transition_post_status($data['post_status'], $previous_status, $post);
    32983242
    32993243    if ( $update ) {
     
    45044448 * Insert an attachment.
    45054449 *
    4506  * If you set the 'ID' in the $args parameter, it will mean that you are
     4450 * If you set the 'ID' in the $object parameter, it will mean that you are
    45074451 * updating and attempt to update the attachment. You can also set the
    45084452 * attachment name or title by setting the key 'post_name' or 'post_title'.
     
    45154459 * setting the value for the 'comment_status' key.
    45164460 *
     4461 * The $object parameter can have the following:
     4462 *     'post_status'   - Default is 'draft'. Can not be overridden, set the same as parent post.
     4463 *     'post_type'     - Default is 'post', will be set to attachment. Can not override.
     4464 *     'post_author'   - Default is current user ID. The ID of the user, who added the attachment.
     4465 *     'ping_status'   - Default is the value in default ping status option. Whether the attachment
     4466 *                       can accept pings.
     4467 *     'post_parent'   - Default is 0. Can use $parent parameter or set this for the post it belongs
     4468 *                       to, if any.
     4469 *     'menu_order'    - Default is 0. The order it is displayed.
     4470 *     'to_ping'       - Whether to ping.
     4471 *     'pinged'        - Default is empty string.
     4472 *     'post_password' - Default is empty string. The password to access the attachment.
     4473 *     'guid'          - Global Unique ID for referencing the attachment.
     4474 *     'post_content_filtered' - Attachment post content filtered.
     4475 *     'post_excerpt'  - Attachment excerpt.
     4476 *
    45174477 * @since 2.0.0
    4518  *
    4519  * @see wp_insert_post()
    4520  *
    4521  * @param string|array $args   Arguments for inserting an attachment.
    4522  * @param string       $file   Optional. Filename.
    4523  * @param int          $parent Optional. Parent post ID.
     4478 * @uses $wpdb
     4479 *
     4480 * @param string|array $object Arguments to override defaults.
     4481 * @param string $file Optional filename.
     4482 * @param int $parent Parent post ID.
    45244483 * @return int Attachment ID.
    45254484 */
    4526 function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
    4527     $defaults = array(
    4528         'file'        => $file,
    4529         'post_parent' => $parent
    4530     );
    4531     $data = wp_parse_args( $args, $defaults );
    4532 
    4533     $data['post_type'] = 'attachment';
    4534 
    4535     return wp_insert_post( $data );
     4485function wp_insert_attachment($object, $file = false, $parent = 0) {
     4486    global $wpdb;
     4487
     4488    $user_id = get_current_user_id();
     4489
     4490    $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id,
     4491        'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
     4492        'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
     4493        'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
     4494        'post_title' => '', 'post_content' => '', 'context' => '');
     4495
     4496    $object = wp_parse_args($object, $defaults);
     4497    if ( ! empty( $parent ) ) {
     4498        $object['post_parent'] = $parent;
     4499    }
     4500    unset( $object[ 'filter' ] );
     4501
     4502    $object = sanitize_post($object, 'db');
     4503
     4504    $post_ID = 0;
     4505    $update = false;
     4506    $guid = $object['guid'];
     4507
     4508    // Are we updating or creating?
     4509    if ( ! empty( $object['ID'] ) ) {
     4510        $update = true;
     4511        $post_ID = (int) $object['ID'];
     4512
     4513        // wp_insert_post() checks for the existence of this post....
     4514    }
     4515
     4516    $post_type = 'attachment';
     4517
     4518    $post_title = $object['post_title'];
     4519    $post_content = $object['post_content'];
     4520    $post_excerpt = $object['post_excerpt'];
     4521    if ( isset( $object['post_name'] ) ) {
     4522        $post_name = $object['post_name'];
     4523    }
     4524
     4525    // wp_insert_post() checks $maybe_empty
     4526
     4527    $post_status = $object['post_status'];
     4528    if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
     4529        $post_status = 'inherit';
     4530    }
     4531
     4532    if ( ! empty( $object['post_category'] ) ) {
     4533        $post_category = array_filter( $object['post_category'] ); // Filter out empty terms
     4534    }
     4535
     4536    // Make sure we set a valid category.
     4537    if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
     4538        // ironically, the default post_type for this function is 'post'
     4539        // as such, this should be probably have the same checks as wp_insert_post(),
     4540        // since all 'post's require a category. BUT, you can't override the post_type, because the
     4541        // variable is explicitly set.
     4542        $post_category = array();
     4543    }
     4544
     4545    // Create a valid post name.
     4546    if ( empty( $post_name ) ) {
     4547        $post_name = sanitize_title($post_title);
     4548    } else {
     4549        // missing check from wp_insert_post on update:
     4550        // "On updates, we need to check to see if it's using the old, fixed sanitization context."
     4551        $post_name = sanitize_title( $post_name );
     4552    }
     4553
     4554    if ( isset( $object['post_parent'] ) ) {
     4555        $post_parent = (int) $object['post_parent'];
     4556    } else {
     4557        $post_parent = 0;
     4558    }
     4559
     4560    if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) {
     4561        $post_date = current_time( 'mysql' );
     4562    } else {
     4563        $post_date = $object['post_date'];
     4564    }
     4565
     4566    // wp_insert_post() validates the date here
     4567
     4568    if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) {
     4569        $post_date_gmt = get_gmt_from_date( $post_date );
     4570    } else {
     4571        $post_date_gmt = $object['post_date_gmt'];
     4572    }
     4573
     4574    if ( $update || '0000-00-00 00:00:00' == $post_date ) {
     4575        $post_modified     = current_time( 'mysql' );
     4576        $post_modified_gmt = current_time( 'mysql', 1 );
     4577    } else {
     4578        $post_modified     = $post_date;
     4579        $post_modified_gmt = $post_date_gmt;
     4580    }
     4581
     4582    // wp_insert_post() does "future" checks
     4583
     4584    if ( empty( $object['comment_status'] ) ) {
     4585        if ( $update ) {
     4586            $comment_status = 'closed';
     4587        } else {
     4588            $comment_status = get_option('default_comment_status');
     4589        }
     4590    } else {
     4591        $comment_status = $object['comment_status'];
     4592    }
     4593
     4594    // these variables are needed by compact() later
     4595    $post_content_filtered = $object['post_content_filtered'];
     4596    $post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author'];
     4597    $ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status'];
     4598    $to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : '';
     4599    $pinged = isset( $object['pinged'] ) ? $object['pinged'] : '';
     4600    $import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0;
     4601
     4602    if ( isset( $object['menu_order'] ) ) {
     4603        $menu_order = (int) $object['menu_order'];
     4604    } else {
     4605        $menu_order = 0;
     4606    }
     4607
     4608    $post_password = isset( $object['post_password'] ) ? $object['post_password'] : '';
     4609
     4610    // skips the 'wp_insert_post_parent' filter, present in wp_insert_post()
     4611
     4612    // expected_slashed ($post_name)
     4613    $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
     4614
     4615    // don't unslash
     4616    $post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : '';
     4617
     4618    // expected_slashed (everything!)
     4619    $data = compact( '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' );
     4620
     4621    /**
     4622     * Filter attachment post data before it is updated in or added
     4623     * to the database.
     4624     *
     4625     * @since 3.9.0
     4626     *
     4627     * @param array $data   Array of sanitized attachment post data.
     4628     * @param array $object Array of un-sanitized attachment post data.
     4629     */
     4630    $data = apply_filters( 'wp_insert_attachment_data', $data, $object );
     4631    $data = wp_unslash( $data );
     4632    $where = array( 'ID' => $post_ID );
     4633
     4634    if ( $update ) {
     4635        // skips 'pre_post_update' action
     4636        $wpdb->update( $wpdb->posts, $data, $where );
     4637    } else {
     4638        // If there is a suggested ID, use it if not already present
     4639        if ( ! empty( $import_id ) ) {
     4640            $import_id = (int) $import_id;
     4641            if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
     4642                $data['ID'] = $import_id;
     4643            }
     4644        }
     4645
     4646        $wpdb->insert( $wpdb->posts, $data );
     4647        $post_ID = (int) $wpdb->insert_id;
     4648        $where = array( 'ID' => $post_ID );
     4649    }
     4650
     4651    if ( empty( $data['post_name'] ) ) {
     4652        $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
     4653        $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
     4654    }
     4655
     4656    if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) {
     4657        wp_set_post_categories( $post_ID, $post_category );
     4658    }
     4659
     4660    if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) {
     4661        wp_set_post_tags( $post_ID, $object['tags_input'] );
     4662    }
     4663
     4664    // new-style support for all custom taxonomies
     4665    if ( ! empty( $object['tax_input'] ) ) {
     4666        foreach ( $object['tax_input'] as $taxonomy => $tags ) {
     4667            $taxonomy_obj = get_taxonomy($taxonomy);
     4668            if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical.
     4669                $tags = array_filter($tags);
     4670            }
     4671            if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
     4672                wp_set_post_terms( $post_ID, $tags, $taxonomy );
     4673            }
     4674        }
     4675    }
     4676
     4677    if ( $file ) {
     4678        update_attached_file( $post_ID, $file );
     4679    }
     4680
     4681    // wp_insert_post() fills in guid if it is empty
     4682
     4683    clean_post_cache( $post_ID );
     4684
     4685    if ( ! empty( $object['context'] ) ) {
     4686        add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true );
     4687    }
     4688
     4689    // skips wp_transition_post_status
     4690
     4691    // the actions completely diverge from wp_insert_post()
     4692
     4693    if ( $update ) {
     4694        /**
     4695         * Fires once an existing attachment has been updated.
     4696         *
     4697         * @since 2.0.0
     4698         *
     4699         * @param int $post_ID Attachment ID.
     4700         */
     4701        do_action( 'edit_attachment', $post_ID );
     4702    } else {
     4703
     4704        /**
     4705         * Fires once an attachment has been added.
     4706         *
     4707         * @since 2.0.0
     4708         *
     4709         * @param int $post_ID Attachment ID.
     4710         */
     4711        do_action( 'add_attachment', $post_ID );
     4712    }
     4713
     4714    return $post_ID;
    45364715}
    45374716
     
    56195798    }
    56205799}
    5621 
    5622 
Note: See TracChangeset for help on using the changeset viewer.