Index: wp-admin/admin-functions.php
===================================================================
--- wp-admin/admin-functions.php	(revision 3672)
+++ wp-admin/admin-functions.php	(working copy)
@@ -545,7 +545,7 @@
 		echo ' checked="checked"';
 }
 
-function return_categories_list($parent = 0) {
+function return_categories_list($parent = 0) { // deprecated
 	global $wpdb;
 	return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY category_count DESC LIMIT 100");
 }
@@ -554,45 +554,52 @@
 	return strcasecmp($cat1['cat_name'], $cat2['cat_name']);
 }
 
-function get_nested_categories($default = 0, $parent = 0) {
+function get_nested_categories($default = 0, $parent = 0, $cats = false, $checked_categories = false ) {
 	global $post_ID, $link_id, $mode, $wpdb;
 
-	if ($post_ID) {
-		$checked_categories = $wpdb->get_col("
-		     SELECT category_id
-		     FROM $wpdb->categories, $wpdb->post2cat
-		     WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID'
-		     ");
+	if ( !$cats )
+		$cats = get_all_category_ids();
+	if ( !$cats )
+		return false;
 
-		if (count($checked_categories) == 0) {
-			// No selected categories, strange
+	if ( false === $checked_categories ) {
+		if ( $post_ID ) {
+			$checked_categories = $wpdb->get_col("
+			     SELECT category_id
+			     FROM $wpdb->categories, $wpdb->post2cat
+			     WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID'
+			     ");
+
+			if (count($checked_categories) == 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;
 		}
-	} 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;
 	}
 
-	$cats = return_categories_list($parent);
 	$result = array ();
 
-	if (is_array($cats)) {
-		foreach ($cats as $cat) {
-			$result[$cat]['children'] = get_nested_categories($default, $cat);
-			$result[$cat]['cat_ID'] = $cat;
-			$result[$cat]['checked'] = in_array($cat, $checked_categories);
-			$result[$cat]['cat_name'] = get_the_category_by_ID($cat);
-		}
+	foreach ( (array) $cats as $cat ) {
+		$_cat = &get_category( $cat );
+		if ( $parent != $_cat->category_parent )
+			continue;
+		$result[$cat]['children'] = get_nested_categories( $default, $cat, $cats, $checked_categories );
+		$result[$cat]['cat_ID'] = $cat;
+		$result[$cat]['checked'] = in_array($cat, $checked_categories);
+		$result[$cat]['cat_name'] = $_cat->cat_name;
 	}
 
 	usort($result, 'sort_cats');
@@ -617,42 +624,41 @@
 }
 
 // Dandy new recursive multiple category stuff.
-function cat_rows($parent = 0, $level = 0, $categories = 0) {
-	global $wpdb, $class;
+function cat_rows($level = 0, $categories = 0) {
+	global $class;
 
 	if (!$categories)
-		$categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
+		$categories = get_nested_categories();
+	if (!$categories)
+		return false;
 
-	if ($categories) {
-		foreach ($categories as $category) {
-			if ($category->category_parent == $parent) {
-				$category->cat_name = wp_specialchars($category->cat_name,'double');
-				$pad = str_repeat('&#8212; ', $level);
-				if ( current_user_can('manage_categories') ) {
-					$edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
-					$default_cat_id = get_option('default_category');
-					$default_link_cat_id = get_option('default_link_category');
+	$default_cat_id = get_option('default_category');
+	$default_link_cat_id = get_option('default_link_category');
 
-					if ( ($category->cat_ID != $default_cat_id) && ($category->cat_ID != $default_link_cat_id) )
-						$edit .= "<td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;.\\nAll of its posts will go into the default category of &quot;%s&quot;\\nAll of its bookmarks will go into the default category of &quot;%s&quot;.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), addslashes($category->cat_name), addslashes(wp_specialchars(get_catname($default_cat_id),'double')), addslashes(wp_specialchars(get_catname($default_link_cat_id),'double')))."' );\" class='delete'>".__('Delete')."</a>";
-					else
-						$edit .= "<td style='text-align:center'>".__("Default");
-				}
-				else
-					$edit = '';
+	foreach ($categories as $_category) {
+		$category = &get_category( $_category['cat_ID'] );
+		$_category['cat_name'] = wp_specialchars($_category['cat_name'],'double');
+		$pad = str_repeat('&#8212; ', $level);
+		if ( current_user_can('manage_categories') ) {
+			$edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
 
-				$class = ('alternate' == $class) ? '' : 'alternate';
-				echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td>
-								<td>$category->category_description</td>
-								<td>$category->category_count</td>
-								<td>$category->link_count</td>
-								<td>$edit</td>
-								</tr>";
-				cat_rows($category->cat_ID, $level +1, $categories);
-			}
+			if ( ($category->cat_ID != $default_cat_id) && ($category->cat_ID != $default_link_cat_id) )
+				$edit .= "<td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;.\\nAll of its posts will go into the default category of &quot;%s&quot;\\nAll of its bookmarks will go into the default category of &quot;%s&quot;.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), addslashes($_category['cat_name']), addslashes(wp_specialchars(get_catname($default_cat_id),'double')), addslashes(wp_specialchars(get_catname($default_link_cat_id),'double')))."' );\" class='delete'>".__('Delete')."</a>";
+			else
+				$edit .= "<td style='text-align:center'>".__("Default");
 		}
-	} else {
-		return false;
+		else
+			$edit = '';
+
+		$class = ('alternate' == $class) ? '' : 'alternate';
+		echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad {$_category['cat_name']}</td>
+						<td>$category->category_description</td>
+						<td>$category->category_count</td>
+						<td>$category->link_count</td>
+						<td>$edit</td>
+						</tr>";
+		if ( $_category['children'] )
+			cat_rows($level + 1, $_category['children']);
 	}
 }
 
@@ -692,25 +698,24 @@
 	}
 }
 
-function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) {
-	global $wpdb, $bgcolor;
-	if (!$categories) {
-		$categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
-	}
-	if ($categories) {
-		foreach ($categories as $category) {
-			if ($currentcat != $category->cat_ID && $parent == $category->category_parent) {
-				$pad = str_repeat('&#8211; ', $level);
-				$category->cat_name = wp_specialchars($category->cat_name);
-				echo "\n\t<option value='$category->cat_ID'";
-				if ($currentparent == $category->cat_ID)
-					echo " selected='selected'";
-				echo ">$pad$category->cat_name</option>";
-				wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level +1, $categories);
-			}
+function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $level = 0, $categories = 0) {
+	global $bgcolor;
+	if ( !$categories )
+		$categories = get_nested_categories();
+	if ( !$categories )
+		return false;
+
+	foreach ($categories as $category) {
+		if ($currentcat != $category['cat_ID'] ) {
+			$pad = str_repeat('&#8211; ', $level);
+			$category['cat_name'] = wp_specialchars($category['cat_name']);
+			echo "\n\t<option value='{$category['cat_ID']}'";
+			if ($currentparent == $category['cat_ID'])
+				echo " selected='selected'";
+			echo ">$pad{$category['cat_name']}</option>";
+			if ( $category['children'] )
+				wp_dropdown_cats($currentcat, $currentparent, $level + 1, $category['children']);
 		}
-	} else {
-		return false;
 	}
 }
 
