Reinstate the changes from [28579] with some adjustments:

* Check `! empty( $postarr['file'] )` before calling `update_attached_file()`
* Add a unit test: `test_update_attachment_fields()`
* Run the same logic for empty `guid` for attachments that always ran in `wp_insert_post()`, as per #18310. This fixes a unit test that would have broken when this ticket was marked closed.
* Updated the unit test in `Tests_Media::test_wp_prepare_attachment_for_js()` to account for `url` no longer being empty

Props kovshenin, wonderboymusic.
See #21963.


git-svn-id: https://develop.svn.wordpress.org/trunk@28788 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-06-20 18:26:17 +00:00
parent 05abd7e56f
commit a7c1728f2e
3 changed files with 129 additions and 279 deletions

View File

@ -2927,7 +2927,7 @@ function wp_insert_post( $postarr, $wp_error = false ) {
'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
'post_content' => '', 'post_title' => '');
'post_content' => '', 'post_title' => '', 'context' => '');
$postarr = wp_parse_args($postarr, $defaults);
@ -2968,8 +2968,11 @@ function wp_insert_post( $postarr, $wp_error = false ) {
$post_name = $postarr['post_name'];
}
$maybe_empty = ! $post_content && ! $post_title && ! $post_excerpt && post_type_supports( $post_type, 'editor' )
&& post_type_supports( $post_type, 'title' ) && post_type_supports( $post_type, 'excerpt' );
$maybe_empty = 'attachment' !== $post_type
&& ! $post_content && ! $post_title && ! $post_excerpt
&& post_type_supports( $post_type, 'editor' )
&& post_type_supports( $post_type, 'title' )
&& post_type_supports( $post_type, 'excerpt' );
/**
* Filter whether the post should be considered "empty".
@ -2996,6 +2999,9 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
$post_status = 'inherit';
}
if ( ! empty( $postarr['post_category'] ) ) {
$post_category = array_filter( $postarr['post_category'] ); // Filter out empty terms
@ -3072,15 +3078,17 @@ function wp_insert_post( $postarr, $wp_error = false ) {
$post_modified_gmt = $post_date_gmt;
}
if ( 'publish' == $post_status ) {
$now = gmdate('Y-m-d H:i:59');
if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
$post_status = 'future';
}
} elseif( 'future' == $post_status ) {
$now = gmdate('Y-m-d H:i:59');
if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
$post_status = 'publish';
if ( 'attachment' !== $post_type ) {
if ( 'publish' == $post_status ) {
$now = gmdate('Y-m-d H:i:59');
if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
$post_status = 'future';
}
} elseif( 'future' == $post_status ) {
$now = gmdate('Y-m-d H:i:59');
if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
$post_status = 'publish';
}
}
}
@ -3135,18 +3143,33 @@ function wp_insert_post( $postarr, $wp_error = false ) {
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
// expected_slashed (everything!)
$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' );
// don't unslash
$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
/**
* Filter slashed post data just before it is inserted into the database.
*
* @since 2.7.0
*
* @param array $data Array of slashed post data.
* @param array $postarr Array of sanitized, but otherwise unmodified post data.
*/
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
// expected_slashed (everything!)
$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' );
if ( 'attachment' === $post_type ) {
/**
* Filter attachment post data before it is updated in or added to the database.
*
* @since 3.9.0
*
* @param array $data An array of sanitized attachment post data.
* @param array $postarr An array of unsanitized attachment post data.
*/
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
} else {
/**
* Filter slashed post data just before it is inserted into the database.
*
* @since 2.7.0
*
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
*/
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
}
$data = wp_unslash( $data );
$where = array( 'ID' => $post_ID );
@ -3168,9 +3191,6 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
}
} else {
if ( isset( $postarr['post_mime_type'] ) ) {
$data['post_mime_type'] = wp_unslash( $postarr['post_mime_type'] ); // This isn't in the update
}
// If there is a suggested ID, use it if not already present
if ( ! empty( $import_id ) ) {
$import_id = (int) $import_id;
@ -3191,8 +3211,8 @@ function wp_insert_post( $postarr, $wp_error = false ) {
$where = array( 'ID' => $post_ID );
}
if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
}
@ -3223,6 +3243,17 @@ function wp_insert_post( $postarr, $wp_error = false ) {
if ( ! $update && '' == $current_guid ) {
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
}
if ( 'attachment' === $postarr['post_type'] ) {
if ( ! empty( $postarr['file'] ) ) {
update_attached_file( $post_ID, $postarr['file'] );
}
if ( ! empty( $postarr['context'] ) ) {
add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
}
}
clean_post_cache( $post_ID );
$post = get_post( $post_ID );
@ -3240,7 +3271,32 @@ function wp_insert_post( $postarr, $wp_error = false ) {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
wp_transition_post_status($data['post_status'], $previous_status, $post);
if ( 'attachment' !== $postarr['post_type'] ) {
wp_transition_post_status( $data['post_status'], $previous_status, $post );
} else {
if ( $update ) {
/**
* Fires once an existing attachment has been updated.
*
* @since 2.0.0
*
* @param int $post_ID Attachment ID.
*/
do_action( 'edit_attachment', $post_ID );
} else {
/**
* Fires once an attachment has been added.
*
* @since 2.0.0
*
* @param int $post_ID Attachment ID.
*/
do_action( 'add_attachment', $post_ID );
}
return $post_ID;
}
if ( $update ) {
/**
@ -4449,7 +4505,7 @@ function is_local_attachment($url) {
/**
* Insert an attachment.
*
* If you set the 'ID' in the $object parameter, it will mean that you are
* If you set the 'ID' in the $args parameter, it will mean that you are
* updating and attempt to update the attachment. You can also set the
* attachment name or title by setting the key 'post_name' or 'post_title'.
*
@ -4460,260 +4516,25 @@ function is_local_attachment($url) {
* comments are allowed. You can close them manually or keep them open by
* setting the value for the 'comment_status' key.
*
* The $object parameter can have the following:
* 'post_status' - Default is 'draft'. Can not be overridden, set the same as parent post.
* 'post_type' - Default is 'post', will be set to attachment. Can not override.
* 'post_author' - Default is current user ID. The ID of the user, who added the attachment.
* 'ping_status' - Default is the value in default ping status option. Whether the attachment
* can accept pings.
* 'post_parent' - Default is 0. Can use $parent parameter or set this for the post it belongs
* to, if any.
* 'menu_order' - Default is 0. The order it is displayed.
* 'to_ping' - Whether to ping.
* 'pinged' - Default is empty string.
* 'post_password' - Default is empty string. The password to access the attachment.
* 'guid' - Global Unique ID for referencing the attachment.
* 'post_content_filtered' - Attachment post content filtered.
* 'post_excerpt' - Attachment excerpt.
*
* @since 2.0.0
* @uses $wpdb
*
* @param string|array $object Arguments to override defaults.
* @param string $file Optional filename.
* @param int $parent Parent post ID.
* @see wp_insert_post()
*
* @param string|array $args Arguments for inserting an attachment.
* @param string $file Optional. Filename.
* @param int $parent Optional. Parent post ID.
* @return int Attachment ID.
*/
function wp_insert_attachment($object, $file = false, $parent = 0) {
global $wpdb;
function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
$defaults = array(
'file' => $file,
'post_parent' => $parent
);
$data = wp_parse_args( $args, $defaults );
$user_id = get_current_user_id();
$data['post_type'] = 'attachment';
$defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id,
'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
'post_title' => '', 'post_content' => '', 'context' => '');
$object = wp_parse_args($object, $defaults);
if ( ! empty( $parent ) ) {
$object['post_parent'] = $parent;
}
unset( $object[ 'filter' ] );
$object = sanitize_post($object, 'db');
$post_ID = 0;
$update = false;
$guid = $object['guid'];
// Are we updating or creating?
if ( ! empty( $object['ID'] ) ) {
$update = true;
$post_ID = (int) $object['ID'];
// wp_insert_post() checks for the existence of this post....
}
$post_type = 'attachment';
$post_title = $object['post_title'];
$post_content = $object['post_content'];
$post_excerpt = $object['post_excerpt'];
if ( isset( $object['post_name'] ) ) {
$post_name = $object['post_name'];
}
// wp_insert_post() checks $maybe_empty
$post_status = $object['post_status'];
if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
$post_status = 'inherit';
}
if ( ! empty( $object['post_category'] ) ) {
$post_category = array_filter( $object['post_category'] ); // Filter out empty terms
}
// Make sure we set a valid category.
if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
// ironically, the default post_type for this function is 'post'
// as such, this should be probably have the same checks as wp_insert_post(),
// since all 'post's require a category. BUT, you can't override the post_type, because the
// variable is explicitly set.
$post_category = array();
}
// Create a valid post name.
if ( empty( $post_name ) ) {
$post_name = sanitize_title($post_title);
} else {
// missing check from wp_insert_post on update:
// "On updates, we need to check to see if it's using the old, fixed sanitization context."
$post_name = sanitize_title( $post_name );
}
if ( isset( $object['post_parent'] ) ) {
$post_parent = (int) $object['post_parent'];
} else {
$post_parent = 0;
}
if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) {
$post_date = current_time( 'mysql' );
} else {
$post_date = $object['post_date'];
}
// wp_insert_post() validates the date here
if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) {
$post_date_gmt = get_gmt_from_date( $post_date );
} else {
$post_date_gmt = $object['post_date_gmt'];
}
if ( $update || '0000-00-00 00:00:00' == $post_date ) {
$post_modified = current_time( 'mysql' );
$post_modified_gmt = current_time( 'mysql', 1 );
} else {
$post_modified = $post_date;
$post_modified_gmt = $post_date_gmt;
}
// wp_insert_post() does "future" checks
if ( empty( $object['comment_status'] ) ) {
if ( $update ) {
$comment_status = 'closed';
} else {
$comment_status = get_option('default_comment_status');
}
} else {
$comment_status = $object['comment_status'];
}
// these variables are needed by compact() later
$post_content_filtered = $object['post_content_filtered'];
$post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author'];
$ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status'];
$to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : '';
$pinged = isset( $object['pinged'] ) ? $object['pinged'] : '';
$import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0;
if ( isset( $object['menu_order'] ) ) {
$menu_order = (int) $object['menu_order'];
} else {
$menu_order = 0;
}
$post_password = isset( $object['post_password'] ) ? $object['post_password'] : '';
// skips the 'wp_insert_post_parent' filter, present in wp_insert_post()
// expected_slashed ($post_name)
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
// don't unslash
$post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : '';
// expected_slashed (everything!)
$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' );
/**
* Filter attachment post data before it is updated in or added
* to the database.
*
* @since 3.9.0
*
* @param array $data Array of sanitized attachment post data.
* @param array $object Array of un-sanitized attachment post data.
*/
$data = apply_filters( 'wp_insert_attachment_data', $data, $object );
$data = wp_unslash( $data );
$where = array( 'ID' => $post_ID );
if ( $update ) {
// skips 'pre_post_update' action
$wpdb->update( $wpdb->posts, $data, $where );
} else {
// If there is a suggested ID, use it if not already present
if ( ! empty( $import_id ) ) {
$import_id = (int) $import_id;
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
$data['ID'] = $import_id;
}
}
$wpdb->insert( $wpdb->posts, $data );
$post_ID = (int) $wpdb->insert_id;
$where = array( 'ID' => $post_ID );
}
if ( empty( $data['post_name'] ) ) {
$data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
}
if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) {
wp_set_post_categories( $post_ID, $post_category );
}
if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) {
wp_set_post_tags( $post_ID, $object['tags_input'] );
}
// new-style support for all custom taxonomies
if ( ! empty( $object['tax_input'] ) ) {
foreach ( $object['tax_input'] as $taxonomy => $tags ) {
$taxonomy_obj = get_taxonomy($taxonomy);
if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical.
$tags = array_filter($tags);
}
if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
wp_set_post_terms( $post_ID, $tags, $taxonomy );
}
}
}
if ( $file ) {
update_attached_file( $post_ID, $file );
}
// wp_insert_post() fills in guid if it is empty
clean_post_cache( $post_ID );
if ( ! empty( $object['context'] ) ) {
add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true );
}
// skips wp_transition_post_status
// the actions completely diverge from wp_insert_post()
if ( $update ) {
/**
* Fires once an existing attachment has been updated.
*
* @since 2.0.0
*
* @param int $post_ID Attachment ID.
*/
do_action( 'edit_attachment', $post_ID );
} else {
/**
* Fires once an attachment has been added.
*
* @since 2.0.0
*
* @param int $post_ID Attachment ID.
*/
do_action( 'add_attachment', $post_ID );
}
return $post_ID;
return wp_insert_post( $data );
}
/**
@ -5799,3 +5620,5 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
}
}

View File

@ -148,7 +148,8 @@ CAP;
$this->assertEquals( '', $prepped['mime'] );
$this->assertEquals( '', $prepped['type'] );
$this->assertEquals( '', $prepped['subtype'] );
$this->assertEquals( '', $prepped['url'] );
// #21963, there will be a guid always, so there will be a URL
$this->assertNotEquals( '', $prepped['url'] );
$this->assertEquals( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
// Fake a mime

View File

@ -231,4 +231,30 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
$this->assertFalse( empty( $guid ) );
}
/**
* @ticket 21963
*/
function test_update_attachment_fields() {
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents($filename);
$upload = wp_upload_bits( basename( $filename ), null, $contents );
$this->assertTrue( empty( $upload['error'] ) );
$id = $this->_make_attachment( $upload );
$attached_file = get_post_meta( $id, '_wp_attached_file', true );
$post = get_post( $id, ARRAY_A );
$post['post_title'] = 'title';
$post['post_excerpt'] = 'caption';
$post['post_content'] = 'description';
wp_update_post( $post );
// Make sure the update didn't remove the attached file.
$this->assertEquals( $attached_file, get_post_meta( $id, '_wp_attached_file', true ) );
}
}