diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 4f1b0835e..0a50ee0cc 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -647,7 +647,7 @@ function checked( $checked, $current) { function return_categories_list( $parent = 0 ) { global $wpdb; - 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" ); + 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" ); } function sort_cats( $cat1, $cat2 ) { @@ -657,6 +657,29 @@ function sort_cats( $cat1, $cat2 ) { return strcasecmp( $cat1['cat_name'], $cat2['cat_name'] ); } +function get_tags_to_edit( $post_id ) { + global $wpdb; + + $post_id = (int) $post_id; + if ( !$post_id ) + return false; + + $tags = $wpdb->get_results( " + SELECT category_id, cat_name + FROM $wpdb->categories, $wpdb->post2cat + WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_id' AND rel_type = 'tag' + " ); + if ( !$tags ) + return false; + + foreach ( $tags as $tag ) + $tag_names[] = $tag->cat_name; + $tags_to_edit = join( ', ', $tag_names ); + $tags_to_edit = attribute_escape( $tags_to_edit ); + $tags_to_edit = apply_filters( 'tags_to_edit', $tags_to_edit ); + return $tags_to_edit; +} + function get_nested_categories( $default = 0, $parent = 0 ) { global $post_ID, $link_id, $mode, $wpdb; @@ -664,7 +687,7 @@ function get_nested_categories( $default = 0, $parent = 0 ) { $checked_categories = $wpdb->get_col( " SELECT category_id FROM $wpdb->categories, $wpdb->post2cat - WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' + WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' AND rel_type = 'category' " ); if ( count( $checked_categories ) == 0 ) { @@ -721,7 +744,7 @@ function dropdown_categories( $default = 0 ) { function return_link_categories_list( $parent = 0 ) { global $wpdb; - 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" ); + 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" ); } function get_nested_link_categories( $default = 0, $parent = 0 ) { diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php index cdf467a47..897222046 100644 --- a/wp-admin/edit-form-advanced.php +++ b/wp-admin/edit-form-advanced.php @@ -152,6 +152,9 @@ endforeach; +

Enter tags (keywords) that you feel describe this post separated by commas: (ex: dogs, san francisco, cats.)
+ +

diff --git a/wp-admin/upgrade-schema.php b/wp-admin/upgrade-schema.php index 77aa8a112..a41ca9479 100644 --- a/wp-admin/upgrade-schema.php +++ b/wp-admin/upgrade-schema.php @@ -18,6 +18,7 @@ $wp_queries="CREATE TABLE $wpdb->categories ( category_parent bigint(20) NOT NULL default '0', category_count bigint(20) NOT NULL default '0', link_count bigint(20) NOT NULL default '0', + tag_count bigint(20) NOT NULL default '0', posts_private tinyint(1) NOT NULL default '0', links_private tinyint(1) NOT NULL default '0', PRIMARY KEY (cat_ID), @@ -88,6 +89,7 @@ CREATE TABLE $wpdb->post2cat ( rel_id bigint(20) NOT NULL auto_increment, post_id bigint(20) NOT NULL default '0', category_id bigint(20) NOT NULL default '0', + rel_type varchar(64) NOT NULL default 'category', PRIMARY KEY (rel_id), KEY post_id (post_id,category_id) ) $charset_collate; @@ -389,4 +391,4 @@ function populate_roles_210() { } } -?> +?> \ No newline at end of file diff --git a/wp-admin/wp-admin.css b/wp-admin/wp-admin.css index d65e5eb45..06663866d 100644 --- a/wp-admin/wp-admin.css +++ b/wp-admin/wp-admin.css @@ -558,7 +558,7 @@ input.disabled, textarea.disabled { line-height: 140%; } -#titlediv input, #guiddiv input { +#titlediv input, #guiddiv input, #tags_input { margin: 0; width: 100%; } diff --git a/wp-includes/post.php b/wp-includes/post.php index d74769520..581c9391a 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -517,6 +517,7 @@ function wp_insert_post($postarr = array()) { $post_name = apply_filters('name_save_pre', $post_name); $comment_status = apply_filters('comment_status_pre', $comment_status); $ping_status = apply_filters('ping_status_pre', $ping_status); + $tags_input = apply_filters('tags_input_pre', $tags_input); } if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) @@ -651,7 +652,8 @@ function wp_insert_post($postarr = array()) { $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" ); } - wp_set_post_categories($post_ID, $post_category); + wp_set_post_categories( $post_ID, $post_category ); + wp_set_post_tags( $post_ID, $tags_input ); if ( 'page' == $post_type ) { clean_page_cache($post_ID); @@ -769,6 +771,76 @@ function wp_publish_post($post_id) { return wp_update_post(array('post_status' => 'publish', 'ID' => $post_id, 'no_filter' => true)); } +function wp_set_post_tags( $post_id = 0, $tags = '' ) { + global $wpdb; + + $post_id = (int) $post_id; + + if ( !$post_id ) + return false; + + $tags = explode( ',', $tags ); + foreach ( $tags as $tag ) { + $tag = trim( $tag ); + if ( !$tag_slug = sanitize_title( $tag ) ) + continue; // discard + if ( !$tag_id = category_exists( $tag ) ) + $tag_id = wp_create_category( $tag ); + $tag_ids[] = $tag_id; + } + + $tag_ids = array_unique( $tag_ids ); + + // First the old tags + $old_tags = $wpdb->get_col(" + SELECT category_id + FROM $wpdb->post2cat + WHERE post_id = '$post_id' AND rel_type = 'tag'"); + + if ( !$old_tags ) { + $old_tags = array(); + } else { + $old_tags = array_unique( $old_tags ); + } + + // Delete any? + $delete_tags = array_diff( $old_tags, $tag_ids); + if ( $delete_tags ) { + foreach ( $delete_tags as $del ) { + $wpdb->query(" + DELETE FROM $wpdb->post2cat + WHERE category_id = '$del' + AND post_id = '$post_id' + AND rel_type = 'tag' + "); + } + } + + // Add any? + $add_tags = array_diff( $tag_ids, $old_tags ); + if ( $add_tags ) { + foreach ( $add_tags as $new_tag ) { + $new_tag = (int) $new_tag; + if ( !empty($new_tag) ) + $wpdb->query(" + INSERT INTO $wpdb->post2cat (post_id, category_id, rel_type) + VALUES ('$post_id', '$new_tag', 'tag')"); + } + } + + // Update category counts. + $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) ); + foreach ( $all_affected_tags as $tag_id ) { + $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'" ); + $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count' WHERE cat_ID = '$tag_id'" ); + if ( $count == 0 ) + $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '-1' WHERE cat_ID = '$tag_id'" ); + clean_category_cache( $tag_id ); + do_action( 'edit_category', $tag_id ); + do_action( 'edit_tag', $tag_id ); + } +} + function wp_set_post_categories($post_ID = 0, $post_categories = array()) { global $wpdb; @@ -783,7 +855,7 @@ function wp_set_post_categories($post_ID = 0, $post_categories = array()) { $old_categories = $wpdb->get_col(" SELECT category_id FROM $wpdb->post2cat - WHERE post_id = '$post_ID'"); + WHERE post_id = '$post_ID' AND rel_type = 'category'"); if (!$old_categories) { $old_categories = array(); @@ -820,7 +892,7 @@ function wp_set_post_categories($post_ID = 0, $post_categories = array()) { // Update category counts. $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); foreach ( $all_affected_cats as $cat_id ) { - $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'"); + $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'"); $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); clean_category_cache($cat_id); do_action('edit_category', $cat_id); diff --git a/wp-includes/version.php b/wp-includes/version.php index 929e2f37c..860f99eb7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -3,6 +3,6 @@ // This holds the version number in a separate file so we can bump it without cluttering the SVN $wp_version = '2.2-bleeding'; -$wp_db_version = 4860; +$wp_db_version = 4865; ?>