Ronald Huereca 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 Ronald Huereca (email : ron alfy [a t ] g m ail 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 = 30; var $admin = "true"; var $cookieName = "WPrapAjaxEditCommentId";//Do not edit this name var $optionsName = "WPAjaxEditComments";//Do not edit this name var $wpAuthorOptions = "WPAjaxEditAuthorUserOptions"; //Do not edit this name //constructor function WPrapAjaxEditComments() { } //Adds a div to the comment text with a class name so that the AJAX can change the text to a textbox function addCommentDiv($text) { global $comment; if (!$this->canEdit($comment->comment_ID, $comment->comment_post_ID) || !$this->canEditComment()) { return $text; } //Skip this if admin if (!$this->canEditPostsPage($comment->comment_ID, $comment->comment_post_ID)) { $text = "

You may click on your name and/or comment to edit.

" . $text; } if ($this->skipDiv) { return $text; } //Regular expression to check if there is a div already added $pattern = '/div class=\"' . $this->commentClassName . '\"/i'; //If there is not a div already, add it in if (!preg_match($pattern, $text)) { $text = '
' . $text . '
'; } return $text; } //End function addCommentDiv() function addAuthorSpan($text) { global $comment; if (!$this->canEdit($comment->comment_ID, $comment->comment_post_ID) || !$this->canEditAuthor()) { return $text; } //Regular expression to check if there is a div already added $pattern = '/span class=\"' . $this->commentAuthorName . '\"/i'; //If there is not a div already, add it in if (!preg_match($pattern, $text)) { $text = '' . $text . ''; } return $text; } //End function addAuthorDiv() //Prints out the admin page function printAdminPage() { $WPAjaxEditComments = array('allow_editing' => 'true', 'minutes' => '30'); $savedOptions = get_option($this->optionsName); if (!empty($savedOptions)) { $WPAjaxEditComments = $savedOptions; } if (isset($_POST['update_wp_ajaxEditCommentSettings'])) { $error = false; $updated = false; //Validate the comment time entered if (isset($_POST['wpAJAXCommentTime'])) { $commentTimeErrorMessage = ''; $commentClass = 'error'; if (!preg_match('/^\d+$/i', $_POST['wpAJAXCommentTime'])) { $commentTimeErrorMessage = "Comment time must be a numerical value"; $error = true; } elseif($_POST['wpAJAXCommentTime'] < 1) { $commentTimeErrorMessage = "Comment time must be greater than one minute."; $error = true; } else { $WPAjaxEditComments['minutes'] = $_POST['wpAJAXCommentTime']; $updated = true; } if (!empty($commentTimeErrorMessage)) { ?>
">

optionsName, $WPAjaxEditComments); ?>

Settings successfully updated.

">

WP AJAX Edit Comment

Allow Anyone to Edit Their Own Comments?

Selecting "No" will turn off comment editing for everyone except admin types who have post and page editing permissions.

    

wpAuthorOptions); //Save the updated options to the database if (isset($_POST['wpAJAXAuthor']) && isset($_POST['wpAJAXComment']) && isset($_POST['wpAJAXAdminEdits'])) { if (isset($user_email)) { $author_options[$user_email] = $_POST['wpAJAXAuthor'] . "," . $_POST['wpAJAXComment'] . "," . $_POST['wpAJAXAdminEdits']; ?>

Settings successfully updated.

wpAuthorOptions, $author_options); } } //Get the author options if (!empty($author_options[$user_email])) { $author_options = $author_options[$user_email]; $author_options = explode(",", $author_options); if (sizeof($author_options) >= 3) { $author = $author_options[0]; $comment = $author_options[1]; $adminEdits = $author_options[2]; } } ?>
">

WP AJAX Edit Comment - Individual User Options

Turn On Name Editing?

Selecting "Yes" will enable your ability to edit the author details.

    

Turn On Comment Editing?

Selecting "Yes" will enable your ability to edit a user's comment.

    

Turn Off Comment Editing in Admin Panel?

Selecting "Yes" will disable comment editing in the Admin Comments Panel.

    

JS(); } }//End function addHeaderJS function addAdminHeaderJS() { global $user_email; if (empty($user_email)) { get_currentuserinfo(); } if (empty($user_email)) { return; } $author_options = get_option($this->wpAuthorOptions); //Get the author options if (!empty($author_options[$user_email])) { $author_options = $author_options[$user_email]; $author_options = explode(",", $author_options); if (sizeof($author_options) >= 2) { $author = $author_options[0]; $comment = $author_options[1]; $adminEdits = $author_options[2]; if ($adminEdits == "true") { return; } } } $this->JS(); } function JS() { echo "\n"; echo "\n"; echo "\n"; } //Returns a comment for editing in a textbox //Takes in a comment id function getComment($id) { global $wpdb; if (!$this->canEdit($id, $post->ID)) { return 0; } //mysql_query("SET NAMES 'utf8'", $conn); $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = ".$id); if (!$comment) { return 0; } //Get the comment ready for the textbox $content = apply_filters('comment_edit_save', $comment->comment_content); return $content; }//End function getComment //Adds input tags so that JavaScript can use them such as the blog's url function addHiddenInput($content) { //Do not add it in if home, archive, search if (is_home()) { return $content; } if (is_single() || is_page()) { $content .= '

'; } return $content; }//End function addHiddenInput function addFooterHiddenInput() { echo '

'; } //Strips the ID to just numbers function getCommentID($id) { preg_match('/([0-9]+)$/i', $id, $matches); if (is_numeric($matches[1])) { return $matches[1]; } else { return 0; } } //End function getCommentID //Saves the author to the database /*id = The comment id url = The author's url name = The author's name*/ function saveAuthor($id, $url, $name) { global $wpdb; if (!$this->canEdit($id, $post->ID)) { return 0; } if (!isset($name)) { return 0; } if (trim($name) == '') { return 0; } $url = urldecode($url); $name = urldecode($name); $url = trim(apply_filters('comment_url', $url)); $saveUrl = apply_filters('pre_comment_author_url', $url); $name = apply_filters('comment_author', $name); $saveName = apply_filters('pre_commment_author_name', $name); if(!$wpdb->query("UPDATE $wpdb->comments SET comment_author = '{$saveName}', comment_author_url = '{$saveUrl}' WHERE comment_ID = $id")) { return 0; } $url = stripslashes($url); $name = stripslashes($name); $returnText = ''; if (!empty($url)) { $returnText = '' . $name . ''; } else { $returnText = $name; } //For WP Cache if (function_exists("wp_cache_get_postid_from_comment")) { wp_cache_get_postid_from_comment($id); } return $returnText; } //End function saveAuthor //Saves the edited comment to the database function saveComment($id, $content) { global $wpdb, $post; if (trim($content) == '') { return 0; } if (!$this->canEdit($id, $post->ID)) { return 0; } $content = urldecode($content); $content = apply_filters('comment_save_pre', $content); $saveContent = apply_filters('pre_comment_content', $content); if(!$wpdb->query("UPDATE $wpdb->comments SET comment_content = '{$saveContent}' WHERE comment_ID = $id")) { return 0; } $this->skipDiv = true; $content = apply_filters('comment_text', $content); $this->skipDiv = false; //For WP Cache if (function_exists("wp_cache_get_postid_from_comment")) { wp_cache_get_postid_from_comment($id); } return stripslashes($content); } //end function saveComment //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) { //Return true if the user can edit the comment if ($this->canEditPostsPage($id, $postId)) { return true; } //Checks to see if the admin has allowed comment editing $adminEdit = get_option($this->optionsName); $adminEdit = $adminEdit['allow_editing']; if (empty($adminEdit)) { $adminEdit = $this->admin; } if ($adminEdit != 'true') { return false; } //Check to see if there's a valid cookie if (isset($_COOKIE[$this->cookieName . $id])) { if ($this->getCommentID($_COOKIE[$this->cookieName . $id]) == $id) { return true; } } return false; }//end function canEdit //Returns true if a person can edit a post/page //Returns false otherwise //Called by canEdit() and setCommentCookie function canEditPostsPage($id, $postId) { //Stolen, err, borrowed from edit_comment_link() if (!current_user_can('edit_page', $postId)) { return false; } if (!current_user_can('edit_post', $postId)) { return false; } return true; }//end function canEditPostsPage //Returns true unless the user has disabled author editing function canEditAuthor() { global $user_email; if (empty($user_email)) { get_currentuserinfo(); } if (empty($user_email)) { return true; } $author_options = get_option($this->wpAuthorOptions); //Get the author options if (!empty($author_options[$user_email])) { $author_options = $author_options[$user_email]; $author_options = explode(",", $author_options); if (sizeof($author_options) >= 2) { $author = $author_options[0]; $comment = $author_options[1]; if ($author == "false") { return false; } } } return true; } //end function canEditAuthor //Returns true unless the user has disabled comment editing function canEditComment() { global $user_email; if (empty($user_email)) { get_currentuserinfo(); } if (empty($user_email)) { return true; } $author_options = get_option($this->wpAuthorOptions); //Get the author options if (!empty($author_options[$user_email])) { $author_options = $author_options[$user_email]; $author_options = explode(",", $author_options); if (sizeof($author_options) >= 2) { $author = $author_options[0]; $comment = $author_options[1]; $adminEdits = $author_options[2]; if ($comment == "false") { return false; } } } return true; } //Sets a comment cookie for the commentator (treat as a private function) function setCommentCookie($commentId) { global $comment_type, $post; //Return if setting the cookie is unnecessary if ($this->canEditPostsPage($commentId, $post->ID)) { return $commentId; } //Get the minutes $minutes = get_option($this->optionsName); $minutes = $minutes['minutes']; if (!is_numeric($minutes)) { $minutes = $this->minutes; } if ($minutes < 1) { $minutes = $this->minutes; } //Set the cookie $cookieName = $this->cookieName . $commentId; $value = $cookieName; $expire = time()+60*$minutes; if (!isset($_COOKIE[$cookieName])) { setcookie($cookieName, $value, $expire, COOKIEPATH, COOKIE_DOMAIN); } return $commentId; } //end function setCommentCookie }//End Class WPrapAjaxEditComments if (class_exists("WPrapAjaxEditComments")) { $rap_ajaxComment = new WPrapAjaxEditComments(); } //Initialize the admin panel function WPrapAjaxEditComments_ap() { global $rap_ajaxComment; if (!isset($rap_ajaxComment)) { return; } if (function_exists('add_options_page')) { add_options_page('AJAX Edit Comments', 'AJAX Edit Comments', 9, basename(__FILE__), array(&$rap_ajaxComment, 'printAdminPage')); } if (function_exists('add_submenu_page')) { add_submenu_page('profile.php', "AJAX Edit Comments Options","AJAX Edit Comments", 9, basename(__FILE__), array(&$rap_ajaxComment, 'printAuthorPage')); } } //Template tag WPrapAjaxEditComments_CanEdit //Returns true if a user can edit - False if they can't - Must be run within the loop //Also returns false if people are admins function WPrapAjaxEditComments_CanEdit() { global $comment, $rap_ajaxComment; //If we're not in the loop, return false if (!isset($comment) || !isset($rap_ajaxComment)) { return false; } //If admin, return false if ($rap_ajaxComment->canEditPostsPage($comment->comment_ID, $comment->comment_post_ID)) { return false; } //Find out if the user can edit if ($rap_ajaxComment->canEdit($comment->comment_ID, $comment->comment_post_ID)) { return true; } return false; } //Yay, actions. if (isset($rap_ajaxComment)) { add_action('admin_menu', 'WPrapAjaxEditComments_ap'); add_action('admin_head', array(&$rap_ajaxComment, 'addAdminHeaderJS')); add_action('admin_footer', array(&$rap_ajaxComment, 'addFooterHiddenInput')); add_action('wp_head', array(&$rap_ajaxComment, 'addHeaderJS')); add_action('comment_post', array(&$rap_ajaxComment, 'setCommentCookie')); //Yay, filters. add_filter('comment_text', array(&$rap_ajaxComment, 'addCommentDiv'), '1000'); //Low priority so other HTML can be added first add_filter('get_comment_author_link', array(&$rap_ajaxComment, 'addAuthorSpan'), '1000'); //Low priority so other HTML can be added first add_filter('the_content', array(&$rap_ajaxComment, 'addHiddenInput'), '1000'); //Low priority so other HTML can be added first } ?>