Make WordPress Core


Ignore:
Timestamp:
03/26/2007 07:28:29 AM (19 years ago)
Author:
matt
Message:

A backend interface for tagging.

File:
1 edited

Legend:

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

    r5090 r5110  
    518518                $comment_status  = apply_filters('comment_status_pre', $comment_status);
    519519                $ping_status     = apply_filters('ping_status_pre',    $ping_status);
     520                $tags_input      = apply_filters('tags_input_pre',     $tags_input);
    520521        }
    521522
     
    652653        }
    653654
    654         wp_set_post_categories($post_ID, $post_category);
     655        wp_set_post_categories( $post_ID, $post_category );
     656        wp_set_post_tags( $post_ID, $tags_input );
    655657
    656658        if ( 'page' == $post_type ) {
     
    770772}
    771773
     774function wp_set_post_tags( $post_id = 0, $tags = '' ) {
     775        global $wpdb;
     776       
     777        $post_id = (int) $post_id;
     778       
     779        if ( !$post_id )
     780                return false;
     781
     782        $tags = explode( ',', $tags );
     783        foreach ( $tags as $tag ) {
     784                $tag = trim( $tag );
     785                if ( !$tag_slug = sanitize_title( $tag ) )
     786                        continue; // discard
     787                if ( !$tag_id = category_exists( $tag ) )
     788                        $tag_id = wp_create_category( $tag );
     789                $tag_ids[] = $tag_id;
     790        }
     791
     792        $tag_ids = array_unique( $tag_ids );
     793
     794        // First the old tags
     795        $old_tags = $wpdb->get_col("
     796                SELECT category_id
     797                FROM $wpdb->post2cat
     798                WHERE post_id = '$post_id' AND rel_type = 'tag'");
     799
     800        if ( !$old_tags ) {
     801                $old_tags = array();
     802        } else {
     803                $old_tags = array_unique( $old_tags );
     804        }
     805
     806        // Delete any?
     807        $delete_tags = array_diff( $old_tags, $tag_ids);
     808        if ( $delete_tags ) {
     809                foreach ( $delete_tags as $del ) {
     810                        $wpdb->query("
     811                                DELETE FROM $wpdb->post2cat
     812                                WHERE category_id = '$del'
     813                                        AND post_id = '$post_id'
     814                                        AND rel_type = 'tag'
     815                                ");
     816                }
     817        }
     818
     819        // Add any?
     820        $add_tags = array_diff( $tag_ids, $old_tags );
     821        if ( $add_tags ) {
     822                foreach ( $add_tags as $new_tag ) {
     823                        $new_tag = (int) $new_tag;
     824                        if ( !empty($new_tag) )
     825                                $wpdb->query("
     826                                        INSERT INTO $wpdb->post2cat (post_id, category_id, rel_type)
     827                                        VALUES ('$post_id', '$new_tag', 'tag')");
     828                }
     829        }
     830
     831        // Update category counts.
     832        $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) );
     833        foreach ( $all_affected_tags as $tag_id ) {
     834                $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'" );
     835                $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count' WHERE cat_ID = '$tag_id'" );
     836                if ( $count == 0 )
     837                        $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '-1' WHERE cat_ID = '$tag_id'" );
     838                clean_category_cache( $tag_id );
     839                do_action( 'edit_category', $tag_id );
     840                do_action( 'edit_tag', $tag_id );
     841        }
     842}
     843
    772844function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
    773845        global $wpdb;
     
    784856                SELECT category_id
    785857                FROM $wpdb->post2cat
    786                 WHERE post_id = '$post_ID'");
     858                WHERE post_id = '$post_ID' AND rel_type = 'category'");
    787859
    788860        if (!$old_categories) {
     
    821893        $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
    822894        foreach ( $all_affected_cats as $cat_id ) {
    823                 $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 = '$cat_id'");
     895                $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 = '$cat_id' AND rel_type = 'category'");
    824896                $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
    825897                clean_category_cache($cat_id);
Note: See TracChangeset for help on using the changeset viewer.