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:
parent
57cf80f5d1
commit
7f0a905ee6
|
@ -791,11 +791,14 @@ function cat_rows( $parent = 0, $level = 0, $categories = 0 ) {
|
||||||
if (!$categories )
|
if (!$categories )
|
||||||
$categories = get_categories( 'hide_empty=0' );
|
$categories = get_categories( 'hide_empty=0' );
|
||||||
|
|
||||||
|
$children = _get_category_hierarchy();
|
||||||
|
|
||||||
if ( $categories ) {
|
if ( $categories ) {
|
||||||
ob_start();
|
ob_start();
|
||||||
foreach ( $categories as $category ) {
|
foreach ( $categories as $category ) {
|
||||||
if ( $category->category_parent == $parent) {
|
if ( $category->category_parent == $parent) {
|
||||||
echo "\t" . _cat_row( $category, $level );
|
echo "\t" . _cat_row( $category, $level );
|
||||||
|
if ( isset($children[$category->cat_ID]) )
|
||||||
cat_rows( $category->cat_ID, $level +1, $categories );
|
cat_rows( $category->cat_ID, $level +1, $categories );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,8 +109,11 @@ function &get_categories($args = '') {
|
||||||
unset($cat_stamps);
|
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);
|
$categories = & _get_cat_children($child_of, $categories);
|
||||||
|
}
|
||||||
|
|
||||||
// Update category counts to include children.
|
// Update category counts to include children.
|
||||||
if ( $pad_counts )
|
if ( $pad_counts )
|
||||||
|
@ -260,12 +263,16 @@ function &_get_cat_children($category_id, $categories) {
|
||||||
return array();
|
return array();
|
||||||
|
|
||||||
$category_list = array();
|
$category_list = array();
|
||||||
|
$children = _get_category_hierarchy();
|
||||||
foreach ( $categories as $category ) {
|
foreach ( $categories as $category ) {
|
||||||
if ( $category->cat_ID == $category_id )
|
if ( $category->cat_ID == $category_id )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ( $category->category_parent == $category_id ) {
|
if ( $category->category_parent == $category_id ) {
|
||||||
$category_list[] = $category;
|
$category_list[] = $category;
|
||||||
|
if ( !isset($children[$category->cat_ID]) )
|
||||||
|
continue;
|
||||||
|
|
||||||
if ( $children = _get_cat_children($category->cat_ID, $categories) )
|
if ( $children = _get_cat_children($category->cat_ID, $categories) )
|
||||||
$category_list = array_merge($category_list, $children);
|
$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);
|
$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;
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -749,6 +749,7 @@ function clean_category_cache($id) {
|
||||||
wp_cache_delete($id, 'category');
|
wp_cache_delete($id, 'category');
|
||||||
wp_cache_delete('all_category_ids', 'category');
|
wp_cache_delete('all_category_ids', 'category');
|
||||||
wp_cache_delete('get_categories', 'category');
|
wp_cache_delete('get_categories', 'category');
|
||||||
|
delete_option('category_children');
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue