diff --git a/wp-admin/admin-db.php b/wp-admin/admin-db.php index 51021e260..62e7a0fc4 100644 --- a/wp-admin/admin-db.php +++ b/wp-admin/admin-db.php @@ -105,11 +105,21 @@ 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) { - $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$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 ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')"); $cat_ID = $wpdb->insert_id; } 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 == '' ) { @@ -153,7 +163,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); @@ -166,9 +179,29 @@ function wp_delete_category($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'"); + // 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'); @@ -244,6 +277,7 @@ 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; @@ -280,19 +314,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_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 @@ -311,8 +352,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); } @@ -321,9 +370,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_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; diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 7d1254622..c4dc4c0e4 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -476,6 +476,7 @@ function get_link_to_edit($link_id) { $link->link_description = wp_specialchars($link->link_description); $link->link_notes = wp_specialchars($link->link_notes); $link->link_rss = wp_specialchars($link->link_rss); + $link->post_category = $link->link_category; return $link; } @@ -491,6 +492,8 @@ function get_default_link_to_edit() { else $link->link_name = ''; + $link->link_visible = 'Y'; + return $link; } @@ -507,14 +510,7 @@ function edit_link($link_id = '') { $_POST['link_name'] = wp_specialchars($_POST['link_name']); $_POST['link_image'] = wp_specialchars($_POST['link_image']); $_POST['link_rss'] = wp_specialchars($_POST['link_rss']); - $auto_toggle = get_autotoggle($_POST['link_category']); - - // if we are in an auto toggle category and this one is visible then we - // need to make the others invisible before we add this new one. - // FIXME Add category toggle func. - //if (($auto_toggle == 'Y') && ($link_visible == 'Y')) { - // $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category"); - //} + $_POST['link_category'] = $_POST['post_category']; if ( !empty($link_id) ) { $_POST['link_id'] = $link_id; @@ -554,7 +550,7 @@ function sort_cats($cat1, $cat2) { } function get_nested_categories($default = 0, $parent = 0) { - global $post_ID, $mode, $wpdb; + global $post_ID, $link_id, $mode, $wpdb; if ($post_ID) { $checked_categories = $wpdb->get_col(" @@ -567,7 +563,17 @@ function get_nested_categories($default = 0, $parent = 0) { // No selected categories, strange $checked_categories[] = $default; } + } else if ($link_id) { + $checked_categories = $wpdb->get_col(" + SELECT category_id + FROM $wpdb->categories, $wpdb->link2cat + WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id' + "); + if (count($checked_categories) == 0) { + // No selected categories, strange + $checked_categories[] = $default; + } } else { $checked_categories[] = $default; } @@ -620,9 +626,10 @@ function cat_rows($parent = 0, $level = 0, $categories = 0) { if ( current_user_can('manage_categories') ) { $edit = "".__('Edit').""; $default_cat_id = get_option('default_category'); + $default_link_cat_id = get_option('default_link_category'); - if ($category->cat_ID != $default_cat_id) - $edit .= "cat_ID, '".sprintf(__("You are about to delete the category "%s". All of its posts will go to the default category.\\n"OK" to delete, "Cancel" to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete').""; + if ( ($category->cat_ID != $default_cat_id) && ($category->cat_ID != $default_link_cat_id) ) + $edit .= "cat_ID, '".sprintf(__("You are about to delete the category "%s". All of its posts and bookmarks will go to the default categories.\\n"OK" to delete, "Cancel" to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete').""; else $edit .= "".__("Default"); } @@ -633,6 +640,7 @@ function cat_rows($parent = 0, $level = 0, $categories = 0) { echo "$category->cat_ID$pad $category->cat_name $category->category_description $category->category_count + $category->link_count $edit "; cat_rows($category->cat_ID, $level +1, $categories); @@ -687,7 +695,6 @@ function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $lev if ($categories) { foreach ($categories as $category) { if ($currentcat != $category->cat_ID && $parent == $category->category_parent) { - $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); $pad = str_repeat('– ', $level); $category->cat_name = wp_specialchars($category->cat_name); echo "\n\t