diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 6090901cf3..575c2d192b 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -791,12 +791,15 @@ function cat_rows( $parent = 0, $level = 0, $categories = 0 ) { if (!$categories ) $categories = get_categories( 'hide_empty=0' ); + $children = _get_category_hierarchy(); + if ( $categories ) { ob_start(); foreach ( $categories as $category ) { if ( $category->category_parent == $parent) { echo "\t" . _cat_row( $category, $level ); - cat_rows( $category->cat_ID, $level +1, $categories ); + if ( isset($children[$category->cat_ID]) ) + cat_rows( $category->cat_ID, $level +1, $categories ); } } $output = ob_get_contents(); diff --git a/wp-includes/category.php b/wp-includes/category.php index ba438a1840..25051b0a66 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -109,8 +109,11 @@ function &get_categories($args = '') { unset($cat_stamps); } - if ( $child_of || $hierarchical ) - $categories = & _get_cat_children($child_of, $categories); + if ( $child_of || $hierarchical ) { + $children = _get_category_hierarchy(); + if ( ! empty($children) ) + $categories = & _get_cat_children($child_of, $categories); + } // Update category counts to include children. if ( $pad_counts ) @@ -260,12 +263,16 @@ function &_get_cat_children($category_id, $categories) { return array(); $category_list = array(); + $children = _get_category_hierarchy(); foreach ( $categories as $category ) { if ( $category->cat_ID == $category_id ) continue; if ( $category->category_parent == $category_id ) { $category_list[] = $category; + if ( !isset($children[$category->cat_ID]) ) + continue; + if ( $children = _get_cat_children($category->cat_ID, $categories) ) $category_list = array_merge($category_list, $children); } @@ -313,4 +320,19 @@ function _pad_category_counts($type, &$categories) { $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items); } +function _get_category_hierarchy() { + $children = get_option('category_children'); + if ( is_array($children) ) + return $children; + + $children = array(); + $categories = get_categories('hide_empty=0&hierarchical=0'); + foreach ( $categories as $cat ) { + if ( $cat->category_parent > 0 ) + $children[$cat->category_parent][] = $cat->cat_ID; + } + update_option('category_children', $children); + + return $children; +} ?> diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 6df88a2649..b0d02ba28f 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -749,6 +749,7 @@ function clean_category_cache($id) { wp_cache_delete($id, 'category'); wp_cache_delete('all_category_ids', 'category'); wp_cache_delete('get_categories', 'category'); + delete_option('category_children'); } /*