From 01fef613ff78c83d9e201ddb6389a24c8c858ff5 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sun, 3 Jul 2005 19:26:51 +0000 Subject: [PATCH] wp_insert_category(), wp_update_category(), wp_delete_category(). git-svn-id: https://develop.svn.wordpress.org/trunk@2695 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/admin-functions.php | 97 ++++++++++++++++++++++++++++++++++++ wp-admin/categories.php | 43 +++++----------- 2 files changed, 110 insertions(+), 30 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 2cb3393a47..56cb1fa63b 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -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); diff --git a/wp-admin/categories.php b/wp-admin/categories.php index 755c976545..5b327bed10 100644 --- a/wp-admin/categories.php +++ b/wp-admin/categories.php @@ -23,18 +23,12 @@ for ($i=0; $iget_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’ 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 %s category: this is the default one"), $cat_name)); - if ( $user_level < 3 ) - die (__('Cheatin’ 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); ?>
@@ -76,8 +64,8 @@ case 'edit': - + @@ -86,7 +74,7 @@ case 'edit': @@ -108,12 +96,7 @@ case 'editedcat': if ($user_level < 3) die (__('Cheatin’ 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();



-

- +
- cat_ID, $category->category_parent); ?>