diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
index 47633d1..dbaee41 100644
|
|
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
2485 | 2485 | if ( !strlen(trim($term)) ) |
2486 | 2486 | continue; |
2487 | 2487 | |
2488 | | if ( !$term_info = term_exists($term, $taxonomy) ) { |
2489 | | // Skip if a non-existent term ID is passed. |
2490 | | if ( is_int($term) ) |
| 2488 | if ( is_int( $term ) ) { |
| 2489 | if ( ! $term_info = term_exists( $term, $taxonomy ) ) { |
2491 | 2490 | continue; |
2492 | | $term_info = wp_insert_term($term, $taxonomy); |
| 2491 | } |
| 2492 | } else { |
| 2493 | // Give preference to slug. |
| 2494 | $found_term = get_term_by( 'slug', $term, $taxonomy ); |
| 2495 | print_r( $found_term ); |
| 2496 | if ( ! $found_term ) { |
| 2497 | $found_term = get_term_by( 'name', $term, $taxonomy ); |
| 2498 | } |
| 2499 | |
| 2500 | if ( $found_term ) { |
| 2501 | // For legacy purposes, these values should be numeric strings. |
| 2502 | $term_info = array( |
| 2503 | 'term_id' => (string) $found_term->term_id, |
| 2504 | 'term_taxonomy_id' => (string) $found_term->term_taxonomy_id, |
| 2505 | ); |
| 2506 | } else { |
| 2507 | $term_info = wp_insert_term( $term, $taxonomy ); |
| 2508 | } |
2493 | 2509 | } |
| 2510 | |
2494 | 2511 | if ( is_wp_error($term_info) ) |
2495 | 2512 | return $term_info; |
2496 | 2513 | $term_ids[] = $term_info['term_id']; |
diff --git tests/phpunit/tests/term/wpSetObjectTerms.php tests/phpunit/tests/term/wpSetObjectTerms.php
index 2dde5fd..ba6a329 100644
|
|
class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { |
318 | 318 | $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' ); |
319 | 319 | |
320 | 320 | $this->assertNotEmpty( $tt_ids ); |
321 | | $term = get_term( $tt_ids[0] ); |
| 321 | $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] ); |
322 | 322 | $this->assertInstanceOf( 'WP_Term', $term ); |
323 | 323 | $this->assertSame( 'foo', $term->slug ); |
324 | 324 | } |
… |
… |
class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { |
335 | 335 | $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' ); |
336 | 336 | |
337 | 337 | $this->assertNotEmpty( $tt_ids ); |
338 | | $term = get_term( $tt_ids[0] ); |
| 338 | $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] ); |
339 | 339 | $this->assertInstanceOf( 'WP_Term', $term ); |
340 | 340 | $this->assertSame( $t, $term->term_id ); |
341 | 341 | } |
… |
… |
class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { |
352 | 352 | $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' ); |
353 | 353 | |
354 | 354 | $this->assertNotEmpty( $tt_ids ); |
355 | | $term = get_term( $tt_ids[0] ); |
| 355 | $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] ); |
356 | 356 | $this->assertInstanceOf( 'WP_Term', $term ); |
357 | 357 | $this->assertSame( $t, $term->term_id ); |
358 | 358 | } |
… |
… |
class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { |
375 | 375 | $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' ); |
376 | 376 | |
377 | 377 | $this->assertNotEmpty( $tt_ids ); |
378 | | $term = get_term( $tt_ids[0] ); |
| 378 | $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] ); |
379 | 379 | $this->assertInstanceOf( 'WP_Term', $term ); |
380 | 380 | $this->assertSame( $t2, $term->term_id ); |
381 | 381 | } |
… |
… |
class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { |
387 | 387 | |
388 | 388 | $this->assertSame( array(), $tt_ids ); |
389 | 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @ticket 37009 |
| 393 | * @group bbg |
| 394 | */ |
| 395 | public function test_slug_of_newly_created_term_should_be_suffixed_when_matching_slug_of_existing_term() { |
| 396 | register_taxonomy( 'wptests_tax', 'post' ); |
| 397 | |
| 398 | $t = self::factory()->term->create( array( |
| 399 | 'taxonomy' => 'wptests_tax', |
| 400 | 'name' => '$foo', |
| 401 | 'slug' => 'foo', |
| 402 | ) ); |
| 403 | |
| 404 | $tt_ids = wp_set_object_terms( self::$post_ids[0], array( '$foo', '#foo' ), 'wptests_tax' ); |
| 405 | |
| 406 | print_r( $tt_ids ); |
| 407 | } |
390 | 408 | } |