Ticket #19954: 19954.diff
File 19954.diff, 3.7 KB (added by , 5 years ago) |
---|
-
src/wp-includes/post.php
3031 3031 * @type string $post_mime_type The mime type of the post. Default empty. 3032 3032 * @type string $guid Global Unique ID for referencing the post. Default empty. 3033 3033 * @type array $post_category Array of category names, slugs, or IDs. 3034 * Defaults to value of the 'default_category' option. 3034 * Defaults to value of the 'default_category' option if inserting a new 3035 * post. 3035 3036 * @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty. 3036 3037 * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. 3037 3038 * } … … 3140 3141 $post_status = 'inherit'; 3141 3142 } 3142 3143 3144 // Take note of whether updates to post categories were requested. 3145 $update_post_categories = isset( $postarr['post_category'] ); 3146 3143 3147 if ( ! empty( $postarr['post_category'] ) ) { 3144 3148 // Filter out empty terms. 3145 3149 $post_category = array_filter( $postarr['post_category'] ); … … 3400 3404 clean_post_cache( $post_ID ); 3401 3405 } 3402 3406 3407 /** 3408 * Add/update categories if: 3409 * - we're inserting a new post 3410 * - we're updating a post and post_categories were specifically requested 3411 */ 3403 3412 if ( is_object_in_taxonomy( $post_type, 'category' ) ) { 3404 wp_set_post_categories( $post_ID, $post_category ); 3413 if ( ! $update || ( $update && $update_post_categories ) ) { 3414 wp_set_post_categories( $post_ID, $post_category ); 3415 } 3405 3416 } 3406 3417 3407 3418 if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { -
tests/phpunit/tests/post/wpInsertPost.php
89 89 $this->assertEquals( 'about', get_post( $another_about_page_id )->post_name ); 90 90 $this->assertEquals( 'about-2', get_post( $about_page_id )->post_name ); 91 91 } 92 93 function test_updating_a_post_should_not_trash_categories() { 94 // Create a post, and a term, and attach them. 95 $post_id = self::factory()->post->create( array( 96 'post_type' => 'post', 97 'post_title' => 'Post with categories', 98 'post_status' => 'publish' 99 ) ); 100 $term_id = self::factory()->term->create( array( 101 'name' => 'Term', 102 'taxonomy' => 'category' 103 ) ); 104 wp_set_object_terms( $post_id, array( $term_id ), 'category' ); 105 106 // Validate that the term got assigned. 107 $assigned_terms = wp_get_object_terms( array( $post_id ), array( 'category' ), array() ); 108 $this->assertCount( 1, $assigned_terms ); 109 $this->assertEquals( $term_id, $assigned_terms[0]->term_id ); 110 111 // Update the post with no changes. 112 $post = get_post( $post_id ); 113 wp_insert_post( $post ); 114 115 // Validate that the term is still assigned. 116 $assigned_terms = wp_get_object_terms( array( $post_id ), array( 'category' ), array() ); 117 $this->assertCount( 1, $assigned_terms ); 118 $this->assertEquals( $term_id, $assigned_terms[0]->term_id ); 119 120 // Remove the term from the post. 121 $post->post_category = array(); 122 wp_insert_post($post); 123 $assigned_terms = wp_get_object_terms( array( $post_id ), array( 'category' ), array() ); 124 125 // Validate that the post has had the default category assigned again. 126 $this->assertCount( 1, $assigned_terms ); 127 $this->assertEquals( get_option( 'default_category' ), $assigned_terms[0]->term_id ); 128 } 92 129 }