Make WordPress Core


Ignore:
Timestamp:
07/03/2005 07:26:51 PM (21 years ago)
Author:
ryan
Message:

wp_insert_category(), wp_update_category(), wp_delete_category().

File:
1 edited

Legend:

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

    r2662 r2695  
    203203
    204204    return $comment;
     205}
     206
     207function get_category_to_edit($id) {
     208    $category = get_category($id);
     209
     210    return $category;
     211}
     212
     213function wp_insert_category($catarr) {
     214    global $wpdb;
     215
     216    extract($catarr);
     217
     218    $cat_ID = (int) $cat_ID;
     219
     220    // Are we updating or creating?
     221    if ( !empty($cat_ID) ) {
     222        $update = true;
     223    } else {
     224        $update = false;
     225        $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
     226        $cat_ID = $id_result->Auto_increment;
     227    }
     228
     229    $cat_name = wp_specialchars($cat_name);
     230
     231    if ( empty($category_nicename) )
     232        $category_nicename = sanitize_title($cat_name, $cat_ID);
     233    else
     234        $category_nicename = sanitize_title($category_nicename, $cat_ID);
     235
     236    if ( empty($category_description) )
     237        $category_description = '';
     238
     239    if ( empty($category_parent) )
     240        $category_parent = 0;
     241
     242    if ( !$update)
     243        $query = "INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$cat')";
     244    else
     245        $query = "UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'";
     246
     247    $result = $wpdb->query($query);
     248
     249    if ( $update ) {
     250        $rval = $wpdb->rows_affected;
     251        do_action('edit_category', $cat_ID);
     252    } else {
     253        $rval = $wpdb->insert_id;
     254        do_action('create_category', $cat_ID);
     255    }
     256
     257    return $rval;
     258}
     259
     260function wp_update_category($catarr) {
     261    global $wpdb;
     262
     263    $cat_ID = (int) $catarr['cat_ID'];
     264   
     265    // First, get all of the original fields
     266    $category = get_category($cat_ID, ARRAY_A);
     267
     268    // Escape data pulled from DB.
     269    $category = add_magic_quotes($category);
     270
     271    // Merge old and new fields with new fields overwriting old ones.
     272    $catarr = array_merge($category, $catarr);
     273
     274    return wp_insert_category($catarr);
     275}
     276
     277function wp_delete_category($cat_ID) {
     278    global $wpdb;
     279
     280    $cat_ID = (int) $cat_ID;
     281
     282    // Don't delete the default cat.
     283    if ( 1 == $cat_ID )
     284        return 0;
     285
     286    $category = get_category($cat_ID);
     287
     288    $parent = $category->category_parent;
     289
     290    // Delete the category.
     291    $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
     292
     293    // Update children to point to new parent.
     294    $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
     295
     296    // TODO: Only set categories to general if they're not in another category already
     297    $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
     298
     299    do_action('delete_category', $cat_ID);
     300
     301    return 1;
    205302}
    206303
Note: See TracChangeset for help on using the changeset viewer.