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

  1. <?php 
  2. /*WP Ajax Edit Script
  3. --Created by Ronald Huereca
  4. --Created on: 03/28/2007
  5. --Last modified on: 02/08/2008
  6. --Relies on jQuery
  7. Classnames referenced in here for the Author are:
  8.     editAuthor (default)
  9.     editableAuthor (Shows it can be edited)
  10.     textAuthor (When the author field is being edited)
  11. Classnames referenced in here for the Comment are:
  12.     editComment (default)
  13.     editableComment (Shows it can be edited)
  14.     textComment (When the comment field is being edited)
  15.     
  16.     Copyright 2007  Ronald Huereca  (email : ron alfy [a t ] g m ail DOT com)
  17.  
  18.     This program is free software; you can redistribute it and/or modify
  19.     it under the terms of the GNU General Public License as published by
  20.     the Free Software Foundation; either version 2 of the License, or
  21.     (at your option) any later version.
  22.  
  23.     This program is distributed in the hope that it will be useful,
  24.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.     GNU General Public License for more details.
  27.  
  28.     You should have received a copy of the GNU General Public License
  29.     along with this program; if not, write to the Free Software
  30.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  31. */
  32. if (!function_exists('add_action'))
  33. {
  34.     require_once("../../../../wp-config.php");
  35. }
  36. ?>
  37. jQuery(document).ready(function(){
  38.    AjaxEditComments.init();
  39.      
  40. });
  41. var AjaxEditComments = function() {
  42.     var $j = jQuery;
  43.     var timers = new Array();
  44.     var PluginUrl = "<?php bloginfo('wpurl') ?>/wp-content/plugins/wp-ajax-edit-comments";
  45.     var CommentAuthorName = "editAuthor";
  46.     var CommentClassName = "editComment";
  47.     var EditCommentAuthorName = "editableAuthor"; //not edible author :)
  48.     var EditCommentClassName = "editableComment";
  49.     var TextAuthorClassName = "textAuthor";
  50.     var TextCommentClassName = "textComment";
  51.     //Scans the spans and divs for a specific class and sets events to make them editable
  52.     function MakeEdit() {
  53.         StopEdit();
  54.         //Make the Author portions editable
  55.         $j('.' + CommentAuthorName).bind("mouseover",function() {ShowAsEditable($j(this));});
  56.         $j('.' + CommentAuthorName).bind("mouseout",function() {ShowAsUnEditable($j(this));});
  57.         $j('.' + CommentAuthorName).bind("click",function() {Edit($j(this)); return false;});
  58.         //Make the comment portions editable
  59.         $j('.' + CommentClassName).bind("mouseover",function() {ShowAsEditable($j(this));});
  60.         $j('.' + CommentClassName).bind("mouseout",function() {ShowAsUnEditable($j(this));});
  61.         $j('.' + CommentClassName).bind("click",function() {Edit($j(this)); return false;});    
  62.         <?php if ($rap_ajaxComment->canInlineEdit() == "false") {?>
  63.             $j('.' + "post").unbind();
  64.         <?php } ?>
  65.         //Remove the extra classes
  66.         $j('.' + CommentClassName).removeClass("post");
  67.         $j('.' + CommentClassName).removeClass("admin");
  68.         $j('.' + CommentAuthorName).removeClass("post");
  69.         $j('.' + CommentAuthorName).removeClass("admin");
  70.         
  71.         GetEditText();
  72.         AddAdminLinks();
  73.     }
  74.     //Disable all the edit events
  75.     function StopEdit() {
  76.         $j('.' + CommentAuthorName).unbind();
  77.         $j('.' + CommentClassName).unbind();
  78.         $j("." + 'ajax-admin-edit-links').unbind();
  79.     }
  80.     //Adds in the admin links and sets up the events
  81.     function AddAdminLinks() {
  82.         $j("." + 'ajax-admin-edit-links').each(function() { // Adds Edit | Delete | Spam functionality
  83.             var id = $j(this).attr("id");
  84.             var element = $j(this);
  85.             var option = id.match(/([a-zA-Z]+)/i)[1];
  86.             var id = GetId(id);
  87.             if (option == "edit") {
  88.             element.bind("click", function() { Edit($j("#" + CommentClassName + id));Edit($j("#" + CommentAuthorName + id)); return false; });
  89.             } else if (option == "delete")  {
  90.                 element.bind("click", function() { if (confirm("<?php _e('Delete this comment?', "WPAjaxEditComments") ?>")) { DeleteSpamComment(option, id);} return false;})
  91.             } else if (option == "spam") { 
  92.                 element.bind("click", function() { if (confirm("<?php _e('Mark this comment as Spam?', "WPAjaxEditComments") ?>")) { DeleteSpamComment(option, id);   } return false;})
  93.             } 
  94.         });
  95.     $j("." + 'ajax-akismet-links').each(function() { //Adds Akismet functionality
  96.             var id = $j(this).attr("id");
  97.             var element = $j(this);
  98.             var option = id.match(/([a-zA-Z]+)/i)[1];
  99.             var id = GetId(id);
  100.             if (option == "unspam") {
  101.                 element.bind("click", function() { DeleteSpamComment(option, id); return false;})
  102.             } 
  103.         });
  104.         $j("." + "ajax-admin-edit-paragraph").show();
  105.     }
  106.     //Deletes or Marks a comment as spam
  107.     //Option = "delete" or "spam"| id is the comment ID
  108.     function DeleteSpamComment(option, id) {
  109.         $j.ajax({
  110.             type: "post",
  111.             url: PluginUrl + '/php-includes/AjaxEditComments.php',
  112.             timeout: 30000,
  113.             global: false,
  114.             data: {
  115.                 CommentOption: option,
  116.                 CommentId: id,
  117.                 <?php echo $rap_ajaxComment->getNonce('wp-ajax-edit-comments-delete_spam_comment') ?>},
  118.             success: function(msg) { DeleteSpamCommentComplete(msg, id); },
  119.             error: function(msg) { DeleteSpamCommentFailure(msg, id); }
  120.         })
  121.     }
  122.     //Outputs the edit text
  123.     function GetEditText() {
  124.         //Output edit text
  125.         $j("." + "ajax-edit").html('<?php echo $rap_ajaxComment->getEditText() ?>');
  126.         GetTimeLeft();        
  127.     }
  128.   //Finds the time left for editing comments
  129.     function GetTimeLeft() {
  130.         $j("." + "ajax-edit").each(function() {
  131.             var text = $j(this).attr("id");
  132.             var element = $j(this);
  133.             $j.ajax({
  134.                     type: "post",
  135.                     url: PluginUrl + '/php-includes/AjaxEditComments.php',
  136.                     timeout: 30000,
  137.                     global: false,
  138.                     data: {LoadTimeId: $j(this).attr("id"),
  139.                     <?php echo $rap_ajaxComment->getNonce('wp-ajax-edit-comments-load_time') ?>},
  140.                     success: function(msg) { LoadTimeComplete(msg, text, element); }
  141.                 })
  142.         });
  143.     }
  144.     //Allows the comment and author areas to be editable
  145.     //obj - The object that triggered the event
  146.     function Edit(obj) {
  147.         if (obj.is("span")) {
  148.             //Create a new span element to replace the author span
  149.             if ($j('body').find('#' + TextAuthorClassName + obj.attr("id")).is("span")) { return; } //Return if the span has already been created
  150.             var newSpan = '<span class="' + TextAuthorClassName + '" id="' + TextAuthorClassName + obj.attr("id") + '">'
  151.                 
  152.             //if url and name both return zero matches, use innerHTML as is.
  153.             var url = obj.html().match(/href=\"([^"]*)\"/i);
  154.             if (url == undefined) { url = obj.html().match(/href=\'([^']*)\'/i)    }
  155.             if (url == undefined) {
  156.                 url = '';
  157.                 name = obj.html();
  158.             } else {
  159.                 url = url[1];
  160.                 name = obj.html().match(/>([^<]*)<\/a>/i)[1];
  161.             }
  162.             
  163.             var authorInputBox = '<label for="' + obj.attr("id") + '_URL"><?php _e('URL:', "WPAjaxEditComments") ?> </label><input type="text" class="authorURL" id="' + obj.attr("id") + '_URL" value="' + url + '" /><label for="' + obj.attr("id") + '_author"> <?php _e('Name:', "WPAjaxEditComments") ?> </label><input type="text" id="' + obj.attr("id") + '_author" class="authorInput" value=\'' + name + '\' />  ';
  164.             var authorButtons = '<input type="button" value="<?php _e('Save', "WPAjaxEditComments") ?>" id="' + obj.attr("id") + '_saveAuthor" /> <?php _e('or',"WPAjaxEditComments"); ?> <input type="button" value="<?php _e('Cancel', "WPAjaxEditComments") ?>" id="' + obj.attr("id") + '_cancelAuthor" />';
  165.             newSpan = newSpan + authorInputBox + authorButtons + "</span>";
  166.             
  167.             //Hide the old span
  168.             obj.hide();
  169.             obj.before(newSpan);
  170.             
  171.             //Setup save and cancel button events
  172.             var saveButton = $j("#" + obj.attr("id") + "_saveAuthor");
  173.             var cancelButton = $j("#" + obj.attr("id") + "_cancelAuthor");
  174.             saveButton.click(function() { AuthorSave($j(this), obj, $j("#" + TextAuthorClassName + obj.attr("id"))); });
  175.             cancelButton.click(function() { EditCancel($j(this), obj, $j("#" + TextAuthorClassName + obj.attr("id"))); });
  176.             //Setup the events for the input boxes
  177.             $j("#" + obj.attr("id") + '_URL').keypress(function() { EnableSave(saveButton) });
  178.             $j("#" + obj.attr("id") + '_author').keypress(function() { EnableSave(saveButton) });
  179.             
  180.         } else if (obj.is("div")) {
  181.             if ($j('body').find('#' + TextCommentClassName + obj.attr("id")).is("div")) { return; } //Return if the textarea has already been created
  182.             var newDiv = '<div class="' + TextCommentClassName + '" id="' + TextCommentClassName + obj.attr("id") + '">';
  183.             //Create the input box and buttons
  184.             var textarea = '<textarea id="' + obj.attr("id") + '_edit" name="' + obj.attr("id") + '"><?php _e('Loading comment...', "WPAjaxEditComments") ?></textarea>';
  185.             var commentButtons = '<p class="ajax-save-buttons" id="' + obj.attr("id") + '_saveCommentButtons"><input type="button" value="<?php _e('Save', "WPAjaxEditComments") ?>" id="' + obj.attr("id") + '_saveComment" /> <?php _e('or',"WPAjaxEditComments"); ?> <input type="button" value="<?php _e('Cancel', "WPAjaxEditComments") ?>" id="' + obj.attr("id") + '_cancelComment" /></p>';
  186.                             
  187.             newDiv = newDiv + textarea + commentButtons + "</div>";
  188.             //Hide the old span and show the new one
  189.             obj.hide();
  190.             obj.before(newDiv);
  191.             var newDiv = $j("#" + TextCommentClassName + obj.attr("id"));
  192.             //Setup save and cancel button events
  193.             var saveCommentButtons = $j("#" + obj.attr("id") + "_saveCommentButtons").hide();
  194.             var saveButton = $j("#" + obj.attr("id") + "_saveComment");
  195.             var cancelButton = $j("#" + obj.attr("id") + "_cancelComment");
  196.             saveButton.click(function() { CommentSave($j(this), obj, newDiv); });
  197.             cancelButton.click(function() { EditCancel($j(this), obj, newDiv); });
  198.             //Setup the event for the textarea
  199.             $j("#" + obj.attr("id") + '_edit').keypress(function() { EnableSave(saveButton) });
  200.             
  201.             //Load the comment
  202.             $j.ajax({
  203.                 type: "post",
  204.                 url: PluginUrl + '/php-includes/AjaxEditComments.php',
  205.                 timeout: 30000,
  206.                 global: false,
  207.                 data: {GetCommentId: obj.attr("id"),<?php echo $rap_ajaxComment->getNonce('wp-ajax-edit-comments-edit_comment') ?>},
  208.                 success: function(msg) { LoadCommentComplete(msg, obj.attr("id"), saveCommentButtons, obj, newDiv); },
  209.                 error: function(msg) { LoadCommentFailure(msg, obj.attr("id"), obj, newDiv);}
  210.             })
  211.             
  212.         } 
  213.     }
  214.     //Returns a match if valid (i.e., you pass it editComment45, it returns 45) 
  215.     function GetId(id) { 
  216.         return id.match(/([0-9]+)/i)[1];
  217.     }
  218.     //Changes the classname of the span/div so that it appears editable
  219.     function ShowAsEditable(obj) {
  220.         if (obj.attr("class") == CommentAuthorName) {
  221.             obj.attr("class", EditCommentAuthorName);
  222.         } else if (obj.attr("class") == CommentClassName) {
  223.             obj.attr("class", EditCommentClassName);            
  224.         }
  225.     }
  226.     //Changes the classname of the span/div so that it appears uneditable
  227.     function ShowAsUnEditable(obj) {
  228.         if (obj.attr("class") == EditCommentAuthorName) {
  229.             obj.attr("class", CommentAuthorName)
  230.         } else if (obj.attr("class") == EditCommentClassName) {
  231.             obj.attr("class", CommentClassName)        
  232.         }
  233.     }
  234.     //Saves the Author Data to the Database
  235.     //saveButton - The save button
  236.     //oldSpan - The element to show saved information
  237.     //newSpan - The element to retreive saved information
  238.     //display - The display type of the oldspan
  239.     function AuthorSave(saveButton, oldSpan, newSpan) {
  240.         var url = $j("#" + oldSpan.attr("id") + "_URL");
  241.         var author = $j("#" + oldSpan.attr("id") + "_author");
  242.         saveButton.attr("value", "<?php _e('Saving...', "WPAjaxEditComments") ?>");
  243.         saveButton.attr("disabled", "disabled");
  244.         //Check for empty url or author values
  245.         if (author.attr("value") == undefined) { SaveAuthorFailure('', saveButton); return; }
  246.         urlValue = url.attr("value");
  247.         if (urlValue == undefined) { urlValue = ""; }
  248.         authorValue = $j.trim(author.attr("value"));
  249.         if (authorValue == '') { SaveAuthorFailure('', saveButton); return; }
  250.         
  251.         $j.ajax({
  252.                 type: "post",
  253.                 url: PluginUrl + '/php-includes/AjaxEditComments.php',
  254.                 timeout: 30000,
  255.                 global: false,
  256.                 data: {SaveAuthorCommentId: oldSpan.attr("id"),
  257.                 AuthorURL: encodeURIComponent(urlValue),
  258.                 AuthorName: encodeURIComponent(authorValue),
  259.                 <?php echo $rap_ajaxComment->getNonce('wp-ajax-edit-comments-save_author') ?>},
  260.                 success: function(msg) { SaveAuthorComplete(msg, saveButton, oldSpan, newSpan) },
  261.                 error: function(msg) { SaveAuthorFailure(msg, saveButton)}
  262.             })
  263.     }
  264.     //Cancels an Edit
  265.     //evt - The event object
  266.     //obj1 - The element to show
  267.     //obj2 - The element to destroy
  268.     function EditCancel(cancelButton, obj1, obj2) {
  269.         obj2.remove();
  270.         obj1.show();
  271.     }
  272.     //Saves the comment data to the database
  273.     //saveButton - The save button
  274.     //oldDiv - The original argument
  275.     function CommentSave(saveButton, oldDiv, newDiv) {
  276.         var textarea = $j("#" + oldDiv.attr("id") + "_edit");
  277.         saveButton.attr("value", "<?php _e('Saving...', "WPAjaxEditComments") ?>");
  278.         saveButton.attr("disabled", "disabled");
  279.         if (textarea.attr("value") == undefined) { SaveCommentFailure("", saveButton); return; }
  280.         if ($j.trim(textarea.attr("value")) == '') { SaveCommentFailure('', saveButton); return; }
  281.         $j.ajax({
  282.                 type: "post",
  283.                 url: PluginUrl + '/php-includes/AjaxEditComments.php',
  284.                 timeout: 30000,
  285.                 global: false,
  286.                 data: {SaveCommentId: oldDiv.attr("id"),
  287.                 SaveContent: encodeURIComponent(textarea.attr("value")),
  288.                 <?php echo $rap_ajaxComment->getNonce('wp-ajax-edit-comments-save_comment') ?>},
  289.                 success: function(msg) { SaveCommentComplete(msg, saveButton, oldDiv, newDiv) },
  290.                 error: function(msg) { SaveCommentFailure(msg, saveButton)}
  291.             })
  292.     }
  293.     //Enables the save button passed to it and restores the default value
  294.     function EnableSave(saveButton) {
  295.         saveButton.removeClass("ajax-error");
  296.         saveButton.removeAttr("disabled");
  297.         saveButton.attr("value", "<?php _e('Save', "WPAjaxEditComments") ?>");
  298.     }
  299.     function LoadTimeComplete(msg, id, element) {
  300.         var p = element;
  301.         var minSecs = msg.split(",", 2);
  302.         var minutes = minSecs[0];
  303.         var seconds = minSecs[1];
  304.         if (p.index($j("#" + 'time' + id)) < 0) {
  305.             p.html(p.html() + '<span id="time' + id + '" class="ajax-timer"></span>');
  306.         }
  307.         var secs = minutes * 60 + seconds - 1;
  308.         if (timers[id] != undefined) {
  309.             clearTimeout(timers[id]);
  310.         }
  311.         var time = setTimeout(function() {LoadTimeSubtract(time, p, id, minutes, seconds, secs ) }, 1000);
  312.         timers[id] = time;
  313.     }
  314.     //t = The time variable | p = the paragraph that contains the time
  315.     //id = The HTML element ID to put the minutes and seconds
  316.     //minutes = the minutes | seconds = the seconds | totalSeconds = total seconds
  317.     function LoadTimeSubtract(t, p, id, minutes, seconds, totalSeconds ) {
  318.         seconds -= 1;
  319.         if (minutes <=0 && seconds <= 0) { //Time has run out
  320.             var regex = /(\d+)$/;
  321.             if (regex.test(id)) {
  322.                 id = id.match(regex)[1];
  323.                 $j("#" + CommentAuthorName +  id).unbind();
  324.                 $j("#" + CommentClassName +  id).unbind();
  325.             }    
  326.             p.remove();
  327.             clearTimeout(t)
  328.             return;
  329.         }
  330.         if (seconds < 0) { minutes -= 1; seconds = 59; }
  331.         var text = "";
  332.         if (minutes >= 1) {
  333.             if (minutes >= 2) { text = " " + minutes + " <?php _e('minutes', "WPAjaxEditComments") ?>"; } else { text = minutes + " <?php _e('minute', "WPAjaxEditComments") ?>"; }
  334.             if (seconds > 0) { text += " <?php _e('and', "WPAjaxEditComments") ?> "; }
  335.         }
  336.         if (seconds > 0) {
  337.             if (seconds >= 2) { text += seconds + " <?php _e('seconds', "WPAjaxEditComments") ?>"; } else { text += seconds + " <?php _e('second', "WPAjaxEditComments") ?>"; }
  338.         }
  339. <?php $options = $rap_ajaxComment->getAdminOptions();
  340.             if ($options['show_timer'] == "true") {        ?> 
  341.                 $j('#time' + id).html("  (" + text + ")");
  342. <?php    }     ?>
  343.         totalSeconds -= 1;
  344.         if (timers[id] != undefined) {
  345.             clearTimeout(timers[id]);
  346.         }
  347.         var time = setTimeout(function() { LoadTimeSubtract(t, p, id, minutes, seconds, totalSeconds ) }, 1000);
  348.         timers[id] = time;
  349.     }
  350.     function LoadCommentComplete(msg, id, commentButtons, obj1, obj2) {    
  351.         var textBox = $j("#" + id + "_edit");
  352.         if (msg == 0) {
  353.                 LoadCommentFailure(msg, id, obj1, obj2);
  354.                 return;
  355.         }
  356.         textBox.attr("value", msg);
  357.         commentButtons.show();
  358.     }
  359.     function LoadCommentFailure(msg, id, obj1, obj2) {
  360.         var textBox = $j("#" + id + "_edit");
  361.         textBox.attr("value", "<?php _e('Load Comment Failed', "WPAjaxEditComments") ?>");
  362.         textBox.addClass("ajax-error");
  363.         obj2.children().hide();
  364.         textBox.show();
  365.         obj2.fadeOut(3000, function() { EditCancel('', obj1, obj2);});
  366.     }
  367.     function SaveCommentComplete(msg, saveButton, oldDiv, newDiv) {
  368.         if (msg == "0") {         
  369.             SaveCommentFailure(msg, saveButton);
  370.             return;
  371.         }
  372.         //Check for the existence of a spam message
  373.         var regex = /^(spam)/;
  374.         if (regex.test(msg)) {
  375.             id = GetId(oldDiv.attr("id"));
  376.             msg = msg.substring(4, msg.length -1);
  377.             $j("#" + CommentAuthorName +  id).unbind();
  378.             $j("#" + CommentClassName +  id).unbind();
  379.             oldDiv.html(msg).show(); newDiv.remove();
  380.             return;
  381.         }
  382.         
  383.         oldDiv.html(msg).show();
  384.         newDiv.remove();
  385.  
  386.         MakeEdit();
  387.     }
  388.     function SaveCommentFailure(msg, saveButton) {
  389.         saveButton.attr("value", "<?php _e('Saving Comment Failed', "WPAjaxEditComments") ?>");
  390.         saveButton.addClass("ajax-error");
  391.     }
  392.     function SaveAuthorComplete(msg, saveButton, obj1, obj2) {
  393.         if (msg == "0") { 
  394.             SaveAuthorFailure(msg, saveButton);
  395.             return;
  396.         }
  397.         var regex = /^(spam)/;
  398.         if (regex.test(msg)) {
  399.             id = GetId(obj1.attr("id"));
  400.             msg = msg.substring(4, msg.length -1);
  401.             $j("#" + CommentAuthorName +  id).unbind();
  402.             $j("#" + CommentClassName +  id).unbind();
  403.             obj1.html(msg).show(); obj2.remove();
  404.             return;
  405.         }        
  406.         obj1.html(msg);
  407.         obj1.show();
  408.         obj2.remove();
  409.     }
  410.     function SaveAuthorFailure(msg, saveButton) {
  411.         saveButton.attr("value", "<?php _e('Saving Author Failed', "WPAjaxEditComments") ?>");
  412.         saveButton.addClass("ajax-error");
  413.     }
  414.     function DeleteSpamCommentComplete(msg, id) {
  415.      if (msg != "1") { DeleteSpamCommentFailure(msg); return; }
  416.         var li = $j("#" + "comment-" + id);
  417.         if (li.is("li") || li.is("div") ) {
  418.             li.removeClass();
  419.             li.addClass("ajax-error");
  420.             li.slideUp(1000, function() { li.remove(); });
  421.         }
  422.     }
  423.     function DeleteSpamCommentFailure(msg, id) {
  424.         alert(msg);
  425.     }
  426.     return {
  427.             init : function() { //AKA the constructor - Plugin authors can tap into the plugin by calling AjaxEditComments.init()
  428.                 MakeEdit();
  429.             }
  430.     };
  431.     
  432. }();