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

git-svn-id: http://svn.automattic.com/wordpress/trunk@2695 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-07-03 19:26:51 +00:00
parent 6b0cc52fe1
commit 740381afdf
2 changed files with 110 additions and 30 deletions

View File

@ -204,6 +204,103 @@ function get_comment_to_edit($id) {
return $comment;
}
function get_category_to_edit($id) {
$category = get_category($id);
return $category;
}
function wp_insert_category($catarr) {
global $wpdb;
extract($catarr);
$cat_ID = (int) $cat_ID;
// Are we updating or creating?
if ( !empty($cat_ID) ) {
$update = true;
} else {
$update = false;
$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
$cat_ID = $id_result->Auto_increment;
}
$cat_name = wp_specialchars($cat_name);
if ( empty($category_nicename) )
$category_nicename = sanitize_title($cat_name, $cat_ID);
else
$category_nicename = sanitize_title($category_nicename, $cat_ID);
if ( empty($category_description) )
$category_description = '';
if ( empty($category_parent) )
$category_parent = 0;
if ( !$update)
$query = "INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$cat')";
else
$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'";
$result = $wpdb->query($query);
if ( $update ) {
$rval = $wpdb->rows_affected;
do_action('edit_category', $cat_ID);
} else {
$rval = $wpdb->insert_id;
do_action('create_category', $cat_ID);
}
return $rval;
}
function wp_update_category($catarr) {
global $wpdb;
$cat_ID = (int) $catarr['cat_ID'];
// First, get all of the original fields
$category = get_category($cat_ID, ARRAY_A);
// Escape data pulled from DB.
$category = add_magic_quotes($category);
// Merge old and new fields with new fields overwriting old ones.
$catarr = array_merge($category, $catarr);
return wp_insert_category($catarr);
}
function wp_delete_category($cat_ID) {
global $wpdb;
$cat_ID = (int) $cat_ID;
// Don't delete the default cat.
if ( 1 == $cat_ID )
return 0;
$category = get_category($cat_ID);
$parent = $category->category_parent;
// Delete the category.
$wpdb->query("DELETE FROM $wpdb->categories 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'");
// TODO: Only set categories to general if they're not in another category already
$wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
do_action('delete_category', $cat_ID);
return 1;
}
function url_shorten ($url) {
$short_url = str_replace('http://', '', stripslashes($url));
$short_url = str_replace('www.', '', $short_url);

View File

@ -23,18 +23,12 @@ for ($i=0; $i<count($wpvarstoreset); $i += 1) {
switch($action) {
case 'addcat':
if ($user_level < 3)
die (__('Cheatin&#8217; uh?'));
$cat_name= wp_specialchars($_POST['cat_name']);
$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
$cat_ID = $id_result->Auto_increment;
$category_nicename = sanitize_title($cat_name, $cat_ID);
$category_description = $_POST['category_description'];
$cat = intval($_POST['cat']);
$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')");
wp_insert_category($_POST);
header('Location: categories.php?message=1#addcat');
break;
@ -42,21 +36,16 @@ case 'delete':
check_admin_referer();
if ( $user_level < 3 )
die (__('Cheatin&#8217; uh?'));
$cat_ID = (int) $_GET['cat_ID'];
$cat_name = get_catname($cat_ID);
$category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
$cat_parent = $category->category_parent;
if ( 1 == $cat_ID )
die(sprintf(__("Can't delete the <strong>%s</strong> category: this is the default one"), $cat_name));
if ( $user_level < 3 )
die (__('Cheatin&#8217; uh?'));
$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
$wpdb->query("UPDATE $wpdb->categories SET category_parent = '$cat_parent' WHERE category_parent = '$cat_ID'");
// TODO: Only set categories to general if they're not in another category already
$wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
wp_delete_category($cat_ID);
header('Location: categories.php?message=2');
@ -66,8 +55,7 @@ case 'edit':
require_once ('admin-header.php');
$cat_ID = (int) $_GET['cat_ID'];
$category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
$cat_name = $category->cat_name;
$category = get_category_to_edit($cat_ID);
?>
<div class="wrap">
@ -76,8 +64,8 @@ case 'edit':
<table class="editform" width="100%" cellspacing="2" cellpadding="5">
<tr>
<th width="33%" scope="row"><?php _e('Category name:') ?></th>
<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" />
<input type="hidden" name="cat_ID" value="<?php echo $cat_ID ?>" /></td>
<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" />
<input type="hidden" name="cat_ID" value="<?php echo $category->cat_ID ?>" /></td>
</tr>
<tr>
<th scope="row"><?php _e('Category slug:') ?></th>
@ -86,7 +74,7 @@ case 'edit':
<tr>
<th scope="row"><?php _e('Category parent:') ?></th>
<td>
<select name='cat'>
<select name='category_parent'>
<option value='0' <?php if (!$category->category_parent) echo " selected='selected'"; ?>><?php _e('None') ?></option>
<?php wp_dropdown_cats($category->cat_ID, $category->category_parent); ?>
</select></td>
@ -108,12 +96,7 @@ case 'editedcat':
if ($user_level < 3)
die (__('Cheatin&#8217; uh?'));
$cat_name = wp_specialchars($_POST['cat_name']);
$cat_ID = (int) $_POST['cat_ID'];
$category_nicename = sanitize_title($_POST['category_nicename'], $cat_ID);
$category_description = $_POST['category_description'];
$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'");
wp_update_category($_POST);
header('Location: categories.php?message=3');
break;
@ -165,7 +148,7 @@ cat_rows();
<p><?php _e('Name:') ?><br />
<input type="text" name="cat_name" value="" /></p>
<p><?php _e('Category parent:') ?><br />
<select name='cat' class='postform'>
<select name='category_parent' class='postform'>
<option value='0'><?php _e('None') ?></option>
<?php wp_dropdown_cats(0); ?>
</select></p>