Make WordPress Core


Ignore:
Timestamp:
05/26/2014 11:19:03 PM (10 years ago)
Author:
wonderboymusic
Message:

Combine wp_insert_attachment() and wp_insert_post(). wp_insert_attachment() becomes a wrapper. Update inline docs.

Props wonderboymusic, DrewAPicture.
See #21963.

File:
1 edited

Legend:

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

    r28553 r28579  
    29262926        'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    29272927        'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    2928         'post_content' => '', 'post_title' => '');
     2928        'post_content' => '', 'post_title' => '', 'context' => '');
    29292929
    29302930    $postarr = wp_parse_args($postarr, $defaults);
     
    29672967    }
    29682968
    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' );
     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' );
    29712974
    29722975    /**
     
    29952998
    29962999    $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    }
    29973003
    29983004    if ( ! empty( $postarr['post_category'] ) ) {
     
    30713077    }
    30723078
    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';
     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            }
    30823090        }
    30833091    }
     
    31343142    $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
    31353143
     3144    // don't unslash
     3145    $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
     3146
    31363147    // expected_slashed (everything!)
    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' );
     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' );
    31383149
    31393150    /**
     
    31453156     * @param array $postarr Array of sanitized, but otherwise unmodified post data.
    31463157     */
    3147     $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3158    if ( 'attachment' === $post_type ) {
     3159        $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
     3160    } else {
     3161        $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3162    }
    31483163    $data = wp_unslash( $data );
    31493164    $where = array( 'ID' => $post_ID );
     
    31673182        }
    31683183    } 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         }
    31723184        // If there is a suggested ID, use it if not already present
    31733185        if ( ! empty( $import_id ) ) {
     
    31903202    }
    31913203
    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);
     3204    if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3205        $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
    31943206        $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    31953207    }
     
    32163228    }
    32173229
    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     }
     3230    if ( 'attachment' !== $postarr['post_type'] ) {
     3231        $current_guid = get_post_field( 'guid', $post_ID );
     3232
     3233        // Set GUID
     3234        if ( ! $update && '' == $current_guid ) {
     3235            $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
     3236        }
     3237    } else {
     3238        if ( isset( $postarr['file'] ) ) {
     3239            update_attached_file( $post_ID, $postarr['file'] );
     3240        }
     3241
     3242        if ( ! empty( $postarr['context'] ) ) {
     3243            add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
     3244        }
     3245    }
     3246
    32243247    clean_post_cache( $post_ID );
    32253248
     
    32393262    }
    32403263
    3241     wp_transition_post_status($data['post_status'], $previous_status, $post);
     3264    if ( 'attachment' !== $postarr['post_type'] ) {
     3265        wp_transition_post_status( $data['post_status'], $previous_status, $post );
     3266    } else {
     3267        if ( $update ) {
     3268            /**
     3269             * Fires once an existing attachment has been updated.
     3270             *
     3271             * @since 2.0.0
     3272             *
     3273             * @param int $post_ID Attachment ID.
     3274             */
     3275            do_action( 'edit_attachment', $post_ID );
     3276        } else {
     3277
     3278            /**
     3279             * Fires once an attachment has been added.
     3280             *
     3281             * @since 2.0.0
     3282             *
     3283             * @param int $post_ID Attachment ID.
     3284             */
     3285            do_action( 'add_attachment', $post_ID );
     3286        }
     3287
     3288        return $post_ID;
     3289    }
    32423290
    32433291    if ( $update ) {
     
    44484496 * Insert an attachment.
    44494497 *
    4450  * If you set the 'ID' in the $object parameter, it will mean that you are
     4498 * If you set the 'ID' in the $args parameter, it will mean that you are
    44514499 * updating and attempt to update the attachment. You can also set the
    44524500 * attachment name or title by setting the key 'post_name' or 'post_title'.
     
    44594507 * setting the value for the 'comment_status' key.
    44604508 *
    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  *
    44774509 * @since 2.0.0
    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.
     4510 *
     4511 * @see wp_insert_post()
     4512 *
     4513 * @param string|array $args   Arguments for inserting an attachment.
     4514 * @param string       $file   Optional. Filename.
     4515 * @param int          $parent Optional. Parent post ID.
    44834516 * @return int Attachment ID.
    44844517 */
    4485 function 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;
     4518function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
     4519    $defaults = array(
     4520        'file'        => $file,
     4521        'post_parent' => $parent
     4522    );
     4523    $data = wp_parse_args( $args, $defaults );
     4524
     4525    $data['post_type'] = 'attachment';
     4526
     4527    return wp_insert_post( $data );
    47154528}
    47164529
     
    57985611    }
    57995612}
     5613
     5614
Note: See TracChangeset for help on using the changeset viewer.