diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
index 2db5038ef1..d374d2fc44 100644
a
|
b
|
function wp_unique_term_slug( $slug, $term ) { |
2745 | 2745 | if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) { |
2746 | 2746 | if ( $parent_suffix ) { |
2747 | 2747 | $slug .= $parent_suffix; |
| 2748 | } |
| 2749 | |
| 2750 | if ( ! empty( $term->term_id ) ) { |
| 2751 | $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); |
2748 | 2752 | } else { |
2749 | | if ( ! empty( $term->term_id ) ) { |
2750 | | $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); |
2751 | | } else { |
2752 | | $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); |
2753 | | } |
| 2753 | $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); |
| 2754 | } |
2754 | 2755 | |
2755 | | if ( $wpdb->get_var( $query ) ) { |
2756 | | $num = 2; |
2757 | | do { |
2758 | | $alt_slug = $slug . "-$num"; |
2759 | | $num++; |
2760 | | $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); |
2761 | | } while ( $slug_check ); |
2762 | | $slug = $alt_slug; |
2763 | | } |
| 2756 | if ( $wpdb->get_var( $query ) ) { |
| 2757 | $num = 2; |
| 2758 | do { |
| 2759 | $alt_slug = $slug . "-$num"; |
| 2760 | $num++; |
| 2761 | $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); |
| 2762 | } while ( $slug_check ); |
| 2763 | $slug = $alt_slug; |
2764 | 2764 | } |
2765 | 2765 | } |
2766 | 2766 | |
diff --git a/tests/phpunit/tests/term/wpUniqueTermSlug.php b/tests/phpunit/tests/term/wpUniqueTermSlug.php
index b81dca5b40..0663a0c626 100644
a
|
b
|
class Tests_Term_WpUniqueTermSlug extends WP_UnitTestCase { |
127 | 127 | $actual = wp_unique_term_slug( 'bar', $term2_object ); |
128 | 128 | $this->assertEquals( 'bar-2', $actual ); |
129 | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @group 46431 |
| 133 | */ |
| 134 | public function test_duplicate_parent_suffixed_slug_should_be_get_numeric_suffix() { |
| 135 | $t1 = self::factory()->term->create( |
| 136 | array( |
| 137 | 'taxonomy' => 'wptests_tax2', |
| 138 | 'name' => 'Animal', |
| 139 | 'slug' => 'animal', |
| 140 | ) |
| 141 | ); |
| 142 | |
| 143 | $t2 = self::factory()->term->create( |
| 144 | array( |
| 145 | 'taxonomy' => 'wptests_tax2', |
| 146 | 'name' => 'Dog', |
| 147 | 'slug' => 'dog', |
| 148 | ) |
| 149 | ); |
| 150 | |
| 151 | $t3 = self::factory()->term->create( |
| 152 | array( |
| 153 | 'taxonomy' => 'wptests_tax2', |
| 154 | 'name' => 'Cat', |
| 155 | 'slug' => 'dog-animal', |
| 156 | 'parent' => $t1, |
| 157 | ) |
| 158 | ); |
| 159 | |
| 160 | $t4 = self::factory()->term->create( |
| 161 | array( |
| 162 | 'taxonomy' => 'wptests_tax2', |
| 163 | 'name' => 'Giraffe', |
| 164 | 'slug' => 'giraffe', |
| 165 | 'parent' => $t1, |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | $term = get_term( $t4 ); |
| 170 | |
| 171 | $slug = wp_unique_term_slug( 'dog', $term ); |
| 172 | |
| 173 | $this->assertSame( 'dog-animal-2', $slug ); |
| 174 | } |
130 | 175 | } |