home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Plugins / wp-ajax-edit-comments.1.1.4.3 / wp-ajax-edit-comments.php < prev   
PHP Script  |  2008-02-19  |  47KB  |  863 lines

  1. <?php
  2. /* 
  3. Plugin Name: WP AJAX Edit Comments
  4. Plugin URI: http://www.raproject.com/wordpress/wp-ajax-edit-comments/
  5. Version: v1.1.4.3
  6. Author: Ronald Huereca
  7. Description: Allows you and your users to edit their comments inline.  Admin and editors can edit all comments.
  8. 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.
  9.  
  10. Copyright 2007-2008  Ronald Huereca  (email : ronalfy at(@) gmail dot(.) com
  11.  
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation; either version 2 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software
  24.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  25.  
  26. */
  27.  
  28.     class WPrapAjaxEditComments {
  29.                 var $commentClassName = "editComment"; //Do not edit this name
  30.                 var $commentAuthorName = "editAuthor"; //Do not edit this name
  31.                 var $skipDiv = false;
  32.                 var $minutes = 15;
  33.                 var $admin = "true";
  34.                 var $cookieName = "WPrapAjaxEditCommentId";//Do not edit this name
  35.                 var $optionsName = "WPAjaxEditComments"; //Do not edit this name
  36.                 var $userOptionsName = "WPAjaxEditAuthorUserOptions";
  37.                 var $adminOptions = false;
  38.                 var $userOptions = false; //Do not edit this name
  39.                 var $session = false;
  40.                 //constructor
  41.                 function WPrapAjaxEditComments() {
  42.                 }
  43.                 //Run when the plugin is first activated
  44.                 function init() {
  45.                     $this->getAdminOptions();
  46.                 }
  47.                 //Returns an array of admin options
  48.                 function getAdminOptions() {
  49.                     if ($this->adminOptions == false) {
  50.                         $optionsName = $this->optionsName;
  51.                         $adminOptions = array(
  52.                             'allow_editing' => 'true',
  53.                             'minutes' => '15', 
  54.                             'edit_text' => 'You may click on your name and/or comment to edit.', 
  55.                             'show_timer' => 'true',
  56.                             'spam_text' => 'Your edited comment was marked as spam.  If this is in error, please contact the admin.',
  57.                             'email_edits' => 'false',
  58.                             'number_edits' => '0',
  59.                             'use_akismet' => 'true',
  60.                             'use_mb_convert' => 'true'
  61.                         );
  62.                         $options = get_option($optionsName);
  63.                         if (!empty($options)) {
  64.                             foreach ($options as $key => $option)
  65.                                 $adminOptions[$key] = $option;
  66.                         }                
  67.                         update_option($optionsName, $adminOptions);
  68.                         $this->adminOptions = $adminOptions;
  69.                     }
  70.                     return $this->adminOptions;
  71.                 }
  72.                 //Returns an array of user options
  73.                 function getUserOptions() {
  74.                     global $user_email;
  75.                     if ($this->userOptions == false) { 
  76.                         if (empty($user_email)) {get_currentuserinfo();}
  77.                         if (empty($user_email)) { return ''; }
  78.                         $optionsName = $this->userOptionsName;
  79.                         $defaults = array('author_editing' => 'true',
  80.                         'comment_editing' => 'true', 
  81.                         'admin_editing' => 'false',
  82.                         'inline_editing' => 'false',
  83.                         'show_links' => 'false'
  84.                         );
  85.                         $userOptions = get_option($optionsName);
  86.                         if (!isset($userOptions)) {
  87.                             $userOptions = array();
  88.                         }
  89.                         //See if an older version doesn't match the new defaults
  90.                         if (empty($userOptions[$user_email])) {
  91.                             $userOptions[$user_email] = $defaults;
  92.                             update_option(optionsName, $userOptions);
  93.                         }    elseif(!is_array($userOptions[$user_email])) {
  94.                             $userOptions[$user_email] = $defaults;
  95.                             update_option(optionsName, $userOptions);
  96.                         } else {
  97.                                 foreach ($userOptions[$user_email] as $key => $option) {
  98.                                     $defaults[$key] = $option;                                
  99.                                 }
  100.                                 $userOptions[$user_email] = $defaults;
  101.                                 update_option(optionsName, $userOptions);
  102.                         }
  103.                         $this->userOptions = $userOptions;
  104.                     }
  105.                     return $this->userOptions;
  106.                 }
  107.                 //Adds a paragraph to the beginning of the text
  108.                 //Takes in the text, a comment ID, and a post ID
  109.                 //Called by saveComment and addCommentDiv
  110.                 function addEditParagraph($text, $id, $postId) {
  111.                     if (empty($id)||empty($postId)) { return $text; }
  112.                     if (!$this->canEditPostsPage($postId)) {
  113.                             $text = "<p class='ajax-edit' id='ajax-edit" . $id . "'></p>" . $text;
  114.                             return $text;
  115.                     }
  116.                     return $text;
  117.                 }
  118.                 //Add in place holders for the Edit, Delete, Spam, Disapprove buttons - Skip out if in the admin panel
  119.                 function addApprovalParagraph($text, $commentId, $postId) {
  120.                     if ((empty($commentId) || empty($postId) || is_admin()) && $_GET['page'] != 'akismet-admin') { return $text; }
  121.                     global $user_email;
  122.                     if (empty($user_email)) {
  123.                         get_currentuserinfo();
  124.                     }
  125.                     if (empty($user_email)) { return $text; }
  126.                     $author_options = $this->getUserOptions();
  127.                     //Get the author options
  128.                     if (!empty($author_options[$user_email])) {
  129.                         if ($author_options[$user_email]['show_links'] == "true") { return $text; }
  130.                     }
  131.                     $links = ''; 
  132.                     if ($this->canEditPostsPage() && $_GET['page'] != 'akismet-admin') {
  133.                         $links .= '<span><a class="ajax-admin-edit-links" href="#" id="edit' . $commentId . '">';
  134.                         $links .= __('Edit', "WPAjaxEditComments");
  135.                         $links .= '</a></span> | <span><a class="ajax-admin-edit-links" href="#" id="delete' . $commentId . '">';
  136.                         $links .= __('Delete',"WPAjaxEditComments" );
  137.                         $links .= '</a></span> | <span><a class="ajax-admin-edit-links" href="#" id="spam' . $commentId . '">';
  138.                         $links .= __('Spam',"WPAjaxEditComments" );
  139.                         $links .= '</a></span>';
  140.                     } elseif($this->canEditPostsPage() && $_GET['page'] == 'akismet-admin') {
  141.                         $links .= '<span><a class="ajax-akismet-links" href="#" id="unspam' . $commentId . '">';
  142.                         $links .= __('Not Spam', "WPAjaxEditComments" );
  143.                         $links .= '</a></span>';
  144.                     }
  145.                     if (strlen($links) > 0) 
  146.                         $text = $text . '<p style="display: none;" class="ajax-admin-edit-paragraph">[ ' . $links . ' ]</p>';
  147.                     
  148.                     return $text;
  149.                 }
  150.                 //Increments the number of edits users have made (excluding admin) - Takes in a post ID
  151.                 function incrementEdits($postId) {
  152.                     if (empty($postId)) { return; }
  153.                     $options = $this->getAdminOptions();
  154.                     $numEdits = intval($options['number_edits']);
  155.                     if (!$this->canEditPostsPage($postId)) {
  156.                         $numEdits += 1;
  157.                         $options['number_edits'] = $numEdits;
  158.                         update_option($this->optionsName, $options);
  159.                     }
  160.                 }
  161.                 //Returns true or false depending if the admin can inline edit
  162.                 function canInlineEdit() {
  163.                     global $user_email;
  164.                     if (empty($user_email)) {
  165.                         get_currentuserinfo();
  166.                     }
  167.                     if (empty($user_email)) { return "true"; } //not logged in so they don't have a choice
  168.                     $author_options = $this->getUserOptions();
  169.                     //Get the author options
  170.                     if (!empty($author_options[$user_email])) {
  171.                         if ($author_options[$user_email]['inline_editing'] == "true") { return "false"; }
  172.                     }
  173.                     return "true";
  174.                 }
  175.                 //Adds a div to the comment text with a class name so that the AJAX can change the text to a textbox
  176.                 function addCommentDiv($text) { 
  177.                     global $comment;
  178.                     $className = "post";
  179.                     if (is_admin()) { $className = "admin"; }
  180.                     if (empty($comment)) { return $text; }
  181.                     if (!$this->canEdit($comment->comment_ID, $comment->comment_post_ID) || !$this->canEditComment()) { return $text; }
  182.                     
  183.                     //Skip this if admin
  184.                     $text = $this->addEditParagraph($text, $comment->comment_ID, $comment->comment_post_ID);
  185.                     
  186.                     if ($this->skipDiv) { return $text; }
  187.                     //Regular expression to check if there is a div already added
  188.                     $pattern = '/div class=\"' . $this->commentClassName . '\"/i';
  189.                     //If there is not a div already, add it in
  190.                     if (!preg_match($pattern, $text)) {
  191.                         $text = '<div class="'.$this->commentClassName. ' ' . $className.'" id="' . $this->commentClassName . $comment->comment_ID . '">' . $text .  '</div>';
  192.                     }
  193.                     $text = $this->addApprovalParagraph($text, $comment->comment_ID, $comment->comment_post_ID); 
  194.                     return $text;
  195.                 } //End function addCommentDiv()
  196.                 
  197.                 function addAuthorSpan($text) { 
  198.                     global $comment;
  199.                     $className = "post";
  200.                     if (is_admin()) { $className = "admin"; }
  201.                     if (!$this->canEdit($comment->comment_ID, $comment->comment_post_ID) || !$this->canEditAuthor()) { return $text; }
  202.                     //Regular expression to check if there is a div already added
  203.                     $pattern = '/span class=\"' . $this->commentAuthorName . '\"/i';
  204.                     
  205.                     //If there is not a div already, add it in
  206.                     if (!preg_match($pattern, $text)) {
  207.                         $text = '<span id="' . $this->commentAuthorName . $comment->comment_ID . '" class="' . $this->commentAuthorName . ' ' . $className .'">' . $text . '</span>';
  208.                     } 
  209.                     return $text;
  210.                 } //End function addAuthorDiv()
  211.                 
  212.                 //Prints out the admin page
  213.                 function printAdminPage() {
  214.                     global $wpdb;
  215.                     $WPAjaxEditComments = $this->getAdminOptions();
  216.                     if (!empty($savedOptions)) {
  217.                         $WPAjaxEditComments = $savedOptions;
  218.                     }
  219.                     if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  220.                                  die("nope");
  221.                     //Delete security keys 
  222.                     if (isset($_POST['update_wp_ajaxEditSecurityKeys'])) {
  223.                         check_admin_referer('wp-ajax-edit-comments_admin-options-security');
  224.                         $query = "delete from $wpdb->postmeta where left(meta_value, 6) = 'wpAjax'";
  225.                         if (@!$wpdb->query($query)) {
  226.                         ?>
  227.                             <div class="error"><p><strong><?php _e('Security keys could not be deleted.', "WPAjaxEditComments") ?></strong></p></div>
  228.                         <?php
  229.                         } else {
  230.                             ?>
  231.                             <div class="updated"><p><strong><?php _e('Security keys deleted', "WPAjaxEditComments") ?></strong></p></div>
  232.                         <?php
  233.                         }
  234.                     }
  235.                     //Update settings
  236.                     if (isset($_POST['update_wp_ajaxEditCommentSettings'])) { 
  237.                          check_admin_referer('wp-ajax-edit-comments_admin-options');
  238.                         $error = false;
  239.                         $updated = false;
  240.                       //Validate the comment time entered
  241.                         if (isset($_POST['wpAJAXCommentTime'])) {
  242.                             $commentTimeErrorMessage = '';
  243.                             $commentClass = 'error';
  244.                             if (!preg_match('/^\d+$/i', $_POST['wpAJAXCommentTime'])) {
  245.                                 $commentTimeErrorMessage = "Comment time must be a numerical value";
  246.                                 $error = true;
  247.                             }    elseif($_POST['wpAJAXCommentTime'] < 1) {
  248.                                 $commentTimeErrorMessage = "Comment time must be greater than one minute.";
  249.                                 $error = true;
  250.                             } else {
  251.                                 $WPAjaxEditComments['minutes'] = $_POST['wpAJAXCommentTime'];
  252.                                 $updated = true;
  253.                             }
  254.                             if (!empty($commentTimeErrorMessage)) {
  255.                             ?>
  256. <div class="<?php _e($commentClass, "WPAjaxEditComments");?>"><p><strong><?php _e($commentTimeErrorMessage, "WPAjaxEditComments");?></p></strong></div>
  257.                             <?php
  258.                             }
  259.                         }
  260.                         if (isset($_POST['wpAJAXCommentAllowEdit'])) {
  261.                             $WPAjaxEditComments['allow_editing'] = $_POST['wpAJAXCommentAllowEdit'];
  262.                             $WPAjaxEditComments['edit_text'] = apply_filters('title_save_pre',$_POST['wpAJAXEditText']);
  263.                             $WPAjaxEditComments['spam_text'] = apply_filters('pre_comment_content',apply_filters('comment_save_pre', $_POST['wpAJAXSpamText']));
  264.                             $WPAjaxEditComments['show_timer'] = $_POST['wpAJAXShowTimer'];
  265.                             $WPAjaxEditComments['email_edits'] = $_POST['wpAJAXEmailEdits'];
  266.                             $WPAjaxEditComments['use_akismet'] = $_POST['wpAJAXAkismet'];
  267.                             $WPAjaxEditComments['use_mb_convert'] = $_POST['wpAJAXmbConvert'];
  268.                             $update = true;
  269.                         }
  270.                         if ($updated && !$error) {
  271.                             update_option($this->optionsName, $WPAjaxEditComments);
  272.                             $this->adminOptions = $WPAjaxEditComments;
  273.                         ?>
  274. <div class="updated"><p><strong><?php _e('Settings successfully updated.', "WPAjaxEditComments") ?></strong></p></div>
  275.                     <?php
  276.                         }
  277.                     } ?>
  278. <div class="wrap">
  279. <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
  280. <?php wp_nonce_field('wp-ajax-edit-comments_admin-options') ?>
  281. <h2><?php _e("WP AJAX Edit Comment", 'WPAjaxEditComments') ?></h2>
  282. <p><?php _e("Your commentators have edited their comments ", 'WPAjaxEditComments') ?><?php _e(number_format(intval($WPAjaxEditComments['number_edits'])),"WPAjaxEditComments") ?> <?php _e("times.", "WPAjaxEditComments") ?></p>
  283. <p><label for="comment_time"><?php _e('Set comment time (minutes):', "WPAjaxEditComments") ?> </label><input type="text" name="wpAJAXCommentTime" value="<?php echo $WPAjaxEditComments['minutes'] ?>" id="comment_time"/></p>
  284. <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>
  285. <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>
  286. <h3><?php _e('Allow Anyone to Edit Their Own Comments?', "WPAjaxEditComments") ?></h3>
  287. <p><?php _e('Selecting "No" will turn off comment editing for everyone except admin types who have post and page editing permissions.', "WPAjaxEditComments") ?></p>
  288. <p><label for="wpAJAXCommentAllowEdit_yes"><input type="radio" id="wpAJAXCommentAllowEdit_yes" name="wpAJAXCommentAllowEdit" value="true" <?php if ($WPAjaxEditComments['allow_editing'] == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes','WPAjaxEditComments'); ?></label>    <label for="wpAJAXCommentAllowEdit_no"><input type="radio" id="wpAJAXCommentAllowEdit_no" name="wpAJAXCommentAllowEdit" value="false" <?php if ($WPAjaxEditComments['allow_editing'] == "false") { echo('checked="checked"'); }?>/> <?php _e('No','WPAjaxEditComments'); ?></label></p>
  289. <h3><?php _e('Show a Countdown Timer?', "WPAjaxEditComments") ?></h3>
  290. <p><?php _e('Selecting "No" will turn off the countdown timer for non-admin commentators.', "WPAjaxEditComments") ?></p>
  291. <p><label for="wpAJAXShowTimer_yes"><input type="radio" id="wpAJAXShowTimer_yes" name="wpAJAXShowTimer" value="true" <?php if ($WPAjaxEditComments['show_timer'] == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes','WPAjaxEditComments'); ?></label>    <label for="wpAJAXShowTimer_no"><input type="radio" id="wpAJAXShowTimer_no" name="wpAJAXShowTimer" value="false" <?php if ($WPAjaxEditComments['show_timer'] == "false") { echo('checked="checked"'); }?>/> <?php _e('No','WPAjaxEditComments'); ?></label></p>
  292. <h3><?php _e('Allow Edit E-mails?', "WPAjaxEditComments") ?></h3>
  293. <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>
  294. <p><label for="wpAJAXEmailEdits_yes"><input type="radio" id="wpAJAXEmailEdits_yes" name="wpAJAXEmailEdits" value="true" <?php if ($WPAjaxEditComments['email_edits'] == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes','WPAjaxEditComments'); ?></label>    <label for="wpAJAXEmailEdits_no"><input type="radio" id="wpAJAXEmailEdits_no" name="wpAJAXEmailEdits" value="false" <?php if ($WPAjaxEditComments['email_edits'] == "false") { echo('checked="checked"'); }?>/> <?php _e('No','WPAjaxEditComments'); ?></label></p>
  295. <h3><?php _e('Check Edited Comments Against Akismet (if installed)?', "WPAjaxEditComments") ?></h3>
  296. <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>
  297. <p><label for="wpAJAXAkismet_yes"><input type="radio" id="wpAJAXAkismet_yes" name="wpAJAXAkismet" value="true" <?php if ($WPAjaxEditComments['use_akismet'] == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes','WPAjaxEditComments'); ?></label>    <label for="wpAJAXAkismet_no"><input type="radio" id="wpAJAXAkismet_no" name="wpAJAXAkismet" value="false" <?php if ($WPAjaxEditComments['use_akismet'] == "false") { echo('checked="checked"'); }?>/> <?php _e('No','WPAjaxEditComments'); ?></label></p>
  298. <h3><?php _e('Disable mb_convert_encoding?', "WPAjaxEditComments") ?></h3>
  299. <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>
  300. <p><label for="wpAJAXmbConvert_yes"><input type="radio" id="wpAJAXmbConvert_yes" name="wpAJAXmbConvert" value="true" <?php if ($WPAjaxEditComments['use_mb_convert'] == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes','WPAjaxEditComments'); ?></label>    <label for="wpAJAXmbConvert_no"><input type="radio" id="wpAJAXmbConvert_no" name="wpAJAXmbConvert" value="false" <?php if ($WPAjaxEditComments['use_mb_convert'] == "false") { echo('checked="checked"'); }?>/> <?php _e('No','WPAjaxEditComments'); ?></label></p>
  301. <div class="submit">
  302. <input type="submit" name="update_wp_ajaxEditCommentSettings" value="<?php _e('Update Settings', 'WPAjaxEditComments') ?>" /></div>
  303. </form>
  304.  </div>
  305.  <div class="wrap">
  306. <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
  307. <?php wp_nonce_field('wp-ajax-edit-comments_admin-options-security') ?>
  308. <h3><?php _e("Delete all security keys (ensure you have a recent backup)?", "WPAjaxEditComments") ?></h3>
  309. <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>
  310. <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>
  311. <div class="submit">
  312. <input type="submit" name="update_wp_ajaxEditSecurityKeys" value="<?php _e('Delete security keys', 'WPAjaxEditComments') ?>" /></div>
  313. </form>
  314.  </div>
  315.                     <?php
  316.                 }//End function printAdminPage()
  317.                 
  318.                 //Prints out the author page
  319.                 function printAuthorPage() {
  320.                     global $wpdb, $user_email;
  321.                     if (empty($user_email)) {
  322.                         get_currentuserinfo();
  323.                     }
  324.                     if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  325.                                  die("nope");
  326.                     $author_options = $this->getUserOptions();
  327.                     //Save the updated options to the database
  328.                     if (isset($_POST['wpAJAXAuthor']) && isset($_POST['wpAJAXComment']) && isset($_POST['wpAJAXAdminEdits'])&& isset($_POST['wpAJAXInlineEdits'])&& isset($_POST['wpAJAXLinks'])) {
  329.                     check_admin_referer('wp-ajax-edit-comments_user-options');
  330.                         if (isset($user_email)) {
  331.                             $author_options[$user_email]['author_editing'] = $_POST['wpAJAXAuthor'];
  332.                             $author_options[$user_email]['comment_editing'] = $_POST['wpAJAXComment'];
  333.                             $author_options[$user_email]['admin_editing'] = $_POST['wpAJAXAdminEdits'];
  334.                             $author_options[$user_email]['inline_editing'] = $_POST['wpAJAXInlineEdits'];
  335.                             $author_options[$user_email]['show_links'] = $_POST['wpAJAXLinks'];
  336.                             ?>
  337.                                 <div class="updated"><p><strong>Settings successfully updated.</strong></p></div>
  338.                             <?php
  339.                             update_option($this->userOptionsName, $author_options);
  340.                             $this->userOptions = $author_options;
  341.                         }
  342.                     }
  343.                     //Get the author options
  344.                     if (!empty($author_options[$user_email])) {
  345.                             $author = $author_options[$user_email]['author_editing'];
  346.                             $comment = $author_options[$user_email]['comment_editing'];
  347.                             $adminEdits = $author_options[$user_email]['admin_editing'];
  348.                             $inlineEdits = $author_options[$user_email]['inline_editing'];
  349.                             $showLinks = $author_options[$user_email]['show_links'];
  350.                         }
  351.                     ?>
  352. <div class=wrap>
  353. <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
  354. <?php wp_nonce_field('wp-ajax-edit-comments_user-options') ?>
  355. <h2><?php _e('WP AJAX Edit Comment - Individual User Options', "WPAjaxEditComments") ?></h2>
  356. <h3><?php _e('Turn Off Comment Editing in Admin Panel?', "WPAjaxEditComments") ?></h3>
  357. <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>
  358. <p><label for="wpAJAXAdminEdits_yes"><input type="radio" id="wpAJAXAdminEdits_yes" name="wpAJAXAdminEdits" value="true" <?php if ($adminEdits == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes', "WPAjaxEditComments") ?></label>    <label for="wpAJAXAdminEdits_no"><input type="radio" id="wpAJAXAdminEdits_no" name="wpAJAXAdminEdits" value="false" <?php if ($adminEdits == "false") { echo('checked="checked"'); }?>/> <?php _e('No', "WPAjaxEditComments") ?></label></p>
  359. <h3><?php _e('Turn Off Inline Editing?', "WPAjaxEditComments") ?></h3>
  360. <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>
  361. <p><label for="wpAJAXInlineEdits_yes"><input type="radio" id="wpAJAXInlineEdits_yes" name="wpAJAXInlineEdits" value="true" <?php if ($inlineEdits == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes', "WPAjaxEditComments") ?></label>    <label for="wpAJAXInlineEdits_no"><input type="radio" id="wpAJAXInlineEdits_no" name="wpAJAXInlineEdits" value="false" <?php if ($inlineEdits == "false") { echo('checked="checked"'); }?>/> <?php _e('No', "WPAjaxEditComments") ?></label></p>
  362. <h3><?php _e('Turn Off Comment Post Links?', "WPAjaxEditComments") ?></h3>
  363. <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>
  364. <p><label for="wpAJAXLinks_yes"><input type="radio" id="wpAJAXLinks_yes" name="wpAJAXLinks" value="true" <?php if ($showLinks == "true") { echo('checked="checked"'); }?> /><?php _e(' Yes', "WPAjaxEditComments") ?></label>    <label for="wpAJAXLinks_no"><input type="radio" id="wpAJAXLinks_no" name="wpAJAXLinks" value="false" <?php if ($showLinks == "false") { echo('checked="checked"'); }?>/> <?php _e('No', "WPAjaxEditComments") ?></label></p>
  365. <h3><?php _e('Turn On Name Editing?', "WPAjaxEditComments") ?></h3>
  366. <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>
  367. <p><label for="wpAJAXAuthor_yes"><input type="radio" id="wpAJAXAuthor_yes" name="wpAJAXAuthor" value="true" <?php if ($author == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes', "WPAjaxEditComments") ?></label>    <label for="wpAJAXAuthor_no"><input type="radio" id="wpAJAXAuthor_no" name="wpAJAXAuthor" value="false" <?php if ($author == "false") { echo('checked="checked"'); }?>/> <?php _e('No', "WPAjaxEditComments") ?></label></p>
  368. <h3><?php _e('Turn On Comment Editing?', "WPAjaxEditComments") ?></h3>
  369. <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>
  370. <p><label for="wpAJAXComment_yes"><input type="radio" id="wpAJAXComment_yes" name="wpAJAXComment" value="true" <?php if ($comment == "true") { echo('checked="checked"'); }?> /> <?php _e('Yes', "WPAjaxEditComments") ?></label>    <label for="wpAJAXComment_no"><input type="radio" id="wpAJAXComment_no" name="wpAJAXComment" value="false" <?php if ($comment == "false") { echo('checked="checked"'); }?>/> <?php _e('No', "WPAjaxEditComments") ?></label></p>
  371.  
  372. <div class="submit">
  373. <input type="submit" name="update_wp_ajaxEditCommentSettings" value="<?php _e('Update Settings', 'WPAjaxEditComments') ?>" /></div>
  374. </form>
  375.  </div>
  376.                     <?php
  377.                 }//End function printAuthorPage()
  378.                 
  379.                 //Adds the appropriate JavaScript files into the head tag
  380.                 function addHeaderJS() {
  381.                     global $post;
  382.                     if (empty($post)) { return; }
  383.                     //Load the appropriate files only if the page is single -- Wouldn't want to hit on a non-single post. :)
  384.                     if (is_single() || is_page()) {
  385.                         $this->JS();
  386.                     }
  387.                 }//End function addHeaderJS
  388.                 
  389.                 function addAdminHeaderJS() {
  390.                    //Page detection for other plugin authors
  391.                    if ('index.php' != basename($_SERVER['SCRIPT_FILENAME']) && 'edit-comments.php' != basename($_SERVER['SCRIPT_FILENAME']) && 'moderation.php' != basename($_SERVER['SCRIPT_FILENAME']))
  392.                       return;
  393.                    
  394.                     global $user_email;
  395.                     if (empty($user_email)) {
  396.                         get_currentuserinfo();
  397.                     }
  398.                     if (empty($user_email)) { return; }
  399.                     $author_options = $this->getUserOptions();
  400.                     //Get the author options
  401.                     if (!empty($author_options[$user_email])) {
  402.                         if ($author_options[$user_email]['admin_editing'] == "true") { return; }
  403.                     }
  404.                     $this->JS();
  405.                 }
  406.                 function JS() {
  407.                     if (function_exists('wp_enqueue_script') && function_exists('wp_register_script')) {
  408.                         wp_register_script('jquery', get_bloginfo('wpurl') . '/wp-content/plugins/wp-ajax-edit-comments/js-includes/jquery.js');
  409.                         wp_enqueue_script('ajax_edit_comments', get_bloginfo('wpurl') . '/wp-content/plugins/wp-ajax-edit-comments/js-includes/wp-ajax-edit-comments.js.php', array('jquery'), '1.1');
  410.                     }
  411.                 }
  412.                 function CSS() {
  413.                     echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"" . get_bloginfo('wpurl') . "/wp-content/plugins/wp-ajax-edit-comments/css/editComments.css\" />\n";
  414.                 }
  415.                 //Returns a comment for editing in a textbox
  416.                 //Takes in a comment id
  417.                 function getComment($id) {
  418.                     global $wpdb;
  419.                     $comment = get_comment( $id );
  420.                     if (!$comment) { return 0; }
  421.                     if (!$this->canEditPostsPage($comment->comment_post_ID)) { if ($comment->comment_approved === "spam") { return false; } }
  422.                     if (!$this->canEdit($id, $comment->comment_post_ID)) { return false; }    
  423.                     //Get the comment ready for the textbox
  424.                     $content = $comment->comment_content;
  425.                     $content = apply_filters('comment_edit_save', $content);
  426.                     return $content;
  427.                 }//End function getComment
  428.                 //Strips the ID to just numbers
  429.                 function convert_smart_quotes($string) { //Necessary for reading in comments - from http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php
  430.                         $search = array(chr(145), chr(146), chr(147),    chr(148), chr(151)); 
  431.                          $replace = array("'","'",'"','"','-');                 
  432.                         return str_replace($search, $replace, $string); 
  433.                 } 
  434.                 function getCommentID($id) {
  435.                     preg_match('/([0-9]+)$/i', $id, $matches);
  436.                     if (is_numeric($matches[1])) {
  437.                         return $matches[1];
  438.                     } else { 
  439.                         return 0;
  440.                     }
  441.                 } //End function getCommentID
  442.                 //Returns the text such as "You may click on author/comment to edit"
  443.                 function getEditText() {
  444.                     $options = $this->getAdminOptions();
  445.                     return  attribute_escape($options['edit_text']);    
  446.                 }
  447.                 //Returns the spam text such as "Your comment has been flagged as spam.  Please contact admin..."
  448.                 function getSpamText() {
  449.                     $options = $this->getAdminOptions();
  450.                     $content = $options['spam_text'];
  451.                     
  452.                     $this->skipDiv = true;
  453.                     $content = apply_filters('comment_text', $content);
  454.                     $this->skipDiv = false;
  455.                     
  456.                     return stripslashes($content);                    
  457.                 }
  458.                 
  459.                 function deleteComment($id) {
  460.                     if (empty($id)) { return false; }
  461.                     if ($this->canEditPostsPage()) {
  462.                         if (!wp_delete_comment($id)) { return false; }
  463.                         return true;
  464.                     } 
  465.                     return false;
  466.                 }
  467.                 //Spams or De-spams a comment
  468.                 //$id = the comment id - $action ("0"=de-spam, "1"=spam).
  469.                 function spamComment($id, $action) {
  470.                     if (empty($id)) { return false; }
  471.                     if ( $this->canEditPostsPage() ) { 
  472.                         if ($action == "1" ) { if (!wp_set_comment_status($id, 'spam')) { return false; } }
  473.                         if ($action == "0" ) { if (!wp_set_comment_status($id, 'approve')) { return false; } }
  474.                         return true;
  475.                     }
  476.                     return false;
  477.                 }
  478.                 //Returns how much time is left to edit on a comment in a comma deliminated variable 
  479.                 //Returns $minutes,$seconds
  480.                 //Takes in a comment ID
  481.                 //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.
  482.                 function getTimeLeft($id) {
  483.                     global $wpdb;
  484.                     if (empty($id)) { return "0,0"; }
  485.                     $adminEdit = $this->getAdminOptions();
  486.                     $adminMinutes = $adminEdit['minutes'];
  487.                         $query = "SELECT (UNIX_TIMESTAMP('" . current_time('mysql') . "') - UNIX_TIMESTAMP(comment_date)) time FROM $wpdb->comments where comment_ID = $id";
  488.                     //Get the IP, Date, and Timestamp
  489.                     $comment = $wpdb->get_row($query, ARRAY_A);
  490.                     //Get the time elapsed since making the comment
  491.                     $time = $comment['time'];
  492.                     $timeLeft = ($adminMinutes * 60) - $time; //seconds
  493.                     if ($timeLeft <= 0) { return "0,0"; }
  494.                     $minutesPassed = floor($timeLeft/60);
  495.                     $seconds = $timeLeft - ($minutesPassed*60);
  496.                 return "$minutesPassed,$seconds";        
  497.                 }//end function getTimeLeft
  498.                 
  499.                 //Saves the author to the database
  500.                 /*id = The comment id
  501.                 url = The author's url
  502.                 name = The author's name*/
  503.                 function saveAuthor($id, $url, $name) {
  504.                     global $wpdb;
  505.                     if (strtolower(get_option('blog_charset')) != 'utf-8') { $wpdb->query("SET names 'utf8'");} //comment out if getting char errors
  506.                     $comment = get_comment($id, ARRAY_A);
  507.                     if (!$comment) { return 0; }
  508.                     if (!$this->canEditPostsPage($comment['comment_post_ID'])) { if ($comment['comment_approved'] === "spam") { return 0; } }
  509.                     if (!$this->canEdit($id, $comment['comment_post_ID'])) { return 0; }
  510.                     if (!isset($name)) {
  511.                         return 0;
  512.                     }
  513.                     if (trim($name) == '') { return 0; }
  514.                     $url = urldecode($url);
  515.                     $name = urldecode($name);
  516.                     $url = trim(apply_filters('comment_url', $url));
  517.                     $saveUrl = apply_filters('pre_comment_author_url', $url);
  518.                     $name = apply_filters('comment_author', $name);
  519.                     $saveName = apply_filters('pre_commment_author_name', $name);
  520.                     
  521.                     if(!$wpdb->query("UPDATE $wpdb->comments SET comment_author = '$saveName', comment_author_url = '{$saveUrl}' WHERE comment_ID = $id")) {
  522.                         return 0;
  523.                     }
  524.                     //For WP Cache
  525.                     if (function_exists("wp_cache_get_postid_from_comment")) {
  526.                         wp_cache_get_postid_from_comment($id);
  527.                     }
  528.                     //Check for spam
  529.                     if ($this->checkSpam($id, $comment['comment_post_ID'])) { return "spam"; }
  530.                     //Send out edit notifications
  531.                     $this->editNotification($id, $comment['comment_post_ID']);
  532.                     $this->incrementEdits($comment['comment_post_ID']);
  533.  
  534.                     $url = stripslashes($this->encode($url));
  535.                     $name = stripslashes($this->encode($name));
  536.                     $returnText = '';
  537.                     if (!empty($url)) {
  538.                         $returnText = '<a href="' . $url . '">' . $name . '</a>';
  539.                     } else {
  540.                         $returnText = $name;
  541.                     }
  542.                     return $returnText;
  543.                 
  544.                 } //End function saveAuthor
  545.                 //Saves the edited comment to the database
  546.                 function saveComment($id, $content) {
  547.                         global $wpdb; //ISO-8859-1
  548.                     if (strtolower(get_option('blog_charset')) != 'utf-8') { $wpdb->query("SET names 'utf8'");} //comment out if getting char errors
  549.                     $comment = get_comment($id, ARRAY_A);
  550.                     //return $content;
  551.                     $content = urldecode($content);
  552.                     $savecontent = $content;
  553.                                         
  554.                     if (trim($content) == '' || !$comment ) { return 0; }
  555.                     if (!$this->canEditPostsPage($comment['comment_post_ID'])) { if ($comment['comment_approved'] === "spam") { return 0; } }
  556.                     if (!$this->canEdit($id, $comment['comment_post_ID'])) { return 0; }
  557.                     $content = apply_filters('comment_save_pre', $savecontent);
  558.                     if(!$wpdb->query("UPDATE $wpdb->comments SET comment_content = '$content' WHERE comment_ID = $id")) {    
  559.                         return 0;
  560.                     }
  561.                     if (function_exists('clean_comment_cache')) { clean_comment_cache($id); } //Clear cache for comment
  562.                     //For WP Cache
  563.                     if (function_exists("wp_cache_get_postid_from_comment")) {
  564.                         wp_cache_get_postid_from_comment($id);
  565.                     }
  566.                     //Check for spam
  567.                     if ($this->checkSpam($id, $comment['comment_post_ID'])) { return "spam" ;}
  568.                     //Send out edit notifications
  569.                     $this->editNotification($id, $comment['comment_post_ID']);
  570.                     $this->incrementEdits($comment['comment_post_ID']);
  571.                     $this->skipDiv = true;
  572.                     $content = apply_filters('comment_text', $this->encode($content));
  573.                     $content = $this->addEditParagraph($content, $id, $comment['comment_post_ID']);
  574.                     $this->skipDiv = false;
  575.                     return stripslashes($content);
  576.                 } //end function saveComment
  577.                 function encode($content) {
  578.                    $adminEncode = $this->getAdminOptions();
  579.                    if ($adminEncode['use_mb_convert'] == "false" || !function_exists("mb_convert_encoding")) { return $content; }
  580.                    return mb_convert_encoding($content, ''.get_option('blog_charset').'', mb_detect_encoding($content, "UTF-8, ISO-8859-1, ISO-8859-15", true));
  581.                 }
  582.                 //Return true if spam, return false if not (or if ID is invalid or Akismet isn't installed)
  583.                 function checkSpam($id, $postId) {
  584.                     if (empty($id) || empty($postId)) { return false; }
  585.                     $options = $this->getAdminOptions();
  586.                     if ($options['use_akismet'] == "false") { return false;} //Check to see if the admin has enabled akismet checking
  587.                     //Check for SPAM if Akismet is installed
  588.                     if (function_exists("akismet_check_db_comment")) {
  589.                         if (!$this->canEditPostsPage()) { //Skip if admin
  590.                             if (akismet_verify_key(get_option('wordpress_api_key')) != "failed") { //If API key fails, keep on going as if Akismet isn't installed
  591.                                 $response = akismet_check_db_comment($id);
  592.                                 if ($response == "true") { //You have spam
  593.                                     wp_set_comment_status($id, 'spam');
  594.                                     return true;
  595.                                 }
  596.                             }
  597.                         }
  598.                     }
  599.                     return false;            
  600.                 }
  601.                 //Sends a notification of an edit via e-mail - Code sampled from Subscribe to Comments
  602.                 //Takes in a comment ID and Post ID - Returns false if email failed
  603.                 function editNotification($id, $postId) {
  604.                     global $wpdb;
  605.                     
  606.                     if (empty($id) || empty($postId)) { return false; }
  607.                     //Make sure this option is enabled and the user editing isn't admin
  608.                     $options = $this->getAdminOptions();
  609.                     if ($options['email_edits'] == "false" || $this->canEditPostsPage()) { return false; }
  610.                     
  611.                     $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$id' LIMIT 1", ARRAY_A);
  612.                     $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1", ARRAY_A);
  613.                     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
  614.                         $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']));
  615.                         $message .= sprintf(__("Author: %s\n", 'WPAjaxEditComments'), $comment['comment_author']);
  616.                         $message .= sprintf(__("Author URL: %s\n", 'WPAjaxEditComments'), stripslashes($comment['comment_author_url']));
  617.                         $message .= __("Comment:\n", 'WPAjaxEditComments') . stripslashes($comment['comment_content']) . "\n\n";
  618.                         $message .= __("See all comments on this post here:\n", 'WPAjaxEditComments');
  619.                         $message .= get_permalink($comment['comment_post_ID']) . "#comments\n\n";
  620.                         $subject = sprintf(__('New Edited Comment On: %s', 'WPAjaxEditCommentss'), stripslashes($post['post_title']));
  621.                         $subject = '[' . get_bloginfo('name') . '] ' . $subject;
  622.                         $email = get_bloginfo('admin_email');
  623.                         $site_name = str_replace('"', "'", get_bloginfo('name'));
  624.                         $charset = get_settings('blog_charset');
  625.                         $headers  = "From: \"{$site_name}\" <{$email}>\n";
  626.                         $headers .= "MIME-Version: 1.0\n";
  627.                         $headers .= "Content-Type: text/plain; charset=\"{$charset}\"\n";
  628.                         return wp_mail($email, $subject, $message, $headers);
  629.                     }
  630.                     return false;
  631.                 }
  632.                 //Determines if a comment can be edited or not based on the ID and time elapsed
  633.                 //$id = The comment id
  634.                 //$postId = The post id for the comment
  635.                 //Returns true if the comment can be edited, false if not
  636.                 function canEdit($id, $postId) {
  637.                     global $wpdb;
  638.                     if (empty($id) || empty($postId)) { return false; }
  639.                     //Return true if the user can edit the comment
  640.                     if ($this->canEditPostsPage()) { return true; }
  641.                     
  642.                     //Checks to see if the admin has allowed comment editing
  643.                     $adminEdit = $this->getAdminOptions();
  644.                     $adminEdit = $adminEdit['allow_editing'];
  645.                     if ($adminEdit != 'true') {
  646.                         return false;
  647.                     }
  648.                     $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";
  649.                     //Get the IP, Date, and Timestamp
  650.                     $comment = $wpdb->get_row($query, ARRAY_A); if (!comment) { return false; }
  651.                     $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);
  652.                     if (!meta || !$comment) { return false; }
  653.                     $timestamp = $comment['time'];
  654.                     $hash = md5($comment['comment_author_IP'] . $comment['comment_date_gmt']);
  655.                     //Check to see if there's a valid cookie
  656.                     if (!$this->session) { @session_start(); $this->session = true; }
  657.                     if (isset($_COOKIE[$this->cookieName . $id . $hash]) || isset($_SESSION[$this->cookieName . $id . $hash])) {
  658.                         if (!($_COOKIE[$this->cookieName . $id . $hash] === $meta['meta_value']) && !($_SESSION[$this->cookieName . $id . $hash] === $meta['meta_value'])) { return false; }
  659.                         //Get the time elapsed since making the comment
  660.                         $time = current_time('timestamp',get_option('gmt_offset'))-$timestamp;
  661.                         $minutesPassed = round(((($time%604800)%86400)%3600)/60); 
  662.                         //Get the time the admin has set for minutes
  663.                         $minutes = $this->getAdminOptions();
  664.                         $minutes = $minutes['minutes'];
  665.                         if (!is_numeric($minutes)) {
  666.                             $minutes = $this->minutes;
  667.                         }
  668.                         if ($minutes < 1) {
  669.                             $minutes = $this->minutes;
  670.                         }
  671.                         //Compare the minutes passed to the admin minutes
  672.                         if (($minutesPassed - $minutes) > 0) {
  673.                             unset($_SESSION[$this->cookieName . $id . $hash]); return false; 
  674.                         }
  675.                         return true;
  676.                     }
  677.                     @session_write_close();
  678.                     return false;
  679.                 }//end function canEdit
  680.                 
  681.                 //Returns true if a person can edit a post/page - User level 9 and above
  682.                 //Returns false otherwise
  683.                 //Called by canEdit() and setCommentCookie
  684.                 function canEditPostsPage() {
  685.                     if (function_exists("current_user_can")) {
  686.                         if(current_user_can("manage_options") && current_user_can("edit_others_posts") && current_user_can("switch_themes") && current_user_can("edit_users")) { return true;}
  687.                     }
  688.                     return false;
  689.                 }//end function canEditPostsPage
  690.                 
  691.                 //Returns true unless the user has disabled author editing
  692.                 function canEditAuthor() {
  693.                     global $user_email;
  694.                     if (empty($user_email)) {
  695.                         get_currentuserinfo();
  696.                     }
  697.                     if (empty($user_email)) { return true; }
  698.                     $author_options = $this->getUserOptions();
  699.                     //Get the author options
  700.                     if (!empty($author_options[$user_email])) {
  701.                         if ($author_options[$user_email]['author_editing'] == "false") { return false; }
  702.                     }
  703.                     return true;
  704.                 } //end function canEditAuthor
  705.                 
  706.                 //Returns true unless the user has disabled comment editing
  707.                 function canEditComment() { 
  708.                     global $user_email;
  709.                     if (empty($user_email)) {
  710.                         get_currentuserinfo();
  711.                     }
  712.                     if (empty($user_email)) { return true; }
  713.                     $author_options = $this->getUserOptions();
  714.                     //Get the author options
  715.                     if (!empty($author_options[$user_email])) {
  716.                         if ($author_options[$user_email]['comment_editing'] == "false") { return false; }
  717.                     }
  718.                     return true;
  719.                 
  720.                 }
  721.                 
  722.                 //Sets a comment cookie for the commentator (treat as a private function)
  723.                 function setCommentCookie($commentId) {
  724.                     global $comment, $wpdb;
  725.                     //Get the IP, Date, and Timestamp
  726.                     if (empty($comment)) {
  727.                         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments where comment_ID = $commentId", ARRAY_A);
  728.                     }
  729.                     if (!$comment) { return $commentId; }
  730.                     if ($comment['comment_approved'] === "spam") { return $commentId; }
  731.                     
  732.                     //Return if setting the cookie is unnecessary
  733.                     if ($this->canEditPostsPage()) { return $commentId; }
  734.                     
  735.                     $hash = md5($comment['comment_author_IP'] . $comment['comment_date_gmt']);
  736.                     $rand = 'wpAjax' . $hash . md5($this->random()) . md5($this->random());
  737.                     //Get the time the admin has set for minutes
  738.                     $minutes = $this->getAdminOptions();
  739.                     $minutes = $minutes['minutes'];
  740.                     if (!is_numeric($minutes)) {
  741.                         $minutes = $this->minutes;
  742.                     }
  743.                     if ($minutes < 1) {
  744.                         $minutes = $this->minutes;
  745.                     }        
  746.                     
  747.                     //Insert the random key into the database
  748.                     $query = "INSERT INTO " . $wpdb->postmeta .
  749.             "(meta_id, post_id, meta_key, meta_value) " .
  750.             "VALUES ('', " . $comment['comment_post_ID'] . "," . $comment['comment_ID'] . ",'" . $rand . "')";
  751.                     @$wpdb->query($query);
  752.                         
  753.                     //Set the cookie
  754.                     $cookieName = $this->cookieName . $commentId . $hash;
  755.                     $value = $rand;
  756.                     $expire = time()+60*$minutes;
  757.                     if (!isset($_COOKIE[$cookieName])) {
  758.                         setcookie($cookieName, $value, $expire, COOKIEPATH,COOKIE_DOMAIN);
  759.                         session_start();
  760.                         $_SESSION[$cookieName] = $value;
  761.                         session_write_close();
  762.                     }
  763.                     return $commentId;
  764.                 } //end function setCommentCookie
  765.                 
  766.                 function random() {
  767.                  $chars = "%CDEF#cGHIJ\:ab!@defg9ABhijklmn<>;opqrstuvwxyz10234/+_-=5678MKL^&*NOP";
  768.                  $pass = '';
  769.                  for ($i = 0; $i < 50; $i++) {
  770.                      $pass .= $chars{rand(0, strlen($chars)-1)};
  771.                  }
  772.                  return $pass;
  773.                 }
  774.                 //Returns a nonce - Takes in the nonce name - Returns a nonce in the format of _wpnonce:"404049"
  775.                 function getNonce($name) {
  776.                     $nonce = $this->nonceReplace(wp_nonce_url("#", $name));
  777.                     if ($this->canEditPostsPage()) { //yay, admin
  778.                         $nonce = '_wpnonce:"' . md5($nonce) . '"';
  779.                     } else { //yay, non-admin user
  780.                         $nonce = '_wpnonce:"' . md5(md5($nonce) . $nonce) . '"';
  781.                     }
  782.                     return $nonce;                
  783.                 }
  784.                 //takes in a wp_nonce and changes it from format  _wpnonce=404049# to 404049
  785.                 function nonceReplace($nonce) {
  786.                     $nonce = str_replace("_wpnonce=", '', $nonce);
  787.                     $nonce = str_replace("#", '', $nonce);
  788.                     return $nonce;
  789.                 }
  790.                 //Takes in the nonce with format 404049, and the nonce name
  791.                 //Return true if the nonce is valid, false if not
  792.                 function verifyNonce($nonce, $name) {
  793.                     $newNonce = $this->nonceReplace(wp_nonce_url("#", $name));
  794.                     //Figure out if we are admin or just a user
  795.                     if ($this->canEditPostsPage()) { //yay, admin
  796.                         $newNonce = md5($newNonce);
  797.                         if ($nonce != $newNonce) {
  798.                             return false;
  799.                         }
  800.                     } else { //yay, non-admin user
  801.                         $newNonce = md5(md5($newNonce) . $newNonce);
  802.                         if ($nonce != $newNonce) {
  803.                             return false;
  804.                         }
  805.                     }
  806.                 return true;
  807.                 } //end function verifyNonce
  808.         }//End Class WPrapAjaxEditComments
  809. if (class_exists("WPrapAjaxEditComments")) {
  810. $rap_ajaxComment = new WPrapAjaxEditComments();
  811.  
  812. //locale stuff
  813. $rap_ajaxComment_locale = get_locale();
  814. $rap_ajaxComment_mofile = dirname(__FILE__) . "/languages/ajaxEdit-$rap_ajaxComment_locale.mo";
  815. load_textdomain('WPAjaxEditComments', $rap_ajaxComment_mofile);
  816. }
  817. //Initialize the admin panel
  818. function WPrapAjaxEditComments_ap() {
  819.     global $rap_ajaxComment;
  820.     if (!isset($rap_ajaxComment)) {
  821.         return;
  822.     }
  823.     if (function_exists('add_options_page')) {
  824. add_options_page('AJAX Edit Comments', 'AJAX Edit Comments', 10, basename(__FILE__), array(&$rap_ajaxComment, 'printAdminPage'));
  825.     }
  826.     if (function_exists('add_submenu_page')) {
  827.         add_submenu_page('profile.php', "AJAX Edit Comments Options","AJAX Edit Comments", 9, basename(__FILE__), array(&$rap_ajaxComment, 'printAuthorPage'));
  828.     }
  829. }    
  830. //Template tag WPrapAjaxEditComments_CanEdit 
  831. //Returns true if a user can edit - False if they can't - Must be run within the loop
  832. //Also returns false if people are admins
  833. function WPrapAjaxEditComments_CanEdit() {
  834.     global $comment, $rap_ajaxComment;
  835.     //If we're not in the loop, return false
  836.     if (!isset($comment) || !isset($rap_ajaxComment)) { return false; }
  837.     
  838.     //If admin, return false
  839.     if ($rap_ajaxComment->canEditPostsPage()) {
  840.         return false;
  841.     }
  842.     //Find out if the user can edit
  843.     if ($rap_ajaxComment->canEdit($comment->comment_ID, $comment->comment_post_ID)) {
  844.         return true;
  845.     }
  846.     return false;    
  847. }
  848. //Yay, actions.    
  849. if (isset($rap_ajaxComment)) {
  850.     add_action('admin_menu', 'WPrapAjaxEditComments_ap');
  851.     add_action('admin_print_scripts', array(&$rap_ajaxComment, 'addAdminHeaderJS')); 
  852.     add_action('admin_head', array(&$rap_ajaxComment, 'CSS')); 
  853.     add_action('wp_print_scripts', array(&$rap_ajaxComment, 'addHeaderJS'));
  854.     add_action('wp_head', array(&$rap_ajaxComment, 'CSS'));
  855.     add_action('comment_post', array(&$rap_ajaxComment, 'setCommentCookie'));
  856.     add_action('activate_wp-ajax-edit-comments/wp-ajax-edit-comments.php',  array(&$rap_ajaxComment, 'init'));
  857.     
  858.     //Yay, filters.
  859.     add_filter('comment_text', array(&$rap_ajaxComment, 'addCommentDiv'), '1000'); //Low priority so other HTML can be added first
  860.     add_filter('get_comment_author_link', array(&$rap_ajaxComment, 'addAuthorSpan'), '1000'); //Low priority so other HTML can be added first
  861. }
  862.  
  863. ?>