WordPress.org

Make WordPress Core

Ticket #5809: terms.diff

File terms.diff, 2.7 KB (added by wonderboymusic, 6 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                        _update_generic_term_count( array( $new_tt_id ), get_taxonomy( $taxonomy ) );
     2402                        _update_post_term_count( array( $new_tt_id ), get_taxonomy( $taxonomy ) );
     2403                        // reset term_id to new term_id
     2404                        $term_id = $term_ids['term_id'];
     2405                } else {
     2406                        return $term_ids;
     2407                }
     2408        } else {
     2409                $wpdb->update( $wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
     2410        }
     2411       
     2412        if ( empty( $slug ) ) {
     2413                $slug = sanitize_title( $name, $term_id );
    23882414                $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
    23892415        }
     2416
    23902417        do_action( 'edited_terms', $term_id );
    23912418
    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) );
     2419        $tt_id = $wpdb->get_var( $wpdb->prepare( $tt_id_sql, $taxonomy, $term_id) );
    23932420        do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
    23942421        $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
    23952422        do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );