A backend interface for tagging.

git-svn-id: http://svn.automattic.com/wordpress/trunk@5110 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
matt 2007-03-26 07:28:29 +00:00
parent ee323a457b
commit 613dbfcffa
6 changed files with 109 additions and 9 deletions

View File

@ -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 ) {

View File

@ -152,6 +152,9 @@ endforeach;
<?php echo $form_pingback ?>
<?php echo $form_prevstatus ?>
<div class="tagdiv"><p>Enter tags (keywords) that you feel describe this post separated by commas: (ex: dogs, san francisco, cats.)<br />
<input type="text" name="tags_input" id="tags_input" size="30" tabindex="3" value="<?php echo get_tags_to_edit( $post_ID ); ?>" />
</p></div>
<p class="submit">
<span id="autosave"></span>

View File

@ -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() {
}
}
?>
?>

View File

@ -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%;
}

View File

@ -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);

View File

@ -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;
?>