Changeset 5558

Show
Ignore:
Timestamp:
05/27/07 06:17:50 (1 year ago)
Author:
ryan
Message:

Change term count callback style. see #4189

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/taxonomy.php

    r5557 r5558  
    22 
    33$wp_taxonomies = 
    4 array('category' => array('object_type' => 'post', 'hierarchical' => true), 
    5 'post_tag' => array('object_type' => 'post', 'hierarchical' => false), 
     4array('category' => array('object_type' => 'post', 'hierarchical' => true, 'update_count_callback' => '_update_post_term_count'), 
     5'post_tag' => array('object_type' => 'post', 'hierarchical' => false, 'update_count_callback' => '_update_post_term_count'), 
    66'link_category' => array('object_type' => 'link', 'hierarchical' => false)); 
    77 
     
    112112        $taxonomies = array($taxonomies); 
    113113 
    114     $terms = get_object_terms($object_id, $taxonomies, 'fields=tt_ids'); 
    115     $in_terms = "'" . implode("', '", $terms) . "'"
    116     $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_terms)")
    117  
    118     // Assume all taxonomies have the same object type 
    119     $taxonomy = get_taxonomy($taxonomies[0]); 
    120     wp_update_term_count($terms, $taxonomy['object_type']); 
     114    foreach ( $taxonomies as $taxonomy ) { 
     115       $terms = get_object_terms($object_id, $taxonomy, 'fields=tt_ids')
     116       $in_terms = "'" . implode("', '", $terms) . "'"
     117        $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_terms)"); 
     118 
     119       wp_update_term_count($terms, $taxonomy); 
     120    } 
    121121     
    122122    // TODO clear the cache 
     
    238238} 
    239239 
    240 function wp_update_term_count( $terms, $object_type ) { 
     240function wp_update_term_count( $terms, $taxonomy ) { 
    241241    if ( empty($terms) ) 
    242242        return false; 
    243243 
    244     // TODO validate object_type 
    245      
    246244    if ( !is_array($terms) ) 
    247245        $terms = array($terms); 
     
    249247    $terms = array_map('intval', $terms); 
    250248 
    251     do_action("count_${object_type}_terms", $terms); 
     249    $taxonomy = get_taxonomy($taxonomy); 
     250    if ( isset($taxonomy['update_count_callback']) ) 
     251        return call_user_func($taxonomy['update_count_callback'], $terms); 
     252 
     253    // Default count updater 
     254    $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$term'"); 
     255    $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term'"); 
    252256 
    253257    return true; 
     
    310314        $terms = array($terms); 
    311315 
    312     if ( ! $append ) { 
    313         $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'"); 
    314         if ( empty($old_terms) ) 
    315             $old_terms = array(); 
    316     } 
     316    if ( ! $append ) 
     317        $old_terms =  get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); 
    317318 
    318319    $tt_ids = array(); 
     
    330331    } 
    331332 
    332     $taxonomy_data = get_taxonomy($taxonomy); 
    333     wp_update_term_count($tt_ids, $taxonomy_data['object_type']); 
     333    wp_update_term_count($tt_ids, $taxonomy); 
    334334 
    335335    if ( ! $append ) { 
     
    339339            $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)"); 
    340340            $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count - 1 WHERE term_taxonomy_id IN ($delete_terms)"); 
    341             wp_update_term_count($delete_terms, $taxonomy_data['object_type']); 
    342         } 
    343     } 
    344  
     341            wp_update_term_count($delete_terms, $taxonomy); 
     342        } 
     343    } 
     344 
     345    // TODO clean old_terms. Need term_id instead of tt_id 
    345346    clean_term_cache($term_ids, $taxonomy); 
    346347 
     
    683684    } 
    684685} 
    685 add_action('count_post_terms', '_update_post_term_count'); 
    686  
    687 function _update_link_term_count( $terms ) { 
    688     global $wpdb; 
    689  
    690     foreach ( $terms as $term ) { 
    691         $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$term'"); 
    692         $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term'"); 
    693     } 
    694 } 
    695 add_action('count_link_terms', '_update_link_term_count'); 
    696686 
    697687?>