[wp-trac] [WordPress Trac] #30780: Inconsistent behavior/results of wp_insert_term and wp_update_term

WordPress Trac noreply at wordpress.org
Fri Dec 19 16:08:29 UTC 2014


#30780: Inconsistent behavior/results of wp_insert_term and wp_update_term
-------------------------+------------------
 Reporter:  ipm-frommen  |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  4.2
Component:  Taxonomy     |     Version:  4.1
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+------------------

Comment (by ipm-frommen):

 Sorry, me again.

 I just realized that moving a top-level term `foo` to a parent
 '''always''' generates a new slug incorporating the parent's slug, even if
 there is no other term with the current slug.

 This is due to this code fragment in `wp_unique_term_slug` function:

 {{{
 if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) )
 {
         $the_parent = $term->parent;
         while ( ! empty($the_parent) ) {
                 $parent_term = get_term($the_parent, $term->taxonomy);
                 if ( is_wp_error($parent_term) || empty($parent_term) )
                         break;
                 $slug .= '-' . $parent_term->slug;
                 if ( ! term_exists( $slug ) )
                         return $slug;

                 if ( empty($parent_term->parent) )
                         break;
                 $the_parent = $parent_term->parent;
         }
 }
 }}}

 I suggest to change this into

 {{{
 if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) )
 {
         $the_parent = $term->parent;

         $args = array(
                 'slug'    => $slug,
                 'exclude' => $term->term_id,
                 'get'     => 'all',
                 'parent'  => $the_parent,
         );
         $siblings_with_the_same_slug = get_terms( $term->taxonomy,$args );
         if ( is_wp_error( $siblings_with_the_same_slug ) || empty(
 $siblings_with_the_same_slug ) ) {
                 return $slug;
         }

         while ( ! empty($the_parent) ) {
                 $parent_term = get_term($the_parent, $term->taxonomy);
                 if ( is_wp_error($parent_term) || empty($parent_term) )
                         break;
                 $slug .= '-' . $parent_term->slug;
                 if ( ! term_exists( $slug ) )
                         return $slug;

                 if ( empty($parent_term->parent) )
                         break;
                 $the_parent = $parent_term->parent;
         }
 }
 }}}

 The new part is this check right after having confirmed we have a
 hierarchical taxonomy and a parent:

 {{{
         $args = array(
                 'slug'    => $slug,
                 'exclude' => $term->term_id,
                 'get'     => 'all',
                 'parent'  => $the_parent,
         );
         $siblings_with_the_same_slug = get_terms( $term->taxonomy,$args );
         if ( is_wp_error( $siblings_with_the_same_slug ) || empty(
 $siblings_with_the_same_slug ) ) {
                 return $slug;
         }
 }}}

 What do you say? Do I miss something here?

 If this is good, I'd happily provide a patch for this.

 Cheers,
 Thorsten

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


More information about the wp-trac mailing list