Make WordPress Core

Changeset 53883


Ignore:
Timestamp:
08/11/2022 07:58:21 PM (4 years ago)
Author:
johnbillion
Message:

Posts, Post Types: Prevent categories from being overwritten when updating a post using wp_insert_post().

This prevents existing category relationships being overridden with the default category when none is provided in the post data.

Props markoheijnen, leewillis77, desrosj

Fixes #19954

Location:
trunk
Files:
2 edited

Legend:

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

    r53878 r53883  
    41434143                // Filter out empty terms.
    41444144                $post_category = array_filter( $postarr['post_category'] );
     4145        } elseif ( $update && ! isset( $postarr['post_category'] ) ) {
     4146                $post_category = $post_before->post_category;
    41454147        }
    41464148
  • trunk/tests/phpunit/tests/post/wpInsertPost.php

    r53789 r53883  
    878878
    879879        /**
     880         * @ticket 19954
     881         */
     882        function test_updating_a_post_should_not_trash_categories() {
     883                // Create a category and attach it to a new post.
     884                $term_id = self::factory()->term->create(
     885                        array(
     886                                'name'     => 'Term',
     887                                'taxonomy' => 'category',
     888                        )
     889                );
     890
     891                $post_id = self::factory()->post->create(
     892                        array(
     893                                'post_type'     => 'post',
     894                                'post_title'    => 'Post with categories',
     895                                'post_status'   => 'publish',
     896                                'post_category' => array( $term_id ),
     897                        )
     898                );
     899
     900                // Validate that the term got assigned.
     901                $assigned_terms = wp_get_object_terms( array( $post_id ), array( 'category' ), array() );
     902                $this->assertCount( 1, $assigned_terms );
     903                $this->assertEquals( $term_id, $assigned_terms[0]->term_id );
     904
     905                // Update the post with no changes.
     906                $post = get_post( $post_id );
     907                wp_insert_post( $post );
     908
     909                // Validate the term is still assigned.
     910                $assigned_terms = wp_get_object_terms( array( $post_id ), array( 'category' ), array() );
     911                $this->assertCount( 1, $assigned_terms );
     912                $this->assertEquals( $term_id, $assigned_terms[0]->term_id );
     913
     914                // Remove the term from the post.
     915                $post->post_category = array();
     916                wp_insert_post( $post );
     917                $assigned_terms = wp_get_object_terms( array( $post_id ), array( 'category' ), array() );
     918
     919                // Validate that the post has had the default category assigned again.
     920                $this->assertCount( 1, $assigned_terms );
     921                $this->assertEquals( get_option( 'default_category' ), $assigned_terms[0]->term_id );
     922        }
     923
     924        /**
    880925         * @ticket 48113
    881926         */
Note: See TracChangeset for help on using the changeset viewer.