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.


Built from https://develop.svn.wordpress.org/trunk@25234


git-svn-id: http://core.svn.wordpress.org/trunk@25204 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-09-04 17:42:10 +00:00
parent 1d79b0bdf3
commit f1a33028f6
1 changed files with 10 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 );
}
/**