321
322
$term = (int) $term;
322
323
if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
323
324
$_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %s LIMIT 1", $taxonomy, $term) );
324
327
wp_cache_add($term, $_term, $taxonomy);
1078
1081
foreach ( (array) $taxonomies as $taxonomy ) {
1079
1082
$tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
1080
1083
$in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
1084
do_action( 'delete_term_relationships', $object_id, $tt_ids );
1081
1085
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
1086
do_action( 'deleted_term_relationships', $object_id, $tt_ids );
1082
1087
wp_update_term_count($tt_ids, $taxonomy);
1137
1142
return $term_obj;
1138
1143
$parent = $term_obj->parent;
1145
$edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
1146
do_action( 'edit_term_taxonomies', $edit_tt_ids );
1140
1147
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
1148
do_action( 'edited_term_taxonomies', $edit_tt_ids );
1143
1151
$objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
1155
1163
wp_set_object_terms($object, $terms, $taxonomy);
1166
do_action( 'delete_term_taxonomy', $tt_id );
1158
1167
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
1168
do_action( 'deleted_term_taxonomy', $tt_id );
1160
1170
// Delete the term if no taxonomies use it.
1161
1171
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
1378
1388
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
1379
1389
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
1390
do_action( 'edit_terms', $alias->term_id );
1380
1391
$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
1392
do_action( 'edited_terms', $alias->term_id );
1397
1409
if ( empty($slug) ) {
1398
1410
$slug = sanitize_title($slug, $term_id);
1411
do_action( 'edit_terms', $term_id );
1399
1412
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
1413
do_action( 'edited_terms', $term_id );
1402
1416
$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 ) );
1407
1421
$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
1408
1422
$tt_id = (int) $wpdb->insert_id;
1410
do_action("create_term", $term_id, $tt_id);
1424
do_action("create_term", $term_id, $tt_id, $taxonomy);
1411
1425
do_action("create_$taxonomy", $term_id, $tt_id);
1413
1427
$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
1415
1429
clean_term_cache($term_id, $taxonomy);
1417
do_action("created_term", $term_id, $tt_id);
1431
do_action("created_term", $term_id, $tt_id, $taxonomy);
1418
1432
do_action("created_$taxonomy", $term_id, $tt_id);
1420
1434
return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
1475
1489
if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )
1491
do_action( 'add_term_relationship', $object_id, $tt_id );
1477
1492
$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
1493
do_action( 'added_term_relationship', $object_id, $tt_id );
1480
1496
wp_update_term_count($tt_ids, $taxonomy);
1483
1499
$delete_terms = array_diff($old_tt_ids, $tt_ids);
1484
1500
if ( $delete_terms ) {
1485
1501
$in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
1502
do_action( 'delete_term_relationships', $object_id, $delete_terms );
1486
1503
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
1504
do_action( 'deleted_term_relationships', $object_id, $delete_terms );
1487
1505
wp_update_term_count($delete_terms, $taxonomy);
1500
1518
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
1503
do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append);
1521
do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);
1504
1522
return $tt_ids;
1647
1665
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
1648
1666
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
1667
do_action( 'edit_terms', $alias->term_id );
1649
1668
$wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
1669
do_action( 'edited_terms', $alias->term_id );
1661
1681
return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
1683
do_action( 'edit_terms', $term_id );
1664
1684
$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
1666
1685
if ( empty($slug) ) {
1667
1686
$slug = sanitize_title($name, $term_id);
1668
1687
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
1689
do_action( 'edited_terms', $term_id );
1671
1691
$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) );
1692
do_action( 'edit_term_taxonomy', $tt_id );
1673
1693
$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
1694
do_action( 'edited_term_taxonomy', $tt_id );
1675
do_action("edit_term", $term_id, $tt_id);
1696
do_action("edit_term", $term_id, $tt_id, $taxonomy);
1676
1697
do_action("edit_$taxonomy", $term_id, $tt_id);
1678
1699
$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
1680
1701
clean_term_cache($term_id, $taxonomy);
1682
do_action("edited_term", $term_id, $tt_id);
1703
do_action("edited_term", $term_id, $tt_id, $taxonomy);
1683
1704
do_action("edited_$taxonomy", $term_id, $tt_id);
1685
1706
return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
1771
1792
// Default count updater
1772
1793
foreach ( (array) $terms as $term) {
1773
1794
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term) );
1795
do_action( 'edit_term_taxonomy', $term );
1774
1796
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
1797
do_action( 'edited_term_taxonomy', $term );
2154
2177
foreach ( (array) $terms as $term ) {
2155
2178
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term ) );
2179
do_action( 'edit_term_taxonomy', $term );
2156
2180
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
2181
do_action( 'edited_term_taxonomy', $term );
2191
2216
$slug = $term->slug;
2193
2218
if ( empty($termlink) ) {
2194
$file = get_option('home') . '/';
2219
$file = trailingslashit( get_option('home') );
2195
2220
$t = get_taxonomy($taxonomy);
2196
2221
if ( $t->query_var )
2197
2222
$termlink = "$file?$t->query_var=$slug";