Make WordPress Core

Changeset 5110


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

A backend interface for tagging.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r5089 r5110  
    648648function return_categories_list( $parent = 0 ) {
    649649    global $wpdb;
    650     return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( link_count = 0 OR category_count != 0 OR ( link_count = 0 AND category_count = 0 ) ) ORDER BY category_count DESC" );
     650    return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( ( link_count = 0 AND tag_count = 0 ) OR category_count != 0 OR ( link_count = 0 AND category_count = 0 AND tag_count = 0 ) ) ORDER BY category_count DESC" );
    651651}
    652652
     
    658658}
    659659
     660function get_tags_to_edit( $post_id ) {
     661    global $wpdb;
     662
     663    $post_id = (int) $post_id;
     664    if ( !$post_id )
     665        return false;
     666
     667    $tags = $wpdb->get_results( "
     668             SELECT category_id, cat_name
     669             FROM $wpdb->categories, $wpdb->post2cat
     670             WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_id' AND rel_type = 'tag'
     671             " );
     672    if ( !$tags )
     673        return false;
     674
     675    foreach ( $tags as $tag )
     676        $tag_names[] = $tag->cat_name;
     677    $tags_to_edit = join( ', ', $tag_names );
     678    $tags_to_edit = attribute_escape( $tags_to_edit );
     679    $tags_to_edit = apply_filters( 'tags_to_edit', $tags_to_edit );
     680    return $tags_to_edit;
     681}
     682
    660683function get_nested_categories( $default = 0, $parent = 0 ) {
    661684    global $post_ID, $link_id, $mode, $wpdb;
     
    665688             SELECT category_id
    666689             FROM $wpdb->categories, $wpdb->post2cat
    667              WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID'
     690             WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' AND rel_type = 'category'
    668691             " );
    669692
     
    722745function return_link_categories_list( $parent = 0 ) {
    723746    global $wpdb;
    724     return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( category_count = 0  OR link_count != 0 OR ( link_count = 0 AND category_count = 0 ) ) ORDER BY link_count DESC" );
     747    return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( ( category_count = 0 AND tag_count = 0 ) OR link_count != 0 OR ( link_count = 0 AND category_count = 0 AND tag_count = 0 ) ) ORDER BY link_count DESC" );
    725748}
    726749
  • trunk/wp-admin/edit-form-advanced.php

    r5056 r5110  
    153153<?php echo $form_prevstatus ?>
    154154
     155<div class="tagdiv"><p>Enter tags (keywords) that you feel describe this post separated by commas: (ex: dogs, san francisco, cats.)<br />
     156<input type="text" name="tags_input" id="tags_input" size="30" tabindex="3" value="<?php echo get_tags_to_edit( $post_ID ); ?>" />
     157</p></div>
    155158
    156159<p class="submit">
  • trunk/wp-admin/upgrade-schema.php

    r4953 r5110  
    1919  category_count bigint(20) NOT NULL default '0',
    2020  link_count bigint(20) NOT NULL default '0',
     21  tag_count bigint(20) NOT NULL default '0',
    2122  posts_private tinyint(1) NOT NULL default '0',
    2223  links_private tinyint(1) NOT NULL default '0',
     
    8990  post_id bigint(20) NOT NULL default '0',
    9091  category_id bigint(20) NOT NULL default '0',
     92  rel_type varchar(64) NOT NULL default 'category',
    9193  PRIMARY KEY  (rel_id),
    9294  KEY post_id (post_id,category_id)
  • trunk/wp-admin/wp-admin.css

    r5103 r5110  
    559559}
    560560
    561 #titlediv input, #guiddiv input {
     561#titlediv input, #guiddiv input, #tags_input {
    562562    margin: 0;
    563563    width: 100%;
  • 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);
  • trunk/wp-includes/version.php

    r4860 r5110  
    44
    55$wp_version = '2.2-bleeding';
    6 $wp_db_version = 4860;
     6$wp_db_version = 4865;
    77
    88?>
Note: See TracChangeset for help on using the changeset viewer.