Changeset 3843
- Timestamp:
- 06/04/2006 09:36:52 PM (19 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 1 added
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/category-template.php
r3809 r3843 1 1 <?php 2 2 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; 32 19 } 33 20 … … 50 37 } 51 38 return apply_filters('category_link', $catlink, $category_id); 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; 52 83 } 53 84 … … 107 138 } 108 139 140 function in_category($category) { // Check if the current post is in the given category 141 global $category_cache, $post; 142 143 if ( isset( $category_cache[$post->ID][$category] ) ) 144 return true; 145 else 146 return false; 147 } 148 109 149 function the_category($separator = '', $parents='') { 110 150 echo get_the_category_list($separator, $parents); 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 else126 $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 else134 $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;154 151 } 155 152 … … 253 250 } 254 251 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); 364 266 } 365 267 -
trunk/wp-includes/functions-post.php
r3788 r3843 608 608 } 609 609 610 // Get the name of a category from its ID611 function get_cat_name($cat_id) {612 global $wpdb;613 614 $cat_id -= 0; // force numeric615 $name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID=$cat_id");616 617 return $name;618 }619 620 // Get the ID of a category from its name621 function get_cat_ID($cat_name='General') {622 global $wpdb;623 624 $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");625 626 return $cid?$cid:1; // default to cat 1627 }628 629 610 // Get author's preferred display name 630 611 function get_author_name( $auth_id ) { -
trunk/wp-includes/functions.php
r3842 r3843 625 625 return $_page; 626 626 } 627 }628 629 function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {630 global $wpdb;631 $category_path = rawurlencode(urldecode($category_path));632 $category_path = str_replace('%2F', '/', $category_path);633 $category_path = str_replace('%20', ' ', $category_path);634 $category_paths = '/' . trim($category_path, '/');635 $leaf_path = sanitize_title(basename($category_paths));636 $category_paths = explode('/', $category_paths);637 foreach($category_paths as $pathdir)638 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);639 640 $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'");641 642 if ( empty($categories) )643 return NULL;644 645 foreach ($categories as $category) {646 $path = '/' . $leaf_path;647 $curcategory = $category;648 while ($curcategory->category_parent != 0) {649 $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'");650 $path = '/' . $curcategory->category_nicename . $path;651 }652 653 if ( $path == $full_path )654 return get_category($category->cat_ID, $output);655 }656 657 // If full matching is not required, return the first cat that matches the leaf.658 if ( ! $full_match )659 return get_category($categories[0]->cat_ID, $output);660 661 return NULL;662 }663 664 // Retrieves category data given a category ID or category object.665 // Handles category caching.666 function &get_category(&$category, $output = OBJECT) {667 global $wpdb;668 669 if ( empty($category) )670 return null;671 672 if ( is_object($category) ) {673 wp_cache_add($category->cat_ID, $category, 'category');674 $_category = $category;675 } else {676 if ( ! $_category = wp_cache_get($category, 'category') ) {677 $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");678 wp_cache_add($category, $_category, 'category');679 }680 }681 682 if ( $output == OBJECT ) {683 return $_category;684 } elseif ( $output == ARRAY_A ) {685 return get_object_vars($_category);686 } elseif ( $output == ARRAY_N ) {687 return array_values(get_object_vars($_category));688 } else {689 return $_category;690 }691 }692 693 function get_catname($cat_ID) {694 $category = &get_category($cat_ID);695 return $category->cat_name;696 }697 698 function get_all_category_ids() {699 global $wpdb;700 701 if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {702 $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");703 wp_cache_add('all_category_ids', $cat_ids, 'category');704 }705 706 return $cat_ids;707 627 } 708 628
Note: See TracChangeset
for help on using the changeset viewer.