Changeset 5525
- Timestamp:
- 05/23/2007 10:32:33 AM (17 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/category.php
r5521 r5525 8 8 9 9 if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) { 10 $cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");10 $cat_ids = get_terms('category', 'get=ids&hierarchical=0&hide_empty=0'); 11 11 wp_cache_add('all_category_ids', $cat_ids, 'category'); 12 12 } … … 16 16 17 17 function &get_categories($args = '') { 18 global $wpdb, $category_links; 19 20 $defaults = array( 21 'type' => 'post', 'child_of' => 0, 22 'orderby' => 'name', 'order' => 'ASC', 23 'hide_empty' => true, 'include_last_update_time' => false, 24 'hierarchical' => 1, 'exclude' => '', 25 'include' => '', 'number' => '', 26 'pad_counts' => false 27 ); 28 29 $r = wp_parse_args( $args, $defaults ); 30 31 if ( $r['orderby'] == 'count' ) { 32 $r['orderby'] = 'category_count'; 33 } else { 34 $r['orderby'] = 'cat_' . $r['orderby']; 35 } 36 37 extract( $r ); 38 39 $key = md5( serialize( $r ) ); 40 if ( $cache = wp_cache_get( 'get_categories', 'category' ) ) 41 if ( isset( $cache[ $key ] ) ) 42 return apply_filters('get_categories', $cache[$key], $r); 43 44 $where = 'cat_ID > 0'; 45 $inclusions = ''; 46 if ( !empty($include) ) { 47 $child_of = 0; //ignore child_of and exclude params if using include 48 $exclude = ''; 49 $incategories = preg_split('/[\s,]+/',$include); 50 if ( count($incategories) ) { 51 foreach ( $incategories as $incat ) { 52 if (empty($inclusions)) 53 $inclusions = ' AND ( cat_ID = ' . intval($incat) . ' '; 54 else 55 $inclusions .= ' OR cat_ID = ' . intval($incat) . ' '; 56 } 57 } 58 } 59 if (!empty($inclusions)) 60 $inclusions .= ')'; 61 $where .= $inclusions; 62 63 $exclusions = ''; 64 if ( !empty($exclude) ) { 65 $excategories = preg_split('/[\s,]+/',$exclude); 66 if ( count($excategories) ) { 67 foreach ( $excategories as $excat ) { 68 if (empty($exclusions)) 69 $exclusions = ' AND ( cat_ID <> ' . intval($excat) . ' '; 70 else 71 $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; 72 // TODO: Exclude children of excluded cats? Note: children are getting excluded 73 } 74 } 75 } 76 if (!empty($exclusions)) 77 $exclusions .= ')'; 78 $exclusions = apply_filters('list_cats_exclusions', $exclusions, $r ); 79 $where .= $exclusions; 80 81 if ( $hide_empty && !$hierarchical ) { 82 if ( 'link' == $type ) 83 $where .= ' AND link_count > 0'; 84 else 85 $where .= ' AND category_count > 0'; 86 } else { 87 $where .= ' AND ( type & ' . TAXONOMY_CATEGORY . ' != 0 ) '; 88 } 89 90 91 92 if ( !empty($number) ) 93 $number = 'LIMIT ' . $number; 94 else 95 $number = ''; 96 97 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE $where ORDER BY $orderby $order $number"); 98 99 if ( empty($categories) ) 100 return array(); 101 102 // TODO: Integrate this into the main query. 103 if ( $include_last_update_time ) { 104 $stamps = $wpdb->get_results("SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories 105 WHERE post_status = 'publish' AND post_id = ID AND $where GROUP BY category_id"); 106 global $cat_stamps; 107 foreach ($stamps as $stamp) 108 $cat_stamps[$stamp->category_id] = $stamp->ts; 109 function stamp_cat($cat) { 110 global $cat_stamps; 111 $cat->last_update_timestamp = $cat_stamps[$cat->cat_ID]; 112 return $cat; 113 } 114 $categories = array_map('stamp_cat', $categories); 115 unset($cat_stamps); 116 } 117 118 if ( $child_of || $hierarchical ) { 119 $children = _get_category_hierarchy(); 120 if ( ! empty($children) ) 121 $categories = & _get_cat_children($child_of, $categories); 122 } 123 124 // Update category counts to include children. 125 if ( $pad_counts ) 126 _pad_category_counts($type, $categories); 127 128 // Make sure we show empty categories that have children. 129 if ( $hierarchical && $hide_empty ) { 130 foreach ( $categories as $k => $category ) { 131 if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) { 132 $children = _get_cat_children($category->cat_ID, $categories); 133 foreach ( $children as $child ) 134 if ( $child->{'link' == $type ? 'link_count' : 'category_count'} ) 135 continue 2; 136 137 // It really is empty 138 unset($categories[$k]); 139 } 140 } 141 } 142 reset ( $categories ); 143 144 $cache[ $key ] = $categories; 145 wp_cache_add( 'get_categories', $cache, 'category' ); 146 147 $categories = apply_filters('get_categories', $categories, $r); 148 return $categories; 18 // TODO Add back compat fields into each object. 19 return get_terms('category', $args); 149 20 } 150 21 … … 152 23 // Handles category caching. 153 24 function &get_category(&$category, $output = OBJECT) { 154 global $wpdb; 155 156 if ( empty($category) ) 157 return null; 158 159 if ( is_object($category) ) { 160 wp_cache_add($category->cat_ID, $category, 'category'); 161 $_category = $category; 162 } else { 163 $category = (int) $category; 164 if ( ! $_category = wp_cache_get($category, 'category') ) { 165 $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1"); 166 wp_cache_add($category, $_category, 'category'); 167 } 168 } 169 170 $_category = apply_filters('get_category', $_category); 171 172 if ( $output == OBJECT ) { 173 return $_category; 174 } elseif ( $output == ARRAY_A ) { 175 return get_object_vars($_category); 176 } elseif ( $output == ARRAY_N ) { 177 return array_values(get_object_vars($_category)); 178 } else { 179 return $_category; 180 } 25 return get_term($category, 'category', $output); 181 26 } 182 27 … … 193 38 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir); 194 39 195 $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'");40 $categories = get_terms('category', "slug=$leaf_path"); 196 41 197 42 if ( empty($categories) ) … … 201 46 $path = '/' . $leaf_path; 202 47 $curcategory = $category; 203 while ( ($curcategory-> category_parent != 0) && ($curcategory->category_parent != $curcategory->cat_ID) ) {204 $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'");205 $path = '/' . $curcategory-> category_nicename. $path;48 while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) { 49 $curcategory = get_term($curcategory->parent); 50 $path = '/' . $curcategory->slug . $path; 206 51 } 207 52 208 53 if ( $path == $full_path ) 209 return get_category($category-> cat_ID, $output);54 return get_category($category->term_id, $output); 210 55 } 211 56 212 57 // If full matching is not required, return the first cat that matches the leaf. 213 58 if ( ! $full_match ) 214 return get_category($categories[0]-> cat_ID, $output);59 return get_category($categories[0]->term_id, $output); 215 60 216 61 return NULL; … … 218 63 219 64 function get_category_by_slug( $slug ) { 220 global $wpdb; 221 $slug = sanitize_title( $slug ); 222 if ( empty( $slug ) ) 223 return false; 224 $category = $wpdb->get_var( "SELECT * FROM $wpdb->categories WHERE category_nicename = '$slug' " ); 225 return get_category( $category ); 65 return get_term_by('slug', $slug, 'category'); 226 66 } 227 67 … … 230 70 global $wpdb; 231 71 232 $cid = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'"); 233 234 return $cid?$cid:1; // default to cat 1 72 $cat = get_term_by('name', $cat_name, 'category'); 73 if ($cat) 74 return $cat->term_id; 75 return 0; 235 76 } 236 77 … … 253 94 $cat2 = & get_category($cat2); 254 95 255 if ( !$cat1-> cat_ID || !$cat2->category_parent )96 if ( !$cat1->term_id || !$cat2->parent ) 256 97 return false; 257 98 258 if ( $cat2-> category_parent == $cat1->cat_ID)99 if ( $cat2->parent == $cat1->term_id ) 259 100 return true; 260 101 261 return cat_is_ancestor_of($cat1, get_category($cat2->parent _category));102 return cat_is_ancestor_of($cat1, get_category($cat2->parent)); 262 103 } 263 104 -
trunk/wp-includes/taxonomy.php
r5524 r5525 1 1 <?php 2 3 $wp_taxonomies = 4 array('category' => array('object_type' => 'post', 'hierarchical' => true), 5 'post_tag' => array('object_type' => 'post', 'hierarchical' => false), 6 'link_category' => array('object_type' => 'link', 'hierarchical' => false)); 7 8 function is_taxonomy( $taxonomy ) { 9 global $wp_taxonomies; 10 11 return isset($wp_taxonomies[$taxonomy]); 12 } 13 14 function get_taxonomy( $taxonomy ) { 15 global $wp_taxonomies; 16 17 if ( ! is_taxonomy($taxonomy) ) 18 return false; 19 20 return $wp_taxonomies[$taxonomy]; 21 } 22 23 function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 24 global $wp_taxonomies; 25 26 $args['object_type'] = $object_type; 27 $wp_taxonomies[$taxonomy] = $args; 28 } 2 29 3 30 /** … … 9 36 function wp_insert_term( $term, $taxonomy, $args = array() ) { 10 37 global $wpdb; 38 39 if ( ! is_taxonomy($taxonomy) ) 40 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); 11 41 12 42 $update = false; … … 258 288 global $wpdb; 259 289 260 if ( !is_array($taxonomies) ) 290 $single_taxonomy = false; 291 if ( !is_array($taxonomies) ) { 292 $single_taxonomy = true; 261 293 $taxonomies = array($taxonomies); 294 } 295 262 296 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; 263 297 264 298 $defaults = array('orderby' => 'name', 'order' => 'ASC', 265 299 'hide_empty' => true, 'exclude' => '', 'include' => '', 266 'number' => '', 'get' => 'everything'); 300 'number' => '', 'get' => 'everything', 'slug' => '', 'parent' => '', 301 'hierarchical' => true, 'child_of' => 0); 267 302 $args = wp_parse_args( $args, $defaults ); 268 303 $args['number'] = (int) $args['number']; 304 if ( ! $single_taxonomy ) { 305 $args['child_of'] = 0; 306 $args['hierarchical'] = false; 307 } else { 308 $tax = get_taxonomy($taxonomy); 309 if ( !$tax['hierarchical'] ) { 310 $args['child_of'] = 0; 311 $args['hierarchical'] = false; 312 } 313 } 269 314 extract($args); 315 316 $key = md5( serialize( $args ) . serialize( $taxonomies ) ); 317 if ( $cache = wp_cache_get( 'get_terms', 'terms' ) ) { 318 if ( isset( $cache[ $key ] ) ) 319 return apply_filters('get_terms', $cache[$key], $taxonomies, $args); 320 } 270 321 271 322 if ( 'count' == $orderby ) … … 273 324 else if ( 'name' == $orderby ) 274 325 $orderby = 't.name'; 326 else 327 $orderby = 't.term_id'; 275 328 276 329 $where = ''; … … 311 364 $where .= $exclusions; 312 365 313 if ( $hide_empty ) 366 if ( !empty($slug) ) { 367 $slug = sanitize_title($slug); 368 $where = " AND t.slug = '$slug'"; 369 } 370 371 if ( !empty($parent) ) { 372 $parent = (int) $parent; 373 $where = " AND tt.parent = '$parent'"; 374 } 375 376 if ( $hide_empty && !$hierarchical ) 314 377 $where .= ' AND tt.count > 0'; 315 378 … … 334 397 return array(); 335 398 399 if ( $child_of || $hierarchical ) { 400 $children = _get_term_hierarchy($taxonomies[0]); 401 if ( ! empty($children) ) 402 $terms = & _get_term_children($child_of, $terms); 403 } 404 405 /* 406 // Update category counts to include children. 407 if ( $pad_counts ) 408 _pad_category_counts($type, $categories); 409 410 // Make sure we show empty categories that have children. 411 if ( $hierarchical && $hide_empty ) { 412 foreach ( $categories as $k => $category ) { 413 if ( ! $category->{'link' == $type ? 'link_count' : 'category_count'} ) { 414 $children = _get_cat_children($category->cat_ID, $categories); 415 foreach ( $children as $child ) 416 if ( $child->{'link' == $type ? 'link_count' : 'category_count'} ) 417 continue 2; 418 419 // It really is empty 420 unset($categories[$k]); 421 } 422 } 423 } 424 reset ( $categories ); 425 */ 426 427 $cache[ $key ] = $terms; 428 wp_cache_add( 'get_terms', $cache, 'terms' ); 429 336 430 $terms = apply_filters('get_terms', $terms, $taxonomies, $args); 337 431 return $terms; … … 345 439 346 440 if ( is_object($term) ) { 347 wp_cache_add($term->term_id, $term, "term:$taxonomy");441 wp_cache_add($term->term_id, $term, $taxonomy); 348 442 $_term = $term; 349 443 } else { 350 444 $term = (int) $term; 351 if ( ! $_term = wp_cache_get($term, "term:$taxonomy") ) {445 if ( ! $_term = wp_cache_get($term, $taxonomy) ) { 352 446 $_term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = '$term' LIMIT 1"); 353 wp_cache_add($term, $_term, "term:$taxonomy");447 wp_cache_add($term, $_term, $taxonomy); 354 448 } 355 449 } 356 450 357 451 $_term = apply_filters('get_term', $_term, $taxonomy); 452 $_term = apply_filters("get_$taxonomy", $_term, $taxonomy); 358 453 359 454 if ( $output == OBJECT ) { … … 368 463 } 369 464 465 function get_term_by($field, $value, $taxonomy, $output = OBJECT) { 466 global $wpdb; 467 468 if ( ! is_taxonomy($taxonomy) ) 469 return false; 470 471 if ( 'slug' == $field ) { 472 $field = 't.slug'; 473 $value = sanitize_title($field); 474 if ( empty($value) ) 475 return false; 476 } else if ( 'name' == $field ) { 477 // Assume already escaped 478 $field = 't.name'; 479 } else { 480 $field = 't.term_id'; 481 $value = (int) $value; 482 } 483 484 $term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND $field = '$value' LIMIT 1"); 485 if ( !$term ) 486 return false; 487 488 wp_cache_add($term->term_id, $term, $taxonomy); 489 490 if ( $output == OBJECT ) { 491 return $term; 492 } elseif ( $output == ARRAY_A ) { 493 return get_object_vars($term); 494 } elseif ( $output == ARRAY_N ) { 495 return array_values(get_object_vars($term)); 496 } else { 497 return $term; 498 } 499 } 500 501 function _get_term_hierarchy($taxonomy) { 502 // TODO Make sure taxonomy is hierarchical 503 $children = get_option("{$taxonomy}_children"); 504 if ( is_array($children) ) 505 return $children; 506 507 $children = array(); 508 $terms = get_terms('category', 'hide_empty=0&hierarchical=0'); 509 foreach ( $terms as $term ) { 510 if ( $term->parent > 0 ) 511 $children[$cterm->parent][] = $term->term_id; 512 } 513 update_option("{$taxonomy}_children", $children); 514 515 return $children; 516 } 517 518 function &_get_term_children($term_id, $terms) { 519 if ( empty($terms) ) 520 return array(); 521 522 $term_list = array(); 523 $has_children = _get_term_hierarchy(); 524 525 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) 526 return array(); 527 528 foreach ( $terms as $term ) { 529 if ( $term->term_id == $term_id ) 530 continue; 531 532 if ( $term->parent == $term_id ) { 533 $term_list[] = $term; 534 535 if ( !isset($has_children[$term->term_id]) ) 536 continue; 537 538 if ( $children = _get_cat_children($term->term_id, $terms) ) 539 $term_list = array_merge($term_list, $children); 540 } 541 } 542 543 return $term_list; 544 } 545 370 546 ?>
Note: See TracChangeset
for help on using the changeset viewer.