Description: Allows you and your users to edit their comments inline. Admin and editors can edit all comments.
Special Thanks: An extra special thanks goes out to Vivien from Inspiration Bit (http://www.inspirationbit.com). Without her, this plugin release wouldn't have been possible. Brett Terpstra from Circle Six Design (http://blog.circlesixdesign.com) helped fix a rather major bug as well during testing. Thank you Brett.
Copyright 2007-2008 Ronald Huereca (email : ronalfy at(@) gmail dot(.) com
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
*/
class WPrapAjaxEditComments {
var $commentClassName = "editComment"; //Do not edit this name
var $commentAuthorName = "editAuthor"; //Do not edit this name
var $skipDiv = false;
var $minutes = 15;
var $admin = "true";
var $cookieName = "WPrapAjaxEditCommentId";//Do not edit this name
var $optionsName = "WPAjaxEditComments"; //Do not edit this name
var $userOptionsName = "WPAjaxEditAuthorUserOptions";
var $adminOptions = false;
var $userOptions = false; //Do not edit this name
var $session = false;
//constructor
function WPrapAjaxEditComments() {
}
//Run when the plugin is first activated
function init() {
$this->getAdminOptions();
}
//Returns an array of admin options
function getAdminOptions() {
if ($this->adminOptions == false) {
$optionsName = $this->optionsName;
$adminOptions = array(
'allow_editing' => 'true',
'minutes' => '15',
'edit_text' => 'You may click on your name and/or comment to edit.',
'show_timer' => 'true',
'spam_text' => 'Your edited comment was marked as spam. If this is in error, please contact the admin.',
'email_edits' => 'false',
'number_edits' => '0',
'use_akismet' => 'true',
'use_mb_convert' => 'true'
);
$options = get_option($optionsName);
if (!empty($options)) {
foreach ($options as $key => $option)
$adminOptions[$key] = $option;
}
update_option($optionsName, $adminOptions);
$this->adminOptions = $adminOptions;
}
return $this->adminOptions;
}
//Returns an array of user options
function getUserOptions() {
global $user_email;
if ($this->userOptions == false) {
if (empty($user_email)) {get_currentuserinfo();}
if (empty($user_email)) { return ''; }
$optionsName = $this->userOptionsName;
$defaults = array('author_editing' => 'true',
'comment_editing' => 'true',
'admin_editing' => 'false',
'inline_editing' => 'false',
'show_links' => 'false'
);
$userOptions = get_option($optionsName);
if (!isset($userOptions)) {
$userOptions = array();
}
//See if an older version doesn't match the new defaults
if (empty($userOptions[$user_email])) {
$userOptions[$user_email] = $defaults;
update_option(optionsName, $userOptions);
} elseif(!is_array($userOptions[$user_email])) {
$userOptions[$user_email] = $defaults;
update_option(optionsName, $userOptions);
} else {
foreach ($userOptions[$user_email] as $key => $option) {
<p><label for="edit_text"><?php _e('Set edit text (no HTML allowed):', "WPAjaxEditComments") ?> </label><br/><input size="75" type="text" name="wpAJAXEditText" value="<?php _e(format_to_edit(stripslashes($WPAjaxEditComments['edit_text'])), 'WPAjaxEditComments') ?>" id="edit_text"/></p>
<p><label for="spam_text"><?php _e('Set spam notification text. Please limit to one line if possible since this text will show up when editing the comment or author (Tags allowed: em, a, strong, blockquote):', "WPAjaxEditComments") ?> </label><br/><textarea cols="100" rows="3" name="wpAJAXSpamText" id="spam_text"><?php _e(stripslashes(apply_filters('comment_edit_save', $WPAjaxEditComments['spam_text'])), "WPAjaxEditComments")?></textarea></p>
<h3><?php _e('Allow Anyone to Edit Their Own Comments?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "No" will turn off comment editing for everyone except admin types who have post and page editing permissions.', "WPAjaxEditComments") ?></p>
<p><?php _e('Selecting "Yes" will send you an email each time someone edits their comment. This is not recommended for high-traffic blogs.', "WPAjaxEditComments") ?></p>
<h3><?php _e('Check Edited Comments Against Akismet (if installed)?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "Yes" will submit the comment to Akismet for re-validation after every edit. This is strongly recommended since users can write a benign comment, but add in spam links after an edit.', "WPAjaxEditComments") ?></p>
<p><?php _e('Some servers do not have this installed. If you disable this option, be sure to test out various characters. The mb_convert_encoding function is necessary to convert from UTF-8 to various charsets.', "WPAjaxEditComments") ?></p>
<h3><?php _e("Delete all security keys (ensure you have a recent backup)?", "WPAjaxEditComments") ?></h3>
<p><?php _e("Each time a user leaves a comment, a security key is stored as a custom key. Periodically you may want to delete this information. Please backup your database first.", "WPAjaxEditComments") ?></p>
<p><?php _e("For versions earlier than 1.1.0.4, you will have to log into phpMyAdmin and run this query (please back up your database first): <strong>delete from wp_postmeta where length(meta_value) = 96</strong>", "WPAjaxEditComments") ?></p>
<h3><?php _e('Turn Off Comment Editing in Admin Panel?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "Yes" will disable comment editing in the Admin Comments Panel. This option is irrelevant if Name Editing and Comment Editing are disabled (see below).', "WPAjaxEditComments") ?></p>
<h3><?php _e('Turn Off Inline Editing?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "Yes" will disable the "edit in place" type comment editing in a post. The admin panel is unaffected by this option. This option is irrelevant if Name Editing and Comment Editing are disabled (see below).', "WPAjaxEditComments") ?></p>
<h3><?php _e('Turn Off Comment Post Links?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "Yes" will disable the links that show up underneath a comment. These links allow you to edit, delete, or mark a comment as spam. These links are also disabled if you disable comment editing (see below).', "WPAjaxEditComments") ?></p>
<h3><?php _e('Turn On Name Editing?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "Yes" will enable your ability to edit the author details. Selecting "No" will disable your ability to edit comment author details in the post and admin panel.', "WPAjaxEditComments") ?></p>
<h3><?php _e('Turn On Comment Editing?', "WPAjaxEditComments") ?></h3>
<p><?php _e('Selecting "Yes" will enable your ability to edit a user\'s comment. Selecting "No" will disable your ability to edit comments in the post and admin panel.', "WPAjaxEditComments") ?></p>
//$id = the comment id - $action ("0"=de-spam, "1"=spam).
function spamComment($id, $action) {
if (empty($id)) { return false; }
if ( $this->canEditPostsPage() ) {
if ($action == "1" ) { if (!wp_set_comment_status($id, 'spam')) { return false; } }
if ($action == "0" ) { if (!wp_set_comment_status($id, 'approve')) { return false; } }
return true;
}
return false;
}
//Returns how much time is left to edit on a comment in a comma deliminated variable
//Returns $minutes,$seconds
//Takes in a comment ID
//One concern here is if the admin increases the time in the admin panel - The user will be under the impression that there is more time to edit than there really is. This is a minor and rare bug, so I chose to leave it alone.
function getTimeLeft($id) {
global $wpdb;
if (empty($id)) { return "0,0"; }
$adminEdit = $this->getAdminOptions();
$adminMinutes = $adminEdit['minutes'];
$query = "SELECT (UNIX_TIMESTAMP('" . current_time('mysql') . "') - UNIX_TIMESTAMP(comment_date)) time FROM $wpdb->comments where comment_ID = $id";
//Return true if spam, return false if not (or if ID is invalid or Akismet isn't installed)
function checkSpam($id, $postId) {
if (empty($id) || empty($postId)) { return false; }
$options = $this->getAdminOptions();
if ($options['use_akismet'] == "false") { return false;} //Check to see if the admin has enabled akismet checking
//Check for SPAM if Akismet is installed
if (function_exists("akismet_check_db_comment")) {
if (!$this->canEditPostsPage()) { //Skip if admin
if (akismet_verify_key(get_option('wordpress_api_key')) != "failed") { //If API key fails, keep on going as if Akismet isn't installed
$response = akismet_check_db_comment($id);
if ($response == "true") { //You have spam
wp_set_comment_status($id, 'spam');
return true;
}
}
}
}
return false;
}
//Sends a notification of an edit via e-mail - Code sampled from Subscribe to Comments
//Takes in a comment ID and Post ID - Returns false if email failed
function editNotification($id, $postId) {
global $wpdb;
if (empty($id) || empty($postId)) { return false; }
//Make sure this option is enabled and the user editing isn't admin
$options = $this->getAdminOptions();
if ($options['email_edits'] == "false" || $this->canEditPostsPage()) { return false; }
$comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$id' LIMIT 1", ARRAY_A);
$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1", ARRAY_A);
if ( $comment['comment_approved'] == '1' && $comment['comment_type'] == '' ) { // Comment has been approved and isn't a trackback or a pingback, so we should send out notifications
$message = sprintf(__("A comment has been edited on post %s", 'WPAjaxEditComments') . ": \n%s\n\n", stripslashes($post['post_title']), get_permalink($comment['comment_post_ID']));
//Determines if a comment can be edited or not based on the ID and time elapsed
//$id = The comment id
//$postId = The post id for the comment
//Returns true if the comment can be edited, false if not
function canEdit($id, $postId) {
global $wpdb;
if (empty($id) || empty($postId)) { return false; }
//Return true if the user can edit the comment
if ($this->canEditPostsPage()) { return true; }
//Checks to see if the admin has allowed comment editing
$adminEdit = $this->getAdminOptions();
$adminEdit = $adminEdit['allow_editing'];
if ($adminEdit != 'true') {
return false;
}
$query = "SELECT UNIX_TIMESTAMP(comment_date) time, comment_author_IP, comment_date_gmt, comment_post_ID, comment_ID FROM $wpdb->comments where comment_ID = $id";
//Get the IP, Date, and Timestamp
$comment = $wpdb->get_row($query, ARRAY_A); if (!comment) { return false; }
$meta = $wpdb->get_row("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = " . $comment['comment_post_ID'] . " and meta_key = " . $comment['comment_ID'], ARRAY_A);