Make WordPress Core

Changeset 5533


Ignore:
Timestamp:
05/23/2007 10:03:24 PM (17 years ago)
Author:
ryan
Message:

wp_delete_term(). see #4189

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-db.php

    r5528 r5533  
    151151
    152152    $cat_ID = (int) $cat_ID;
    153     $default_cat = get_option('default_category');
    154     $default_link_cat = get_option('default_link_category');
    155 
    156     // Don't delete either of the default cats
    157     if ( $cat_ID == $default_cat || $cat_ID == $default_link_cat )
    158         return 0;
    159 
    160     $category = get_category($cat_ID);
    161 
    162     $parent = $category->category_parent;
    163 
    164     // Delete the category if it is not also a tag.
    165     if ( 0 == ($category->type & TAXONOMY_TAG) ) {
    166         if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
    167             return 0;
    168     } else {
    169         $wpdb->query("UPDATE $wpdb->categories SET type = type & ~" . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_ID'");
    170     }
    171     // Update children to point to new parent
    172     $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
    173 
    174     // Only set posts and links to the default category if they're not in another category already
    175     $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID' AND rel_type = 'category'");
    176     foreach ( (array) $posts as $post_id ) {
    177         $cats = wp_get_post_categories($post_id);
    178         if ( 1 == count($cats) )
    179             $cats = array($default_cat);
    180         else
    181             $cats = array_diff($cats, array($cat_ID));
    182         wp_set_post_categories($post_id, $cats);
    183     }
    184 
    185     $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
    186     foreach ( (array) $links as $link_id ) {
    187         $cats = wp_get_link_cats($link_id);
    188         if ( 1 == count($cats) )
    189             $cats = array($default_link_cat);
    190         else
    191             $cats = array_diff($cats, array($cat_ID));
    192         wp_set_link_cats($link_id, $cats);
    193     }
    194 
    195     clean_category_cache($cat_ID);
    196     do_action('delete_category', $cat_ID);
    197     return 1;
     153    $default = get_option('default_category');
     154
     155    // Don't delete the default cat
     156    if ( $cat_ID == $default )
     157        return 0;
     158
     159    return wp_delete_term($cat_ID, 'category', "default=$default");
    198160}
    199161
  • trunk/wp-includes/taxonomy.php

    r5529 r5533  
    1919
    2020    return $wp_taxonomies[$taxonomy];
     21}
     22
     23function is_taxonomy_hierarchical($taxonomy) {
     24    if ( ! is_taxonomy($taxonomy) )
     25        return false;
     26
     27    $taxonomy = get_taxonomy($taxonomy);
     28    return $taxonomy['hierarchical'];
    2129}
    2230
     
    99107 * Removes a term from the database.
    100108 */
    101 function wp_delete_term() {}
     109function wp_delete_term( $term, $taxonomy, $args = array() ) {
     110    global $wpdb;
     111
     112    $term = (int) $term;
     113
     114    if ( ! $ids = is_term($term, $taxonomy) )
     115        return false;
     116    $tt_id = $ids['term_taxonomy_id'];
     117
     118    $defaults = array();
     119    $args = wp_parse_args($args, $defaults);
     120    extract($args);
     121
     122    if ( isset($default) ) {
     123        $default = (int) $default;
     124        if ( ! is_term($default, $taxonomy) )
     125            unset($default);
     126    }
     127
     128    // Update children to point to new parent
     129    if ( is_taxonomy_hierarchical($taxonomy) ) {
     130        $term_obj = get_term($term, $taxonomy);
     131        $parent = $term_obj->parent;
     132
     133        $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = '$parent' WHERE parent = '$term_obj->term_id' AND taxonomy = '$taxonomy'");
     134    }
     135
     136    $objects = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$tt_id'");
     137
     138    foreach ( (array) $objects as $object ) {
     139        $terms = get_object_terms($object, $taxonomy, 'get=ids');
     140        if ( 1 == count($terms) && isset($default) )
     141            $terms = array($default);
     142        else
     143            $terms = array_diff($terms, array($term));
     144        wp_set_object_terms($object, $terms, $taxonomy);
     145    }
     146
     147    $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$tt_id'");
     148
     149    //clean_term_cache($term, $taxonomy);
     150    do_action("delete_$taxonomy", $term, $tt_id);
     151
     152    return true;
     153}
    102154
    103155function wp_update_term( $term, $taxonomy, $args = array() ) {
Note: See TracChangeset for help on using the changeset viewer.