diff options
| author | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2007-01-09 11:30:05 +0000 |
|---|---|---|
| committer | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2007-01-09 11:30:05 +0000 |
| commit | 819f131230b6d2e4ff88a8773eef754228989ffc (patch) | |
| tree | 547908e700cf516755e371de9a2149ff5a772832 /wp-includes/category.php | |
| parent | 90888943466530c65823eac7e07e5a6f1c8ab5d5 (diff) | |
| download | wordpress-mu-819f131230b6d2e4ff88a8773eef754228989ffc.tar.gz wordpress-mu-819f131230b6d2e4ff88a8773eef754228989ffc.tar.xz wordpress-mu-819f131230b6d2e4ff88a8773eef754228989ffc.zip | |
WP Merge to rev 4709
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@835 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-includes/category.php')
| -rw-r--r-- | wp-includes/category.php | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/wp-includes/category.php b/wp-includes/category.php index 56cdbb9..6025a67 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -21,7 +21,7 @@ function &get_categories($args = '') { $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => '', 'include' => '', - 'number' => ''); + 'number' => '', 'pad_counts' => false); $r = array_merge($defaults, $r); if ( 'count' == $r['orderby'] ) $r['orderby'] = 'category_count'; @@ -109,17 +109,21 @@ function &get_categories($args = '') { $categories = & _get_cat_children($child_of, $categories); // Update category counts to include children. - if ( $hierarchical ) { + if ( $pad_counts ) + _pad_category_counts($type, $categories); + + // Make sure we show empty categories that have children. + if ( $hierarchical && $hide_empty ) { foreach ( $categories as $k => $category ) { - $progeny = 'link' == $type ? $category->link_count : $category->category_count; - if ( $children = _get_cat_children($category->cat_ID, $categories) ) { + if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) { + $children = _get_cat_children($category->cat_ID, $categories); foreach ( $children as $child ) - $progeny += 'link' == $type ? $child->link_count : $child->category_count; - } - if ( !$progeny && $hide_empty ) + if ( $child->{'link' == $type ? 'link_count' : 'category_count'} ) + continue 2; + + // It really is empty unset($categories[$k]); - else - $categories[$k]->{'link' == $type ? 'link_count' : 'category_count'} = $progeny; + } } } reset ( $categories ); @@ -256,4 +260,43 @@ function &_get_cat_children($category_id, $categories) { return $category_list; } +// Recalculates link or post counts by including items from child categories +// Assumes all relevant children are already in the $categories argument +function _pad_category_counts($type, &$categories) { + global $wpdb; + + // Set up some useful arrays + foreach ( $categories as $key => $cat ) { + $cats[$cat->cat_ID] = & $categories[$key]; + $cat_IDs[] = $cat->cat_ID; + } + + // Get the relevant post2cat or link2cat records and stick them in a lookup table + if ( $type == 'post' ) { + $results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'"); + foreach ( $results as $row ) + ++$cat_items[$row->category_id][$row->post_id]; + } else { + $results = $wpdb->get_results("SELECT $wpdb->link2cat.link_id, category_id FROM $wpdb->link2cat LEFT JOIN $wpdb->links USING (link_id) WHERE category_id IN (".join(',', $cat_IDs).") AND link_visible = 'Y'"); + foreach ( $results as $row ) + ++$cat_items[$row->category_id][$row->link_id]; + } + + // Touch every ancestor's lookup row for each post in each category + foreach ( $cat_IDs as $cat_ID ) { + $child = $cat_ID; + while ( $parent = $cats[$child]->category_parent ) { + if ( !empty($cat_items[$cat_ID]) ) + foreach ( $cat_items[$cat_ID] as $item_id => $touches ) + ++$cat_items[$parent][$item_id]; + $child = $parent; + } + } + + // Transfer the touched cells + foreach ( $cat_items as $id => $items ) + if ( isset($cats[$id]) ) + $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items); +} + ?> |
