Skip to content
Permalink
Browse files

Taxonomy: Fix unique-slug check for terms with parents.

`wp_unique_term_slug()` appends numeric suffixes when the requested slug is
already in use by a sibling term. Changes introduced in [32837] inadvertently
caused this suffixing to be skipped in cases where the requested slug is
suffixed with the parent slug, so that it became possible to have two terms
`childslug-parentslug` underneath to the same `parentslug`. We fix this
regression by ensuring that the numeric-suffix routine runs in all cases.

Props yashar_hv, saskak, dlh.
Fixes #46431.

git-svn-id: https://develop.svn.wordpress.org/trunk@45893 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
boonebgorges committed Aug 26, 2019
1 parent 152083b commit c2c8a6848d861118f1ba8c42c798fe37a9a95021
Showing with 59 additions and 14 deletions.
  1. +14 −14 src/wp-includes/taxonomy.php
  2. +45 −0 tests/phpunit/tests/term/wpUniqueTermSlug.php
@@ -2745,22 +2745,22 @@ function wp_unique_term_slug( $slug, $term ) {
if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
if ( $parent_suffix ) {
$slug .= $parent_suffix;
}
if ( ! empty( $term->term_id ) ) {
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
} else {
if ( ! empty( $term->term_id ) ) {
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
} else {
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
}
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
}
if ( $wpdb->get_var( $query ) ) {
$num = 2;
do {
$alt_slug = $slug . "-$num";
$num++;
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
} while ( $slug_check );
$slug = $alt_slug;
}
if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$num = 2;
do {
$alt_slug = $slug . "-$num";
$num++;
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
} while ( $slug_check );
$slug = $alt_slug;
}
}
@@ -127,4 +127,49 @@ public function test_nonunique_slug_in_same_hierarchical_taxonomy_at_different_l
$actual = wp_unique_term_slug( 'bar', $term2_object );
$this->assertEquals( 'bar-2', $actual );
}
/**
* @ticket 46431
*/
public function test_duplicate_parent_suffixed_slug_should_get_numeric_suffix() {
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
'name' => 'Animal',
'slug' => 'animal',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
'name' => 'Dog',
'slug' => 'dog',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
'name' => 'Cat',
'slug' => 'dog-animal',
'parent' => $t1,
)
);
$t4 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
'name' => 'Giraffe',
'slug' => 'giraffe',
'parent' => $t1,
)
);
$term = get_term( $t4 );
$slug = wp_unique_term_slug( 'dog', $term );
$this->assertSame( 'dog-animal-2', $slug );
}
}

0 comments on commit c2c8a68

Please sign in to comment.
You can’t perform that action at this time.