Make WordPress Core

Changeset 5556


Ignore:
Timestamp:
05/27/2007 05:15:18 AM (17 years ago)
Author:
ryan
Message:

Fix object term relationship deletion and count updating.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/bookmark.php

    r5542 r5556  
    4545    do_action('delete_link', $link_id);
    4646
    47     $categories = wp_get_link_cats($link_id);
    48     if( is_array( $categories ) ) {
    49         foreach ( $categories as $category ) {
    50             $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
    51             wp_cache_delete($category, 'category');
    52             do_action('edit_category', $cat_id);
    53         }
    54     }
     47    wp_delete_object_term_relationships($link_id, 'link_category');
    5548
    56     $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
    57     return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
     49    $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
    5850   
    5951    do_action('deleted_link', $link_id);
  • trunk/wp-includes/post.php

    r5555 r5556  
    411411    do_action('delete_post', $postid);
    412412
    413     if ( 'publish' == $post->post_status && 'post' == $post->post_type ) {
    414         $categories = wp_get_post_categories($post->ID);
    415         if ( is_array( $categories ) ) {
    416             foreach ( $categories as $cat_id ) {
    417                 $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'");
    418                 wp_cache_delete($cat_id, 'category');
    419                 do_action('edit_category', $cat_id);
    420             }
    421         }
    422     }
     413    // TODO delete for pluggable post taxonomies too
     414    wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
    423415
    424416    if ( 'page' == $post->post_type )
     
    430422
    431423    $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
    432 
    433     $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
    434424
    435425    $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = $postid");
     
    445435}
    446436
    447 function wp_get_post_categories($post_id = 0) {
     437function wp_get_post_categories( $post_id = 0, $args = array() ) {
    448438    $post_id = (int) $post_id;
    449439
    450     $cats = get_object_terms($post_id, 'category', 'get=fields');
     440    $defaults = array('fields' => 'ids');
     441    $args = wp_parse_args( $args, $defaults );
     442
     443    $cats = get_object_terms($post_id, 'category', $args);
    451444    return $cats;
    452445}
    453446
    454 function wp_get_post_tags( $post_id = 0 ) {
    455     global $tag_cache, $blog_id;
    456 
     447function wp_get_post_tags( $post_id = 0, $args = array() ) {
    457448    $post_id = (int) $post_id;
     449
     450    $defaults = array('fields' => 'all');
     451    $args = wp_parse_args( $args, $defaults );
    458452   
    459     $tags = get_object_terms($post_id, 'post_tag');
     453    $tags = get_object_terms($post_id, 'post_tag', $args);
     454
    460455    return $tags;
    461456}
  • trunk/wp-includes/taxonomy.php

    r5555 r5556  
    104104}
    105105
     106function 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
    106126/**
    107127 * Removes a term from the database.
     
    219239}
    220240
     241function 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
    221257/**
    222258 * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
     
    262298 * @param int $object_id The object to relate to.
    263299 * @param array|int|string $term The slug or id of the term.
    264  * @param array|string $taxonomies 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.
    265301 */
    266 function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
     302function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {
    267303    global $wpdb;
    268304
    269305    $object_id = (int) $object_id;
    270306
    271     if ( !is_array($taxonomies) )
    272         $taxonomies = array($taxonomies);
     307    if ( ! is_taxonomy($taxonomy) )
     308        return false;
    273309   
    274310    if ( !is_array($terms) )
     
    276312
    277313    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'");
    280315        if ( empty($old_terms) )
    281316            $old_terms = array();
     
    283318
    284319    $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']);
    298335
    299336    if ( ! $append ) {
     
    303340            $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)");
    304341            $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);
    307347
    308348    return $tt_ids;
     
    315355 * @return array The requested term data.           
    316356 */
    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;
     357function 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);
    322366
    323367    $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
     
    344388    else if ( 'ids' == $fields )
    345389        $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");
    346392
    347393    if ( ! $taxonomy_data )
    348394        return array();
    349395
    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;
    368397}
    369398
     
    485514        $children = _get_term_hierarchy($taxonomies[0]);
    486515        if ( ! empty($children) )
    487             $terms = & _get_term_children($child_of, $terms);
     516            $terms = & _get_term_children($child_of, $terms, $taxonomies[0]);
    488517    }
    489518
     
    584613}
    585614
    586 function clean_term_cache($id, $taxonomy) {
    587     wp_cache_delete($id, $taxonomy);
     615function 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
    588623    wp_cache_delete('all_ids', $taxonomy);
    589624    wp_cache_delete('get', $taxonomy);
     
    599634
    600635    $children = array();
    601     $terms = get_terms('category', 'hide_empty=0&hierarchical=0');
     636    $terms = get_terms($taxonomy, 'hide_empty=0&hierarchical=0');
    602637    foreach ( $terms as $term ) {
    603638        if ( $term->parent > 0 )
     
    609644}
    610645
    611 function &_get_term_children($term_id, $terms) {
     646function &_get_term_children($term_id, $terms, $taxonomy) {
    612647    if ( empty($terms) )
    613648        return array();
    614649
    615650    $term_list = array();
    616     $has_children = _get_term_hierarchy();
     651    $has_children = _get_term_hierarchy($taxonomy);
    617652
    618653    if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
     
    629664                continue;
    630665
    631             if ( $children = _get_cat_children($term->term_id, $terms) )
     666            if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )
    632667                $term_list = array_merge($term_list, $children);
    633668        }
     
    637672}
    638673
     674//
     675// Default callbacks
     676//
     677
     678function _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}
     686add_action('count_post_terms', '_update_post_term_count');
     687
     688function _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}
     696add_action('count_link_terms', '_update_link_term_count');
     697
    639698?>
Note: See TracChangeset for help on using the changeset viewer.