wp_delete_term(). see #4189

git-svn-id: http://svn.automattic.com/wordpress/trunk@5533 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-05-23 22:03:24 +00:00
parent 1b783b9b9a
commit 6251eb3ca6
2 changed files with 57 additions and 43 deletions

View File

@ -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) {

View File

@ -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;