diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index c2b60a83e9..87aaeef190 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3228,24 +3228,28 @@ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $a * @since 2.1.0 * * @param int $post_ID Post ID. - * @param array $post_categories Optional. List of categories. + * @param array|int $post_categories Optional. List of categories or ID of category. + * @param bool $append If true, don't delete existing categories, just add on. If false, replace the categories with the new categories. * @return bool|mixed */ -function wp_set_post_categories($post_ID = 0, $post_categories = array()) { +function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { $post_ID = (int) $post_ID; $post_type = get_post_type( $post_ID ); $post_status = get_post_status( $post_ID ); // If $post_categories isn't already an array, make it one: - if ( !is_array($post_categories) || empty($post_categories) ) { - if ( 'post' == $post_type && 'auto-draft' != $post_status ) + $post_categories = (array) $post_categories; + if ( empty( $post_categories ) ) { + if ( 'post' == $post_type && 'auto-draft' != $post_status ) { $post_categories = array( get_option('default_category') ); - else + $append = false; + } else { $post_categories = array(); + } } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) { return true; } - return wp_set_post_terms($post_ID, $post_categories, 'category'); + return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); } /** diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index e32894fbe1..ff3fd8a541 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -466,4 +466,37 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( $tag_id, $terms[0]->term_id ); $this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); } + + function test_wp_set_post_categories() { + $post_id = $this->factory->post->create(); + $post = get_post( $post_id ); + + $this->assertInternalType( 'array', $post->post_category ); + $this->assertEquals( 1, count( $post->post_category ) ); + $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); + $term1 = wp_insert_term( 'Foo', 'category' ); + $term2 = wp_insert_term( 'Bar', 'category' ); + $term3 = wp_insert_term( 'Baz', 'category' ); + wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) ); + $this->assertEquals( 2, count( $post->post_category ) ); + $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category ); + + wp_set_post_categories( $post_id, $term3['term_id'], true ); + $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category ); + + $term4 = wp_insert_term( 'Burrito', 'category' ); + wp_set_post_categories( $post_id, $term4['term_id'] ); + $this->assertEquals( array( $term4['term_id'] ), $post->post_category ); + + wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true ); + $this->assertEquals( array( $term2['term_id'], $term4['term_id'],$term1['term_id'] ), $post->post_category ); + + wp_set_post_categories( $post_id, array(), true ); + $this->assertEquals( 1, count( $post->post_category ) ); + $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); + + wp_set_post_categories( $post_id, array() ); + $this->assertEquals( 1, count( $post->post_category ) ); + $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); + } }