-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally Force 2fa #239
base: master
Are you sure you want to change the base?
Optionally Force 2fa #239
Changes from all commits
c5e1502
571bc63
48e4ba1
4e2873c
7d478a9
de2d5bb
28024b8
8ace959
0ed9963
884baed
82ee390
1db7029
5b644f7
1ace71e
bad9706
681da69
8d531bf
d4712ce
bcbfec3
0898fdc
26824fc
51b93a3
2723f88
8980d90
e8ccab1
13a8cf7
0b356f3
de8ea84
7b4cce3
f7cc27e
252e3e0
d923927
4d6d681
98a3165
1adfe47
57bfc73
753c258
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* global ajaxurl, jQuery */ | ||
|
||
/** | ||
* Checks that an element has a non-empty `name` and `value` property. | ||
* | ||
* @param {Element} element The element to check | ||
* @return {Boolean} true if the element is an input, false if not | ||
*/ | ||
var isValidElement = function( element ) { | ||
return element.name && element.value; | ||
}; | ||
|
||
/** | ||
* Checks if an element’s value can be saved (e.g. not an unselected checkbox). | ||
* | ||
* @param {Element} element The element to check | ||
* @return {Boolean} true if the value should be added, false if not | ||
*/ | ||
var isValidValue = function( element ) { | ||
return ( ! [ 'checkbox', 'radio' ].includes( element.type ) || element.checked ); | ||
}; | ||
|
||
/** | ||
* Checks if an input is a checkbox, because checkboxes allow multiple values. | ||
* | ||
* @param {Element} element The element to check | ||
* @return {Boolean} true if the element is a checkbox, false if not | ||
*/ | ||
var isCheckbox = function( element ) { | ||
return 'checkbox' === element.type; | ||
}; | ||
|
||
/** | ||
* Retrieves input data from a form and returns it as a JSON object. | ||
* | ||
* @param {HTMLFormControlsCollection} elements the form elements | ||
* @return {Object} form data as an object literal | ||
*/ | ||
var formToJSON = function( elements ) { | ||
return [].reduce.call( elements, function( data, element ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jQuery has |
||
|
||
// Make sure the element has the required properties and should be added. | ||
if ( ! isValidElement( element ) || ! isValidValue( element ) ) { | ||
return data; | ||
} | ||
|
||
/* | ||
* Some fields allow for more than one value, so we need to check if this | ||
* is one of those fields and, if so, store the values as an array. | ||
*/ | ||
if ( isCheckbox( element ) ) { | ||
data[ element.name ] = ( data[ element.name ] || [] ).concat( element.value ); | ||
} else { | ||
data[ element.name ] = element.value; | ||
} | ||
|
||
return data; | ||
}, {} ); | ||
}; | ||
|
||
/** | ||
* A handler function to prevent default submission and run our custom script. | ||
* | ||
* @param {Event} event the submit event triggered by the user | ||
*/ | ||
var handleFormSubmit = function( event ) { | ||
|
||
// Get form data. | ||
var formData = formToJSON( event.target.elements ); | ||
|
||
event.preventDefault(); | ||
|
||
formData.action = 'two_factor_force_form_submit'; | ||
|
||
// Submit data to WordPress. | ||
jQuery.post( | ||
ajaxurl, | ||
formData, | ||
function () { | ||
window.location.reload(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're not using the AJAX response, could we just skip all AJAX/serialization concept and point the form to the login URL? |
||
} | ||
); | ||
}; | ||
|
||
window.addEventListener( 'load', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could stick to jQuery helpers to support more browsers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this would support more browsers - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just suggesting that switching to the jQuery helpers (since we're using jQuery for AJAX already) would help with the compatibility even more. And would also keep it consistent with the rest of the plugin JS files until we switch to a proper build pipeline which adds the necessary polyfills. |
||
var form = document.querySelector( '#force_2fa_form' ); | ||
form.addEventListener( 'submit', handleFormSubmit ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be replaced with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the above comment on compatibility, it really doesn't improve things much if at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm only suggesting that it would make it consistent with the rest of the plugin JS and the compatibility it currently provides. |
||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we wrap these methods in an anonymous function to isolate the namespace? Some of these function names are very generic and could cause conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really rather not - anonymous functions are a scourge to test and/or maintain and make the code far less readable. I would be happy to make a "namespace" via variable although the plugin would be better to add a build pipeline from ES6 to address this kind of issue instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, it would work great if this was imported as an ES6 module but currently everything in the WP core is added to the same global namespace and we should do our best to avoid any collisions.
Most of the core JS files appear to just wrap things in
jQuery(document).ready( function($) {} );
. Using a variable or prefixing the function names would work, too.