From 9e81e209161751a1b312a834459f8aa6382ceb3e Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Fri, 22 Apr 2016 21:42:38 +0000 Subject: [PATCH] Comments: Keep comments safe in the Edit Post screen. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Warns users that have added a new Comment or began editing an existing without saving their changes, before they press the “update” button which would wipe out their comment changes. Fixes #32818. Props polevaultweb. git-svn-id: https://develop.svn.wordpress.org/trunk@37303 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/edit-comments.js | 23 +++++++++++++++++++++++ src/wp-admin/js/post.js | 8 ++++++++ src/wp-includes/script-loader.php | 1 + 3 files changed, 32 insertions(+) diff --git a/src/wp-admin/js/edit-comments.js b/src/wp-admin/js/edit-comments.js index e0f8a7c048..3de031022c 100644 --- a/src/wp-admin/js/edit-comments.js +++ b/src/wp-admin/js/edit-comments.js @@ -566,6 +566,7 @@ setCommentsList = function() { commentReply = { cid : '', act : '', + originalContent : '', init : function() { var row = $('#replyrow'); @@ -649,6 +650,7 @@ commentReply = { $( '.spinner', replyrow ).removeClass( 'is-active' ); this.cid = ''; + this.originalContent = ''; }, open : function(comment_id, post_id, action) { @@ -658,6 +660,10 @@ commentReply = { h = c.height(), colspanVal = 0; + if ( ! this.discardCommentChanges() ) { + return false; + } + t.close(); t.cid = comment_id; @@ -666,6 +672,7 @@ commentReply = { action = action || 'replyto'; act = 'edit' == action ? 'edit' : 'replyto'; act = t.act = act + '-comment'; + t.originalContent = $('textarea.comment', rowData).val(); colspanVal = $( '> th:visible, > td:visible', c ).length; // Make sure it's actually a table and there's a `colspan` value to apply. @@ -853,6 +860,22 @@ commentReply = { $('table.comments-box').css('display', ''); $('#no-comments').remove(); }); + }, + + /** + * Alert the user if they have unsaved changes on a comment that will be + * lost if they proceed. + * + * @returns {boolean} + */ + discardCommentChanges: function() { + var editRow = $( '#replyrow' ); + + if ( this.originalContent === $( '#replycontent', editRow ).val() ) { + return true; + } + + return window.confirm( adminCommentsL10n.warnCommentChanges ); } }; diff --git a/src/wp-admin/js/post.js b/src/wp-admin/js/post.js index a4011b449d..1ead4bed1c 100644 --- a/src/wp-admin/js/post.js +++ b/src/wp-admin/js/post.js @@ -271,6 +271,14 @@ jQuery(document).ready( function($) { } if ( typeof commentReply !== 'undefined' ) { + /* + * Warn the user they have an unsaved comment before submitting + * the post data for update. + */ + if ( ! commentReply.discardCommentChanges() ) { + return false; + } + /* * Close the comment edit/reply form if open to stop the form * action from interfering with the post's form action. diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index e3a319c3d3..19d16f3cd9 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -495,6 +495,7 @@ function wp_default_scripts( &$scripts ) { 'replyApprove' => __( 'Approve and Reply' ), 'reply' => __( 'Reply' ), 'warnQuickEdit' => __( "Are you sure you want to edit this comment?\nThe changes you made will be lost." ), + 'warnCommentChanges' => __( "Are you sure you want to do this?\nThe comment changes you made will be lost." ), 'docTitleComments' => __( 'Comments' ), /* translators: %s: comments count */ 'docTitleCommentsCount' => __( 'Comments (%s)' ),