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

git-svn-id: https://develop.svn.wordpress.org/trunk@5178 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2007-04-04 20:44:45 +00:00
parent 8a155e75f5
commit 93cdc6deac
3 changed files with 29 additions and 3 deletions

View File

@ -791,11 +791,14 @@ 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 );
if ( isset($children[$category->cat_ID]) )
cat_rows( $category->cat_ID, $level +1, $categories );
}
}

View File

@ -109,8 +109,11 @@ function &get_categories($args = '') {
unset($cat_stamps);
}
if ( $child_of || $hierarchical )
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');
}
/*