Cache category hierarchy to make cat listing faster. Phase 1. see #3985

git-svn-id: http://svn.automattic.com/wordpress/trunk@5178 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-04-04 20:44:45 +00:00
parent 57cf80f5d1
commit 7f0a905ee6
3 changed files with 29 additions and 3 deletions

View File

@ -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();

View File

@ -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;
}
?>

View File

@ -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');
}
/*