| 3 | | function walk_category_tree() { |
|---|
| 4 | | $walker = new Walker_Category; |
|---|
| 5 | | $args = func_get_args(); |
|---|
| 6 | | return call_user_func_array(array(&$walker, 'walk'), $args); |
|---|
| 7 | | } |
|---|
| 8 | | |
|---|
| 9 | | function walk_category_dropdown_tree() { |
|---|
| 10 | | $walker = new Walker_CategoryDropdown; |
|---|
| 11 | | $args = func_get_args(); |
|---|
| 12 | | return call_user_func_array(array(&$walker, 'walk'), $args); |
|---|
| 13 | | } |
|---|
| 14 | | |
|---|
| 15 | | function get_the_category($id = false) { |
|---|
| 16 | | global $post, $category_cache; |
|---|
| 17 | | |
|---|
| 18 | | if ( !$id ) |
|---|
| 19 | | $id = $post->ID; |
|---|
| 20 | | |
|---|
| 21 | | if ( !isset($category_cache[$id]) ) |
|---|
| 22 | | update_post_category_cache($id); |
|---|
| 23 | | |
|---|
| 24 | | $categories = $category_cache[$id]; |
|---|
| 25 | | |
|---|
| 26 | | if ( !empty($categories) ) |
|---|
| 27 | | sort($categories); |
|---|
| 28 | | else |
|---|
| 29 | | $categories = array(); |
|---|
| 30 | | |
|---|
| 31 | | return $categories; |
|---|
| | 3 | function get_category_children($id, $before = '/', $after = '') { |
|---|
| | 4 | if ( 0 == $id ) |
|---|
| | 5 | return ''; |
|---|
| | 6 | |
|---|
| | 7 | $cat_ids = get_all_category_ids(); |
|---|
| | 8 | foreach ( $cat_ids as $cat_id ) { |
|---|
| | 9 | if ( $cat_id == $id) |
|---|
| | 10 | continue; |
|---|
| | 11 | |
|---|
| | 12 | $category = get_category($cat_id); |
|---|
| | 13 | if ( $category->category_parent == $id ) { |
|---|
| | 14 | $chain .= $before.$category->cat_ID.$after; |
|---|
| | 15 | $chain .= get_category_children($category->cat_ID, $before, $after); |
|---|
| | 16 | } |
|---|
| | 17 | } |
|---|
| | 18 | return $chain; |
|---|
| | 39 | } |
|---|
| | 40 | |
|---|
| | 41 | function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ |
|---|
| | 42 | $chain = ''; |
|---|
| | 43 | $parent = &get_category($id); |
|---|
| | 44 | |
|---|
| | 45 | if ( $nicename ) |
|---|
| | 46 | $name = $parent->category_nicename; |
|---|
| | 47 | else |
|---|
| | 48 | $name = $parent->cat_name; |
|---|
| | 49 | |
|---|
| | 50 | if ( $parent->category_parent ) |
|---|
| | 51 | $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename); |
|---|
| | 52 | |
|---|
| | 53 | if ( $link ) |
|---|
| | 54 | $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; |
|---|
| | 55 | else |
|---|
| | 56 | $chain .= $name.$separator; |
|---|
| | 57 | return $chain; |
|---|
| | 58 | } |
|---|
| | 59 | |
|---|
| | 60 | function get_the_category($id = false) { |
|---|
| | 61 | global $post, $category_cache; |
|---|
| | 62 | |
|---|
| | 63 | if ( !$id ) |
|---|
| | 64 | $id = $post->ID; |
|---|
| | 65 | |
|---|
| | 66 | if ( !isset($category_cache[$id]) ) |
|---|
| | 67 | update_post_category_cache($id); |
|---|
| | 68 | |
|---|
| | 69 | $categories = $category_cache[$id]; |
|---|
| | 70 | |
|---|
| | 71 | if ( !empty($categories) ) |
|---|
| | 72 | sort($categories); |
|---|
| | 73 | else |
|---|
| | 74 | $categories = array(); |
|---|
| | 75 | |
|---|
| | 76 | return $categories; |
|---|
| | 77 | } |
|---|
| | 78 | |
|---|
| | 79 | function get_the_category_by_ID($cat_ID) { |
|---|
| | 80 | $cat_ID = (int) $cat_ID; |
|---|
| | 81 | $category = &get_category($cat_ID); |
|---|
| | 82 | return $category->cat_name; |
|---|
| 111 | | } |
|---|
| 112 | | |
|---|
| 113 | | function get_the_category_by_ID($cat_ID) { |
|---|
| 114 | | $cat_ID = (int) $cat_ID; |
|---|
| 115 | | $category = &get_category($cat_ID); |
|---|
| 116 | | return $category->cat_name; |
|---|
| 117 | | } |
|---|
| 118 | | |
|---|
| 119 | | function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ |
|---|
| 120 | | $chain = ''; |
|---|
| 121 | | $parent = &get_category($id); |
|---|
| 122 | | |
|---|
| 123 | | if ( $nicename ) |
|---|
| 124 | | $name = $parent->category_nicename; |
|---|
| 125 | | else |
|---|
| 126 | | $name = $parent->cat_name; |
|---|
| 127 | | |
|---|
| 128 | | if ( $parent->category_parent ) |
|---|
| 129 | | $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename); |
|---|
| 130 | | |
|---|
| 131 | | if ( $link ) |
|---|
| 132 | | $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; |
|---|
| 133 | | else |
|---|
| 134 | | $chain .= $name.$separator; |
|---|
| 135 | | return $chain; |
|---|
| 136 | | } |
|---|
| 137 | | |
|---|
| 138 | | function get_category_children($id, $before = '/', $after = '') { |
|---|
| 139 | | if ( 0 == $id ) |
|---|
| 140 | | return ''; |
|---|
| 141 | | |
|---|
| 142 | | $cat_ids = get_all_category_ids(); |
|---|
| 143 | | foreach ( $cat_ids as $cat_id ) { |
|---|
| 144 | | if ( $cat_id == $id) |
|---|
| 145 | | continue; |
|---|
| 146 | | |
|---|
| 147 | | $category = get_category($cat_id); |
|---|
| 148 | | if ( $category->category_parent == $id ) { |
|---|
| 149 | | $chain .= $before.$category->cat_ID.$after; |
|---|
| 150 | | $chain .= get_category_children($category->cat_ID, $before, $after); |
|---|
| 151 | | } |
|---|
| 152 | | } |
|---|
| 153 | | return $chain; |
|---|
| 255 | | function in_category($category) { // Check if the current post is in the given category |
|---|
| 256 | | global $category_cache, $post; |
|---|
| 257 | | |
|---|
| 258 | | if ( isset( $category_cache[$post->ID][$category] ) ) |
|---|
| 259 | | return true; |
|---|
| 260 | | else |
|---|
| 261 | | return false; |
|---|
| 262 | | } |
|---|
| 263 | | |
|---|
| 264 | | function &_get_cat_children($category_id, $categories) { |
|---|
| 265 | | if ( empty($categories) ) |
|---|
| 266 | | return array(); |
|---|
| 267 | | |
|---|
| 268 | | $category_list = array(); |
|---|
| 269 | | foreach ( $categories as $category ) { |
|---|
| 270 | | if ( $category->category_parent == $category_id ) { |
|---|
| 271 | | $category_list[] = $category; |
|---|
| 272 | | if ( $children = _get_cat_children($category->cat_ID, $categories) ) |
|---|
| 273 | | $category_list = array_merge($category_list, $children); |
|---|
| 274 | | } |
|---|
| 275 | | } |
|---|
| 276 | | |
|---|
| 277 | | return $category_list; |
|---|
| 278 | | } |
|---|
| 279 | | |
|---|
| 280 | | function &get_categories($args = '') { |
|---|
| 281 | | global $wpdb, $category_links; |
|---|
| 282 | | |
|---|
| 283 | | if ( is_array($args) ) |
|---|
| 284 | | $r = &$args; |
|---|
| 285 | | else |
|---|
| 286 | | parse_str($args, $r); |
|---|
| 287 | | |
|---|
| 288 | | $defaults = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', |
|---|
| 289 | | 'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, $exclude => '', $include => ''); |
|---|
| 290 | | $r = array_merge($defaults, $r); |
|---|
| 291 | | $r['orderby'] = "cat_" . $r['orderby']; // restricts order by to cat_ID and cat_name fields |
|---|
| 292 | | extract($r); |
|---|
| 293 | | |
|---|
| 294 | | $where = 'cat_ID > 0'; |
|---|
| 295 | | $inclusions = ''; |
|---|
| 296 | | if ( !empty($include) ) { |
|---|
| 297 | | $child_of = 0; //ignore child_of and exclude params if using include |
|---|
| 298 | | $exclude = ''; |
|---|
| 299 | | $incategories = preg_split('/[\s,]+/',$include); |
|---|
| 300 | | if ( count($incategories) ) { |
|---|
| 301 | | foreach ( $incategories as $incat ) { |
|---|
| 302 | | if (empty($inclusions)) |
|---|
| 303 | | $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' '; |
|---|
| 304 | | else |
|---|
| 305 | | $inclusions .= ' OR cat_ID = ' . intval($incat) . ' '; |
|---|
| 306 | | } |
|---|
| 307 | | } |
|---|
| 308 | | } |
|---|
| 309 | | if (!empty($inclusions)) |
|---|
| 310 | | $inclusions .= ')'; |
|---|
| 311 | | $where .= $inclusions; |
|---|
| 312 | | |
|---|
| 313 | | $exclusions = ''; |
|---|
| 314 | | if ( !empty($exclude) ) { |
|---|
| 315 | | $excategories = preg_split('/[\s,]+/',$exclude); |
|---|
| 316 | | if ( count($excategories) ) { |
|---|
| 317 | | foreach ( $excategories as $excat ) { |
|---|
| 318 | | if (empty($exclusions)) |
|---|
| 319 | | $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' '; |
|---|
| 320 | | else |
|---|
| 321 | | $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; |
|---|
| 322 | | // TODO: Exclude children of excluded cats? Note: children are getting excluded |
|---|
| 323 | | } |
|---|
| 324 | | } |
|---|
| 325 | | } |
|---|
| 326 | | if (!empty($exclusions)) |
|---|
| 327 | | $exclusions .= ')'; |
|---|
| 328 | | $exclusions = apply_filters('list_cats_exclusions', $exclusions ); |
|---|
| 329 | | $where .= $exclusions; |
|---|
| 330 | | |
|---|
| 331 | | $having = ''; |
|---|
| 332 | | if ( $hide_empty ) { |
|---|
| 333 | | if ( 'link' == $type ) |
|---|
| 334 | | $having = 'HAVING link_count > 0'; |
|---|
| 335 | | else |
|---|
| 336 | | $having = 'HAVING category_count > 0'; |
|---|
| 337 | | } |
|---|
| 338 | | |
|---|
| 339 | | $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where $having ORDER BY $orderby $order"); |
|---|
| 340 | | |
|---|
| 341 | | if ( empty($categories) ) |
|---|
| 342 | | return array(); |
|---|
| 343 | | |
|---|
| 344 | | // TODO: Integrate this into the main query. |
|---|
| 345 | | if ( $include_last_update_time ) { |
|---|
| 346 | | $stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories |
|---|
| 347 | | WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id"); |
|---|
| 348 | | global $cat_stamps; |
|---|
| 349 | | foreach ($stamps as $stamp) |
|---|
| 350 | | $cat_stamps[$stamp->category_id] = $stamp->ts; |
|---|
| 351 | | function stamp_cat($cat) { |
|---|
| 352 | | global $cat_stamps; |
|---|
| 353 | | $cat->last_update_timestamp = $cat_stamps[$cat->cat_ID]; |
|---|
| 354 | | return $cat; |
|---|
| 355 | | } |
|---|
| 356 | | $categories = array_map('stamp_cat', $categories); |
|---|
| 357 | | unset($cat_stamps); |
|---|
| 358 | | } |
|---|
| 359 | | |
|---|
| 360 | | if ( $child_of || $hierarchical ) |
|---|
| 361 | | $categories = & _get_cat_children($child_of, $categories); |
|---|
| 362 | | |
|---|
| 363 | | return apply_filters('get_categories', $categories); |
|---|
| | 252 | // |
|---|
| | 253 | // Helper functions |
|---|
| | 254 | // |
|---|
| | 255 | |
|---|
| | 256 | function walk_category_tree() { |
|---|
| | 257 | $walker = new Walker_Category; |
|---|
| | 258 | $args = func_get_args(); |
|---|
| | 259 | return call_user_func_array(array(&$walker, 'walk'), $args); |
|---|
| | 260 | } |
|---|
| | 261 | |
|---|
| | 262 | function walk_category_dropdown_tree() { |
|---|
| | 263 | $walker = new Walker_CategoryDropdown; |
|---|
| | 264 | $args = func_get_args(); |
|---|
| | 265 | return call_user_func_array(array(&$walker, 'walk'), $args); |
|---|