Changeset 28470
- Timestamp:
- 05/17/2014 04:53:44 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/post.php
r28469 r28470 4454 4454 4455 4455 $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id, 4456 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'post_title' => '', 4457 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'post_content' => '', 4458 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => ''); 4456 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 4457 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 4458 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 4459 'post_title' => '', 'post_content' => '', 'context' => ''); 4459 4460 4460 4461 $object = wp_parse_args($object, $defaults); 4461 if ( ! empty($parent) )4462 if ( ! empty( $parent ) ) { 4462 4463 $object['post_parent'] = $parent; 4463 4464 } 4464 4465 unset( $object[ 'filter' ] ); 4465 4466 4466 4467 $object = sanitize_post($object, 'db'); 4467 4468 4468 // export array as variables 4469 extract($object, EXTR_SKIP); 4470 4471 if ( empty($post_author) ) 4472 $post_author = $user_id; 4469 $post_ID = 0; 4470 $update = false; 4471 $guid = $object['guid']; 4472 4473 // Are we updating or creating? 4474 if ( ! empty( $object['ID'] ) ) { 4475 $update = true; 4476 $post_ID = (int) $object['ID']; 4477 4478 // wp_insert_post() checks for the existence of this post.... 4479 } 4473 4480 4474 4481 $post_type = 'attachment'; 4475 4482 4476 if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) 4483 $post_title = $object['post_title']; 4484 $post_content = $object['post_content']; 4485 $post_excerpt = $object['post_excerpt']; 4486 if ( isset( $object['post_name'] ) ) { 4487 $post_name = $object['post_name']; 4488 } 4489 4490 // wp_insert_post() checks $maybe_empty 4491 4492 $post_status = $object['post_status']; 4493 if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) { 4477 4494 $post_status = 'inherit'; 4478 4479 if ( !empty($post_category) ) 4480 $post_category = array_filter($post_category); // Filter out empty terms 4495 } 4496 4497 if ( ! empty( $object['post_category'] ) ) { 4498 $post_category = array_filter( $object['post_category'] ); // Filter out empty terms 4499 } 4481 4500 4482 4501 // Make sure we set a valid category. 4483 if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) { 4502 if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) { 4503 // ironically, the default post_type for this function is 'post' 4504 // as such, this should be probably have the same checks as wp_insert_post(), 4505 // since all 'post's require a category. BUT, you can't override the post_type, because the 4506 // variable is explicitly set. 4484 4507 $post_category = array(); 4485 4508 } 4486 4509 4487 // Are we updating or creating? 4488 if ( !empty($ID) ) { 4489 $update = true; 4490 $post_ID = (int) $ID; 4510 // Create a valid post name. 4511 if ( empty( $post_name ) ) { 4512 $post_name = sanitize_title($post_title); 4491 4513 } else { 4492 $update = false; 4493 $post_ID = 0; 4494 } 4495 4496 // Create a valid post name. 4497 if ( empty($post_name) ) 4498 $post_name = sanitize_title($post_title); 4499 else 4500 $post_name = sanitize_title($post_name); 4514 // missing check from wp_insert_post on update: 4515 // "On updates, we need to check to see if it's using the old, fixed sanitization context." 4516 $post_name = sanitize_title( $post_name ); 4517 } 4518 4519 if ( isset( $object['post_parent'] ) ) { 4520 $post_parent = (int) $object['post_parent']; 4521 } else { 4522 $post_parent = 0; 4523 } 4524 4525 if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) { 4526 $post_date = current_time( 'mysql' ); 4527 } else { 4528 $post_date = $object['post_date']; 4529 } 4530 4531 // wp_insert_post() validates the date here 4532 4533 if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) { 4534 $post_date_gmt = get_gmt_from_date( $post_date ); 4535 } else { 4536 $post_date_gmt = $object['post_date_gmt']; 4537 } 4538 4539 if ( $update || '0000-00-00 00:00:00' == $post_date ) { 4540 $post_modified = current_time( 'mysql' ); 4541 $post_modified_gmt = current_time( 'mysql', 1 ); 4542 } else { 4543 $post_modified = $post_date; 4544 $post_modified_gmt = $post_date_gmt; 4545 } 4546 4547 // wp_insert_post() does "future" checks 4548 4549 if ( empty( $object['comment_status'] ) ) { 4550 if ( $update ) { 4551 $comment_status = 'closed'; 4552 } else { 4553 $comment_status = get_option('default_comment_status'); 4554 } 4555 } else { 4556 $comment_status = $object['comment_status']; 4557 } 4558 4559 // these variables are needed by compact() later 4560 $post_content_filtered = $object['post_content_filtered']; 4561 $post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author']; 4562 $ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status']; 4563 $to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : ''; 4564 $pinged = isset( $object['pinged'] ) ? $object['pinged'] : ''; 4565 $import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0; 4566 4567 if ( isset( $object['menu_order'] ) ) { 4568 $menu_order = (int) $object['menu_order']; 4569 } else { 4570 $menu_order = 0; 4571 } 4572 4573 $post_password = isset( $object['post_password'] ) ? $object['post_password'] : ''; 4574 4575 // skips the 'wp_insert_post_parent' filter, present in wp_insert_post() 4501 4576 4502 4577 // expected_slashed ($post_name) 4503 $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent); 4504 4505 if ( empty($post_date) ) 4506 $post_date = current_time('mysql'); 4507 if ( empty($post_date_gmt) ) 4508 $post_date_gmt = current_time('mysql', 1); 4509 4510 if ( empty($post_modified) ) 4511 $post_modified = $post_date; 4512 if ( empty($post_modified_gmt) ) 4513 $post_modified_gmt = $post_date_gmt; 4514 4515 if ( empty($comment_status) ) { 4516 if ( $update ) 4517 $comment_status = 'closed'; 4518 else 4519 $comment_status = get_option('default_comment_status'); 4520 } 4521 if ( empty($ping_status) ) 4522 $ping_status = get_option('default_ping_status'); 4523 4524 if ( isset($to_ping) ) 4525 $to_ping = preg_replace('|\s+|', "\n", $to_ping); 4526 else 4527 $to_ping = ''; 4528 4529 if ( isset($post_parent) ) 4530 $post_parent = (int) $post_parent; 4531 else 4532 $post_parent = 0; 4533 4534 if ( isset($menu_order) ) 4535 $menu_order = (int) $menu_order; 4536 else 4537 $menu_order = 0; 4538 4539 if ( !isset($post_password) ) 4540 $post_password = ''; 4541 4542 if ( ! isset($pinged) ) 4543 $pinged = ''; 4578 $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); 4579 4580 // don't unslash 4581 $post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : ''; 4544 4582 4545 4583 // expected_slashed (everything!) 4546 $data = compact( array( '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' ));4584 $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' ); 4547 4585 4548 4586 /** … … 4557 4595 $data = apply_filters( 'wp_insert_attachment_data', $data, $object ); 4558 4596 $data = wp_unslash( $data ); 4597 $where = array( 'ID' => $post_ID ); 4559 4598 4560 4599 if ( $update ) { 4561 $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) ); 4600 // skips 'pre_post_update' action 4601 $wpdb->update( $wpdb->posts, $data, $where ); 4562 4602 } else { 4563 4603 // If there is a suggested ID, use it if not already present 4564 if ( ! empty($import_id) ) {4604 if ( ! empty( $import_id ) ) { 4565 4605 $import_id = (int) $import_id; 4566 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {4606 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { 4567 4607 $data['ID'] = $import_id; 4568 4608 } … … 4571 4611 $wpdb->insert( $wpdb->posts, $data ); 4572 4612 $post_ID = (int) $wpdb->insert_id; 4573 } 4574 4575 if ( empty($post_name) ) { 4576 $post_name = sanitize_title($post_title, $post_ID); 4577 $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) ); 4578 } 4579 4580 if ( is_object_in_taxonomy($post_type, 'category') ) 4613 $where = array( 'ID' => $post_ID ); 4614 } 4615 4616 if ( empty( $data['post_name'] ) ) { 4617 $data['post_name'] = sanitize_title( $data['post_title'], $post_ID ); 4618 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); 4619 } 4620 4621 if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) { 4581 4622 wp_set_post_categories( $post_ID, $post_category ); 4582 4583 if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') ) 4584 wp_set_post_tags( $post_ID, $tags_input ); 4585 4586 // support for all custom taxonomies 4587 if ( !empty($tax_input) ) { 4588 foreach ( $tax_input as $taxonomy => $tags ) { 4623 } 4624 4625 if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) { 4626 wp_set_post_tags( $post_ID, $object['tags_input'] ); 4627 } 4628 4629 // new-style support for all custom taxonomies 4630 if ( ! empty( $object['tax_input'] ) ) { 4631 foreach ( $object['tax_input'] as $taxonomy => $tags ) { 4589 4632 $taxonomy_obj = get_taxonomy($taxonomy); 4590 if ( is_array( $tags) )// array = hierarchical, string = non-hierarchical.4633 if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical. 4591 4634 $tags = array_filter($tags); 4592 if ( current_user_can($taxonomy_obj->cap->assign_terms) ) 4635 } 4636 if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { 4593 4637 wp_set_post_terms( $post_ID, $tags, $taxonomy ); 4594 } 4595 } 4596 4597 if ( $file ) 4638 } 4639 } 4640 } 4641 4642 if ( $file ) { 4598 4643 update_attached_file( $post_ID, $file ); 4644 } 4645 4646 // wp_insert_post() fills in guid if it is empty 4599 4647 4600 4648 clean_post_cache( $post_ID ); 4601 4649 4602 if ( ! empty( $context ) ) 4603 add_post_meta( $post_ID, '_wp_attachment_context', $context, true ); 4604 4605 if ( $update) { 4650 if ( ! empty( $object['context'] ) ) { 4651 add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true ); 4652 } 4653 4654 // skips wp_transition_post_status 4655 4656 // the actions completely diverge from wp_insert_post() 4657 4658 if ( $update ) { 4606 4659 /** 4607 4660 * Fires once an existing attachment has been updated.
Note: See TracChangeset
for help on using the changeset viewer.