WordPress.org

Make WordPress Core

Ticket #5809: garyc40.5809.diff

File garyc40.5809.diff, 3.8 KB (added by garyc40, 3 years ago)

create new tag instead of modifying existing one if the user changes its name or slug and the term is in multiple taxonomies

  • wp-includes/taxonomy.php

    diff --git wp-includes/taxonomy.php wp-includes/taxonomy.php
    index c3cbf36..cf5962f 100644
    function wp_update_term( $term_id, $taxonomy, $args = array() ) { 
    22352235 
    22362236        // Escape data pulled from DB. 
    22372237        $term = add_magic_quotes($term); 
    2238  
     2238         
     2239        // Changed fields 
     2240        $changed = array_keys( array_diff_assoc( $term, $args ) ); 
     2241         
    22392242        // Merge old and new args with new args overwriting old ones. 
    22402243        $args = array_merge($term, $args); 
    2241  
     2244         
    22422245        $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); 
    22432246        $args = wp_parse_args($args, $defaults); 
    22442247        $args = sanitize_term($args, $taxonomy, 'db'); 
    function wp_update_term( $term_id, $taxonomy, $args = array() ) { 
    22842287                else 
    22852288                        return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); 
    22862289        } 
    2287         do_action( 'edit_terms', $term_id ); 
    2288         $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); 
    2289         if ( empty($slug) ) { 
    2290                 $slug = sanitize_title($name, $term_id); 
    2291                 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); 
    2292         } 
    2293         do_action( 'edited_terms', $term_id ); 
    2294  
     2290         
    22952291        $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) ); 
    2296         do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); 
    2297         $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); 
    2298         do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); 
    2299  
     2292        $exist_tt = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) AS c FROM $wpdb->term_taxonomy AS tt WHERE term_id = %d", $term_id ) ); 
     2293         
     2294        do_action( 'edit_terms', $term_id );     
    23002295        do_action("edit_term", $term_id, $tt_id, $taxonomy); 
    23012296        do_action("edit_$taxonomy", $term_id, $tt_id); 
    23022297 
    2303         $term_id = apply_filters('term_id_filter', $term_id, $tt_id); 
     2298        $name_changed = in_array( 'name', $changed ); 
     2299        $slug_changed = in_array( 'slug', $changed ); 
     2300 
     2301        // create a new term if the current one exists in multiple taxonomies and its name or slug is changed 
     2302        if ( $exist_tt > 1 && ( $name_changed || $slug_changed ) ) { 
     2303                // create a unique slug 
     2304                if ( $slug_changed ) 
     2305                        $slug = wp_unique_term_slug( $slug, (object) $args ); 
     2306                elseif ( $name_changed ) 
     2307                        $slug = wp_unique_term_slug( sanitize_title( $name ), (object) $args ); 
     2308 
     2309                $new_term = wp_insert_term( $name, $taxonomy, compact( 'slug', 'description', 'parent', 'term_group' ) ); 
     2310                if ( is_wp_error( $new_term ) ) 
     2311                        return $new_term; 
     2312                 
     2313                $new_id = $new_term['term_id']; 
     2314                $new_tt_id = $new_term['term_taxonomy_id']; 
     2315                 
     2316                // reassign posts to this new term 
     2317                $wpdb->update( $wpdb->term_relationships, array( 'term_taxonomy_id' => $new_tt_id ), array( 'term_taxonomy_id' => $tt_id ) ); 
     2318                wp_update_term_count( $new_tt_id, $taxonomy ); 
     2319                wp_delete_term($term_id, $taxonomy); 
     2320                 
     2321                $term_id = $new_id; 
     2322                $tt_id = $new_tt_id; 
     2323        } else { 
     2324                $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); 
     2325                do_action( 'edited_terms', $term_id ); 
    23042326 
     2327                do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); 
     2328                $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); 
     2329                do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); 
     2330        } 
     2331 
     2332        $term_id = apply_filters('term_id_filter', $term_id, $tt_id); 
    23052333        clean_term_cache($term_id, $taxonomy); 
    23062334 
    23072335        do_action("edited_term", $term_id, $tt_id, $taxonomy); 
    23082336        do_action("edited_$taxonomy", $term_id, $tt_id); 
    23092337 
    2310         return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); 
     2338        return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 
    23112339} 
    23122340 
    23132341/**