Make WordPress Core

Ticket #3463: wp_delete_category.diff

File wp_delete_category.diff, 2.1 KB (added by markjaquith, 19 years ago)

Patch for trunk

  • wp-admin/admin-db.php

     
    174174        global $wpdb;
    175175
    176176        $cat_ID = (int) $cat_ID;
     177        $default_cat = get_option('default_category');
     178        $default_link_cat = get_option('default_link_category');
    177179
    178         // Don't delete the default cat.
    179         if ( $cat_ID == get_option('default_category') )
     180        // Don't delete either of the default cats
     181        if ( $cat_ID == $default_cat || $cat_ID == $default_link_cat )
    180182                return 0;
    181183
    182         if ( $cat_ID == get_option('default_link_category') )
    183                 return 0;
    184 
    185184        $category = get_category($cat_ID);
    186185
    187186        $parent = $category->category_parent;
    188187
    189         // Delete the category.
     188        // Delete the category
    190189        if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
    191190                return 0;
    192191
    193         // Update children to point to new parent.
     192        // Update children to point to new parent
    194193        $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
    195194
    196         // Only set posts and links to the default category if they're not in another category already.
    197         $default_cat = get_option('default_category');
     195        // Only set posts and links to the default category if they're not in another category already
    198196        $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
    199         if ( is_array($posts) ) foreach ($posts as $post_id) {
     197        foreach ( (array) $posts as $post_id ) {
    200198                $cats = wp_get_post_categories($post_id);
    201199                if ( 1 == count($cats) )
    202200                        $cats = array($default_cat);
     
    205203                wp_set_post_categories($post_id, $cats);
    206204        }
    207205
    208         $default_link_cat = get_option('default_link_category');
    209206        $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
    210         if ( is_array($links) ) foreach ($links as $link_id) {
     207        foreach ( (array) $links as $link_id ) {
    211208                $cats = wp_get_link_cats($link_id);
    212209                if ( 1 == count($cats) )
    213210                        $cats = array($default_link_cat);
     
    217214        }
    218215
    219216        clean_category_cache($cat_ID);
    220 
    221217        do_action('delete_category', $cat_ID);
    222 
    223218        return 1;
    224219}
    225220