[wp-trac] [WordPress Trac] #46431: wp_unique_term_slug() doesn't guarantee unique slugs for child terms

WordPress Trac noreply at wordpress.org
Thu Jun 13 15:35:28 UTC 2019


#46431: wp_unique_term_slug() doesn't guarantee unique slugs for child terms
------------------------------------------+------------------------------
 Reporter:  saskak                        |       Owner:  (none)
     Type:  defect (bug)                  |      Status:  new
 Priority:  normal                        |   Milestone:  Awaiting Review
Component:  Taxonomy                      |     Version:  4.3
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:
------------------------------------------+------------------------------

Comment (by yashar_hv):

 I encountered this bug when I was trying to auto assign custom taxonomy
 term slugs by using `pre_{$taxonomy}_{$field}` filter hook.
 I found out in the last part of `wp_unique_term_slug()` when it tries to
 append sequel numbers in case `$slug` is not unique yet, it checks for
 `$parent_suffix` ('-parent-slug') and if it exists, the function ignores
 appending a number and returns `$slug.$parent_suffix` :
 {{{
 if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix,
 $slug, $term ) ) {
         if ( $parent_suffix ) {
                 $slug .= $parent_suffix;
         } 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 );
                 }

                 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;
                 }
         }
 }
 }}}

 == Possible Solution
 I modified this code as below and it seems that this solves the problem
 and now the number appends to both `$slug.$parent_suffix` and `$slug` in
 case there is a existing term with that slug:
 {{{
 if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix,
 $slug, $term ) ) {

         /*
         * MODIFIED: $parent_suffix conditional if block is closed right
 after appending $parent_suffix to $slug.
         *           Else block content for this condition moved to the
 outside of conditional block.
         */

         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 {
                 $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;
         }
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46431#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list