tablename = $wpdb->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 $post, $content;
/* get this post's tags */
$tags = $this->getPostTags($post->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($post->post_content, $tag) !== false) {
$suggested[] = $tag;
if (count($suggested) >= $this->max_suggest)
break;
}
}
$suggested_tags = '';
foreach ($suggested as $tag)
$suggested_tags .= "
$tag ({$top_tags[$tag]})";
if ($suggested_tags)
$suggested_tags = "";
if (count($suggested) < $this->max_suggest) {
$extra_tags = '';
$i = count($suggested);
foreach($top_tags as $tag => $count) {
if (!in_array($tag, $tags) && !in_array($tag, $suggested)) {
$i++;
$extra_tags .= "$tag ({$top_tags[$tag]})";
if ($i >= $this->max_suggest)
break;
}
}
if ($extra_tags)
$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 "
";
}
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 autoTags($keyword) {
/* automatically applies a tag to a post matching keywords */
global $wpdb;
if (trim(str_replace(',', '', stripslashes($keyword))) == '')
return ('No keywords specified!');
/* split list of new tags */
$keywords = array_unique(explode(',', $keyword));
$tag = '';
$output = '';
foreach($keywords as $kw) {
$tag = mysql_real_escape_string(trim($kw));
if ($tag) {
$sq = "select id as post_id from $wpdb->posts where post_title like '%$tag%'";
/* get list of posts matching the keyword */
$posts = $wpdb->get_results($sq);
if (is_array($posts) && (count($posts) > 0)) {
/* save new tags */
foreach ($posts as $p) {
$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);
}
$output .= "Added tag «$tag» to posts matched with «$kw»\n";
} else {
$output .= "No posts found matching: $kw\n";
}
}
}
}
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);
}
}
?>