Changeset 5525 for trunk/wp-includes/taxonomy.php
- Timestamp:
- 05/23/2007 10:32:33 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.