tablename = $table_prefix . 'tags'; /* add WP filters & actions for saving/editing tags */ add_filter('simple_edit_form', array(&$this, 'showTagEntry')); add_filter('edit_form_advanced',array(&$this, 'showTagEntry')); add_filter('edit_page_form', array(&$this, 'showTagEntry')); add_action('edit_post', array(&$this, 'savePostTags')); add_action('publish_post', array(&$this, 'savePostTags')); add_action('save_post', array(&$this, 'savePostTags')); /* check if we need to upgrade */ if (get_option('lp_opt_version_tags') < $this->version) add_action('plugins_loaded', array(&$this, 'install')); } function getAllTags($sort='desc') { /* get all tags from the db */ global $wpdb; $all_tags = array(); switch($sort) { case 'asc': $orderby = 'tag_count'; break; case 'desc': $orderby = 'tag_count DESC'; break; default: $orderby = 'tag_name'; break; } $tags = $wpdb->get_results("SELECT tag_name, COUNT(post_id) AS tag_count FROM {$this->tablename} GROUP BY tag_name ORDER BY $orderby "); if (is_array($tags)) { foreach($tags as $t) $all_tags[$t->tag_name] = $t->tag_count; switch($sort) { case 'natural': uksort($all_tags, 'strnatcasecmp'); break; default: //do nothing break; } } return $all_tags; } function getPostTags($id) { /* get all tags for the specified post from the db */ global $wpdb; $post_tags = array(); $tags = $wpdb->get_results("SELECT tag_name FROM {$this->tablename} WHERE post_id='$id'"); if (is_array($tags)) { foreach($tags as $t) $post_tags[] = $t->tag_name; } return $post_tags; } function showTagEntry() { /* display tag entry & suggested tag fields */ global $postdata, $content; /* get this post's tags */ $tags = $this->getPostTags($postdata->ID); $post_tags = implode(', ', $tags); /* suggest tags based on existing tags & post content */ $top_tags = $this->getAllTags('desc'); $suggested = array(); foreach($top_tags as $tag => $count) { if (!in_array($tag, $tags) && stristr($content, $tag)) { $suggested[] = $tag; if (count($suggested) >= $this->max_suggest) break; } } if (count($suggested) < $this->max_suggest) { foreach($top_tags as $tag => $count) { if (!in_array($tag, $tags) && !in_array($tag, $suggested)) { $suggested[] = $tag; if (count($suggested) >= $this->max_suggest) break; } } } if (count($suggested) > 0) { $suggested_tags = '' . implode(' ', $suggested) . ''; } else $suggested_tags = ''; // TODO: add word boundaries to the regexp, or use a global array to store // already added tags, too tired now and too many years since I last // used JS --ludo echo ' '; /* display tag entry fields */ echo "
Tags

Suggested Tags $suggested_tags
"; } function saveTag($id, $tag) { global $wpdb; $wpdb->query("INSERT INTO {$this->tablename} VALUES ('$id', '$tag')"); } function savePostTags($id) { /* save new list of post tags to database */ global $wpdb; /* clear old values first */ $wpdb->query("DELETE FROM {$this->tablename} WHERE post_id='$id'"); /* clean up tag list & save */ $post_tags = array_unique(explode(',', $_REQUEST['tag_list'])); if (is_array($post_tags)) { foreach($post_tags as $tag) { $tag = trim($tag); if (!empty($tag)) $this->saveTag($id, $tag); } } } function deleteTags($todelete) { /* deletes list of tags from the database */ global $wpdb; /* split list of tags */ $old_tags = array_unique(explode(',', $todelete)); $old_list = ''; foreach($old_tags as $key=>$tag) { if (!empty($old_list)) $old_list .= ','; $old_list .= "'" . addslashes(trim($tag)) . "'"; } /* delete old tags */ if ($wpdb->query("DELETE FROM {$this->tablename} WHERE tag_name IN ($old_list)") > 0) return "Deleted the following tag(s): $todelete"; else return "Could not find tag(s) in database: $todelete"; } function updateTags($old, $new, $rename=true) { /* resaves list of old tags to new value(s) */ global $wpdb; /* split lists of old & new tags */ $old_tags = array_unique(explode(',', $old)); $old_list = ''; foreach($old_tags as $tag) { if (!empty($old_list)) $old_list .= ','; $old_list .= "'" . addslashes(trim($tag)) . "'"; } if (trim(str_replace(',', '', stripslashes($new))) == '') return ('No new tag specified!'); $new_tags = array_unique(explode(',', $new)); /* get list of posts matching old tags */ $posts = $wpdb->get_results("SELECT post_id FROM {$this->tablename} WHERE tag_name IN ($old_list) GROUP BY post_id"); if (is_array($posts) && (count($posts) > 0)) { if ($rename) { /* delete old tags */ $wpdb->query("DELETE FROM {$this->tablename} WHERE tag_name IN ($old_list)"); } /* save new tags */ foreach ($posts as $p) { foreach($new_tags as $tag) { $tag = addslashes(trim($tag)); /* check if tag already exists for post before saving */ if ($wpdb->query("SELECT post_id, tag_name FROM {$this->tablename} WHERE tag_name='$tag' AND post_id='{$p->post_id}'") <= 0) $this->saveTag($p->post_id, $tag); } } if ($rename) return "Renamed tag(s) «$old» to «$new»"; else return "Added tag(s) «$new» to posts tagged with «$old»"; } else { return "No posts found matching tag(s): $old"; } } function install() { /* creates the LightPress tags table & imports other tag formats */ global $wpdb; /* create tags table if it doesn't exist */ $table =& $this->tablename; $found = false; foreach ($wpdb->get_results("SHOW TABLES;", ARRAY_N) as $row) { if ($row[0] == $table) { $found = true; break; } } if (!$found) { $res = $wpdb->get_results("CREATE TABLE $table " . $this->_tablestruct); } /* import Jerome's Keywords tags */ $qry = "SELECT post_id, meta_id, meta_key, meta_value FROM {$wpdb->postmeta} meta WHERE meta.meta_key = 'keywords'"; $metakeys = $wpdb->get_results($qry); if (is_array($metakeys)) { foreach($metakeys as $post_meta) { if ($post_meta->meta_value != '') { $post_keys = explode(',', $post_meta->meta_value); foreach($post_keys as $keyword) { $keyword = addslashes(trim($keyword)); if ($keyword != '') $this->saveTag($post_meta->post_id, $keyword); } } delete_post_meta($post_meta->post_id, 'keywords'); } } update_option('lp_opt_version_tags', $this->version); } } ?>