Allow `int` to be passed in lieu of `array`, add `append` arg to `wp_set_post_categories()`. Adds more extensive unit tests for `wp_set_post_categories()`.

Props ptahdunbar for initial patch.
Fixes #16550.



git-svn-id: https://develop.svn.wordpress.org/trunk@25234 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-09-04 17:41:03 +00:00
parent 67500203f9
commit 1e0d32406d
2 changed files with 43 additions and 6 deletions

View File

@ -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 );
}
/**

View File

@ -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] );
}
}