Ticket #21963: 21963.7.diff
| File 21963.7.diff, 17.8 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/post.php
2925 2925 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 2926 2926 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 2927 2927 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 2928 'post_content' => '', 'post_title' => '' );2928 'post_content' => '', 'post_title' => '', 'context' => ''); 2929 2929 2930 2930 $postarr = wp_parse_args($postarr, $defaults); 2931 2931 … … 2966 2966 $post_name = $postarr['post_name']; 2967 2967 } 2968 2968 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' ); 2971 2974 2972 2975 /** 2973 2976 * Filter whether the post should be considered "empty". … … 2994 2997 } 2995 2998 2996 2999 $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 } 2997 3003 2998 3004 if ( ! empty( $postarr['post_category'] ) ) { 2999 3005 $post_category = array_filter( $postarr['post_category'] ); // Filter out empty terms … … 3070 3076 $post_modified_gmt = $post_date_gmt; 3071 3077 } 3072 3078 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'; 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 } 3077 3090 } 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';3082 }3083 3091 } 3084 3092 3085 3093 if ( empty( $postarr['comment_status'] ) ) { … … 3133 3141 3134 3142 $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); 3135 3143 3144 // don't unslash 3145 $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; 3146 3136 3147 // 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' ); 3138 3149 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 ); 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 } 3148 3171 $data = wp_unslash( $data ); 3149 3172 $where = array( 'ID' => $post_ID ); 3150 3173 … … 3166 3189 } 3167 3190 } 3168 3191 } else { 3169 if ( isset( $postarr['post_mime_type'] ) ) {3170 $data['post_mime_type'] = wp_unslash( $postarr['post_mime_type'] ); // This isn't in the update3171 }3172 3192 // If there is a suggested ID, use it if not already present 3173 3193 if ( ! empty( $import_id ) ) { 3174 3194 $import_id = (int) $import_id; … … 3189 3209 $where = array( 'ID' => $post_ID ); 3190 3210 } 3191 3211 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);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 ); 3194 3214 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); 3195 3215 } 3196 3216 … … 3215 3235 } 3216 3236 } 3217 3237 3218 $current_guid = get_post_field( 'guid', $post_ID ); 3238 if ( 'attachment' !== $postarr['post_type'] ) { 3239 $current_guid = get_post_field( 'guid', $post_ID ); 3219 3240 3220 // Set GUID 3221 if ( ! $update && '' == $current_guid ) { 3222 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); 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 ( ! empty( $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 } 3223 3253 } 3254 3224 3255 clean_post_cache( $post_ID ); 3225 3256 3226 3257 $post = get_post( $post_ID ); … … 3238 3269 update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); 3239 3270 } 3240 3271 3241 wp_transition_post_status($data['post_status'], $previous_status, $post); 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 { 3242 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 } 3298 3243 3299 if ( $update ) { 3244 3300 /** 3245 3301 * Fires once an existing post has been updated. … … 4447 4503 /** 4448 4504 * Insert an attachment. 4449 4505 * 4450 * If you set the 'ID' in the $ objectparameter, it will mean that you are4506 * If you set the 'ID' in the $args parameter, it will mean that you are 4451 4507 * updating and attempt to update the attachment. You can also set the 4452 4508 * attachment name or title by setting the key 'post_name' or 'post_title'. 4453 4509 * … … 4458 4514 * comments are allowed. You can close them manually or keep them open by 4459 4515 * setting the value for the 'comment_status' key. 4460 4516 * 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 attachment4466 * can accept pings.4467 * 'post_parent' - Default is 0. Can use $parent parameter or set this for the post it belongs4468 * 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 *4477 4517 * @since 2.0.0 4478 * @uses $wpdb4479 4518 * 4480 * @param string|array $object Arguments to override defaults. 4481 * @param string $file Optional filename. 4482 * @param int $parent Parent post ID. 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. 4483 4524 * @return int Attachment ID. 4484 4525 */ 4485 function wp_insert_attachment($object, $file = false, $parent = 0) { 4486 global $wpdb; 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 ); 4487 4532 4488 $ user_id = get_current_user_id();4533 $data['post_type'] = 'attachment'; 4489 4534 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; 4535 return wp_insert_post( $data ); 4715 4536 } 4716 4537 4717 4538 /** … … 5797 5618 update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); 5798 5619 } 5799 5620 } 5621 5622 -
tests/phpunit/tests/post/attachments.php
231 231 $this->assertFalse( empty( $guid ) ); 232 232 } 233 233 234 /** 235 * @ticket 21963 236 */ 237 function test_update_attachment_fields() { 238 $filename = ( DIR_TESTDATA . '/images/test-image.jpg' ); 239 $contents = file_get_contents($filename); 240 241 $upload = wp_upload_bits( basename( $filename ), null, $contents ); 242 $this->assertTrue( empty( $upload['error'] ) ); 243 244 $id = $this->_make_attachment( $upload ); 245 246 $attached_file = get_post_meta( $id, '_wp_attached_file', true ); 247 248 $post = get_post( $id, ARRAY_A ); 249 250 $post['post_title'] = 'title'; 251 $post['post_excerpt'] = 'caption'; 252 $post['post_content'] = 'description'; 253 254 wp_update_post( $post ); 255 256 // Make sure the update didn't remove the attached file. 257 $this->assertEquals( $attached_file, get_post_meta( $id, '_wp_attached_file', true ) ); 258 } 259 234 260 }