jQuery(document).ready(function(){ AjaxEditComments.init(); }); var AjaxEditComments = function() { var $j = jQuery; var timers = new Array(); var PluginUrl = "https://www.bmssafety.com/wp-content/plugins/wp-ajax-edit-comments"; var CommentAuthorName = "editAuthor"; var CommentClassName = "editComment"; var EditCommentAuthorName = "editableAuthor"; //not edible author :) var EditCommentClassName = "editableComment"; var TextAuthorClassName = "textAuthor"; var TextCommentClassName = "textComment"; //Scans the spans and divs for a specific class and sets events to make them editable function MakeEdit() { StopEdit(); //Make the Author portions editable $j('.' + CommentAuthorName).bind("mouseover",function() {ShowAsEditable($j(this));}); $j('.' + CommentAuthorName).bind("mouseout",function() {ShowAsUnEditable($j(this));}); $j('.' + CommentAuthorName).bind("click",function() {Edit($j(this)); return false;}); //Make the comment portions editable $j('.' + CommentClassName).bind("mouseover",function() {ShowAsEditable($j(this));}); $j('.' + CommentClassName).bind("mouseout",function() {ShowAsUnEditable($j(this));}); $j('.' + CommentClassName).bind("click",function() {Edit($j(this)); return false;}); //Remove the extra classes $j('.' + CommentClassName).removeClass("post"); $j('.' + CommentClassName).removeClass("admin"); $j('.' + CommentAuthorName).removeClass("post"); $j('.' + CommentAuthorName).removeClass("admin"); GetEditText(); AddAdminLinks(); } //Disable all the edit events function StopEdit() { $j('.' + CommentAuthorName).unbind(); $j('.' + CommentClassName).unbind(); $j("." + 'ajax-admin-edit-links').unbind(); } //Adds in the admin links and sets up the events function AddAdminLinks() { $j("." + 'ajax-admin-edit-links').each(function() { var id = $j(this).attr("id"); var element = $j(this); var option = id.match(/([a-zA-Z]+)/i)[1]; var id = GetId(id); if (option == "edit") { element.bind("click", function() { Edit($j("#" + CommentClassName + id));Edit($j("#" + CommentAuthorName + id)); return false; }); } else if (option == "delete") { element.bind("click", function() { if (confirm("Delete this comment?")) { DeleteSpamComment(option, id);} return false;}) } else if (option == "spam") { element.bind("click", function() { if (confirm("Mark this comment as Spam?")) { DeleteSpamComment(option, id); } return false;}) } }); $j("." + "ajax-admin-edit-paragraph").show(); } //Deletes or Marks a comment as spam //Option = "delete" or "spam"| id is the comment ID function DeleteSpamComment(option, id) { $j.ajax({ type: "post", url: PluginUrl + '/php-includes/AjaxEditComments.php', timeout: 30000, global: false, data: { CommentOption: option, CommentId: id, _wpnonce:"773992fb83d32d0473fef3baede74d51"}, success: function(msg) { DeleteSpamCommentComplete(msg, id); }, error: function(msg) { DeleteSpamCommentFailure(msg, id); } }) } //Outputs the edit text function GetEditText() { //Output edit text $j("." + "ajax-edit").html('You may click on your name and/or comment to edit.'); GetTimeLeft(); } //Finds the time left for editing comments function GetTimeLeft() { $j("." + "ajax-edit").each(function() { var text = $j(this).attr("id"); var element = $j(this); $j.ajax({ type: "post", url: PluginUrl + '/php-includes/AjaxEditComments.php', timeout: 30000, global: false, data: {LoadTimeId: $j(this).attr("id"), _wpnonce:"32a7f0a5ff626dc02ccb8803d686495c"}, success: function(msg) { LoadTimeComplete(msg, text, element); } }) }); } //Allows the comment and author areas to be editable //obj - The object that triggered the event function Edit(obj) { if (obj.is("span")) { //Create a new span element to replace the author span if ($j('body').find('#' + TextAuthorClassName + obj.attr("id")).is("span")) { return; } //Return if the span has already been created var newSpan = '' //if url and name both return zero matches, use innerHTML as is. var url = obj.html().match(/href=\"([^"]*)\"/i); if (url == undefined) { url = obj.html().match(/href=\'([^']*)\'/i) } if (url == undefined) { url = ''; name = obj.html(); } else { url = url[1]; name = obj.html().match(/>([^<]*)<\/a>/i)[1]; } var authorInputBox = '  '; var authorButtons = ' or '; newSpan = newSpan + authorInputBox + authorButtons + ""; //Hide the old span obj.hide(); obj.before(newSpan); //Setup save and cancel button events var saveButton = $j("#" + obj.attr("id") + "_saveAuthor"); var cancelButton = $j("#" + obj.attr("id") + "_cancelAuthor"); saveButton.click(function() { AuthorSave($j(this), obj, $j("#" + TextAuthorClassName + obj.attr("id"))); }); cancelButton.click(function() { EditCancel($j(this), obj, $j("#" + TextAuthorClassName + obj.attr("id"))); }); //Setup the events for the input boxes $j("#" + obj.attr("id") + '_URL').keypress(function() { EnableSave(saveButton) }); $j("#" + obj.attr("id") + '_author').keypress(function() { EnableSave(saveButton) }); } else if (obj.is("div")) { if ($j('body').find('#' + TextCommentClassName + obj.attr("id")).is("div")) { return; } //Return if the textarea has already been created var newDiv = '
'; //Create the input box and buttons var textarea = ''; var commentButtons = '

or

'; newDiv = newDiv + textarea + commentButtons + "'); } var secs = minutes * 60 + seconds - 1; if (timers[id] != undefined) { clearTimeout(timers[id]); } var time = setTimeout(function() {LoadTimeSubtract(time, p, id, minutes, seconds, secs ) }, 1000); timers[id] = time; } //t = The time variable | p = the paragraph that contains the time //id = The HTML element ID to put the minutes and seconds //minutes = the minutes | seconds = the seconds | totalSeconds = total seconds function LoadTimeSubtract(t, p, id, minutes, seconds, totalSeconds ) { seconds -= 1; if (minutes <=0 && seconds <= 0) { //Time has run out var regex = /(\d+)$/; if (regex.test(id)) { id = id.match(regex)[1]; $j("#" + CommentAuthorName + id).unbind(); $j("#" + CommentClassName + id).unbind(); } p.remove(); clearTimeout(t) return; } if (seconds < 0) { minutes -= 1; seconds = 59; } var text = ""; if (minutes >= 1) { if (minutes >= 2) { text = " " + minutes + " minutes"; } else { text = minutes + " minute"; } if (seconds > 0) { text += " and "; } } if (seconds > 0) { if (seconds >= 2) { text += seconds + " seconds"; } else { text += seconds + " second"; } } $j('#time' + id).html("  (" + text + ")"); totalSeconds -= 1; if (timers[id] != undefined) { clearTimeout(timers[id]); } var time = setTimeout(function() { LoadTimeSubtract(t, p, id, minutes, seconds, totalSeconds ) }, 1000); timers[id] = time; } function LoadCommentComplete(msg, id, commentButtons, obj1, obj2) { var textBox = $j("#" + id + "_edit"); if (msg == 0) { LoadCommentFailure(msg, id, obj1, obj2); return; } textBox.attr("value", msg); commentButtons.show(); } function LoadCommentFailure(msg, id, obj1, obj2) { var textBox = $j("#" + id + "_edit"); textBox.attr("value", "Load Comment Failed"); textBox.addClass("ajax-error"); obj2.children().hide(); textBox.show(); obj2.fadeOut(3000, function() { EditCancel('', obj1, obj2);}); } function SaveCommentComplete(msg, saveButton, oldDiv, newDiv) { if (msg == "0") { SaveCommentFailure(msg, saveButton); return; } //Check for the existence of a spam message var regex = /^(spam)/; if (regex.test(msg)) { id = GetId(oldDiv.attr("id")); msg = msg.substring(4, msg.length -1); $j("#" + CommentAuthorName + id).unbind(); $j("#" + CommentClassName + id).unbind(); oldDiv.html(msg).show(); newDiv.remove(); return; } oldDiv.html(msg).show(); newDiv.remove(); MakeEdit(); } function SaveCommentFailure(msg, saveButton) { saveButton.attr("value", "Saving Comment Failed"); saveButton.addClass("ajax-error"); } function SaveAuthorComplete(msg, saveButton, obj1, obj2) { if (msg == "0") { SaveAuthorFailure(msg, saveButton); return; } var regex = /^(spam)/; if (regex.test(msg)) { id = GetId(obj1.attr("id")); msg = msg.substring(4, msg.length -1); $j("#" + CommentAuthorName + id).unbind(); $j("#" + CommentClassName + id).unbind(); obj1.html(msg).show(); obj2.remove(); return; } obj1.html(msg); obj1.show(); obj2.remove(); } function SaveAuthorFailure(msg, saveButton) { saveButton.attr("value", "Saving Author Failed"); saveButton.addClass("ajax-error"); } function DeleteSpamCommentComplete(msg, id) { if (msg != "1") { DeleteSpamCommentFailure(msg); return; } var li = $j("#" + "comment-" + id); if (li.is("li") || li.is("div") ) { li.removeClass(); li.addClass("ajax-error"); li.slideUp(1000, function() { li.remove(); }); } } function DeleteSpamCommentFailure(msg, id) { alert(msg); } return { init : function() { //AKA the constructor - Plugin authors can tap into the plugin by calling AjaxEditComments.init() MakeEdit(); } }; }();