3009 | | $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); |
3010 | | if ( empty($slug) ) { |
3011 | | $slug = sanitize_title($name, $term_id); |
| 3009 | $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"; |
| 3010 | |
| 3011 | // Don't indiscriminately update the term name if the term is being used by another taxonomy |
| 3012 | $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(taxonomy) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); |
| 3013 | |
| 3014 | // 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 |
| 3015 | if ( $count > 1 ) { |
| 3016 | $old_tt_id = $wpdb->get_var( $wpdb->prepare( $tt_id_sql, $taxonomy, $term_id ) ); |
| 3017 | |
| 3018 | // Retain $args which are necessary for wp_insert_term() and remove the rest |
| 3019 | $insert_term_args = array( 'alias_of', 'description', 'parent', 'slug' ); |
| 3020 | foreach( $args as $index => $values ) { |
| 3021 | if ( ! in_array( $index, $insert_term_args ) ) { |
| 3022 | unset( $args[ $index ] ); |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | $term_ids = wp_insert_term( $name, $taxonomy, $args ); |
| 3027 | if ( ! is_wp_error( $term_ids ) ) { |
| 3028 | $new_tt_id = $term_ids['term_taxonomy_id']; |
| 3029 | // since wp_insert_term creates a new term_tax entry, we need to delete our old one after updating term_rels |
| 3030 | $wpdb->update( $wpdb->term_relationships, array( 'term_taxonomy_id' => $new_tt_id ), array( 'term_taxonomy_id' => $old_tt_id ) ); |
| 3031 | wp_delete_term( $term_id, $taxonomy ); |
| 3032 | _update_generic_term_count( array( $new_tt_id ), get_taxonomy( $taxonomy ) ); |
| 3033 | _update_post_term_count( array( $new_tt_id ), get_taxonomy( $taxonomy ) ); |
| 3034 | // reset term_id to new term_id |
| 3035 | $term_id = $term_ids['term_id']; |
| 3036 | } else { |
| 3037 | return $term_ids; |
| 3038 | } |
| 3039 | } else { |
| 3040 | $wpdb->update( $wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); |
| 3041 | } |
| 3042 | |
| 3043 | if ( empty( $slug ) ) { |
| 3044 | $slug = sanitize_title( $name, $term_id ); |
| 3045 | |