diff options
| author | matt <matt@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2006-05-15 03:19:56 +0000 |
|---|---|---|
| committer | matt <matt@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2006-05-15 03:19:56 +0000 |
| commit | 034694eb53dd71a980745f5594509890fd761998 (patch) | |
| tree | c85517e8af3fce163f1d183afdf7c9ac7094d84e /wp-inst/wp-admin/admin-db.php | |
| parent | a72266931b88c8fa300d2e29a69a2bb38a70654a (diff) | |
| download | wordpress-mu-034694eb53dd71a980745f5594509890fd761998.tar.gz wordpress-mu-034694eb53dd71a980745f5594509890fd761998.tar.xz wordpress-mu-034694eb53dd71a980745f5594509890fd761998.zip | |
Lots and lots of changes.
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@543 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-inst/wp-admin/admin-db.php')
| -rw-r--r-- | wp-inst/wp-admin/admin-db.php | 237 |
1 files changed, 226 insertions, 11 deletions
diff --git a/wp-inst/wp-admin/admin-db.php b/wp-inst/wp-admin/admin-db.php index 32b79f4..84d73fc 100644 --- a/wp-inst/wp-admin/admin-db.php +++ b/wp-inst/wp-admin/admin-db.php @@ -94,6 +94,9 @@ function wp_insert_category($catarr) { $cat_name = wp_specialchars($cat_name); + if ( !$update && category_exists($cat_name) ) + return 0; + if (empty ($category_nicename)) $category_nicename = sanitize_title($cat_name); else @@ -105,12 +108,22 @@ function wp_insert_category($catarr) { if (empty ($category_parent)) $category_parent = 0; + if ( isset($posts_private) ) + $posts_private = (int) $posts_private; + else + $posts_private = 0; + + if ( isset($links_private) ) + $links_private = (int) $links_private; + else + $links_private = 0; + if (!$update) { $maxcat = $wpdb->get_var( "SELECT max(cat_ID) FROM {$wpdb->categories}" ); $cat_ID = mt_rand( $maxcat+100, $maxcat+4000 ); - $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('$cat_ID', '$cat_name', '$category_nicename', '$category_description', '$category_parent')"); + $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('$cat_ID', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')"); } else { - $wpdb->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'"); + $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_private = '$links_private', posts_private = '$posts_private' WHERE cat_ID = '$cat_ID'"); } if ( $category_nicename == '' ) { @@ -129,8 +142,6 @@ function wp_insert_category($catarr) { } $cat_ID = apply_filters( "cat_id_filter", $cat_ID ); - update_option( 'categories_last_updated', time() ); - return $cat_ID; } @@ -157,7 +168,10 @@ function wp_delete_category($cat_ID) { $cat_ID = (int) $cat_ID; // Don't delete the default cat. - if (1 == $cat_ID) + if ( $cat_ID == get_option('default_category') ) + return 0; + + if ( $cat_ID == get_option('default_link_category') ) return 0; $category = get_category($cat_ID); @@ -165,17 +179,37 @@ function wp_delete_category($cat_ID) { $parent = $category->category_parent; // Delete the category. - $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'"); + if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") ) + return 0; // 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'"); + // Only set posts and links to the default category if they're not in another category already. + $default_cat = get_option('default_category'); + $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'"); + if ( is_array($posts) ) foreach ($posts as $post_id) { + $cats = wp_get_post_cats('', $post_id); + if ( 1 == count($cats) ) + $cats = array($default_cat); + else + $cats = array_diff($cats, array($cat_ID)); + wp_set_post_cats('', $post_id, $cats); + } + $default_link_cat = get_option('default_link_category'); + $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'"); + if ( is_array($links) ) foreach ($links as $link_id) { + $cats = wp_get_link_cats($link_id); + if ( 1 == count($cats) ) + $cats = array($default_link_cat); + else + $cats = array_diff($cats, array($cat_ID)); + wp_set_link_cats($link_id, $cats); + } + wp_cache_delete($cat_ID, 'category'); wp_cache_delete('all_category_ids', 'category'); - update_option( 'categories_last_updated', time() ); do_action('delete_category', $cat_ID); @@ -244,10 +278,18 @@ function wp_delete_user($id, $reassign = 'novalue') { return true; } +function wp_revoke_user($id) { + $id = (int) $id; + + $user = new WP_User($id); + $user->remove_all_caps(); +} + function get_link($link_id, $output = OBJECT) { global $wpdb; $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'"); + $link->link_category = wp_get_link_cats($link_id); if ( $output == OBJECT ) { return $link; @@ -284,19 +326,26 @@ function wp_insert_link($linkdata) { if ( empty($link_notes) ) $link_notes = ''; + // Make sure we set a valid category + if (0 == count($link_category) || !is_array($link_category)) { + $link_category = array(get_option('default_link_category')); + } + 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_target='$link_target', 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')"); + $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')"); $link_id = $wpdb->insert_id; } + wp_set_link_cats($link_id, $link_category); + if ( $update ) do_action('edit_link', $link_id); else @@ -315,8 +364,16 @@ function wp_update_link($linkdata) { // Escape data pulled from DB. $link = add_magic_quotes($link); + // Passed link category list overwrites existing category list if not empty. + if ( isset($linkdata['link_category']) && is_array($linkdata['link_category']) + && 0 != count($linkdata['link_category']) ) + $link_cats = $linkdata['link_category']; + else + $link_cats = $link['link_category']; + // Merge old and new fields with new fields overwriting old ones. $linkdata = array_merge($link, $linkdata); + $linkdata['link_category'] = $link_cats; return wp_insert_link($linkdata); } @@ -325,9 +382,88 @@ function wp_delete_link($link_id) { global $wpdb; do_action('delete_link', $link_id); + + $categories = wp_get_link_cats($link_id); + if( is_array( $categories ) ) { + foreach ( $categories as $category ) { + $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'"); + wp_cache_delete($category, 'category'); + } + } + + $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'"); return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'"); } +function wp_get_link_cats($link_ID = 0) { + global $wpdb; + + $sql = "SELECT category_id + FROM $wpdb->link2cat + WHERE link_id = $link_ID + ORDER BY category_id"; + + $result = $wpdb->get_col($sql); + + if ( !$result ) + $result = array(); + + return array_unique($result); +} + +function wp_set_link_cats($link_ID = 0, $link_categories = array()) { + global $wpdb; + // If $link_categories isn't already an array, make it one: + if (!is_array($link_categories) || 0 == count($link_categories)) + $link_categories = array(get_option('default_link_category')); + + $link_categories = array_unique($link_categories); + + // First the old categories + $old_categories = $wpdb->get_col(" + SELECT category_id + FROM $wpdb->link2cat + WHERE link_id = $link_ID"); + + if (!$old_categories) { + $old_categories = array(); + } else { + $old_categories = array_unique($old_categories); + } + + // Delete any? + $delete_cats = array_diff($old_categories,$link_categories); + + if ($delete_cats) { + foreach ($delete_cats as $del) { + $wpdb->query(" + DELETE FROM $wpdb->link2cat + WHERE category_id = $del + AND link_id = $link_ID + "); + } + } + + // Add any? + $add_cats = array_diff($link_categories, $old_categories); + + if ($add_cats) { + foreach ($add_cats as $new_cat) { + $wpdb->query(" + INSERT INTO $wpdb->link2cat (link_id, category_id) + VALUES ($link_ID, $new_cat)"); + } + } + + // Update category counts. + $all_affected_cats = array_unique(array_merge($link_categories, $old_categories)); + foreach ( $all_affected_cats as $cat_id ) { + $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'"); + $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'"); + wp_cache_delete($cat_id, 'category'); + } +} // wp_set_link_cats() + function post_exists($title, $content = '', $post_date = '') { global $wpdb; @@ -350,4 +486,83 @@ function comment_exists($comment_author, $comment_date) { WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'"); } +function wpmu_delete_blog($blog_id, $drop = false) { + global $wpdb, $wpmuBaseTablePrefix; + + if ( $blog_id != $wpdb->blogid ) { + $switch = true; + switch_to_blog($blog_id); + } + + do_action('delete_blog', $blog_id, $drop); + + $users = get_users_of_blog($blog_id); + + // Remove users from this blog. + if ( !empty($users) ) foreach ($users as $user) { + remove_user_from_blog($user->user_id, $blog_id); + } + + update_blog_status( $wpdb->blogid, 'deleted', 1 ); + + if ( $drop ) { + $drop_tables = array( $wpmuBaseTablePrefix . $blog_id . "_categories", + $wpmuBaseTablePrefix . $blog_id . "_comments", + $wpmuBaseTablePrefix . $blog_id . "_linkcategories", + $wpmuBaseTablePrefix . $blog_id . "_links", + $wpmuBaseTablePrefix . $blog_id . "_link2cat", + $wpmuBaseTablePrefix . $blog_id . "_options", + $wpmuBaseTablePrefix . $blog_id . "_post2cat", + $wpmuBaseTablePrefix . $blog_id . "_postmeta", + $wpmuBaseTablePrefix . $blog_id . "_posts", + $wpmuBaseTablePrefix . $blog_id . "_referer_visitLog", + $wpmuBaseTablePrefix . $blog_id . "_referer_blacklist" ); + reset( $drop_tables ); + + while( list( $key, $val ) = each( $drop_tables ) ) + $wpdb->query( "DROP TABLE IF EXISTS $val" ); + + $wpdb->query( "DELETE FROM $wpdb->blogs WHERE blog_id = '$blog_id'" ); + } + + if ( $switch ) + restore_current_blog(); +} + +function wpmu_delete_user($id) { + global $wpdb; + + $id = (int) $id; + $user = get_userdata($id); + + do_action('wpmu_delete_user', $id); + + $blogs = get_blogs_of_user($id); + + if ( ! empty($blogs) ) foreach ($blogs as $blog) { + switch_to_blog($blog->userblog_id); + remove_user_from_blog($id, $blog->userblog_id); + + $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id"); + + if ($post_ids) { + foreach ($post_ids as $post_id) + wp_delete_post($post_id); + } + + // Clean links + $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id"); + + restore_current_blog(); + } + + $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id"); + $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'"); + + wp_cache_delete($id, 'users'); + wp_cache_delete($user->user_login, 'userlogins'); + + return true; +} + ?> |
