Changeset 25234
- Timestamp:
- 09/04/2013 05:41:03 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/post.php
r25182 r25234 3229 3229 * 3230 3230 * @param int $post_ID Post ID. 3231 * @param array $post_categories Optional. List of categories. 3231 * @param array|int $post_categories Optional. List of categories or ID of category. 3232 * @param bool $append If true, don't delete existing categories, just add on. If false, replace the categories with the new categories. 3232 3233 * @return bool|mixed 3233 3234 */ 3234 function wp_set_post_categories( $post_ID = 0, $post_categories = array()) {3235 function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { 3235 3236 $post_ID = (int) $post_ID; 3236 3237 $post_type = get_post_type( $post_ID ); 3237 3238 $post_status = get_post_status( $post_ID ); 3238 3239 // If $post_categories isn't already an array, make it one: 3239 if ( !is_array($post_categories) || empty($post_categories) ) { 3240 if ( 'post' == $post_type && 'auto-draft' != $post_status ) 3240 $post_categories = (array) $post_categories; 3241 if ( empty( $post_categories ) ) { 3242 if ( 'post' == $post_type && 'auto-draft' != $post_status ) { 3241 3243 $post_categories = array( get_option('default_category') ); 3242 else 3244 $append = false; 3245 } else { 3243 3246 $post_categories = array(); 3247 } 3244 3248 } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) { 3245 3249 return true; 3246 3250 } 3247 3251 3248 return wp_set_post_terms( $post_ID, $post_categories, 'category');3252 return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); 3249 3253 } 3250 3254 -
trunk/tests/phpunit/tests/term.php
r25106 r25234 467 467 $this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); 468 468 } 469 470 function test_wp_set_post_categories() { 471 $post_id = $this->factory->post->create(); 472 $post = get_post( $post_id ); 473 474 $this->assertInternalType( 'array', $post->post_category ); 475 $this->assertEquals( 1, count( $post->post_category ) ); 476 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); 477 $term1 = wp_insert_term( 'Foo', 'category' ); 478 $term2 = wp_insert_term( 'Bar', 'category' ); 479 $term3 = wp_insert_term( 'Baz', 'category' ); 480 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) ); 481 $this->assertEquals( 2, count( $post->post_category ) ); 482 $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category ); 483 484 wp_set_post_categories( $post_id, $term3['term_id'], true ); 485 $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category ); 486 487 $term4 = wp_insert_term( 'Burrito', 'category' ); 488 wp_set_post_categories( $post_id, $term4['term_id'] ); 489 $this->assertEquals( array( $term4['term_id'] ), $post->post_category ); 490 491 wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true ); 492 $this->assertEquals( array( $term2['term_id'], $term4['term_id'],$term1['term_id'] ), $post->post_category ); 493 494 wp_set_post_categories( $post_id, array(), true ); 495 $this->assertEquals( 1, count( $post->post_category ) ); 496 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); 497 498 wp_set_post_categories( $post_id, array() ); 499 $this->assertEquals( 1, count( $post->post_category ) ); 500 $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); 501 } 469 502 }
Note: See TracChangeset
for help on using the changeset viewer.