<?php /* Plugin Name: Comment Karma Plugin URI: http://cyber-knowledge.net/blog/2006/10/15/wordpress-plugin-rate-your-comments-comment-karma/ Description: Allows you to rate comments up or down. Author: Alex Bailey Author URI: http://cyber-knowledge.net Version: 1.0 */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ add_action('comment_post', 'ck_comment_posted'); //Hook into WordPress global $table_prefix, $wpdb; $table_name = $table_prefix . "ck_karma"; if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { ck_install(); } function ck_install() //Install the needed SQl entries. { global $table_prefix, $wpdb; $table_name = $table_prefix . "ck_karma"; $sql = 'CREATE TABLE `' . $table_name . '` (' //Add table . ' `ck_comment_id` VARCHAR(10) NOT NULL, ' . ' `ck_ips` BLOB NOT NULL, ' . ' `ck_rating` VARCHAR(4) NOT NULL' . ' )' . ' ENGINE = myisam;'; mysql_query($sql); $ck_result = mysql_query('SELECT comment_ID FROM ' . $table_prefix . 'comments'); //Put all IDs in our new table while($ck_row = mysql_fetch_array($ck_result, MYSQL_ASSOC)) //Wee loop { mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating) VALUES ('" . $ck_row['comment_ID'] . "', '', '0')"); } } function ck_get_karma($comment_id) { global $table_prefix; $table_name = $table_prefix . 'ck_karma'; $query = "SELECT ck_rating FROM `$table_name` WHERE ck_comment_id = $comment_id"; $result = mysql_query($query); if(!$result) { return('error|1'); } else { if($row = mysql_fetch_assoc($result)) { $karma = $row['ck_rating']; } else { return('error'); } if($karma < 0) { $karma = '-'. $karma; } else { $karma = '+'. $karma; } return $karma; } } function ck_comment_posted($ck_comment_id) //When comment posted this executes { global $table_prefix, $wpdb; $table_name = $table_prefix . "ck_karma"; mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating) VALUES ('" . $ck_comment_id . "', '" . getenv("REMOTE_ADDR") . "', '0')"); //Adds the new comment ID into our made table, with the users IP } function ck_display_karma() { $plugin_path = get_bloginfo('url').'/wp-content/plugins/ck-karma/'; $ck_link = str_replace('http://', '', get_bloginfo('url')); $ck_comment_ID = get_comment_ID(); $ck_comment_karma = ck_get_karma($ck_comment_ID); global $table_prefix, $wpdb; $table_name = $table_prefix . "ck_karma"; $ck_sql = "SELECT ck_ips FROM `$table_name` WHERE ck_comment_id = $ck_comment_ID"; $ck_result = mysql_query($ck_sql); if(!$ck_result) { mysql_error(); } if(!$ck_row = mysql_fetch_array($ck_result, MYSQL_ASSOC)) { mysql_error(); } if(strstr($ck_row['ck_ips'], getenv("REMOTE_ADDR"))) { echo("<img src=\"{$plugin_path}/images/gray_up.png\" /> <img src=\"{$plugin_path}/images/gray_down.png\" />"); } else { //Use onClick for the image instead, fixes the style link underline problem as well. echo(" <img id=\"up-$ck_comment_ID\" src=\"{$plugin_path}images/up.png\" alt=\"Add karma\" onclick=\"javascript:karma('$ck_comment_ID', 'add', '{$ck_link}/wp-content/plugins/ck-karma/', '$table_prefix');\" /> "); echo("<img id=\"down-$ck_comment_ID\" src=\"{$plugin_path}images/down.png\" alt=\"Subtract karma\" onclick=\"javascript:karma('$ck_comment_ID', 'subtract', '{$ck_link}/wp-content/plugins/ck-karma/', '$table_prefix')\" />"); //Phew } echo " <small id=\"karma-{$ck_comment_ID}\">{$ck_comment_karma}</small>"; }?>