Ticket #30780: 30780.2.patch
File 30780.2.patch, 6.4 KB (added by , 10 years ago) |
---|
-
src/wp-includes/taxonomy.php
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php index 1d93f69..a0d8ee3 100644
function wp_update_term( $term_id, $taxonomy, $args = array() ) { 3352 3352 */ 3353 3353 $parent = apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args ); 3354 3354 3355 // Check for duplicate slug 3356 $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); 3357 if ( $id && ($id != $term_id) ) { 3358 // If an empty slug was passed or the parent changed, reset the slug to something unique. 3359 // Otherwise, bail. 3360 if ( $empty_slug || ( $parent != $term['parent']) ) 3361 $slug = wp_unique_term_slug($slug, (object) $args); 3362 else 3363 return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); 3355 $parsed_args['parent'] = $parent; 3356 3357 // Ensure a unique slug. 3358 $unique_slug = wp_unique_term_slug( $slug, (object) $parsed_args ); 3359 3360 /* 3361 * If the unique slug is different from the 'slug' param, bail with an error. Exception: A term with a changed 3362 * parent may have had its slug updated to match the new parent, which should not throw an error. 3363 */ 3364 if ( ! $empty_slug && $slug !== $unique_slug && ( ! is_taxonomy_hierarchical( $taxonomy ) || empty( $parent ) ) ) { 3365 return new WP_Error( 'duplicate_term_slug', sprintf( __( 'The slug “%s” is already in use by another term' ), $slug ) ); 3366 } 3367 3368 $slug = $unique_slug; 3369 3370 // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy. 3371 $exists = get_term_by( 'name', $name, $taxonomy ); 3372 if ( $exists && $exists->term_id !== $term_id && $name === $exists->name ) { 3373 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 3374 $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) ); 3375 if ( in_array( $name, $siblings ) ) { 3376 return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists with this parent.' ), $exists->term_id ); 3377 } 3378 3379 } else { 3380 return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists in this taxonomy.' ), $exists->term_id ); 3381 } 3364 3382 } 3365 3383 3366 3384 $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); -
tests/phpunit/tests/term.php
diff --git tests/phpunit/tests/term.php tests/phpunit/tests/term.php index 2cf5fb2..fc59aa4 100644
class Tests_Term extends WP_UnitTestCase { 640 640 /** 641 641 * @ticket 5809 642 642 */ 643 public function test_wp_update_term_ duplicate_slug_same_taxonomy() {643 public function test_wp_update_term_should_not_create_duplicate_slugs_within_the_same_taxonomy() { 644 644 register_taxonomy( 'wptests_tax', 'post' ); 645 645 646 646 $t1 = $this->factory->term->create( array( … … class Tests_Term extends WP_UnitTestCase { 650 650 ) ); 651 651 652 652 $t2 = $this->factory->term->create( array( 653 'name' => ' Foo',653 'name' => 'Bar', 654 654 'slug' => 'bar', 655 655 'taxonomy' => 'wptests_tax', 656 656 ) ); … … class Tests_Term extends WP_UnitTestCase { 666 666 /** 667 667 * @ticket 5809 668 668 */ 669 public function test_wp_update_term_ duplicate_slug_different_taxonomy() {669 public function test_wp_update_term_should_allow_duplicate_slugs_in_different_taxonomy() { 670 670 register_taxonomy( 'wptests_tax', 'post' ); 671 671 register_taxonomy( 'wptests_tax_2', 'post' ); 672 672 … … class Tests_Term extends WP_UnitTestCase { 686 686 'slug' => 'foo', 687 687 ) ); 688 688 689 $this->assertFalse( is_wp_error( $updated ) ); 690 691 $t1_term = get_term( $t1, 'wptests_tax' ); 692 $t2_term = get_term( $t2, 'wptests_tax_2' ); 693 $this->assertSame( $t1_term->slug, $t2_term->slug ); 694 } 695 696 /** 697 * @ticket 30780 698 */ 699 public function test_wp_update_term_should_allow_duplicate_names_in_different_taxonomies() { 700 register_taxonomy( 'wptests_tax', 'post' ); 701 register_taxonomy( 'wptests_tax_2', 'post' ); 702 703 $t1 = $this->factory->term->create( array( 704 'name' => 'Foo', 705 'slug' => 'foo', 706 'taxonomy' => 'wptests_tax', 707 ) ); 708 709 $t2 = $this->factory->term->create( array( 710 'name' => 'Bar', 711 'slug' => 'bar', 712 'taxonomy' => 'wptests_tax_2', 713 ) ); 714 715 $updated = wp_update_term( $t2, 'wptests_tax_2', array( 716 'name' => 'Foo', 717 ) ); 718 719 $this->assertFalse( is_wp_error( $updated ) ); 720 721 $t2_term = get_term( $t2, 'wptests_tax_2' ); 722 $this->assertSame( 'Foo', $t2_term->name ); 723 } 724 725 /** 726 * @ticket 30780 727 */ 728 public function test_wp_update_term_should_allow_duplicate_names_at_different_levels_of_the_same_taxonomy() { 729 register_taxonomy( 'wptests_tax', 'post', array( 730 'hierarchical' => true, 731 ) ); 732 733 $t1 = $this->factory->term->create( array( 734 'name' => 'Foo', 735 'slug' => 'foo', 736 'taxonomy' => 'wptests_tax', 737 ) ); 738 739 $t2 = $this->factory->term->create( array( 740 'name' => 'Bar', 741 'slug' => 'bar', 742 'taxonomy' => 'wptests_tax', 743 'parent' => $t1, 744 ) ); 745 746 $t3 = $this->factory->term->create( array( 747 'name' => 'Bar Child', 748 'slug' => 'bar-child', 749 'taxonomy' => 'wptests_tax', 750 'parent' => $t2, 751 ) ); 752 753 $updated = wp_update_term( $t3, 'wptests_tax', array( 754 'name' => 'Bar', 755 ) ); 756 757 $this->assertFalse( is_wp_error( $updated ) ); 758 759 $t3_term = get_term( $t3, 'wptests_tax' ); 760 $this->assertSame( 'Bar', $t3_term->name ); 761 } 762 763 /** 764 * @ticket 30780 765 */ 766 public function test_wp_update_term_should_not_allow_duplicate_names_at_same_level_of_the_same_taxonomy() { 767 register_taxonomy( 'wptests_tax', 'post', array( 768 'hierarchical' => true, 769 ) ); 770 771 $t1 = $this->factory->term->create( array( 772 'name' => 'Foo', 773 'slug' => 'foo', 774 'taxonomy' => 'wptests_tax', 775 ) ); 776 777 $t2 = $this->factory->term->create( array( 778 'name' => 'Bar', 779 'slug' => 'bar', 780 'taxonomy' => 'wptests_tax', 781 'parent' => $t1, 782 ) ); 783 784 $t3 = $this->factory->term->create( array( 785 'name' => 'Bar 2', 786 'slug' => 'bar-2', 787 'taxonomy' => 'wptests_tax', 788 'parent' => $t1, 789 ) ); 790 791 $updated = wp_update_term( $t3, 'wptests_tax', array( 792 'name' => 'Bar', 793 ) ); 794 689 795 $this->assertWPError( $updated ); 690 $this->assertSame( ' duplicate_term_slug', $updated->get_error_code() );796 $this->assertSame( 'term_exists', $updated->get_error_code() ); 691 797 } 692 798 693 799 public function test_wp_update_term_alias_of_no_term_group() {