diff --git a/wp-admin/admin-db.php b/wp-admin/admin-db.php index 816a69828..487b88256 100644 --- a/wp-admin/admin-db.php +++ b/wp-admin/admin-db.php @@ -80,4 +80,262 @@ function get_nonauthor_user_ids() { return $wpdb->get_col( $query ); } +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) { + do_action('edit_category', $cat_ID); + } else { + do_action('create_category', $rval); + do_action('add_category', $rval); + } + + return $cat_ID; +} + +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 wp_create_category($cat_name) { + $cat_array = compact('cat_name'); + return wp_insert_category($cat_array); +} + +function wp_create_categories($categories, $post_id = '') { + $cat_ids = array (); + foreach ($categories as $category) { + if ($id = category_exists($category)) + $cat_ids[] = $id; + else + if ($id = wp_create_category($category)) + $cat_ids[] = $id; + } + + if ($post_id) + wp_set_post_cats('', $post_id, $cat_ids); + + return $cat_ids; +} + +function category_exists($cat_name) { + global $wpdb; + if (!$category_nicename = sanitize_title($cat_name)) + return 0; + + return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'"); +} + +function wp_delete_user($id, $reassign = 'novalue') { + global $wpdb; + + $id = (int) $id; + + if ($reassign == 'novalue') { + $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id"); + + if ($post_ids) { + $post_ids = implode(',', $post_ids); + + // Delete comments, *backs + $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)"); + // Clean cats + $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id IN ($post_ids)"); + // Clean post_meta + $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids)"); + // Delete posts + $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id"); + } + + // Clean links + $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id"); + } else { + $reassign = (int) $reassign; + $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}"); + $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}"); + } + + // FINALLY, delete user + $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id"); + + do_action('delete_user', $id); + + return true; +} + +function get_link($link_id, $output = OBJECT) { + global $wpdb; + + $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'"); + + if ( $output == OBJECT ) { + return $link; + } elseif ( $output == ARRAY_A ) { + return get_object_vars($link); + } elseif ( $output == ARRAY_N ) { + return array_values(get_object_vars($link)); + } else { + return $link; + } +} + +function wp_insert_link($linkdata) { + global $wpdb, $current_user; + + extract($linkdata); + + $update = false; + if ( !empty($link_id) ) + $update = true; + + if ( empty($link_rating) ) + $link_rating = 0; + + if ( empty($link_target) ) + $link_target = ''; + + if ( empty($link_visible) ) + $link_visible = 'Y'; + + if ( empty($link_owner) ) + $link_owner = $current_user->id; + + if ( $update ) { + $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url', + link_name='$link_name', link_image='$link_image', + link_target='$link_target', link_category='$link_category', + link_visible='$link_visible', link_description='$link_description', + link_rating='$link_rating', link_rel='$link_rel', + link_notes='$link_notes', link_rss = '$link_rss' + WHERE link_id='$link_id'"); + } else { + $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')"); + $link_id = $wpdb->insert_id; + } + + if ( $update ) + do_action('edit_link', $link_id); + else + do_action('add_link', $link_id); + + return $link_id; +} + +function wp_update_link($linkdata) { + global $wpdb; + + $link_id = (int) $linkdata['link_id']; + + $link = get_link($link_id, ARRAY_A); + + // Escape data pulled from DB. + $link = add_magic_quotes($link); + + // Merge old and new fields with new fields overwriting old ones. + $linkdata = array_merge($link, $linkdata); + + return wp_insert_link($linkdata); +} + +function wp_delete_link($link_id) { + global $wpdb; + + return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'"); +} + +function post_exists($title, $content = '', $post_date = '') { + global $wpdb; + + if (!empty ($post_date)) + $post_date = "AND post_date = '$post_date'"; + + if (!empty ($title)) + return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date"); + else + if (!empty ($content)) + return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date"); + + return 0; +} + +function comment_exists($comment_author, $comment_date) { + global $wpdb; + + return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments + WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'"); +} + ?> \ No newline at end of file diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 6a29a6392..784431e16 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -250,125 +250,6 @@ function get_category_to_edit($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) { - do_action('edit_category', $cat_ID); - } else { - do_action('create_category', $rval); - do_action('add_category', $rval); - } - - return $cat_ID; -} - -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 wp_create_category($cat_name) { - $cat_array = compact('cat_name'); - return wp_insert_category($cat_array); -} - -function wp_create_categories($categories, $post_id = '') { - $cat_ids = array (); - foreach ($categories as $category) { - if ($id = category_exists($category)) - $cat_ids[] = $id; - else - if ($id = wp_create_category($category)) - $cat_ids[] = $id; - } - - if ($post_id) - wp_set_post_cats('', $post_id, $cat_ids); - - return $cat_ids; -} - -function category_exists($cat_name) { - global $wpdb; - if (!$category_nicename = sanitize_title($cat_name)) - return 0; - - return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'"); -} - // Creates a new user from the "Users" form using $_POST information. function add_user() { @@ -467,58 +348,6 @@ function edit_user($user_id = 0) { return $errors; } -function wp_delete_user($id, $reassign = 'novalue') { - global $wpdb; - - $id = (int) $id; - - if ($reassign == 'novalue') { - $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id"); - - if ($post_ids) { - $post_ids = implode(',', $post_ids); - - // Delete comments, *backs - $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($post_ids)"); - // Clean cats - $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id IN ($post_ids)"); - // Clean post_meta - $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids)"); - // Delete posts - $wpdb->query("DELETE FROM $wpdb->posts WHERE post_author = $id"); - } - - // Clean links - $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id"); - } else { - $reassign = (int) $reassign; - $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}"); - $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}"); - } - - // FINALLY, delete user - $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id"); - - do_action('delete_user', $id); - - return true; -} - -function get_link($link_id, $output = OBJECT) { - global $wpdb; - - $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'"); - - if ( $output == OBJECT ) { - return $link; - } elseif ( $output == ARRAY_A ) { - return get_object_vars($link); - } elseif ( $output == ARRAY_N ) { - return array_values(get_object_vars($link)); - } else { - return $link; - } -} function get_link_to_edit($link_id) { $link = get_link($link_id); @@ -576,92 +405,6 @@ function edit_link($link_id = '') { } } -function wp_insert_link($linkdata) { - global $wpdb, $current_user; - - extract($linkdata); - - $update = false; - if ( !empty($link_id) ) - $update = true; - - if ( empty($link_rating) ) - $link_rating = 0; - - if ( empty($link_target) ) - $link_target = ''; - - if ( empty($link_visible) ) - $link_visible = 'Y'; - - if ( empty($link_owner) ) - $link_owner = $current_user->id; - - if ( $update ) { - $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url', - link_name='$link_name', link_image='$link_image', - link_target='$link_target', link_category='$link_category', - link_visible='$link_visible', link_description='$link_description', - link_rating='$link_rating', link_rel='$link_rel', - link_notes='$link_notes', link_rss = '$link_rss' - WHERE link_id='$link_id'"); - } else { - $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')"); - $link_id = $wpdb->insert_id; - } - - if ( $update ) - do_action('edit_link', $link_id); - else - do_action('add_link', $link_id); - - return $link_id; -} - -function wp_update_link($linkdata) { - global $wpdb; - - $link_id = (int) $linkdata['link_id']; - - $link = get_link($link_id, ARRAY_A); - - // Escape data pulled from DB. - $link = add_magic_quotes($link); - - // Merge old and new fields with new fields overwriting old ones. - $linkdata = array_merge($link, $linkdata); - - return wp_insert_link($linkdata); -} - -function wp_delete_link($link_id) { - global $wpdb; - - return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'"); -} - -function post_exists($title, $content = '', $post_date = '') { - global $wpdb; - - if (!empty ($post_date)) - $post_date = "AND post_date = '$post_date'"; - - if (!empty ($title)) - return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date"); - else - if (!empty ($content)) - return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date"); - - return 0; -} - -function comment_exists($comment_author, $comment_date) { - global $wpdb; - - return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments - WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'"); -} - function url_shorten($url) { $short_url = str_replace('http://', '', stripslashes($url)); $short_url = str_replace('www.', '', $short_url);