Make WordPress Core

Ticket #21275: terms.diff

File terms.diff, 2.5 KB (added by wonderboymusic, 13 years ago)
  • wp-includes/taxonomy.php

     
    23812381                else
    23822382                        return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
    23832383        }
     2384   
    23842385        do_action( 'edit_terms', $term_id );
    2385         $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
    2386         if ( empty($slug) ) {
    2387                 $slug = sanitize_title($name, $term_id);
     2386
     2387        $tt_id_sql = "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";
     2388       
     2389        // Don't indiscriminately update the term name if the term is being used by another taxonomy
     2390        $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(taxonomy) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
     2391
     2392        // if there are multiple instances, make the updated term a new term so we only have to alter one term_taxonomy row's term_id
     2393        if ( $count > 1 ) { 
     2394                $old_tt_id = $wpdb->get_var( $wpdb->prepare( $tt_id_sql, $taxonomy, $term_id ) );
     2395                $term_ids = wp_insert_term( $name, $taxonomy, $args );
     2396                if ( ! is_wp_error( $term_ids ) ) {
     2397                        $new_tt_id = $term_ids['term_taxonomy_id'];
     2398                        // since wp_insert_term creates a new term_tax entry, we need to delete our old one after updating term_rels
     2399                        $wpdb->update( $wpdb->term_relationships, array( 'term_taxonomy_id' => $new_tt_id ), array( 'term_taxonomy_id' => $old_tt_id ) );
     2400                        wp_delete_term( $term_id, $taxonomy );
     2401                        // reset term_id to new term_id
     2402                        $term_id = $term_ids['term_id'];
     2403                }
     2404        } else {
     2405                $wpdb->update( $wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
     2406        }
     2407       
     2408        if ( empty( $slug ) ) {
     2409                $slug = sanitize_title( $name, $term_id );
    23882410                $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
    23892411        }
     2412
    23902413        do_action( 'edited_terms', $term_id );
    23912414
    2392         $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) );
     2415        $tt_id = $wpdb->get_var( $wpdb->prepare( $tt_id_sql, $taxonomy, $term_id) );
    23932416        do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
    23942417        $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
    23952418        do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );