Make WordPress Core

Changeset 2695


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

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

Location:
trunk/wp-admin
Files:
2 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
  • trunk/wp-admin/categories.php

    r2446 r2695  
    2424
    2525case 'addcat':
     26
    2627    if ($user_level < 3)
    2728        die (__('Cheatin&#8217; uh?'));
    2829   
    29     $cat_name= wp_specialchars($_POST['cat_name']);
    30     $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
    31     $cat_ID = $id_result->Auto_increment;
    32     $category_nicename = sanitize_title($cat_name, $cat_ID);
    33     $category_description = $_POST['category_description'];
    34     $cat = intval($_POST['cat']);
    35    
    36     $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$cat')");
    37    
     30    wp_insert_category($_POST);
     31
    3832    header('Location: categories.php?message=1#addcat');
    3933break;
     
    4337    check_admin_referer();
    4438
     39    if ( $user_level < 3 )
     40        die (__('Cheatin&#8217; uh?'));
     41
    4542    $cat_ID = (int) $_GET['cat_ID'];
    4643    $cat_name = get_catname($cat_ID);
    47     $category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
    48     $cat_parent = $category->category_parent;
    4944
    5045    if ( 1 == $cat_ID )
    5146        die(sprintf(__("Can't delete the <strong>%s</strong> category: this is the default one"), $cat_name));
    5247
    53     if ( $user_level < 3 )
    54         die (__('Cheatin&#8217; uh?'));
    55 
    56     $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
    57     $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$cat_parent' WHERE category_parent = '$cat_ID'");
    58     // TODO: Only set categories to general if they're not in another category already
    59     $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
     48    wp_delete_category($cat_ID);
    6049
    6150    header('Location: categories.php?message=2');
     
    6756    require_once ('admin-header.php');
    6857    $cat_ID = (int) $_GET['cat_ID'];
    69     $category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
    70     $cat_name = $category->cat_name;
     58    $category = get_category_to_edit($cat_ID);
    7159    ?>
    7260
     
    7765        <tr>
    7866          <th width="33%" scope="row"><?php _e('Category name:') ?></th>
    79           <td width="67%"><input name="cat_name" type="text" value="<?php echo wp_specialchars($cat_name); ?>" size="40" /> <input type="hidden" name="action" value="editedcat" />
    80 <input type="hidden" name="cat_ID" value="<?php echo $cat_ID ?>" /></td>
     67          <td width="67%"><input name="cat_name" type="text" value="<?php echo wp_specialchars($category->cat_name); ?>" size="40" /> <input type="hidden" name="action" value="editedcat" />
     68<input type="hidden" name="cat_ID" value="<?php echo $category->cat_ID ?>" /></td>
    8169        </tr>
    8270        <tr>
     
    8775            <th scope="row"><?php _e('Category parent:') ?></th>
    8876            <td>       
    89             <select name='cat'>
     77            <select name='category_parent'>
    9078      <option value='0' <?php if (!$category->category_parent) echo " selected='selected'"; ?>><?php _e('None') ?></option>
    9179      <?php wp_dropdown_cats($category->cat_ID, $category->category_parent); ?>
     
    10997        die (__('Cheatin&#8217; uh?'));
    11098   
    111     $cat_name = wp_specialchars($_POST['cat_name']);
    112     $cat_ID = (int) $_POST['cat_ID'];
    113     $category_nicename = sanitize_title($_POST['category_nicename'], $cat_ID);
    114     $category_description = $_POST['category_description'];
    115    
    116     $wpdb->query("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$cat' WHERE cat_ID = '$cat_ID'");
     99    wp_update_category($_POST);
    117100
    118101    header('Location: categories.php?message=3');
     
    166149        <input type="text" name="cat_name" value="" /></p>
    167150        <p><?php _e('Category parent:') ?><br />
    168         <select name='cat' class='postform'>
     151        <select name='category_parent' class='postform'>
    169152        <option value='0'><?php _e('None') ?></option>
    170153        <?php wp_dropdown_cats(0); ?>
Note: See TracChangeset for help on using the changeset viewer.