From 6251eb3ca655bd46a9979cadbd933d9dcb7a2597 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 23 May 2007 22:03:24 +0000 Subject: [PATCH] wp_delete_term(). see #4189 git-svn-id: http://svn.automattic.com/wordpress/trunk@5533 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-db.php | 46 +++------------------------------- wp-includes/taxonomy.php | 54 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/wp-admin/admin-db.php b/wp-admin/admin-db.php index 8cd708b9b5..d1bb017400 100644 --- a/wp-admin/admin-db.php +++ b/wp-admin/admin-db.php @@ -150,51 +150,13 @@ function wp_delete_category($cat_ID) { global $wpdb; $cat_ID = (int) $cat_ID; - $default_cat = get_option('default_category'); - $default_link_cat = get_option('default_link_category'); + $default = get_option('default_category'); - // Don't delete either of the default cats - if ( $cat_ID == $default_cat || $cat_ID == $default_link_cat ) + // Don't delete the default cat + if ( $cat_ID == $default ) return 0; - $category = get_category($cat_ID); - - $parent = $category->category_parent; - - // Delete the category if it is not also a tag. - if ( 0 == ($category->type & TAXONOMY_TAG) ) { - if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") ) - return 0; - } else { - $wpdb->query("UPDATE $wpdb->categories SET type = type & ~" . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_ID'"); - } - // Update children to point to new parent - $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'"); - - // Only set posts and links to the default category if they're not in another category already - $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID' AND rel_type = 'category'"); - foreach ( (array) $posts as $post_id ) { - $cats = wp_get_post_categories($post_id); - if ( 1 == count($cats) ) - $cats = array($default_cat); - else - $cats = array_diff($cats, array($cat_ID)); - wp_set_post_categories($post_id, $cats); - } - - $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'"); - foreach ( (array) $links as $link_id ) { - $cats = wp_get_link_cats($link_id); - if ( 1 == count($cats) ) - $cats = array($default_link_cat); - else - $cats = array_diff($cats, array($cat_ID)); - wp_set_link_cats($link_id, $cats); - } - - clean_category_cache($cat_ID); - do_action('delete_category', $cat_ID); - return 1; + return wp_delete_term($cat_ID, 'category', "default=$default"); } function wp_create_category($cat_name) { diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 6951ff48c5..8aac37e46a 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -20,6 +20,14 @@ function get_taxonomy( $taxonomy ) { return $wp_taxonomies[$taxonomy]; } +function is_taxonomy_hierarchical($taxonomy) { + if ( ! is_taxonomy($taxonomy) ) + return false; + + $taxonomy = get_taxonomy($taxonomy); + return $taxonomy['hierarchical']; +} + function register_taxonomy( $taxonomy, $object_type, $args = array() ) { global $wp_taxonomies; @@ -98,7 +106,51 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { /** * Removes a term from the database. */ -function wp_delete_term() {} +function wp_delete_term( $term, $taxonomy, $args = array() ) { + global $wpdb; + + $term = (int) $term; + + if ( ! $ids = is_term($term, $taxonomy) ) + return false; + $tt_id = $ids['term_taxonomy_id']; + + $defaults = array(); + $args = wp_parse_args($args, $defaults); + extract($args); + + if ( isset($default) ) { + $default = (int) $default; + if ( ! is_term($default, $taxonomy) ) + unset($default); + } + + // Update children to point to new parent + if ( is_taxonomy_hierarchical($taxonomy) ) { + $term_obj = get_term($term, $taxonomy); + $parent = $term_obj->parent; + + $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = '$parent' WHERE parent = '$term_obj->term_id' AND taxonomy = '$taxonomy'"); + } + + $objects = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$tt_id'"); + + foreach ( (array) $objects as $object ) { + $terms = get_object_terms($object, $taxonomy, 'get=ids'); + if ( 1 == count($terms) && isset($default) ) + $terms = array($default); + else + $terms = array_diff($terms, array($term)); + wp_set_object_terms($object, $terms, $taxonomy); + } + + $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$tt_id'"); + + //clean_term_cache($term, $taxonomy); + do_action("delete_$taxonomy", $term, $tt_id); + + return true; +} function wp_update_term( $term, $taxonomy, $args = array() ) { global $wpdb;