Make WordPress Core


Ignore:
Timestamp:
05/22/2007 05:12:38 AM (19 years ago)
Author:
ryan
Message:

Very rough initial commit of taxonomy for everyone's hacking pleasure. There be dragons. see #4189

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r5459 r5510  
    460460    $post_id = (int) $post_id;
    461461   
    462     if ( !isset( $tag_cache[$blog_id][$post_id] ) )
    463         update_post_category_cache( $post_id ); // loads $tag_cache
    464 
    465     return $tag_cache[$blog_id][$post_id];
     462    $tags = get_object_terms($post_id, 'post_tag');
     463    return $tags;
    466464}
    467465
     
    793791    if ( !$post_id )
    794792        return false;
    795    
    796     // prevent warnings for unintialized variables
    797     $tag_ids = array();
    798793
    799794    if ( empty($tags) )
    800795        $tags = array();
    801796    $tags = (is_array($tags)) ? $tags : explode( ',', $tags );
    802    
    803     foreach ( $tags as $tag ) {
    804         $tag = trim( $tag );
    805         if ( !$tag_slug = sanitize_title( $tag ) )
    806             continue; // discard
    807         if ( !$tag_id = tag_exists( $tag ) )
    808             $tag_id = wp_create_tag( $tag );
    809         $tag_ids[] = $tag_id;
    810     }
    811 
    812     if ( empty($tag_ids) && ( !empty($tags) || $append ) )
    813         return false;
    814    
    815     $tag_ids = array_unique( $tag_ids );
    816    
    817     // First the old tags
    818     $old_tags = $wpdb->get_col("
    819         SELECT category_id
    820         FROM $wpdb->post2cat
    821         WHERE post_id = '$post_id' AND rel_type = 'tag'");
    822    
    823     if ( !$old_tags ) {
    824         $old_tags = array();
    825     } else {
    826         $old_tags = array_unique( $old_tags );
    827     }
    828    
    829     // Delete any?
    830     $delete_tags = array_diff( $old_tags, $tag_ids);
    831     if ( $delete_tags && !$append ) {
    832         foreach ( $delete_tags as $del ) {
    833             $wpdb->query("
    834                 DELETE FROM $wpdb->post2cat
    835                 WHERE category_id = '$del'
    836                     AND post_id = '$post_id'
    837                     AND rel_type = 'tag'
    838                 ");
    839         }
    840     }
    841    
    842     // Add any?
    843     $add_tags = array_diff( $tag_ids, $old_tags );
    844     if ( $add_tags ) {
    845         foreach ( $add_tags as $new_tag ) {
    846             $new_tag = (int) $new_tag;
    847             if ( !empty($new_tag) )
    848                 $wpdb->query("
    849                     INSERT INTO $wpdb->post2cat (post_id, category_id, rel_type)
    850                     VALUES ('$post_id', '$new_tag', 'tag')");
    851         }
    852     }
    853    
    854     // Update category counts.
    855     $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) );
    856     foreach ( $all_affected_tags as $tag_id ) {
    857         $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$tag_id' AND rel_type = 'tag'" );
    858         $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count', type = type | " . TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" );
    859         if ( $count == 0 )
    860             $wpdb->query( "UPDATE $wpdb->categories SET type = type & ~". TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" );
    861         clean_category_cache( $tag_id );
    862         do_action( 'edit_category', $tag_id );
    863         do_action( 'edit_tag', $tag_id );
    864     }
     797    add_term_relationship($tags, $post_id, 'post_tag');
    865798}
    866799
Note: See TracChangeset for help on using the changeset viewer.