From 1ef40251b91873fc0109ed6a824fd9ceac1eeac0 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Wed, 22 Jul 2015 18:36:24 +0000 Subject: [PATCH] Introduce a Cancel button and onUnload warning for password changes. fixes #33079 props johnjamesjacoby Built from https://develop.svn.wordpress.org/trunk@33364 git-svn-id: http://core.svn.wordpress.org/trunk@33336 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/js/user-profile.js | 22 +++++++++++++++++++++- wp-admin/js/user-profile.min.js | 2 +- wp-admin/user-edit.php | 3 +++ wp-admin/user-new.php | 3 +++ wp-includes/script-loader.php | 6 ++++-- wp-includes/version.php | 2 +- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/wp-admin/js/user-profile.js b/wp-admin/js/user-profile.js index e00d92ea78..46d0c48c83 100644 --- a/wp-admin/js/user-profile.js +++ b/wp-admin/js/user-profile.js @@ -7,6 +7,7 @@ pw_field2 = $('#pass2'), pw_togglebtn = pw_new.find('.wp-hide-pw'), pw_generatebtn = pw_new.find('button.wp-generate-pw'), + pw_cancelbtn = pw_new.find('button.wp-cancel-pw'), pw_2 = $('.user-pass2-wrap'), parentform = pw_new.closest('form'), pw_strength = $('#pass-strength-result'), @@ -14,8 +15,10 @@ pw_submitbtn_new = $( '#createusersub' ), pw_checkbox = $('.pw-checkbox'), pw_weak = $('.pw-weak'), + pw_update_lock = false, // Set up a text version of the password input newField = document.createElement( 'input'); + newField.type = 'text'; var pwFieldText = $( newField ); @@ -56,6 +59,7 @@ } parentform.on('submit', function(){ + pw_update_lock = false; pw_field2.val( pw_field.val() ); pwWrapper.removeClass( 'show-password' ); }); @@ -110,6 +114,7 @@ } ); pw_new.on( 'click', 'button.wp-generate-pw', function(){ + pw_update_lock = true; pw_generatebtn.hide(); pw_line.show(); generatePassword(); @@ -119,9 +124,17 @@ pwFieldText[0].setSelectionRange( 0, 100 ); } }, 0 ); - }); + pw_submitbtn_edit.on( 'click', function() { + pw_update_lock = false; + }); + + pw_cancelbtn.on( 'click', function() { + pw_update_lock = false; + pw_generatebtn.show(); + pw_line.hide(); + }); pw_togglebtn.on( 'click', function() { var show = pw_togglebtn.attr( 'data-toggle' ); @@ -145,6 +158,13 @@ } }); + + /* Warn the user if password was generated but not saved */ + $( window ).on( 'beforeunload', function() { + if ( true === pw_update_lock ) { + return userProfileL10n.warn; + } + } ); }); function check_pass_strength() { diff --git a/wp-admin/js/user-profile.min.js b/wp-admin/js/user-profile.min.js index f426a2e742..48ce73f536 100644 --- a/wp-admin/js/user-profile.min.js +++ b/wp-admin/js/user-profile.min.js @@ -1 +1 @@ -!function(a){function b(){var b,c=a("#pass1").val(),d=a("#pass2").val();if(a("#pass-strength-result").removeClass("short bad good strong"),!c)return void a("#pass-strength-result").html(" ");switch(b=wp.passwordStrength.meter(c,wp.passwordStrength.userInputBlacklist(),d)){case 2:a("#pass-strength-result").addClass("bad").html(pwsL10n.bad);break;case 3:a("#pass-strength-result").addClass("good").html(pwsL10n.good);break;case 4:a("#pass-strength-result").addClass("strong").html(pwsL10n.strong);break;case 5:a("#pass-strength-result").addClass("short").html(pwsL10n.mismatch);break;default:a("#pass-strength-result").addClass("short").html(pwsL10n["short"])}}a(function(){var b=a(".user-pass1-wrap"),c=b.find(".wp-pwd"),d=a("#pass1"),e=a("#pass2"),f=b.find(".wp-hide-pw"),g=b.find("button.wp-generate-pw"),h=a(".user-pass2-wrap"),i=b.closest("form"),j=a("#pass-strength-result"),k=a("#submit"),l=a("#createusersub"),m=a(".pw-checkbox"),n=a(".pw-weak"),o=document.createElement("input");o.type="text";var p=a(o);d.length>0&&(p.attr({id:"pass1-text",name:"pass1-text",autocomplete:"off"}).addClass(d[0].className).data("pw",d.data("pw")).val(d.val()),d.wrap('').after(p));var q=d.parent(),r=window.generatePassword=function(){"function"!=typeof zxcvbn?setTimeout(r,50):(d.val(d.data("pw")),d.trigger("propertychange"),q.addClass("show-password"))};h.hide(),c.hide(),f.show(),g.show(),1==d.data("reveal")&&r(),i.on("submit",function(){e.val(d.val()),q.removeClass("show-password")}),p.on("input",function(){d.val(p.val()),d.trigger("propertychange")}),d.on("input propertychange",function(){setTimeout(function(){var a=j.attr("class");p.val(d.val()),d.add(p).removeClass("short bad good strong"),"undefined"!=typeof a&&(d.add(p).addClass(a),"short"==a||"bad"==a?(m.attr("checked")||(l.attr("disabled","disabled"),k.attr("disabled","disabled")),n.show()):(l.removeAttr("disabled"),k.removeAttr("disabled"),n.hide()))},1)}),m.change(function(){m.attr("checked")?(l.removeAttr("disabled"),k.removeAttr("disabled")):(l.attr("disabled","disabled"),k.attr("disabled","disabled"))}),e.on("input propertychange",function(){d.val(e.val()),d.trigger("propertychange")}),b.on("click","button.wp-generate-pw",function(){g.hide(),c.show(),r(),_.defer(function(){p.focus(),_.isUndefined(p[0].setSelectionRange)||p[0].setSelectionRange(0,100)},0)}),f.on("click",function(){var a=f.attr("data-toggle");1==a?(q.addClass("show-password"),f.attr({"data-toggle":0,"aria-label":userProfileL10n.ariaHide}).find(".text").text(userProfileL10n.hide),p.focus(),_.isUndefined(p[0].setSelectionRange)||p[0].setSelectionRange(0,100)):(q.removeClass("show-password"),f.attr({"data-toggle":1,"aria-label":userProfileL10n.ariaShow}).find(".text").text(userProfileL10n.show),d.focus(),_.isUndefined(d[0].setSelectionRange)||d[0].setSelectionRange(0,100))})}),a(document).ready(function(){var c,d,e,f,g=a("#display_name");a("#pass1").val("").on("input propertychange",b),a("#pass2").val("").on("input propertychange",b),a("#pass-strength-result").show(),a(".color-palette").click(function(){a(this).siblings('input[name="admin_color"]').prop("checked",!0)}),g.length&&a("#first_name, #last_name, #nickname").bind("blur.user_profile",function(){var b=[],c={display_nickname:a("#nickname").val()||"",display_username:a("#user_login").val()||"",display_firstname:a("#first_name").val()||"",display_lastname:a("#last_name").val()||""};c.display_firstname&&c.display_lastname&&(c.display_firstlast=c.display_firstname+" "+c.display_lastname,c.display_lastfirst=c.display_lastname+" "+c.display_firstname),a.each(a("option",g),function(a,c){b.push(c.value)}),a.each(c,function(d,e){if(e){var f=e.replace(/<\/?[a-z][^>]*>/gi,"");c[d].length&&-1===a.inArray(f,b)&&(b.push(f),a("