Changeset 5556 for trunk/wp-includes/taxonomy.php
- Timestamp:
- 05/27/2007 05:15:18 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/taxonomy.php
r5555 r5556 104 104 } 105 105 106 function wp_delete_object_term_relationships( $object_id, $taxonomies ) { 107 global $wpdb; 108 109 $object_id = (int) $object_id; 110 111 if ( !is_array($taxonomies) ) 112 $taxonomies = array($taxonomies); 113 114 $terms = get_object_terms($object_id, $taxonomies, 'fields=tt_ids'); 115 $in_terms = "'" . implode("', '", $terms) . "'"; 116 error_log("Terms: " . var_export($terms, true), 0); 117 $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_terms)"); 118 119 // Assume all taxonomies have the same object type 120 $taxonomy = get_taxonomy($taxonomies[0]); 121 wp_update_term_count($terms, $taxonomy['object_type']); 122 123 // TODO clear the cache 124 } 125 106 126 /** 107 127 * Removes a term from the database. … … 219 239 } 220 240 241 function wp_update_term_count( $terms, $object_type ) { 242 if ( empty($terms) ) 243 return false; 244 245 // TODO validate object_type 246 247 if ( !is_array($terms) ) 248 $terms = array($terms); 249 250 $terms = array_map('intval', $terms); 251 252 do_action("count_${object_type}_terms", $terms); 253 254 return true; 255 } 256 221 257 /** 222 258 * Returns the index of a defined term, or 0 (false) if the term doesn't exist. … … 262 298 * @param int $object_id The object to relate to. 263 299 * @param array|int|string $term The slug or id of the term. 264 * @param array|string $taxonom ies The context(s)in which to relate the term to the object.300 * @param array|string $taxonomy The context in which to relate the term to the object. 265 301 */ 266 function wp_set_object_terms($object_id, $terms, $taxonom ies, $append = false) {302 function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { 267 303 global $wpdb; 268 304 269 305 $object_id = (int) $object_id; 270 306 271 if ( ! is_array($taxonomies) )272 $taxonomies = array($taxonomies);307 if ( ! is_taxonomy($taxonomy) ) 308 return false; 273 309 274 310 if ( !is_array($terms) ) … … 276 312 277 313 if ( ! $append ) { 278 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; 279 $old_terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($in_taxonomies) AND tr.object_id = '$object_id'"); 314 $old_terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = '$taxonomy' AND tr.object_id = '$object_id'"); 280 315 if ( empty($old_terms) ) 281 316 $old_terms = array(); … … 283 318 284 319 $tt_ids = array(); 285 286 foreach ( $taxonomies as $taxonomy ) { 287 foreach ($terms as $term) { 288 if ( !$id = is_term($term, $taxonomy) ) 289 $id = wp_insert_term($term, $taxonomy); 290 $id = $id['term_taxonomy_id']; 291 $tt_ids[] = $id; 292 if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) 293 continue; 294 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); 295 $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = $id"); 296 } 297 } 320 $term_ids = array(); 321 322 foreach ($terms as $term) { 323 if ( !$id = is_term($term, $taxonomy) ) 324 $id = wp_insert_term($term, $taxonomy); 325 $term_ids[] = $id['term_id']; 326 $id = $id['term_taxonomy_id']; 327 $tt_ids[] = $id; 328 if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) 329 continue; 330 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); 331 } 332 333 $taxonomy_data = get_taxonomy($taxonomy); 334 wp_update_term_count($tt_ids, $taxonomy_data['object_type']); 298 335 299 336 if ( ! $append ) { … … 303 340 $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)"); 304 341 $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count - 1 WHERE term_taxonomy_id IN ($delete_terms)"); 305 } 306 } 342 wp_update_term_count($delete_terms, $taxonomy_data['object_type']); 343 } 344 } 345 346 clean_term_cache($term_ids, $taxonomy); 307 347 308 348 return $tt_ids; … … 315 355 * @return array The requested term data. 316 356 */ 317 function get_object_terms($object_id, $taxonomy, $args = array()) { 318 global $wpdb; 319 $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy; 320 // TODO cast to int 321 $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; 357 function get_object_terms($object_ids, $taxonomies, $args = array()) { 358 global $wpdb; 359 360 if ( !is_array($taxonomies) ) 361 $taxonomies = array($taxonomies); 362 363 if ( !is_array($object_ids) ) 364 $object_ids = array($object_ids); 365 $object_ids = array_map('intval', $object_ids); 322 366 323 367 $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); … … 344 388 else if ( 'ids' == $fields ) 345 389 $taxonomy_data = $wpdb->get_col($query); 390 else if ( 'tt_ids' == $fields ) 391 $taxonomy_data = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ($object_ids) ORDER BY term_taxonomy_id $order"); 346 392 347 393 if ( ! $taxonomy_data ) 348 394 return array(); 349 395 350 if ($single_taxonomy && $single_object) { 351 // Just one kind of taxonomy for one object. 352 return $taxonomy_data; 353 } else { 354 foreach ($taxonomy_data as $data) { 355 if ($single_taxonomy) { 356 // Many objects, one taxonomy type. 357 $return[$data->object_id][] = $data; 358 } elseif ($single_object) { 359 // One object, many taxonomies. 360 $return[$data->taxonomy][] = $data; 361 } else { 362 // Many objects, many taxonomies. 363 $return[$data->object_id][$data->taxonomy][] = $data; 364 } 365 } 366 return $return; 367 } 396 return $taxonomy_data; 368 397 } 369 398 … … 485 514 $children = _get_term_hierarchy($taxonomies[0]); 486 515 if ( ! empty($children) ) 487 $terms = & _get_term_children($child_of, $terms );516 $terms = & _get_term_children($child_of, $terms, $taxonomies[0]); 488 517 } 489 518 … … 584 613 } 585 614 586 function clean_term_cache($id, $taxonomy) { 587 wp_cache_delete($id, $taxonomy); 615 function clean_term_cache($ids, $taxonomy) { 616 if ( !is_array($ids) ) 617 $ids = array($ids); 618 619 foreach ( $ids as $id ) { 620 wp_cache_delete($id, $taxonomy); 621 } 622 588 623 wp_cache_delete('all_ids', $taxonomy); 589 624 wp_cache_delete('get', $taxonomy); … … 599 634 600 635 $children = array(); 601 $terms = get_terms( 'category', 'hide_empty=0&hierarchical=0');636 $terms = get_terms($taxonomy, 'hide_empty=0&hierarchical=0'); 602 637 foreach ( $terms as $term ) { 603 638 if ( $term->parent > 0 ) … … 609 644 } 610 645 611 function &_get_term_children($term_id, $terms ) {646 function &_get_term_children($term_id, $terms, $taxonomy) { 612 647 if ( empty($terms) ) 613 648 return array(); 614 649 615 650 $term_list = array(); 616 $has_children = _get_term_hierarchy( );651 $has_children = _get_term_hierarchy($taxonomy); 617 652 618 653 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) … … 629 664 continue; 630 665 631 if ( $children = _get_ cat_children($term->term_id, $terms) )666 if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) ) 632 667 $term_list = array_merge($term_list, $children); 633 668 } … … 637 672 } 638 673 674 // 675 // Default callbacks 676 // 677 678 function _update_post_term_count( $terms ) { 679 global $wpdb; 680 681 foreach ( $terms as $term ) { 682 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = '$term'"); 683 $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term'"); 684 } 685 } 686 add_action('count_post_terms', '_update_post_term_count'); 687 688 function _update_link_term_count( $terms ) { 689 global $wpdb; 690 691 foreach ( $terms as $term ) { 692 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$term'"); 693 $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term'"); 694 } 695 } 696 add_action('count_link_terms', '_update_link_term_count'); 697 639 698 ?>
Note: See TracChangeset
for help on using the changeset viewer.