PluginProbe
Authorizer / 2.3.4
Authorizer v2.3.4
3.15.3 3.15.2 3.15.1 3.15.0 3.14.3 3.14.4 3.14.2 3.14.1 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.10 2.9.11 2.9.12 2.9.13 2.9.2 2.9.3 2.9.6 All 126 releases
← All changes | js/authorizer.js +868 -1167 2.9.132.3.4 View file →
@@ -1,1364 +1,1065 @@
1 -/**
2 - * UI wiring for Authorizer Settings page.
3 - */
1 +var animation_speed = 300;
2 +var shake_speed = 600;
4 3
5 -/* global window, document, setTimeout, sessionStorage, ajaxurl, authL10n, history */
6 -( function( $ ) {
7 4
8 - // Milliseconds for jQuery UI animations to complete.
9 - var animationSpeed = 300;
10 - // Milliseconds for shake animation (reject email address) to complete.
11 - var shakeSpeed = 600;
12 5
13 - /**
14 - * Wiring and UI for Authorizer Settings page.
15 - */
6 +// Switch between option tabs.
7 +function choose_tab( list_name, delay ) {
8 + var $ = jQuery;
16 9
17 - // Switch between pages in the Approved User list.
18 - // @calls php wp_ajax_refresh_approved_user_list.
19 - function refreshApprovedUserList( currentPage, searchTerm ) {
20 - var $list = $( '#list_auth_settings_access_users_approved' );
21 - var $spinner = $( '<span class="spinner is-active"></span>' ).css({
22 - position: 'relative',
23 - top: '40%',
24 - left: '-240px',
25 - });
26 - var $overlay = $( '<div id="list_auth_settings_access_users_approved_overlay"></div>' ).css({
27 - 'background-color': '#f1f1f1',
28 - 'z-index': 1,
29 - opacity: 0.8,
30 - position: 'absolute',
31 - top: $list.position().top + parseInt( $list.css( 'margin-top' ) ),
32 - left: $list.position().left,
33 - width: $list.width(),
34 - height: $list.height(),
35 - });
36 - $overlay.append( $spinner );
10 + // default delay is 0
11 + delay = typeof delay !== 'undefined' ? delay : 0;
37 12
38 - // Show overlay and wait cursor.
39 - $list.after( $overlay );
40 - $( 'html' ).addClass( 'busy' );
13 + // default to the access list tab
14 + list_name = typeof list_name !== 'undefined' ? list_name : 'access_list';
41 15
42 - $.post( ajaxurl, {
43 - action: 'refresh_approved_user_list',
44 - nonce: $( '#nonce_save_auth_settings' ).val(),
45 - is_network_admin: authL10n.is_network_admin, // eslint-disable-line camelcase
46 - paged: currentPage,
47 - search: searchTerm,
48 - }, function( response ) {
49 - if ( response.success ) {
50 - // Update user list and total user and page count.
51 - $( '#list_auth_settings_access_users_approved' ).html( response.html );
52 - $( '.displaying-num' ).html( response.total_users_html );
53 - $( '.total-pages' ).html( response.total_pages_html );
16 + // Hide all tab content, then show selected tab content
17 + $( 'div.section_info, div.section_info + table' ).hide();
18 + $( '#section_info_' + list_name + ', #section_info_' + list_name + ' + table' ).show();
54 19
55 - // Adjust our current page if the query changed the total page count.
56 - if ( currentPage > response.total_pages ) {
57 - currentPage = response.total_pages;
58 - }
20 + // Set active tab
21 + $( '.nav-tab-wrapper a' ).removeClass( 'nav-tab-active' );
22 + $( 'a.nav-tab-' + list_name ).addClass( 'nav-tab-active' );
59 23
60 - // Update pager elements.
61 - refreshApprovedUserPager( currentPage );
24 + // Hide site options if they are overridden by a multisite setting.
25 + setTimeout( hide_multisite_overridden_options, delay );
62 26
63 - // Update querystring with new paged param value (but don't reload the page).
64 - if ( history.pushState ) {
65 - var url = window.location.href;
66 - url = updateQueryStringParameter( url, 'paged', currentPage );
67 - url = updateQueryStringParameter( url, 'search', searchTerm );
68 - window.history.pushState( { path: url }, '', url );
69 - }
70 - }
71 - // Remove overlay and wait cursor.
72 - $overlay.remove();
73 - $( 'html' ).removeClass( 'busy' );
74 - }).fail( function() {
75 - // Remove overlay and wait cursor.
76 - $overlay.remove();
77 - $( 'html' ).removeClass( 'busy' );
78 - });
27 + // Hide Save Changes button if we're on the access lists page (changing
28 + // access lists saves automatically via AJAX).
29 + $( 'body:not(.network-admin) #submit' ).toggle( list_name !== 'access_lists' );
30 +
31 + // Save user's active tab to sessionStorage (so we can restore it on reload).
32 + // Note: session storage persists until the browser tab is closed.
33 + sessionStorage.setItem( 'tab', list_name );
34 +}
35 +
36 +// Update user's usermeta field.
37 +function auth_update_usermeta( caller ) {
38 + var $ = jQuery,
39 + $caller = $( caller ),
40 + $usermeta = $caller.parent().children( '.auth-usermeta' ),
41 + email = $caller.siblings( '.auth-email' ).val(),
42 + usermeta = $usermeta.val(),
43 + nonce_save_auth_settings = $( '#nonce_save_auth_settings' ).val();
44 +
45 + // Remove reference to caller if it's the usermeta field itself (not a button triggering the save).
46 + if ( $caller.hasClass( 'auth-usermeta' ) ) {
47 + $caller = $();
79 48 }
80 49
81 - // Update the pager elements when changing pages.
82 - function refreshApprovedUserPager( currentPage ) {
83 - var totalPages = parseInt( $( '.total-pages' ).first().text().replace( /[^0-9]/g, '' ), 10 ) || 1;
50 + // Disable inputs, show spinner.
51 + $caller.attr( 'disabled', 'disabled' );
52 + $usermeta.attr( 'disabled', 'disabled' );
53 + $usermeta.after( '<span class="spinner is-active"></span>' );
54 + $( 'html' ).addClass( 'busy' );
84 55
85 - // If total number of pages changed (because a search filter reduced it), make
86 - // sure current page is not larger than it.
87 - if ( currentPage > totalPages ) {
88 - currentPage = totalPages;
89 - }
90 - if ( currentPage < 1 ) {
91 - currentPage = 1;
92 - }
93 - if ( totalPages < 1 ) {
94 - totalPages = 1;
95 - }
56 + // Call ajax save function.
57 + $.post( ajaxurl, {
58 + 'action': 'update_auth_usermeta',
59 + 'email': email,
60 + 'usermeta': usermeta,
61 + 'nonce_save_auth_settings': nonce_save_auth_settings,
62 + }, function ( response ) {
63 + var succeeded = response === 'success';
64 + var spinner_text = succeeded ? 'Saved.' : '<span style="color: red;">Failed.</span>';
65 + var spinner_wait = succeeded ? 500 : 2000;
96 66
97 - // Update current page text input.
98 - $( '#current-page-selector' ).val( currentPage );
67 + // Enable inputs, remove spinner.
68 + $caller.removeAttr( 'disabled' );
69 + $usermeta.removeAttr( 'disabled' );
70 + $caller.css( 'display', 'none' );
71 + $( 'form .spinner:not(:has(.spinner-text))' ).animate( { width: '60px' }, 'fast' ).append( '<span class="spinner-text">' + spinner_text + '</span>' ).delay( spinner_wait ).hide( animation_speed, function() {
72 + $( this ).remove();
73 + });
74 + $( 'html' ).removeClass( 'busy' );
99 75
100 - // Update current page span.
101 - $( '#table-paging .current-page-text' ).text( currentPage );
76 + }).fail( function ( response ) {
77 + var succeeded = false;
78 + var spinner_text = succeeded ? 'Saved.' : '<span style="color: red;">Failed.</span>';
79 + var spinner_wait = succeeded ? 500 : 2000;
102 80
103 - // Update first page button.
104 - var $first = $( '.first-page' );
105 - if ( $first.is( 'a' ) && currentPage <= 1 ) {
106 - $first.replaceWith( '<span class="button disabled first-page tablenav-pages-navspan" aria-hidden="true">&laquo;</span>' );
107 - } else if ( $first.is( 'span' ) && currentPage > 1 ) {
108 - $first.replaceWith( '<a class="button first-page" href="' + updateQueryStringParameter( window.location.href, 'paged', '1' ) + '"><span class="screen-reader-text">' + authL10n.first_page + '</span><span aria-hidden="true">&laquo;</span></a>' );
109 - }
81 + // Enable inputs, remove spinner.
82 + $caller.removeAttr( 'disabled' );
83 + $usermeta.removeAttr( 'disabled' );
84 + $caller.css( 'display', 'none' );
85 + $( 'form .spinner:not(:has(.spinner-text))' ).animate( { width: '60px' }, 'fast' ).append( '<span class="spinner-text">' + spinner_text + '</span>' ).delay( spinner_wait ).hide( animation_speed, function() {
86 + $( this ).remove();
87 + });
88 + $( 'html' ).removeClass( 'busy' );
110 89
111 - // Update prev page button.
112 - var $prev = $( '.prev-page' );
113 - if ( $prev.is( 'a' ) && currentPage <= 1 ) {
114 - $prev.replaceWith( '<span class="button disabled prev-page tablenav-pages-navspan" aria-hidden="true">&lsaquo;</span>' );
115 - } else if ( currentPage > 1 ) {
116 - $prev.replaceWith( '<a class="button prev-page" href="' + updateQueryStringParameter( window.location.href, 'paged', currentPage - 1 ) + '"><span class="screen-reader-text">' + authL10n.prev_page + '</span><span aria-hidden="true">&lsaquo;</span></a>' );
117 - }
90 + });
118 91
119 - // Update next button.
120 - var $next = $( '.next-page' );
121 - if ( $next.is( 'a' ) && currentPage >= totalPages ) {
122 - $next.replaceWith( '<span class="button disabled next-page tablenav-pages-navspan" aria-hidden="true">&rsaquo;</span>' );
123 - } else if ( currentPage < totalPages ) {
124 - $next.replaceWith( '<a class="button next-page" href="' + updateQueryStringParameter( window.location.href, 'paged', currentPage + 1 ) + '"><span class="screen-reader-text">' + authL10n.next_page + '</span><span aria-hidden="true">&rsaquo;</span></a>' );
125 - }
92 +}
126 93
127 - // Update last button.
128 - var $last = $( '.last-page' );
129 - if ( $last.is( 'a' ) && currentPage >= totalPages ) {
130 - $last.replaceWith( '<span class="button disabled last-page tablenav-pages-navspan" aria-hidden="true">&raquo;</span>' );
131 - } else if ( $last.is( 'span' ) && currentPage < totalPages ) {
132 - $last.replaceWith( '<a class="button last-page" href="' + updateQueryStringParameter( window.location.href, 'paged', totalPages ) + '"><span class="screen-reader-text">' + authL10n.next_page + '</span><span aria-hidden="true">&raquo;</span></a>' );
133 - }
134 - }
94 +// Update user's role (multisite options page).
95 +function auth_multisite_change_role( caller ) {
96 + var is_multisite = true;
97 + auth_change_role( caller, is_multisite );
98 +}
135 99
136 - // Make changes to one of the user lists (pending, approved, blocked) via ajax.
137 - // @calls php wp_ajax_update_auth_user.
138 - function updateAuthUser( caller, setting, usersToEdit ) {
139 - var accessUsersPending = [],
140 - accessUsersApproved = [],
141 - accessUsersBlocked = [],
142 - nonce = $( '#nonce_save_auth_settings' ).val();
100 +// Update user's role.
101 +function auth_change_role( caller, is_multisite ) {
102 + var $ = jQuery;
143 103
144 - // Defaults:
145 - // setting = 'access_users_pending' or 'access_users_approved' or 'access_users_blocked',
146 - // usersToEdit = [
147 - // {
148 - // email: 'johndoe@example.com',
149 - // role: 'subscriber',
150 - // date_added: 'Jun 2014',
151 - // edit_action: 'add' or 'remove' or 'change_role',
152 - // local_user: true or false,
153 - // multisite_user: true or false,
154 - // }, {
155 - // ...
156 - // }
157 - // ]
158 - setting = typeof setting !== 'undefined' ? setting : 'none';
104 + // Set default for multisite flag (run different save routine if multisite)
105 + is_multisite = typeof is_multisite !== 'undefined' ? is_multisite : false;
159 106
160 - // If we are only editing a single user, make that user the only item in the array.
161 - usersToEdit = typeof usersToEdit !== 'undefined' ? usersToEdit : [];
162 - if ( ! Array.isArray( usersToEdit ) ) {
163 - usersToEdit = [ usersToEdit ];
164 - }
107 + var email = $( caller ).parent().find( '.auth-email' );
108 + var role = $( caller ).parent().find( '.auth-role' );
109 + var date_added = $( caller ).parent().find( '.auth-date-added' );
165 110
166 - // Enable wait cursor.
167 - $( 'html' ).addClass( 'busy' );
111 + var user = {
112 + 'email': email.val(),
113 + 'role': role.val(),
114 + 'date_added': date_added.val(),
115 + 'edit_action': 'change_role',
116 + 'multisite_user': is_multisite,
117 + };
168 118
169 - // Disable button (prevent duplicate clicks).
170 - $( caller ).attr( 'disabled', 'disabled' );
119 + // Update the options in the database with this change.
120 + update_auth_user( caller, 'access_users_approved', user );
171 121
172 - // Enable spinner by element that triggered this event (caller).
173 - var $row = $( caller ).closest( 'li' );
174 - if ( $row.length > 0 ) {
175 - var $spinner = $( '<span class="spinner is-active"></span>' ).css({
176 - position: 'absolute',
177 - top: $row.position().top,
178 - left: $row.position().left + $row.width(),
179 - });
180 - $row.append( $spinner );
181 - }
122 + return true;
123 +}
182 124
183 - // Grab the value of the setting we are saving.
184 - if ( setting === 'access_users_pending' ) {
185 - accessUsersPending = usersToEdit;
186 - } else if ( setting === 'access_users_approved' ) {
187 - accessUsersApproved = usersToEdit;
188 - } else if ( setting === 'access_users_blocked' ) {
189 - accessUsersBlocked = usersToEdit;
190 - }
125 +// Add user to list (multisite options page).
126 +function auth_multisite_add_user( caller, list, create_local_account ) {
127 + var is_multisite = true;
191 128
192 - $.post( ajaxurl, {
193 - action: 'update_auth_user',
194 - setting: setting,
195 - access_users_pending: accessUsersPending, // eslint-disable-line camelcase
196 - access_users_approved: accessUsersApproved, // eslint-disable-line camelcase
197 - access_users_blocked: accessUsersBlocked, // eslint-disable-line camelcase
198 - nonce: nonce,
199 - }, function( response ) {
200 - // Server responded, but if success isn't true it failed to save.
201 - var succeeded = response.success;
202 - var spinnerText = succeeded ? authL10n.saved + '.' : '<span class="attention">' + authL10n.failed + '.</span>';
203 - var spinnerWait = succeeded ? 500 : 2000;
129 + // Default to the approved list.
130 + list = typeof list !== 'undefined' ? list : 'approved';
204 131
205 - // Remove any new user entries that were rejected by the server.
206 - if ( response.invalid_emails.length > 0 ) {
207 - for ( var i = 0; i < response.invalid_emails.length; i++ ) {
208 - var duplicateEmail = response.invalid_emails[i];
209 - $( 'li.new-user .auth-email[value="' + duplicateEmail + '"]' )
210 - .siblings( '.spinner' ).addClass( 'duplicate' ).append( '<span class="spinner-text"><span class="attention">' + authL10n.duplicate + '.</span></span>' )
211 - .parent().fadeOut( spinnerWait, function() { $( this ).remove(); }); // jshint ignore:line
212 - }
213 - }
132 + // Default to not creating a local account.
133 + create_local_account = typeof create_local_account !== 'undefined' ? create_local_account : false;
214 134
215 - // Show message ('Saved', 'Failed', or 'Saved, removing duplicates').
216 - $( 'form .spinner:not(:has(.spinner-text)):not(.duplicate)' ).append( '<span class="spinner-text">' + spinnerText + '</span>' ).delay( spinnerWait ).hide( animationSpeed, function() {
217 - $( this ).remove();
218 - });
219 - $( caller ).removeAttr( 'disabled' );
135 + // There currently is no multisite blocked list, so do nothing.
136 + if ( list === 'blocked' ) {
137 + return;
138 + }
220 139
221 - // Disable wait cursor.
222 - $( 'html' ).removeClass( 'busy' );
223 - }).fail( function() {
224 - // Fail fires if the server doesn't respond or responds with 500 codes
225 - var succeeded = false;
226 - var spinnerText = succeeded ? authL10n.saved + '.' : '<span class="attention">' + authL10n.failed + '.</span>';
227 - var spinnerWait = succeeded ? 500 : 2000;
228 - $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinnerText + '</span>' ).delay( spinnerWait ).hide( animationSpeed, function() {
229 - $( this ).remove();
230 - });
231 - $( caller ).removeAttr( 'disabled' );
140 + auth_add_user( caller, list, create_local_account, is_multisite );
141 +}
142 +// Add user to list (list = blocked or approved).
143 +function auth_add_user( caller, list, create_local_account, is_multisite ) {
144 + var $ = jQuery;
232 145
233 - // Disable wait cursor.
234 - $( 'html' ).removeClass( 'busy' );
235 - });
236 - }
146 + // Skip email address validation if adding from pending list (not user-editable).
147 + var skip_validation = $( caller ).parent().parent().attr( 'id' ) === 'list_auth_settings_access_users_pending';
237 148
149 + // Skip email address validation if we're banning an existing user (since they're already in a list).
150 + skip_validation = skip_validation || $( caller ).attr( 'id' ).indexOf( 'block_user' ) > -1;
238 151
239 - // Hide or show (with overlay) the multisite settings based on the "multisite override" setting.
240 - function hideMultisiteSettingsIfDisabled() {
241 - if ( $( '#auth_settings_multisite_override' ).length === 0 ) {
242 - return;
243 - }
152 + // Set default for multisite flag (run different save routine if multisite)
153 + is_multisite = typeof is_multisite !== 'undefined' ? is_multisite : false;
244 154
245 - var settings = $( '#auth_multisite_settings' );
246 - var overlay = $( '#auth_multisite_settings_disabled_overlay' );
155 + // Default to the approved list.
156 + list = typeof list !== 'undefined' ? list : 'approved';
247 157
248 - if ( $( '#auth_settings_multisite_override' ).is( ':checked' ) ) {
249 - overlay.hide( animationSpeed );
250 - } else {
251 - overlay.css({
252 - 'background-color': '#f1f1f1',
253 - 'z-index': 1,
254 - opacity: 0.8,
255 - position: 'absolute',
256 - top: settings.position().top,
257 - left: settings.position().left,
258 - width: settings.width(),
259 - height: settings.height() + 50,
260 - });
261 - overlay.show();
262 - }
263 - }
158 + // Default to not creating a local account.
159 + create_local_account = typeof create_local_account !== 'undefined' ? create_local_account : false;
264 160
265 - // Helper function to remove duplicate entries from an array of strings.
266 - function removeDuplicatesFromArrayOfStrings( arrayOfStrings ) {
267 - var seen = {};
268 - return arrayOfStrings.filter( function( item ) {
269 - return seen.hasOwnProperty( item ) ? false : ( seen[item] = true );
270 - });
271 - }
161 + var email = $( caller ).parent().find( '.auth-email' );
162 + var role = $( caller ).parent().find( '.auth-role' );
163 + var date_added = $( caller ).parent().find( '.auth-date-added' );
272 164
273 - // Helper function to hide/show wordpress option
274 - function animateOption( action, option ) {
275 - if ( action === 'show' ) {
276 - $( 'div.animated_wrapper', option ).slideDown( animationSpeed );
277 - $( 'th, td', option ).removeClass( 'hide-animate hide-no-animate' );
278 - } else if ( action === 'hide' ) {
279 - $( 'div.animated_wrapper', option ).slideUp( animationSpeed );
280 - $( 'td, th', option ).addClass( 'hide-animate' );
281 - } else if ( action === 'hide_immediately' ) {
282 - $( 'div.animated_wrapper', option ).hide();
283 - $( 'td, th', option ).addClass( 'hide-no-animate' );
284 - }
285 - }
165 + // Helper variable for disabling buttons while processing. This will be
166 + // set differently if our clicked button is nested in a div (below).
167 + var buttons = caller;
286 168
287 - // Helper function to grab a querystring param value by name
288 - function getParameterByName( needle, haystack ) {
289 - needle = needle.replace( /[\[]/, '\\\[').replace(/[\]]/, '\\\]' ); // eslint-disable-line no-useless-escape
290 - var regex = new RegExp( '[\\?&]' + needle + '=([^&#]*)' );
291 - var results = regex.exec( haystack );
292 - if ( results === null ) {
293 - return '';
294 - } else {
295 - return decodeURIComponent( results[1].replace( /\+/g, ' ' ) );
296 - }
169 + // Button (caller) might be nested in a div, so we need to walk up one more level
170 + if ( email.length === 0 || role.length === 0 ) {
171 + email = $( caller ).parent().parent().find( '.auth-email' );
172 + role = $( caller ).parent().parent().find( '.auth-role' );
173 + date_added = $( caller ).parent().parent().find( '.auth-date-added' );
174 + buttons = $( caller ).parent().children();
297 175 }
298 176
299 - // Helper function to return a short date (e.g., Jul 2013) for today's date
300 - function getShortDate( date ) {
301 - date = typeof date !== 'undefined' ? date : new Date();
302 - var month = '';
303 - switch ( date.getMonth() ) {
304 - case 0: month = 'Jan'; break;
305 - case 1: month = 'Feb'; break;
306 - case 2: month = 'Mar'; break;
307 - case 3: month = 'Apr'; break;
308 - case 4: month = 'May'; break;
309 - case 5: month = 'Jun'; break;
310 - case 6: month = 'Jul'; break;
311 - case 7: month = 'Aug'; break;
312 - case 8: month = 'Sep'; break;
313 - case 9: month = 'Oct'; break;
314 - case 10: month = 'Nov'; break;
315 - case 11: month = 'Dec'; break;
316 - }
317 - return month + ' ' + date.getFullYear();
318 - }
177 + var user = {
178 + 'email': email.val(),
179 + 'role': role.val(),
180 + 'date_added': date_added.val(),
181 + 'edit_action': 'add',
182 + 'local_user': create_local_account,
183 + 'multisite_user': is_multisite,
184 + };
319 185
320 - // Helper function to grab a querystring value
321 - function getQuerystringValuesByKey( key ) {
322 - var re = new RegExp( '(?:\\?|&)' + key + '=(.*?)(?=&|$)', 'gi' );
323 - var matchingValues = [];
324 - var match;
325 - while ( ( match = re.exec( document.location.search ) ) !== null ) {
326 - matchingValues.push( match[1] );
327 - }
328 - return matchingValues;
329 - }
186 + var next_id = $( '#list_auth_settings_access_users_' + list + ' li' ).length;
187 + var validated = true;
330 188
331 - // Helper function to check if an email address is valid. If allowWildcardEmail
332 - // is true, then any string starting with an @ is valid.
333 - function validEmail( email, allowWildcardEmail ) {
334 - allowWildcardEmail = typeof allowWildcardEmail !== 'undefined' ? allowWildcardEmail : false;
335 - var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
336 - return email.length > 0 && ( re.test( email ) || email.startsWith( '@' ) );
189 + if ( $.trim( email.val() ) === '' ) {
190 + return false;
337 191 }
338 192
339 - // Helper function to set or update a querystring value.
340 - function updateQueryStringParameter( uri, key, value ) {
341 - // Remove the hash before operating on the URI.
342 - var i = uri.indexOf( '#' );
343 - var hash = i === -1 ? '' : uri.substr( i );
344 - uri = i === -1 ? uri : uri.substr( 0, i );
193 + $( buttons ).attr( 'disabled', 'disabled' );
345 194
346 - var re = new RegExp( '([?&])' + key + '=.*?(&|$)', 'i' );
347 - var separator = uri.indexOf( '?' ) !== -1 ? '&' : '?';
195 + // Check if the name being added already exists in any list.
196 + if ( ! skip_validation && validated ) {
197 + $( 'li > input.auth-email' ).each( function() {
198 + if ( this.value == email.val() ) {
199 + validated = false;
200 + email.parent().effect( 'shake', shake_speed );
201 + $( this ).parent().effect( 'shake', shake_speed );
202 + $( buttons ).removeAttr( 'disabled' );
203 + return false;
204 + }
205 + });
206 + }
348 207
349 - if ( ! value ) {
350 - // Remove key-value pair if empty.
351 - uri = uri.replace( new RegExp( '([?&]?)' + key + '=[^&]*', 'i' ), '' );
352 - if ( uri.slice( -1 ) === '?' ) {
353 - uri = uri.slice( 0, -1 );
354 - }
355 - // Replace first occurrence of & by ? if no ? is present.
356 - if ( uri.indexOf( '?' ) === -1 ) {
357 - uri = uri.replace( /&/, '?' );
358 - }
359 - } else if ( uri.match( re ) ) {
360 - uri = uri.replace( re, '$1' + key + '=' + value + '$2' );
361 - } else {
362 - uri = uri + separator + key + '=' + value;
208 + // Check if the user being added has a valid email address.
209 + if ( ! skip_validation && validated ) {
210 + if ( ! valid_email( email.val() ) ) {
211 + validated = false;
212 + $( '#new_' + list + '_user_email' ).parent().effect( 'shake', shake_speed );
213 + $( buttons ).removeAttr( 'disabled' );
363 214 }
364 - return uri + hash;
365 215 }
366 216
217 + if ( validated ) {
218 + // Add the new item.
219 + var auth_js_prefix = is_multisite ? 'auth_multisite_' : 'auth_';
220 + var local_icon = create_local_account ? '&nbsp;<a title="Local WordPress user" class="auth-local-user"><span class="glyphicon glyphicon-user"></span></a>' : '';
221 + var ban_button = list === 'approved' && ! is_multisite ? '<a class="button" onclick="' + auth_js_prefix + 'add_user( this, \'blocked\', false ); ' + auth_js_prefix + 'ignore_user( this, \'approved\' );" title="Block/Ban user"><span class="glyphicon glyphicon-ban-circle"></span></a>' : '';
222 + $( ' \
223 + <li id="new_user_' + next_id + '" style="display: none;"> \
224 + <input type="text" id="auth_settings_access_users_' + list + '_' + next_id + '" name="auth_settings[access_users_' + list + '][' + next_id + '][email]" value="' + email.val() + '" readonly="true" class="auth-email" /> \
225 + <select name="auth_settings[access_users_' + list + '][' + next_id + '][role]" class="auth-role" onchange="' + auth_js_prefix + 'change_role( this );"> \
226 + </select> \
227 + <input type="text" name="auth_settings[access_users_' + list + '][' + next_id + '][date_added]" value="' + getShortDate() + '" readonly="true" class="auth-date-added" /> \
228 + ' + ban_button + ' \
229 + <a class="button" onclick="' + auth_js_prefix + 'ignore_user( this, \'approved\' );" title="Remove user"><span class="glyphicon glyphicon-remove"></span></a> \
230 + ' + local_icon + ' \
231 + <span class="spinner is-active"></span> \
232 + </li> \
233 + ' ).appendTo( '#list_auth_settings_access_users_' + list + '' ).slideDown( 250 );
367 234
368 - /**
369 - * Wire up actions when document has loaded.
370 - */
235 + // Populate the role dropdown in the new element. Because clone() doesn't
236 + // save selected state on select elements, set that too.
237 + $( 'option', role ).clone().appendTo( '#new_user_' + next_id + ' .auth-role' );
238 + $( '#new_user_' + next_id + ' .auth-role' ).val( role.val() );
371 239
240 + // Remove the 'empty list' item if it exists.
241 + $( '#list_auth_settings_access_users_' + list + ' li.auth-empty' ).remove();
372 242
373 - $( document ).ready( function() {
374 - // Grab references to form elements that we will show/hide on page load
375 - /* eslint-disable camelcase */
376 - var auth_settings_access_role_receive_pending_emails = $( '#auth_settings_access_role_receive_pending_emails' ).closest( 'tr' );
377 - var auth_settings_access_pending_redirect_to_message = $( '#wp-auth_settings_access_pending_redirect_to_message-wrap' ).closest( 'tr' );
378 - var auth_settings_access_blocked_redirect_to_message = $( '#wp-auth_settings_access_blocked_redirect_to_message-wrap' ).closest( 'tr' );
379 - var auth_settings_access_should_email_approved_users = $( '#auth_settings_access_should_email_approved_users' ).closest( 'tr' );
380 - var auth_settings_access_email_approved_users_subject = $( '#auth_settings_access_email_approved_users_subject' ).closest( 'tr' );
381 - var auth_settings_access_email_approved_users_body = $( '#wp-auth_settings_access_email_approved_users_body-wrap' ).closest( 'tr' );
382 - var auth_settings_access_public_pages = $( '#auth_settings_access_public_pages' ).closest( 'tr' );
383 - var auth_settings_access_redirect_to_login = $( '#radio_auth_settings_access_redirect_to_login' ).closest( 'tr' );
384 - var auth_settings_access_public_warning = $( '#radio_auth_settings_access_public_warning' ).closest( 'tr' );
385 - var auth_settings_access_redirect_to_message = $( '#wp-auth_settings_access_redirect_to_message-wrap' ).closest( 'tr' );
386 - var auth_settings_external_google_clientid = $( '#auth_settings_google_clientid' ).closest( 'tr' );
387 - var auth_settings_external_google_clientsecret = $( '#auth_settings_google_clientsecret' ).closest( 'tr' );
388 - var auth_settings_external_google_hosteddomain = $( '#auth_settings_google_hosteddomain' ).closest( 'tr' );
389 - var auth_settings_external_cas_custom_label = $( '#auth_settings_cas_custom_label' ).closest( 'tr' );
390 - var auth_settings_external_cas_host = $( '#auth_settings_cas_host' ).closest( 'tr' );
391 - var auth_settings_external_cas_port = $( '#auth_settings_cas_port' ).closest( 'tr' );
392 - var auth_settings_external_cas_path = $( '#auth_settings_cas_path' ).closest( 'tr' );
393 - var auth_settings_external_cas_version = $( '#auth_settings_cas_version' ).closest( 'tr' );
394 - var auth_settings_external_cas_attr_email = $( '#auth_settings_cas_attr_email' ).closest( 'tr' );
395 - var auth_settings_external_cas_attr_first_name = $( '#auth_settings_cas_attr_first_name' ).closest( 'tr' );
396 - var auth_settings_external_cas_attr_last_name = $( '#auth_settings_cas_attr_last_name' ).closest( 'tr' );
397 - var auth_settings_external_cas_attr_update_on_login = $( '#auth_settings_cas_attr_update_on_login' ).closest( 'tr' );
398 - var auth_settings_external_cas_auto_login = $( '#auth_settings_cas_auto_login' ).closest( 'tr' );
399 - var auth_settings_external_cas_link_on_username = $( '#auth_settings_cas_link_on_username' ).closest( 'tr' );
400 - var auth_settings_external_ldap_host = $( '#auth_settings_ldap_host' ).closest( 'tr' );
401 - var auth_settings_external_ldap_port = $( '#auth_settings_ldap_port' ).closest( 'tr' );
402 - var auth_settings_external_ldap_search_base = $( '#auth_settings_ldap_search_base' ).closest( 'tr' );
403 - var auth_settings_external_ldap_uid = $( '#auth_settings_ldap_uid' ).closest( 'tr' );
404 - var auth_settings_external_ldap_attr_email = $( '#auth_settings_ldap_attr_email' ).closest( 'tr' );
405 - var auth_settings_external_ldap_user = $( '#auth_settings_ldap_user' ).closest( 'tr' );
406 - var auth_settings_external_ldap_password = $( '#auth_settings_ldap_password' ).closest( 'tr' );
407 - var auth_settings_external_ldap_tls = $( '#auth_settings_ldap_tls' ).closest( 'tr' );
408 - var auth_settings_external_ldap_lostpassword_url = $( '#auth_settings_ldap_lostpassword_url' ).closest( 'tr' );
409 - var auth_settings_external_ldap_attr_first_name = $( '#auth_settings_ldap_attr_first_name' ).closest( 'tr' );
410 - var auth_settings_external_ldap_attr_last_name = $( '#auth_settings_ldap_attr_last_name' ).closest( 'tr' );
411 - var auth_settings_external_ldap_attr_update_on_login = $( '#auth_settings_ldap_attr_update_on_login' ).closest( 'tr' );
412 - /* eslint-enable */
243 + // Update the options in the database with this change.
244 + update_auth_user( buttons, 'access_users_' + list, user );
413 245
414 - // Wrap the th and td in the rows above so we can animate their heights (can't animate tr heights with jquery)
415 - $( 'th, td', auth_settings_access_role_receive_pending_emails ).wrapInner( '<div class="animated_wrapper" />' );
416 - $( 'th, td', auth_settings_access_pending_redirect_to_message ).wrapInner( '<div class="animated_wrapper" />' );
417 - $( 'th, td', auth_settings_access_blocked_redirect_to_message ).wrapInner( '<div class="animated_wrapper" />' );
418 - $( 'th, td', auth_settings_access_should_email_approved_users ).wrapInner( '<div class="animated_wrapper" />' );
419 - $( 'th, td', auth_settings_access_email_approved_users_subject ).wrapInner( '<div class="animated_wrapper" />' );
420 - $( 'th, td', auth_settings_access_email_approved_users_body ).wrapInner( '<div class="animated_wrapper" />' );
421 - $( 'th, td', auth_settings_access_public_pages ).wrapInner( '<div class="animated_wrapper" />' );
422 - $( 'th, td', auth_settings_access_redirect_to_login ).wrapInner( '<div class="animated_wrapper" />' );
423 - $( 'th, td', auth_settings_access_public_warning ).wrapInner( '<div class="animated_wrapper" />' );
424 - $( 'th, td', auth_settings_access_redirect_to_message ).wrapInner( '<div class="animated_wrapper" />' );
425 - $( 'th, td', auth_settings_external_google_clientid ).wrapInner( '<div class="animated_wrapper" />' );
426 - $( 'th, td', auth_settings_external_google_clientsecret ).wrapInner( '<div class="animated_wrapper" />' );
427 - $( 'th, td', auth_settings_external_google_hosteddomain ).wrapInner( '<div class="animated_wrapper" />' );
428 - $( 'th, td', auth_settings_external_cas_custom_label ).wrapInner( '<div class="animated_wrapper" />' );
429 - $( 'th, td', auth_settings_external_cas_host ).wrapInner( '<div class="animated_wrapper" />' );
430 - $( 'th, td', auth_settings_external_cas_port ).wrapInner( '<div class="animated_wrapper" />' );
431 - $( 'th, td', auth_settings_external_cas_path ).wrapInner( '<div class="animated_wrapper" />' );
432 - $( 'th, td', auth_settings_external_cas_version ).wrapInner( '<div class="animated_wrapper" />' );
433 - $( 'th, td', auth_settings_external_cas_attr_email ).wrapInner( '<div class="animated_wrapper" />' );
434 - $( 'th, td', auth_settings_external_cas_attr_first_name ).wrapInner( '<div class="animated_wrapper" />' );
435 - $( 'th, td', auth_settings_external_cas_attr_last_name ).wrapInner( '<div class="animated_wrapper" />' );
436 - $( 'th, td', auth_settings_external_cas_attr_update_on_login ).wrapInner( '<div class="animated_wrapper" />' );
437 - $( 'th, td', auth_settings_external_cas_auto_login ).wrapInner( '<div class="animated_wrapper" />' );
438 - $( 'th, td', auth_settings_external_cas_link_on_username ).wrapInner( '<div class="animated_wrapper" />' );
439 - $( 'th, td', auth_settings_external_ldap_host ).wrapInner( '<div class="animated_wrapper" />' );
440 - $( 'th, td', auth_settings_external_ldap_port ).wrapInner( '<div class="animated_wrapper" />' );
441 - $( 'th, td', auth_settings_external_ldap_search_base ).wrapInner( '<div class="animated_wrapper" />' );
442 - $( 'th, td', auth_settings_external_ldap_uid ).wrapInner( '<div class="animated_wrapper" />' );
443 - $( 'th, td', auth_settings_external_ldap_attr_email ).wrapInner( '<div class="animated_wrapper" />' );
444 - $( 'th, td', auth_settings_external_ldap_user ).wrapInner( '<div class="animated_wrapper" />' );
445 - $( 'th, td', auth_settings_external_ldap_password ).wrapInner( '<div class="animated_wrapper" />' );
446 - $( 'th, td', auth_settings_external_ldap_tls ).wrapInner( '<div class="animated_wrapper" />' );
447 - $( 'th, td', auth_settings_external_ldap_lostpassword_url ).wrapInner( '<div class="animated_wrapper" />' );
448 - $( 'th, td', auth_settings_external_ldap_attr_first_name ).wrapInner( '<div class="animated_wrapper" />' );
449 - $( 'th, td', auth_settings_external_ldap_attr_last_name ).wrapInner( '<div class="animated_wrapper" />' );
450 - $( 'th, td', auth_settings_external_ldap_attr_update_on_login ).wrapInner( '<div class="animated_wrapper" />' );
246 + // Reset the new user textboxes
247 + if ( email.hasClass( 'new' ) ) {
248 + email.val( '' );
249 + }
451 250
452 - // If we're viewing the dashboard widget, reset a couple of the relevant
453 - // option variables (since they aren't nested in table rows).
454 - if ( $( '#auth_dashboard_widget' ).length ) {
455 - // Remove the helper link, since there are no tabs on the dashboard widget
456 - $( '#dashboard_link_approved_users' ).contents().unwrap();
457 - }
251 + // Re-enable the action buttons now that we're done saving.
252 + $( buttons ).removeAttr( 'disabled' );
458 253
459 - // Hide settings unless "Only approved users" is checked
460 - if ( ! $( '#radio_auth_settings_access_who_can_login_approved_users' ).is( ':checked' ) ) {
461 - animateOption( 'hide_immediately', auth_settings_access_role_receive_pending_emails );
462 - animateOption( 'hide_immediately', auth_settings_access_pending_redirect_to_message );
463 - animateOption( 'hide_immediately', auth_settings_access_blocked_redirect_to_message );
464 - animateOption( 'hide_immediately', auth_settings_access_should_email_approved_users );
465 - }
254 + return true;
255 + }
256 +}
466 257
467 - // Hide Welcome email body/subject options if "Send welcome email" is off.
468 - if ( ! $( '#auth_settings_access_should_email_approved_users' ).is( ':checked' ) ) {
469 - animateOption( 'hide_immediately', auth_settings_access_email_approved_users_subject );
470 - animateOption( 'hide_immediately', auth_settings_access_email_approved_users_body );
471 - }
258 +// Remove user from list (multisite options page).
259 +function auth_multisite_ignore_user( caller, list_name ) {
260 + var is_multisite = true;
472 261
473 - // On load: Show/hide public access options if everyone can see site
474 - if ( ! $( '#radio_auth_settings_access_who_can_view_logged_in_users' ).is( ':checked' ) ) {
475 - animateOption( 'hide_immediately', auth_settings_access_public_pages );
476 - animateOption( 'hide_immediately', auth_settings_access_redirect_to_login );
477 - animateOption( 'hide_immediately', auth_settings_access_public_warning );
478 - animateOption( 'hide_immediately', auth_settings_access_redirect_to_message );
479 - }
262 + // Set default list if not provided.
263 + list_name = typeof list_name !== 'undefined' ? list_name : '';
480 264
481 - // Hide Google options if unchecked
482 - if ( ! $( '#auth_settings_google' ).is( ':checked' ) ) {
483 - animateOption( 'hide_immediately', auth_settings_external_google_clientid );
484 - animateOption( 'hide_immediately', auth_settings_external_google_clientsecret );
485 - animateOption( 'hide_immediately', auth_settings_external_google_hosteddomain );
486 - }
265 + auth_ignore_user( caller, list_name, is_multisite );
266 +}
267 +// Remove user from list.
268 +function auth_ignore_user( caller, list_name, is_multisite ) {
269 + var $ = jQuery;
487 270
488 - // Hide CAS options if unchecked
489 - if ( ! $( '#auth_settings_cas' ).is( ':checked' ) ) {
490 - animateOption( 'hide_immediately', auth_settings_external_cas_custom_label );
491 - animateOption( 'hide_immediately', auth_settings_external_cas_host );
492 - animateOption( 'hide_immediately', auth_settings_external_cas_port );
493 - animateOption( 'hide_immediately', auth_settings_external_cas_path );
494 - animateOption( 'hide_immediately', auth_settings_external_cas_version );
495 - animateOption( 'hide_immediately', auth_settings_external_cas_attr_email );
496 - animateOption( 'hide_immediately', auth_settings_external_cas_attr_first_name );
497 - animateOption( 'hide_immediately', auth_settings_external_cas_attr_last_name );
498 - animateOption( 'hide_immediately', auth_settings_external_cas_attr_update_on_login );
499 - animateOption( 'hide_immediately', auth_settings_external_cas_auto_login );
500 - animateOption( 'hide_immediately', auth_settings_external_cas_link_on_username );
501 - }
271 + // Set default for multisite flag (run different save routine if multisite)
272 + is_multisite = typeof is_multisite !== 'undefined' ? is_multisite : false;
502 273
503 - // Hide LDAP options if unchecked
504 - if ( ! $( '#auth_settings_ldap' ).is( ':checked' ) ) {
505 - animateOption( 'hide_immediately', auth_settings_external_ldap_host );
506 - animateOption( 'hide_immediately', auth_settings_external_ldap_port );
507 - animateOption( 'hide_immediately', auth_settings_external_ldap_search_base );
508 - animateOption( 'hide_immediately', auth_settings_external_ldap_uid );
509 - animateOption( 'hide_immediately', auth_settings_external_ldap_attr_email );
510 - animateOption( 'hide_immediately', auth_settings_external_ldap_user );
511 - animateOption( 'hide_immediately', auth_settings_external_ldap_password );
512 - animateOption( 'hide_immediately', auth_settings_external_ldap_tls );
513 - animateOption( 'hide_immediately', auth_settings_external_ldap_lostpassword_url );
514 - animateOption( 'hide_immediately', auth_settings_external_ldap_attr_first_name );
515 - animateOption( 'hide_immediately', auth_settings_external_ldap_attr_last_name );
516 - animateOption( 'hide_immediately', auth_settings_external_ldap_attr_update_on_login );
517 - }
274 + // Set default list if not provided.
275 + list_name = typeof list_name !== 'undefined' ? list_name : 'approved';
518 276
519 - // Event handler: Hide "Handle unauthorized visitors" option if access is granted to "Everyone"
520 - $( 'input[name="auth_settings[access_who_can_login]"]' ).change( function() {
521 - // Hide settings unless "Only approved users" is checked
522 - var action = $( '#radio_auth_settings_access_who_can_login_approved_users' ).is( ':checked' ) ? 'show' : 'hide';
523 - animateOption( action, auth_settings_access_role_receive_pending_emails );
524 - animateOption( action, auth_settings_access_pending_redirect_to_message );
525 - animateOption( action, auth_settings_access_blocked_redirect_to_message );
526 - animateOption( action, auth_settings_access_should_email_approved_users );
527 - action = action === 'show' && $( '#auth_settings_access_should_email_approved_users' ).is( ':checked' ) ? 'show' : 'hide_immediately';
528 - animateOption( action, auth_settings_access_email_approved_users_subject );
529 - animateOption( action, auth_settings_access_email_approved_users_body );
530 - });
277 + var email = $( caller ).parent().find( '.auth-email' );
531 278
532 - // Event handler: Hide Welcome email body/subject options if "Send welcome email" is off.
533 - $( 'input[name="auth_settings[access_should_email_approved_users]"]' ).change( function() {
534 - var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
535 - animateOption( action, auth_settings_access_email_approved_users_subject );
536 - animateOption( action, auth_settings_access_email_approved_users_body );
537 - });
279 + var user = {
280 + 'email': email.val(),
281 + 'role': '',
282 + 'date_added': '',
283 + 'edit_action': 'remove',
284 + 'multisite_user': is_multisite,
285 + };
538 286
539 - // Event handler: Hide "Handle unauthorized visitors" option if access is granted to "Everyone"
540 - $( 'input[name="auth_settings[access_who_can_view]"]' ).change( function() {
541 - var action = $( '#radio_auth_settings_access_who_can_view_everyone' ).is( ':checked' ) ? 'hide' : 'show';
542 - animateOption( action, auth_settings_access_redirect_to_login );
543 - animateOption( action, auth_settings_access_redirect_to_message );
544 - animateOption( action, auth_settings_access_public_pages );
545 - animateOption( action, auth_settings_access_public_warning );
546 - });
287 + // Show an 'empty list' message if we're deleting the last item
288 + var list = $( caller ).closest( 'ul' );
289 + if ( $( 'li', list ).length <= 1 ) {
290 + $( list ).append( '<li class="auth-empty"><em>No ' + list_name + ' users</em></li>' );
291 + }
547 292
548 - // Event handler: Show/hide Google options based on checkbox
549 - $( 'input[name="auth_settings[google]"]' ).change( function() {
550 - var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
551 - animateOption( action, auth_settings_external_google_clientid );
552 - animateOption( action, auth_settings_external_google_clientsecret );
553 - animateOption( action, auth_settings_external_google_hosteddomain );
554 - });
293 + $( caller ).parent().slideUp( 250, function() {
294 + // Remove the list item.
295 + $( this ).remove();
555 296
556 - // Event handler: Show/hide CAS options based on checkbox
557 - $( 'input[name="auth_settings[cas]"]' ).change( function() {
558 - var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
559 - animateOption( action, auth_settings_external_cas_custom_label );
560 - animateOption( action, auth_settings_external_cas_host );
561 - animateOption( action, auth_settings_external_cas_port );
562 - animateOption( action, auth_settings_external_cas_path );
563 - animateOption( action, auth_settings_external_cas_version );
564 - animateOption( action, auth_settings_external_cas_attr_email );
565 - animateOption( action, auth_settings_external_cas_attr_first_name );
566 - animateOption( action, auth_settings_external_cas_attr_last_name );
567 - animateOption( action, auth_settings_external_cas_attr_update_on_login );
568 - animateOption( action, auth_settings_external_cas_auto_login );
569 - animateOption( action, auth_settings_external_cas_link_on_username );
570 - });
297 + // Update the options in the database with this change.
298 + update_auth_user( caller, 'access_users_' + list_name, user );
299 + });
300 +}
571 301
572 - // Event handler: Show/hide LDAP options based on checkbox
573 - $( 'input[name="auth_settings[ldap]"]' ).change( function() {
574 - var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
575 - animateOption( action, auth_settings_external_ldap_host );
576 - animateOption( action, auth_settings_external_ldap_port );
577 - animateOption( action, auth_settings_external_ldap_search_base );
578 - animateOption( action, auth_settings_external_ldap_uid );
579 - animateOption( action, auth_settings_external_ldap_attr_email );
580 - animateOption( action, auth_settings_external_ldap_user );
581 - animateOption( action, auth_settings_external_ldap_password );
582 - animateOption( action, auth_settings_external_ldap_tls );
583 - animateOption( action, auth_settings_external_ldap_lostpassword_url );
584 - animateOption( action, auth_settings_external_ldap_attr_first_name );
585 - animateOption( action, auth_settings_external_ldap_attr_last_name );
586 - animateOption( action, auth_settings_external_ldap_attr_update_on_login );
587 - });
588 302
589 - // Show save button if usermeta field is modified.
590 - $( 'form input.auth-usermeta' ).on( 'keyup', function( event ) {
591 - // Don't do anything if tab or arrow keys were pressed.
592 - if ( event.which === 9 || event.which === 37 || event.which === 38 || event.which === 39 || event.which === 40 ) {
593 - return;
594 - }
595 - $( this ).siblings( '.button' ).css( 'display', 'inline-block' );
596 - });
303 +// Make changes to one of the user lists (pending, approved, blocked) via ajax.
304 +function update_auth_user( caller, setting, user_to_edit ) {
305 + var $ = jQuery,
306 + access_users_pending = [],
307 + access_users_approved = [],
308 + access_users_blocked = [],
309 + nonce_save_auth_settings = $( '#nonce_save_auth_settings' ).val();
597 310
598 - // List management function: pressing enter in the new approved or new
599 - // blocked user (email or role field) adds the user to the list.
600 - $( '#new_approved_user_email, #new_approved_user_role, #new_blocked_user_email' ).on( 'keyup', function( event ) {
601 - // For textareas, make Enter add the user; for inputs, make enter add the user.
602 - if ( $( this ).is( 'textarea' ) ) {
603 - // Enter key adds a newline; Enter key with Ctrl, Alt, Shift, or Meta adds the user.
604 - if ( event.which === 13 && ( event.ctrlKey || event.altKey || event.metaKey ) ) {
605 - $( this ).parent().find( 'a.button-add-user' ).trigger( 'click' );
606 - event.preventDefault();
607 - }
608 - } else if ( event.which === 13 ) { // Enter key on input[type="text"]
609 - $( this ).parent().find( 'a.button-add-user' ).trigger( 'click' );
610 - event.preventDefault();
611 - }
612 - });
311 + // Defaults:
312 + // setting = 'access_users_pending' or 'access_users_approved' or 'access_users_blocked',
313 + // user_to_edit = {
314 + // email: 'johndoe@example.com',
315 + // role: 'subscriber',
316 + // date_added: 'Jun 2014',
317 + // edit_action: 'add' or 'remove' or 'change_role',
318 + // local_user: true or false,
319 + // multisite_user: true or false,
320 + // }
321 + setting = typeof setting !== 'undefined' ? setting : 'none';
322 + user_to_edit = typeof user_to_edit !== 'undefined' ? user_to_edit : {};
613 323
614 - // Don't submit form (i.e., save options) when hitting enter in any user list field.
615 - $( 'input.auth-email, select.auth-role, input.auth-date-added, input.auth-usermeta' ).on( 'keydown', function( event ) {
616 - if ( event.which === 13 ) { // Enter key
617 - event.preventDefault();
618 - return false;
619 - }
620 - });
324 + // Enable wait cursor.
325 + $( 'html' ).addClass( 'busy' );
621 326
622 - // Enable the user-friendly multiselect form element on the options page.
623 - $( '#auth_settings_access_public_pages' ).multiSelect({
624 - selectableOptgroup: true,
625 - selectableHeader: '<div class="custom-header">' + authL10n.private_pages + '</div>',
626 - selectionHeader: '<div class="custom-header">' + authL10n.public_pages + '</div>',
627 - });
327 + // Enable spinner by element that triggered this event (caller).
328 + $( caller ).attr( 'disabled', 'disabled' );
329 + if ( $( caller ).val() === 'Save Changes' ) {
330 + $( caller ).last().after( '<span class="spinner is-active"></span>' );
331 + } else if ( $( caller ).hasClass( 'auth-role' ) ) {
332 + $( caller ).last().next().next().after( '<span class="spinner is-active"></span>' );
333 + } else {
334 + $( caller ).last().next().after( '<span class="spinner is-active"></span>' );
335 + }
336 + $( 'form .spinner' ).show();
628 337
629 - // Switch to the first tab (or the tab indicated in sessionStorage, or the querystring).
630 - var tab = '';
631 - if ( getQuerystringValuesByKey( 'tab' ).length > 0 ) {
632 - tab = getQuerystringValuesByKey( 'tab' )[0];
633 - } else if ( sessionStorage.getItem( 'tab' ) ) {
634 - tab = sessionStorage.getItem( 'tab' );
635 - }
636 - if ( $.inArray( tab, [ 'access_lists', 'access_login', 'access_public', 'external', 'advanced' ] ) < 0 ) {
637 - tab = 'access_lists';
638 - }
639 - window.chooseTab( tab, animationSpeed );
338 + // Grab the value of the setting we are saving.
339 + if ( setting === 'access_users_pending' ) {
340 + access_users_pending.push( user_to_edit );
341 + } else if ( setting === 'access_users_approved' ) {
342 + access_users_approved.push( user_to_edit );
343 + } else if ( setting === 'access_users_blocked' ) {
344 + access_users_blocked.push( user_to_edit );
345 + }
640 346
641 - // Hide/show multisite settings based on override checkbox.
642 - $( 'input[name="auth_settings[multisite_override]"]' ).change( function() {
643 - hideMultisiteSettingsIfDisabled();
347 + $.post( ajaxurl, {
348 + 'action': 'update_auth_user',
349 + 'setting': setting,
350 + 'access_users_pending': access_users_pending,
351 + 'access_users_approved': access_users_approved,
352 + 'access_users_blocked': access_users_blocked,
353 + 'nonce_save_auth_settings': nonce_save_auth_settings,
354 + }, function( response ) {
355 + // Server responded, but if response isn't 'success' it failed to save.
356 + var succeeded = response === 'success';
357 + var spinner_text = succeeded ? 'Saved.' : '<span style="color: red;">Failed.</span>';
358 + var spinner_wait = succeeded ? 500 : 2000;
359 + $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinner_text + '</span>' ).delay( spinner_wait ).hide( animation_speed, function() {
360 + $( this ).remove();
644 361 });
645 - hideMultisiteSettingsIfDisabled();
362 + $( caller ).removeAttr( 'disabled' );
646 363
647 - // Wire up pager events on Approved User list (first/last/next/previous
648 - // buttons, go to page text input, and search.
649 - $( '#current-page-selector, #user-search-input' ).on( 'keydown', function( event ) {
650 - if ( event.which === 13 ) { // Enter key
651 - var searchTerm = $( '#user-search-input' ).val();
652 - var currentPage = parseInt( $( this ).val(), 10 ) || 1;
653 - var totalPages = parseInt( $( '.total-pages' ).first().text().replace( /[^0-9]/g, '' ), 10 ) || 1;
654 -
655 - // Make sure current page is between 1 and max pages.
656 - if ( currentPage < 1 ) {
657 - currentPage = 1;
658 - } else if ( currentPage > totalPages ) {
659 - currentPage = totalPages;
660 - }
661 -
662 - // Update user list with users on next page.
663 - refreshApprovedUserList( currentPage, searchTerm );
664 -
665 - // Prevent default behavior.
666 - event.preventDefault();
667 - return false;
668 - }
364 + // Disable wait cursor.
365 + $( 'html' ).removeClass( 'busy' );
366 + }).fail( function() {
367 + // Fail fires if the server doesn't respond or responds with 500 codes
368 + var succeeded = false;
369 + var spinner_text = succeeded ? 'Saved.' : '<span style="color: red;">Failed.</span>';
370 + var spinner_wait = succeeded ? 500 : 2000;
371 + $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinner_text + '</span>' ).delay( spinner_wait ).hide( animation_speed, function() {
372 + $( this ).remove();
669 373 });
374 + $( caller ).removeAttr( 'disabled' );
670 375
671 - $( '.tablenav' ).on( 'click', '.pagination-links a, #search-submit', function( event ) {
672 - var searchTerm = $( '#user-search-input' ).val();
673 - var currentPage = parseInt( getParameterByName( 'paged', $( this ).attr( 'href' ) ), 10 ) || 1;
674 - var totalPages = parseInt( $( '.total-pages' ).first().text().replace( /[^0-9]/g, '' ), 10 ) || 1;
675 - if ( currentPage > totalPages ) {
676 - currentPage = totalPages;
677 - }
376 + // Disable wait cursor.
377 + $( 'html' ).removeClass( 'busy' );
378 + });
379 +}
678 380
679 - // Update user list with users on next page.
680 - refreshApprovedUserList( currentPage, searchTerm );
681 381
682 - // Remove focus from clicked element.
683 - $( this ).blur();
382 +// Multisite functions
383 +function save_auth_multisite_settings( caller ) {
384 + var $ = jQuery;
684 385
685 - // Prevent default behavior.
686 - event.preventDefault();
687 - return false;
688 - });
386 + // Enable wait cursor.
387 + $( 'html' ).addClass( 'busy' );
689 388
690 - // Enable growable textarea for new user field.
691 - $( 'textarea#new_approved_user_email' ).autogrow();
389 + $( caller ).attr( 'disabled', 'disabled' );
390 + if ( $( caller ).val() === 'Save Changes' ) {
391 + $( caller ).last().after( '<span class="spinner is-active"></span>' );
392 + } else if ( $( caller ).hasClass( 'auth-role' ) ) {
393 + $( caller ).last().next().next().after( '<span class="spinner is-active"></span>' );
394 + } else {
395 + $( caller ).last().next().after( '<span class="spinner is-active"></span>' );
396 + }
397 + $( 'form .spinner' ).show();
692 398
693 - // Enable growable textarea for config fields.
694 - $( 'textarea#auth_settings_ldap_host' ).autogrow();
695 - $( 'textarea#auth_settings_ldap_search_base' ).autogrow();
696 - $( 'textarea#auth_settings_google_hosteddomain' ).autogrow();
399 + // Get form elements to save
697 400
698 - });
401 + var nonce_save_auth_settings = $( '#nonce_save_auth_settings' ).val();
699 402
403 + var multisite_override = $( '#auth_settings_multisite_override' ).is( ':checked' ) ? '1' : '';
700 404
701 - /**
702 - * Globals.
703 - */
405 + var access_who_can_login = $( 'form input[name="auth_settings[access_who_can_login]"]:checked' ).val();
704 406
407 + var access_who_can_view = $( 'form input[name="auth_settings[access_who_can_view]"]:checked' ).val();
705 408
706 - // Switch between option tabs.
707 - window.chooseTab = function( listName, delay ) {
708 - // default delay is 0
709 - delay = 'undefined' !== typeof delay ? delay : 0;
409 + var access_users_approved = {};
410 + $( '#list_auth_settings_access_users_approved li' ).each( function( index ) {
411 + var user = {};
412 + user.email = $( '.auth-email', this ).val();
413 + user.role = $( '.auth-role', this ).val();
414 + user.date_added = $( '.auth-date-added', this ).val();
415 + user.local_user = $( '.auth-local-user', this ).length !== 0;
416 + access_users_approved[index] = user;
417 + });
710 418
711 - // default to the access list tab
712 - listName = 'undefined' !== typeof listName ? listName : 'access_lists';
419 + var access_default_role = $( '#auth_settings_access_default_role' ).val();
713 420
714 - // Hide all tab content, then show selected tab content
715 - $( 'div.section_info, div.section_info + table' ).hide();
716 - $( '#section_info_' + listName + ', #section_info_' + listName + ' + table' ).show();
421 + var google = $( '#auth_settings_google' ).is( ':checked' ) ? '1' : '';
422 + var google_clientid = $( '#auth_settings_google_clientid' ).val();
423 + var google_clientsecret = $( '#auth_settings_google_clientsecret' ).val();
717 424
718 - // Set active tab
719 - $( '.nav-tab-wrapper a' ).removeClass( 'nav-tab-active' );
720 - $( 'a.nav-tab-' + listName ).addClass( 'nav-tab-active' );
425 + var cas = $( '#auth_settings_cas' ).is( ':checked' ) ? '1' : '';
426 + var cas_custom_label = $( '#auth_settings_cas_custom_label' ).val();
427 + var cas_host = $( '#auth_settings_cas_host' ).val();
428 + var cas_port = $( '#auth_settings_cas_port' ).val();
429 + var cas_path = $( '#auth_settings_cas_path' ).val();
430 + var cas_attr_email = $( '#auth_settings_cas_attr_email' ).val();
431 + var cas_attr_first_name = $( '#auth_settings_cas_attr_first_name' ).val();
432 + var cas_attr_last_name = $( '#auth_settings_cas_attr_last_name' ).val();
433 + var cas_attr_update_on_login = $( '#auth_settings_cas_attr_update_on_login' ).is( ':checked' ) ? '1' : '';
721 434
722 - // Hide site options if they are overridden by a multisite setting.
723 - setTimeout( window.hideMultisiteOverriddenOptions, delay );
435 + var ldap = $( '#auth_settings_ldap' ).is( ':checked' ) ? '1' : '';
436 + var ldap_host = $( '#auth_settings_ldap_host' ).val();
437 + var ldap_port = $( '#auth_settings_ldap_port' ).val();
438 + var ldap_search_base = $( '#auth_settings_ldap_search_base' ).val();
439 + var ldap_uid = $( '#auth_settings_ldap_uid' ).val();
440 + var ldap_attr_email = $( '#auth_settings_ldap_attr_email' ).val();
441 + var ldap_user = $( '#auth_settings_ldap_user' ).val();
442 + var ldap_password = $( '#auth_settings_ldap_password' ).val();
443 + var ldap_tls = $( '#auth_settings_ldap_tls' ).is( ':checked' ) ? '1' : '';
444 + var ldap_lostpassword_url = $( '#auth_settings_ldap_lostpassword_url' ).val();
445 + var ldap_attr_first_name = $( '#auth_settings_ldap_attr_first_name' ).val();
446 + var ldap_attr_last_name = $( '#auth_settings_ldap_attr_last_name' ).val();
447 + var ldap_attr_update_on_login = $( '#auth_settings_ldap_attr_update_on_login' ).is( ':checked' ) ? '1' : '';
724 448
725 - // Hide Save Changes button if we're on the access lists page (changing
726 - // access lists saves automatically via AJAX).
727 - $( 'body:not(.network-admin) #submit' ).toggle( 'access_lists' !== listName );
728 -
729 - // Save user's active tab to sessionStorage (so we can restore it on reload).
730 - // Note: session storage persists until the browser tab is closed.
731 - sessionStorage.setItem( 'tab', listName );
732 -
733 - // Check whether to fade logo.
734 - fadeLogo();
449 + var advanced_lockouts = {
450 + 'attempts_1': $( '#auth_settings_advanced_lockouts_attempts_1' ).val(),
451 + 'duration_1': $( '#auth_settings_advanced_lockouts_duration_1' ).val(),
452 + 'attempts_2': $( '#auth_settings_advanced_lockouts_attempts_2' ).val(),
453 + 'duration_2': $( '#auth_settings_advanced_lockouts_duration_2' ).val(),
454 + 'reset_duration': $( '#auth_settings_advanced_lockouts_reset_duration' ).val()
735 455 };
456 + var advanced_hide_wp_login = $( '#auth_settings_advanced_hide_wp_login' ).is( ':checked' ) ? '1' : '';
736 457
737 - // Hide (with overlay) site options if overridden by a multisite option.
738 - window.hideMultisiteOverriddenOptions = function() {
739 - $( '.auth_multisite_override_overlay' ).each( function() {
740 - // Option to hide is stored in the overlay's id with 'overlay-hide-' prefix.
741 - var optionContainerToHide = $( this ).closest( 'tr' );
742 - if ( optionContainerToHide.length > 0 ) {
743 - $( this ).css({
744 - 'background-color': '#f1f1f1',
745 - 'z-index': 1,
746 - opacity: 0.8,
747 - position: 'absolute',
748 - top: optionContainerToHide.position().top,
749 - left: optionContainerToHide.position().left,
750 - width: '100%',
751 - height: optionContainerToHide.height(),
752 - });
753 - $( this ).show();
754 - }
458 + $.post( ajaxurl, {
459 + action: 'save_auth_multisite_settings',
460 + 'nonce_save_auth_settings': nonce_save_auth_settings,
461 + 'multisite_override': multisite_override,
462 + 'access_who_can_login': access_who_can_login,
463 + 'access_who_can_view': access_who_can_view,
464 + 'access_users_approved': access_users_approved,
465 + 'access_default_role': access_default_role,
466 + 'google': google,
467 + 'google_clientid': google_clientid,
468 + 'google_clientsecret': google_clientsecret,
469 + 'cas': cas,
470 + 'cas_custom_label': cas_custom_label,
471 + 'cas_host': cas_host,
472 + 'cas_port': cas_port,
473 + 'cas_path': cas_path,
474 + 'cas_attr_email': cas_attr_email,
475 + 'cas_attr_first_name': cas_attr_first_name,
476 + 'cas_attr_last_name': cas_attr_last_name,
477 + 'cas_attr_update_on_login': cas_attr_update_on_login,
478 + 'ldap': ldap,
479 + 'ldap_host': ldap_host,
480 + 'ldap_port': ldap_port,
481 + 'ldap_search_base': ldap_search_base,
482 + 'ldap_uid': ldap_uid,
483 + 'ldap_attr_email': ldap_attr_email,
484 + 'ldap_user': ldap_user,
485 + 'ldap_password': ldap_password,
486 + 'ldap_tls': ldap_tls,
487 + 'ldap_lostpassword_url': ldap_lostpassword_url,
488 + 'ldap_attr_first_name': ldap_attr_first_name,
489 + 'ldap_attr_last_name': ldap_attr_last_name,
490 + 'ldap_attr_update_on_login': ldap_attr_update_on_login,
491 + 'advanced_lockouts': advanced_lockouts,
492 + 'advanced_hide_wp_login': advanced_hide_wp_login,
493 + }, function( response ) {
494 + var succeeded = response === 'success';
495 + var spinner_text = succeeded ? 'Saved.' : '<span style="color: red;">Failed.</span>';
496 + var spinner_wait = succeeded ? 500 : 2000;
497 + $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinner_text + '</span>' ).delay( spinner_wait ).hide( animation_speed, function() {
498 + $( this ).remove();
755 499 });
756 - };
500 + $( caller ).removeAttr( 'disabled' );
757 501
758 - // Update user's usermeta field.
759 - // @calls php wp_ajax_update_auth_usermeta.
760 - window.authUpdateUsermeta = function( caller ) {
761 - var $caller = $( caller ),
762 - $usermeta = $caller.parent().children( '.auth-usermeta' ),
763 - email = $caller.siblings( '.auth-email' ).val(),
764 - usermeta = $usermeta.val(),
765 - nonce = $( '#nonce_save_auth_settings' ).val();
766 -
767 - // Remove reference to caller if it's the usermeta field itself (not a button triggering the save).
768 - if ( $caller.hasClass( 'auth-usermeta' ) ) {
769 - $caller = $();
770 - }
771 -
772 - // Disable inputs, show spinner.
773 - $caller.attr( 'disabled', 'disabled' );
774 - $usermeta.attr( 'disabled', 'disabled' );
775 - var $row = $usermeta.closest( 'li' );
776 - var $spinner = $( '<span class="spinner is-active"></span>' ).css({
777 - position: 'absolute',
778 - top: $row.position().top,
779 - left: $row.position().left + $row.width(),
502 + // Disable wait cursor.
503 + $( 'html' ).removeClass( 'busy' );
504 + }).fail( function() {
505 + // Fail fires if the server doesn't respond
506 + var succeeded = false;
507 + var spinner_text = succeeded ? 'Saved.' : '<span style="color: red;">Failed.</span>';
508 + var spinner_wait = succeeded ? 500 : 2000;
509 + $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinner_text + '</span>' ).delay( spinner_wait ).hide( animation_speed, function() {
510 + $( this ).remove();
780 511 });
781 - $usermeta.after( $spinner );
782 - $( 'html' ).addClass( 'busy' );
512 + $( caller ).removeAttr( 'disabled' );
783 513
784 - // Call ajax save function.
785 - $.post( ajaxurl, {
786 - action: 'update_auth_usermeta',
787 - email: email,
788 - usermeta: usermeta,
789 - nonce: nonce,
790 - }, function( response ) {
791 - var succeeded = response === 'success';
792 - var spinnerText = succeeded ? authL10n.saved + '.' : '<span class="attention">' + authL10n.failed + '.</span>';
793 - var spinnerWait = succeeded ? 500 : 2000;
514 + // Disable wait cursor.
515 + $( 'html' ).removeClass( 'busy' );
516 + });
517 +}
794 518
795 - // Enable inputs, remove spinner.
796 - $caller.removeAttr( 'disabled' );
797 - $usermeta.removeAttr( 'disabled' );
798 - $caller.css( 'display', 'none' );
799 - $( 'form .spinner:not(:has(.spinner-text))' ).animate( { width: '60px' }, 'fast' ).append( '<span class="spinner-text">' + spinnerText + '</span>' ).delay( spinnerWait ).hide( animationSpeed, function() {
800 - $( this ).remove();
801 - });
802 - $( 'html' ).removeClass( 'busy' );
519 +// Hide or show (with overlay) the multisite settings based on the "multisite override" setting.
520 +function hide_multisite_settings_if_disabled() {
521 + var $ = jQuery;
803 522
804 - }).fail( function() {
805 - var succeeded = false;
806 - var spinnerText = succeeded ? authL10n.saved + '.' : '<span class="attention">' + authL10n.failed + '.</span>';
807 - var spinnerWait = succeeded ? 500 : 2000;
523 + if ( $( '#auth_settings_multisite_override' ).length === 0 )
524 + return;
808 525
809 - // Enable inputs, remove spinner.
810 - $caller.removeAttr( 'disabled' );
811 - $usermeta.removeAttr( 'disabled' );
812 - $caller.css( 'display', 'none' );
813 - $( 'form .spinner:not(:has(.spinner-text))' ).animate( { width: '60px' }, 'fast' ).append( '<span class="spinner-text">' + spinnerText + '</span>' ).delay( spinnerWait ).hide( animationSpeed, function() {
814 - $( this ).remove();
815 - });
816 - $( 'html' ).removeClass( 'busy' );
526 + var settings = $( '#auth_multisite_settings' );
527 + var overlay = $( '#auth_multisite_settings_disabled_overlay' );
817 528
529 + if ( $( '#auth_settings_multisite_override' ).is( ':checked' ) ) {
530 + overlay.hide( animation_speed );
531 + } else {
532 + overlay.css({
533 + 'background-color': '#f1f1f1',
534 + 'z-index': 1,
535 + 'opacity': 0.8,
536 + 'position': 'absolute',
537 + 'top': settings.position().top,
538 + 'left': settings.position().left - 20,
539 + 'width': '100%',
540 + 'height': settings.height() + 35,
541 + 'padding-left': 20,
818 542 });
819 - };
543 + overlay.show();
544 + }
545 +}
820 546
821 - // Update user's role.
822 - window.authChangeRole = function( caller, isMultisite ) {
823 - // Set default for multisite flag (run different save routine if multisite)
824 - isMultisite = typeof isMultisite !== 'undefined' ? isMultisite : false;
547 +// Hide (with overlay) site options if overridden by a multisite option.
548 +function hide_multisite_overridden_options() {
549 + var $ = jQuery;
825 550
826 - var email = $( caller ).parent().find( '.auth-email' );
827 - var role = $( caller ).parent().find( '.auth-role' );
828 - var dateAdded = $( caller ).parent().find( '.auth-date-added' );
551 + $( '.auth_multisite_override_overlay' ).each( function() {
552 + // Option to hide is stored in the overlay's id with 'overlay-hide-' prefix.
553 + var option_container_to_hide = $( this ).closest( 'tr' );
554 + if ( option_container_to_hide.length > 0 ) {
555 + $( this ).css({
556 + 'background-color': '#f1f1f1',
557 + 'z-index': 1,
558 + 'opacity': 0.8,
559 + 'position': 'absolute',
560 + 'top': option_container_to_hide.position().top,
561 + 'left': option_container_to_hide.position().left,
562 + 'width': '100%',
563 + 'height': option_container_to_hide.height(),
564 + });
565 + $( this ).show();
566 + }
567 + });
568 +}
829 569
830 - var user = {
831 - email: email.val(),
832 - role: role.val(),
833 - date_added: dateAdded.val(), // eslint-disable-line camelcase
834 - edit_action: 'change_role', // eslint-disable-line camelcase
835 - multisite_user: isMultisite, // eslint-disable-line camelcase
836 - };
837 570
838 - // Update the options in the database with this change.
839 - updateAuthUser( caller, 'access_users_approved', user );
840 571
841 - return true;
842 - };
572 +// Helper function to hide/show wordpress option
573 +function animate_option( action, option ) {
574 + var $ = jQuery;
575 + if ( action === 'show' ) {
576 + $( 'div.animated_wrapper', option ).slideDown( animation_speed );
577 + $( 'th', option ).animate({ padding: '20px 10px 20px 0' }, { duration: animation_speed });
578 + $( 'td', option ).animate({ padding: '15px 10px' }, { duration: animation_speed });
579 + } else if ( action === 'hide' ) {
580 + $( 'div.animated_wrapper', option ).slideUp( animation_speed );
581 + $( 'td, th', option ).animate({ padding: '0px' }, { duration: animation_speed });
582 + } else if ( action === 'hide_immediately' ) {
583 + $( 'div.animated_wrapper', option ).hide();
584 + $( 'td, th', option ).css({ padding: '0px' });
585 + }
586 +}
843 587
844 - // Update user's role (multisite options page).
845 - window.authMultisiteChangeRole = function( caller ) {
846 - var isMultisite = true;
847 - window.authChangeRole( caller, isMultisite );
848 - };
588 +// Helper function to grab a querystring param value by name
589 +function getParameterByName( needle, haystack ) {
590 + needle = needle.replace( /[\[]/, "\\\[").replace(/[\]]/, "\\\]" );
591 + var regex = new RegExp( "[\\?&]" + needle + "=([^&#]*)" );
592 + var results = regex.exec( haystack );
593 + if( results === null )
594 + return '';
595 + else
596 + return decodeURIComponent( results[1].replace( /\+/g, " " ) );
597 +}
849 598
850 - // Add user to list (list = blocked or approved).
851 - window.authAddUser = function( caller, list, shouldCreateLocalAccount, isMultisite ) {
852 - // Skip email address validation if adding from pending list (not user-editable).
853 - var skipValidation = $( caller ).parent().parent().attr( 'id' ) === 'list_auth_settings_access_users_pending';
599 +// Helper function to generate a random string
600 +function getRandomId() {
601 + var text = "";
602 + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
603 + for ( var i=0; i < 5; i++ )
604 + text += possible.charAt( Math.floor( Math.random() * possible.length ) );
605 + return text;
606 +}
854 607
855 - // Skip email address validation if we're banning an existing user (since they're already in a list).
856 - var blockingNewUser = $( caller ).attr( 'id' ).indexOf( 'block_user_new' ) > -1;
857 - skipValidation = skipValidation || ( $( caller ).attr( 'id' ).indexOf( 'block_user' ) > -1 && ! blockingNewUser );
608 +// Helper function to return a short date (e.g., Jul 2013) for today's date
609 +function getShortDate( date ) {
610 + date = typeof date !== 'undefined' ? date : new Date();
611 + var month = '';
612 + switch ( date.getMonth() ) {
613 + case 0: month = 'Jan'; break;
614 + case 1: month = 'Feb'; break;
615 + case 2: month = 'Mar'; break;
616 + case 3: month = 'Apr'; break;
617 + case 4: month = 'May'; break;
618 + case 5: month = 'Jun'; break;
619 + case 6: month = 'Jul'; break;
620 + case 7: month = 'Aug'; break;
621 + case 8: month = 'Sep'; break;
622 + case 9: month = 'Oct'; break;
623 + case 10: month = 'Nov'; break;
624 + case 11: month = 'Dec'; break;
625 + }
626 + return month + ' ' + date.getFullYear();
627 +}
858 628
859 - // Set default for multisite flag (run different save routine if multisite)
860 - isMultisite = 'undefined' !== typeof isMultisite ? isMultisite : false;
629 +// Helper function to grab a querystring value
630 +function querystring( key ) {
631 + var re = new RegExp( '(?:\\?|&)'+key+'=(.*?)(?=&|$)', 'gi' );
632 + var r = [], m;
633 + while ( ( m = re.exec( document.location.search ) ) !== null )
634 + r.push( m[1] );
635 + return r;
636 +}
861 637
862 - // Default to the approved list.
863 - list = 'undefined' !== typeof list ? list : 'approved';
638 +// Helper function to check if an email address is valid.
639 +function valid_email( email ) {
640 + var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
641 + return re.test( email );
642 +}
864 643
865 - // Default to not creating a local account.
866 - shouldCreateLocalAccount = 'undefined' !== typeof shouldCreateLocalAccount ? shouldCreateLocalAccount : false;
867 644
868 - var email = $( caller ).parent().find( '.auth-email' );
869 - var role = $( caller ).parent().find( '.auth-role' );
870 - var dateAdded = $( caller ).parent().find( '.auth-date-added' );
645 +jQuery( document ).ready( function( $ ) {
646 + // Grab references to form elements that we will show/hide on page load
647 + var auth_settings_access_users_pending = $( '#list_auth_settings_access_users_pending' ).closest( 'tr' );
648 + var auth_settings_access_users_approved = $( '#list_auth_settings_access_users_approved' ).closest( 'tr' );
649 + var auth_settings_access_users_blocked = $( '#list_auth_settings_access_users_blocked' ).closest( 'tr' );
650 + var auth_settings_access_role_receive_pending_emails = $( '#auth_settings_access_role_receive_pending_emails' ).closest( 'tr' );
651 + var auth_settings_access_pending_redirect_to_message = $( '#wp-auth_settings_access_pending_redirect_to_message-wrap' ).closest( 'tr' );
652 + var auth_settings_access_blocked_redirect_to_message = $( '#wp-auth_settings_access_blocked_redirect_to_message-wrap' ).closest( 'tr' );
653 + var auth_settings_access_should_email_approved_users = $( '#auth_settings_access_should_email_approved_users' ).closest( 'tr' );
654 + var auth_settings_access_email_approved_users_subject = $( '#auth_settings_access_email_approved_users_subject' ).closest( 'tr' );
655 + var auth_settings_access_email_approved_users_body = $( '#wp-auth_settings_access_email_approved_users_body-wrap' ).closest( 'tr' );
656 + var auth_settings_access_public_pages = $( '#auth_settings_access_public_pages' ).closest( 'tr' );
657 + var auth_settings_access_redirect_to_login = $( '#radio_auth_settings_access_redirect_to_login' ).closest( 'tr' );
658 + var auth_settings_access_public_warning = $( '#radio_auth_settings_access_public_warning' ).closest( 'tr' );
659 + var auth_settings_access_redirect_to_message = $( '#wp-auth_settings_access_redirect_to_message-wrap' ).closest( 'tr' );
660 + var auth_settings_external_settings_table = $( '#auth_settings_google' ).closest( 'table' );
661 + var auth_settings_external_google = $( '#auth_settings_google' ).closest( 'tr' );
662 + var auth_settings_external_google_clientid = $( '#auth_settings_google_clientid' ).closest( 'tr' );
663 + var auth_settings_external_google_clientsecret = $( '#auth_settings_google_clientsecret' ).closest( 'tr' );
664 + var auth_settings_external_cas = $( '#auth_settings_cas' ).closest( 'tr' );
665 + var auth_settings_external_cas_custom_label = $( '#auth_settings_cas_custom_label' ).closest( 'tr' );
666 + var auth_settings_external_cas_host = $( '#auth_settings_cas_host' ).closest( 'tr' );
667 + var auth_settings_external_cas_port = $( '#auth_settings_cas_port' ).closest( 'tr' );
668 + var auth_settings_external_cas_path = $( '#auth_settings_cas_path' ).closest( 'tr' );
669 + var auth_settings_external_cas_attr_email = $( '#auth_settings_cas_attr_email' ).closest( 'tr' );
670 + var auth_settings_external_cas_attr_first_name = $( '#auth_settings_cas_attr_first_name' ).closest( 'tr' );
671 + var auth_settings_external_cas_attr_last_name = $( '#auth_settings_cas_attr_last_name' ).closest( 'tr' );
672 + var auth_settings_external_cas_attr_update_on_login = $( '#auth_settings_cas_attr_update_on_login' ).closest( 'tr' );
673 + var auth_settings_external_ldap = $( '#auth_settings_ldap' ).closest( 'tr' );
674 + var auth_settings_external_ldap_host = $( '#auth_settings_ldap_host' ).closest( 'tr' );
675 + var auth_settings_external_ldap_port = $( '#auth_settings_ldap_port' ).closest( 'tr' );
676 + var auth_settings_external_ldap_search_base = $( '#auth_settings_ldap_search_base' ).closest( 'tr' );
677 + var auth_settings_external_ldap_uid = $( '#auth_settings_ldap_uid' ).closest( 'tr' );
678 + var auth_settings_external_ldap_attr_email = $( '#auth_settings_ldap_attr_email' ).closest( 'tr' );
679 + var auth_settings_external_ldap_user = $( '#auth_settings_ldap_user' ).closest( 'tr' );
680 + var auth_settings_external_ldap_password = $( '#auth_settings_ldap_password' ).closest( 'tr' );
681 + var auth_settings_external_ldap_tls = $( '#auth_settings_ldap_tls' ).closest( 'tr' );
682 + var auth_settings_external_ldap_lostpassword_url = $( '#auth_settings_ldap_lostpassword_url' ).closest( 'tr' );
683 + var auth_settings_external_ldap_attr_first_name = $( '#auth_settings_ldap_attr_first_name' ).closest( 'tr' );
684 + var auth_settings_external_ldap_attr_last_name = $( '#auth_settings_ldap_attr_last_name' ).closest( 'tr' );
685 + var auth_settings_external_ldap_attr_update_on_login = $( '#auth_settings_ldap_attr_update_on_login' ).closest( 'tr' );
871 686
872 - // Helper variable for disabling buttons while processing. This will be
873 - // set differently if our clicked button is nested in a div (below).
874 - var buttons = caller;
687 + // Wrap the th and td in the rows above so we can animate their heights (can't animate tr heights with jquery)
688 + $( 'th, td', auth_settings_access_users_pending ).wrapInner( '<div class="animated_wrapper" />' );
689 + $( 'th, td', auth_settings_access_users_approved ).wrapInner( '<div class="animated_wrapper" />' );
690 + $( 'th, td', auth_settings_access_users_blocked ).wrapInner( '<div class="animated_wrapper" />' );
691 + $( 'th, td', auth_settings_access_role_receive_pending_emails ).wrapInner( '<div class="animated_wrapper" />' );
692 + $( 'th, td', auth_settings_access_pending_redirect_to_message ).wrapInner( '<div class="animated_wrapper" />' );
693 + $( 'th, td', auth_settings_access_blocked_redirect_to_message ).wrapInner( '<div class="animated_wrapper" />' );
694 + $( 'th, td', auth_settings_access_should_email_approved_users ).wrapInner( '<div class="animated_wrapper" />' );
695 + $( 'th, td', auth_settings_access_email_approved_users_subject ).wrapInner( '<div class="animated_wrapper" />' );
696 + $( 'th, td', auth_settings_access_email_approved_users_body ).wrapInner( '<div class="animated_wrapper" />' );
697 + $( 'th, td', auth_settings_access_public_pages ).wrapInner( '<div class="animated_wrapper" />' );
698 + $( 'th, td', auth_settings_access_redirect_to_login ).wrapInner( '<div class="animated_wrapper" />' );
699 + $( 'th, td', auth_settings_access_public_warning ).wrapInner( '<div class="animated_wrapper" />' );
700 + $( 'th, td', auth_settings_access_redirect_to_message ).wrapInner( '<div class="animated_wrapper" />' );
701 + $( 'th, td', auth_settings_external_google_clientid ).wrapInner( '<div class="animated_wrapper" />' );
702 + $( 'th, td', auth_settings_external_google_clientsecret ).wrapInner( '<div class="animated_wrapper" />' );
703 + $( 'th, td', auth_settings_external_cas_custom_label ).wrapInner( '<div class="animated_wrapper" />' );
704 + $( 'th, td', auth_settings_external_cas_host ).wrapInner( '<div class="animated_wrapper" />' );
705 + $( 'th, td', auth_settings_external_cas_port ).wrapInner( '<div class="animated_wrapper" />' );
706 + $( 'th, td', auth_settings_external_cas_path ).wrapInner( '<div class="animated_wrapper" />' );
707 + $( 'th, td', auth_settings_external_cas_attr_email ).wrapInner( '<div class="animated_wrapper" />' );
708 + $( 'th, td', auth_settings_external_cas_attr_first_name ).wrapInner( '<div class="animated_wrapper" />' );
709 + $( 'th, td', auth_settings_external_cas_attr_last_name ).wrapInner( '<div class="animated_wrapper" />' );
710 + $( 'th, td', auth_settings_external_cas_attr_update_on_login ).wrapInner( '<div class="animated_wrapper" />' );
711 + $( 'th, td', auth_settings_external_ldap_host ).wrapInner( '<div class="animated_wrapper" />' );
712 + $( 'th, td', auth_settings_external_ldap_port ).wrapInner( '<div class="animated_wrapper" />' );
713 + $( 'th, td', auth_settings_external_ldap_search_base ).wrapInner( '<div class="animated_wrapper" />' );
714 + $( 'th, td', auth_settings_external_ldap_uid ).wrapInner( '<div class="animated_wrapper" />' );
715 + $( 'th, td', auth_settings_external_ldap_attr_email ).wrapInner( '<div class="animated_wrapper" />' );
716 + $( 'th, td', auth_settings_external_ldap_user ).wrapInner( '<div class="animated_wrapper" />' );
717 + $( 'th, td', auth_settings_external_ldap_password ).wrapInner( '<div class="animated_wrapper" />' );
718 + $( 'th, td', auth_settings_external_ldap_tls ).wrapInner( '<div class="animated_wrapper" />' );
719 + $( 'th, td', auth_settings_external_ldap_lostpassword_url ).wrapInner( '<div class="animated_wrapper" />' );
720 + $( 'th, td', auth_settings_external_ldap_attr_first_name ).wrapInner( '<div class="animated_wrapper" />' );
721 + $( 'th, td', auth_settings_external_ldap_attr_last_name ).wrapInner( '<div class="animated_wrapper" />' );
722 + $( 'th, td', auth_settings_external_ldap_attr_update_on_login ).wrapInner( '<div class="animated_wrapper" />' );
875 723
876 - // Button (caller) might be nested in a div, so we need to walk up one more level
877 - if ( 0 === email.length || 0 === role.length ) {
878 - email = $( caller ).parent().parent().find( '.auth-email' );
879 - role = $( caller ).parent().parent().find( '.auth-role' );
880 - dateAdded = $( caller ).parent().parent().find( '.auth-date-added' );
881 - buttons = $( caller ).parent().children();
882 - }
724 + // If we're viewing the dashboard widget, reset a couple of the relevant
725 + // option variables (since they're aren't nested in table rows).
726 + if ( $( '#auth_dashboard_widget' ).length ) {
727 + auth_settings_access_users_pending = $( '#list_auth_settings_access_users_pending' ).closest( 'div' );
728 + auth_settings_access_users_approved = $( '#list_auth_settings_access_users_approved' ).closest( 'div' );
729 + auth_settings_access_users_blocked = $( '#list_auth_settings_access_users_blocked' ).closest( 'div' );
730 + $( auth_settings_access_users_pending ).wrapInner( '<div class="animated_wrapper" />' );
731 + $( auth_settings_access_users_approved ).wrapInner( '<div class="animated_wrapper" />' );
732 + $( auth_settings_access_users_blocked ).wrapInner( '<div class="animated_wrapper" />' );
883 733
884 - // Support a single email address, or multiple (separated by newlines, commas, semicolons, or spaces).
885 - var emails = $.trim( email.val() ).replace( /mailto:/g, '' ).split( /[\s,;]+/ );
734 + // Remove the helper link, since there are no tabs on the dashboard widget
735 + $( '#dashboard_link_approved_users' ).contents().unwrap();
736 + }
886 737
887 - // Remove any invalid email addresses.
888 - if ( ! skipValidation ) {
889 - // Check if the email(s) being added is well-formed.
890 - emails = emails.filter( function( emailToValidate ) {
891 - return validEmail( emailToValidate, blockingNewUser );
892 - });
893 - // Remove any duplicates in the list of emails to add.
894 - emails = removeDuplicatesFromArrayOfStrings( emails );
895 - }
738 + // Hide Welcome email body/subject options if "Send welcome email" is off.
739 + if ( ! $( '#auth_settings_access_should_email_approved_users' ).is( ':checked' ) ) {
740 + animate_option( 'hide_immediately', auth_settings_access_email_approved_users_subject );
741 + animate_option( 'hide_immediately', auth_settings_access_email_approved_users_body );
742 + }
896 743
897 - // Shake and quit if no valid email addresses exist.
898 - if ( 1 > emails.length ) {
899 - $( '#new_' + list + '_user_email' ).parent().effect( 'shake', shakeSpeed );
900 - return false;
901 - }
744 + // On load: Show/hide public access options if everyone can see site
745 + if ( ! $( '#radio_auth_settings_access_who_can_view_logged_in_users' ).is( ':checked' ) ) {
746 + animate_option( 'hide_immediately', auth_settings_access_public_pages );
747 + animate_option( 'hide_immediately', auth_settings_access_redirect_to_login );
748 + animate_option( 'hide_immediately', auth_settings_access_public_warning );
749 + animate_option( 'hide_immediately', auth_settings_access_redirect_to_message );
750 + }
902 751
903 - $( buttons ).attr( 'disabled', 'disabled' );
752 + // Hide Google options if unchecked
753 + if ( ! $( '#auth_settings_google' ).is( ':checked' ) ) {
754 + animate_option( 'hide_immediately', auth_settings_external_google_clientid );
755 + animate_option( 'hide_immediately', auth_settings_external_google_clientsecret );
756 + }
904 757
905 - var users = [];
906 - for ( var i = 0; i < emails.length; i++ ) {
907 - var user = {
908 - email: emails[i],
909 - role: role.val(),
910 - date_added: dateAdded.val(), // eslint-disable-line camelcase
911 - edit_action: 'add', // eslint-disable-line camelcase
912 - local_user: shouldCreateLocalAccount, // eslint-disable-line camelcase
913 - multisite_user: isMultisite, // eslint-disable-line camelcase
914 - };
915 - users.push( user );
758 + // Hide CAS options if unchecked
759 + if ( ! $( '#auth_settings_cas' ).is( ':checked' ) ) {
760 + animate_option( 'hide_immediately', auth_settings_external_cas_custom_label );
761 + animate_option( 'hide_immediately', auth_settings_external_cas_host );
762 + animate_option( 'hide_immediately', auth_settings_external_cas_port );
763 + animate_option( 'hide_immediately', auth_settings_external_cas_path );
764 + animate_option( 'hide_immediately', auth_settings_external_cas_attr_email );
765 + animate_option( 'hide_immediately', auth_settings_external_cas_attr_first_name );
766 + animate_option( 'hide_immediately', auth_settings_external_cas_attr_last_name );
767 + animate_option( 'hide_immediately', auth_settings_external_cas_attr_update_on_login );
768 + }
916 769
917 - // Get next highest user ID.
918 - var nextId = 1 + Math.max.apply(
919 - null,
920 - // eslint-disable-next-line no-unused-vars
921 - $( '#list_auth_settings_access_users_' + list + ' li .auth-email' ).map( function( el ) { // jshint ignore:line
922 - return parseInt( this.id.replace( 'auth_multisite_settings_access_users_' + list + '_', '' ).replace( 'auth_settings_access_users_' + list + '_', '' ), 10 );
923 - })
924 - );
770 + // Hide LDAP options if unchecked
771 + if ( ! $( '#auth_settings_ldap' ).is( ':checked' ) ) {
772 + animate_option( 'hide_immediately', auth_settings_external_ldap_host );
773 + animate_option( 'hide_immediately', auth_settings_external_ldap_port );
774 + animate_option( 'hide_immediately', auth_settings_external_ldap_search_base );
775 + animate_option( 'hide_immediately', auth_settings_external_ldap_uid );
776 + animate_option( 'hide_immediately', auth_settings_external_ldap_attr_email );
777 + animate_option( 'hide_immediately', auth_settings_external_ldap_user );
778 + animate_option( 'hide_immediately', auth_settings_external_ldap_password );
779 + animate_option( 'hide_immediately', auth_settings_external_ldap_tls );
780 + animate_option( 'hide_immediately', auth_settings_external_ldap_lostpassword_url );
781 + animate_option( 'hide_immediately', auth_settings_external_ldap_attr_first_name );
782 + animate_option( 'hide_immediately', auth_settings_external_ldap_attr_last_name );
783 + animate_option( 'hide_immediately', auth_settings_external_ldap_attr_update_on_login );
784 + }
925 785
926 - // Add the new item.
927 - var authJsPrefix = isMultisite ? 'authMultisite' : 'auth';
928 - var banButton = ! isMultisite ? '<a class="button' + ( list === 'approved' ? '' : ' invisible' ) + '" id="block_user_' + nextId + '" onclick="' + authJsPrefix + 'AddUser( this, \'blocked\', false ); ' + authJsPrefix + 'IgnoreUser( this, \'approved\' );" title="' + authL10n.block_ban_user + '"><span class="glyphicon glyphicon-ban-circle"></span></a>' : '';
929 - var ignoreButton = '<a class="button" id="ignore_user_' + nextId + '" onclick="' + authJsPrefix + 'IgnoreUser( this, \'' + list + '\' );" title="' + authL10n.remove_user + '"><span class="glyphicon glyphicon-remove"></span></a>';
930 - var localIcon = '&nbsp;<a title="' + authL10n.local_wordpress_user + '" class="auth-local-user' + ( shouldCreateLocalAccount ? '' : ' invisible' ) + '"><span class="glyphicon glyphicon-user"></span></a>';
931 - var multisiteIcon = '&nbsp;<a title="WordPress Multisite user" class="auth-multisite-user'+ ( isMultisite ? '' : ' invisible' ) + '"><span class="glyphicon glyphicon-globe"></span></a>';
932 - $( ' \
933 - <li id="new_user_' + nextId + '" class="new-user" style="display: none;"> \
934 - <input type="text" id="auth_settings_access_users_' + list + '_' + nextId + '" name="auth_settings[access_users_' + list + '][' + nextId + '][email]" value="' + user.email + '" readonly="true" class="auth-email" /> \
935 - <select name="auth_settings[access_users_' + list + '][' + nextId + '][role]" class="auth-role" onchange="' + authJsPrefix + 'ChangeRole( this );"> \
936 - </select> \
937 - <input type="text" name="auth_settings[access_users_' + list + '][' + nextId + '][date_added]" value="' + getShortDate() + '" readonly="true" class="auth-date-added" /> \
938 - ' + banButton + ignoreButton + localIcon + multisiteIcon + ' \
939 - <span class="spinner is-active"></span> \
940 - </li> \
941 - ' ).appendTo( '#list_auth_settings_access_users_' + list ).slideDown( 250 );
786 + // Event handler: Hide "Handle unauthorized visitors" option if access is granted to "Everyone"
787 + $( 'input[name="auth_settings[access_who_can_login]"]' ).change( function() {
788 + // Hide user whitelist unless "Only specific students below" is checked
789 + var action = $( '#radio_auth_settings_access_who_can_login_approved_users' ).is( ':checked' ) ? 'show' : 'hide';
790 + animate_option( action, auth_settings_access_role_receive_pending_emails );
791 + animate_option( action, auth_settings_access_pending_redirect_to_message );
792 + animate_option( action, auth_settings_access_blocked_redirect_to_message );
793 + animate_option( action, auth_settings_access_should_email_approved_users );
794 + animate_option( action, auth_settings_access_email_approved_users_subject );
795 + animate_option( action, auth_settings_access_email_approved_users_body );
796 + });
942 797
943 - // Populate the role dropdown in the new element. Because clone() doesn't
944 - // save selected state on select elements, set that too.
945 - $( 'option', role ).clone().appendTo( '#new_user_' + nextId + ' .auth-role' );
946 - $( '#new_user_' + nextId + ' .auth-role' ).val( role.val() );
947 - }
798 + // Event handler: Hide Welcome email body/subject options if "Send welcome email" is off.
799 + $( 'input[name="auth_settings[access_should_email_approved_users]"]' ).change( function() {
800 + var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
801 + animate_option( action, auth_settings_access_email_approved_users_subject );
802 + animate_option( action, auth_settings_access_email_approved_users_body );
803 + });
948 804
949 - // Remove the 'empty list' item if it exists.
950 - $( '#list_auth_settings_access_users_' + list + ' li.auth-empty' ).remove();
805 + // Event handler: Hide "Handle unauthorized visitors" option if access is granted to "Everyone"
806 + $( 'input[name="auth_settings[access_who_can_view]"]' ).change( function() {
807 + var action = $( '#radio_auth_settings_access_who_can_view_everyone' ).is( ':checked' ) ? 'hide' : 'show';
808 + animate_option( action, auth_settings_access_redirect_to_login );
809 + animate_option( action, auth_settings_access_redirect_to_message );
810 + animate_option( action, auth_settings_access_public_pages );
811 + animate_option( action, auth_settings_access_public_warning );
812 + });
951 813
952 - // Update the options in the database with this change.
953 - updateAuthUser( buttons, 'access_users_' + list, users );
814 + // Event handler: Show/hide Google options based on checkbox
815 + $( 'input[name="auth_settings[google]"]' ).change( function() {
816 + var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
817 + animate_option( action, auth_settings_external_google_clientid );
818 + animate_option( action, auth_settings_external_google_clientsecret );
819 + });
954 820
955 - // Reset the new user textboxes
956 - if ( email.hasClass( 'new' ) ) {
957 - email.val( '' ).keydown();
958 - }
821 + // Event handler: Show/hide CAS options based on checkbox
822 + $( 'input[name="auth_settings[cas]"]' ).change( function() {
823 + var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
824 + animate_option( action, auth_settings_external_cas_custom_label );
825 + animate_option( action, auth_settings_external_cas_host );
826 + animate_option( action, auth_settings_external_cas_port );
827 + animate_option( action, auth_settings_external_cas_path );
828 + animate_option( action, auth_settings_external_cas_attr_email );
829 + animate_option( action, auth_settings_external_cas_attr_first_name );
830 + animate_option( action, auth_settings_external_cas_attr_last_name );
831 + animate_option( action, auth_settings_external_cas_attr_update_on_login );
832 + });
959 833
960 - // Re-enable the action buttons now that we're done saving.
961 - $( buttons ).removeAttr( 'disabled' );
962 - return true;
963 - };
834 + // Event handler: Show/hide LDAP options based on checkbox
835 + $( 'input[name="auth_settings[ldap]"]' ).change( function() {
836 + var action = $( this ).is( ':checked' ) ? 'show' : 'hide';
837 + animate_option( action, auth_settings_external_ldap_host );
838 + animate_option( action, auth_settings_external_ldap_port );
839 + animate_option( action, auth_settings_external_ldap_search_base );
840 + animate_option( action, auth_settings_external_ldap_uid );
841 + animate_option( action, auth_settings_external_ldap_attr_email );
842 + animate_option( action, auth_settings_external_ldap_user );
843 + animate_option( action, auth_settings_external_ldap_password );
844 + animate_option( action, auth_settings_external_ldap_tls );
845 + animate_option( action, auth_settings_external_ldap_lostpassword_url );
846 + animate_option( action, auth_settings_external_ldap_attr_first_name );
847 + animate_option( action, auth_settings_external_ldap_attr_last_name );
848 + animate_option( action, auth_settings_external_ldap_attr_update_on_login );
849 + });
964 850
965 - // Add user to list (multisite options page).
966 - window.authMultisiteAddUser = function( caller, list, shouldCreateLocalAccount ) {
967 - var isMultisite = true;
968 -
969 - // Default to the approved list.
970 - list = typeof list !== 'undefined' ? list : 'approved';
971 -
972 - // Default to not creating a local account.
973 - shouldCreateLocalAccount = typeof shouldCreateLocalAccount !== 'undefined' ? shouldCreateLocalAccount : false;
974 -
975 - // There currently is no multisite blocked list, so do nothing.
976 - if ( list === 'blocked' ) {
851 + // Show save button if usermeta field is modified.
852 + $( 'form input.auth-usermeta' ).bind( 'keyup', function ( event ) {
853 + // Don't do anything if tab or arrow keys were pressed.
854 + if ( event.which === 9 || event.which === 37 || event.which === 38 || event.which === 39 || event.which === 40 ) {
977 855 return;
978 856 }
857 + $( this ).siblings( '.button' ).css( 'display', 'inline-block' );
858 + });
979 859
980 - window.authAddUser( caller, list, shouldCreateLocalAccount, isMultisite );
981 - };
860 + // List management function: pressing enter in the new approved or new
861 + // blocked user (email or role field) adds the user to the list.
862 + $( '#new_approved_user_email, #new_approved_user_role, #new_blocked_user_email' ).bind( 'keyup', function( e ) {
863 + if ( e.which == 13 ) { // Enter key
864 + $( this ).parent().find( 'a' ).trigger( 'click' );
865 + return false;
866 + }
867 + });
982 868
983 - // Remove user from list.
984 - window.authIgnoreUser = function( caller, listName, isMultisite ) {
985 - // Set default for multisite flag (run different save routine if multisite)
986 - isMultisite = typeof isMultisite !== 'undefined' ? isMultisite : false;
987 -
988 - // Set default list if not provided.
989 - listName = typeof listName !== 'undefined' ? listName : 'approved';
990 -
991 - var email = $( caller ).parent().find( '.auth-email' );
992 -
993 - var user = {
994 - email: email.val(),
995 - role: '',
996 - date_added: '', // eslint-disable-line camelcase
997 - edit_action: 'remove', // eslint-disable-line camelcase
998 - multisite_user: isMultisite, // eslint-disable-line camelcase
999 - };
1000 -
1001 - // Show an 'empty list' message if we're deleting the last item
1002 - var list = $( caller ).closest( 'ul' );
1003 - if ( $( 'li', list ).length <= 1 ) {
1004 - $( list ).append( '<li class="auth-empty"><em>' + authL10n.no_users_in + ' ' + listName + '</em></li>' );
869 + // Don't submit form (i.e., save options) when hitting enter in any user list field.
870 + $( 'input.auth-email, select.auth-role, input.auth-date-added, input.auth-usermeta' ).bind( 'keydown', function( e ) {
871 + if ( e.which == 13 ) { // Enter key
872 + e.preventDefault();
873 + return false;
1005 874 }
875 + });
1006 876
1007 - $( caller ).parent().slideUp( 250, function() {
1008 - // Remove the list item.
1009 - $( this ).remove();
877 + // Enable the user-friendly multiselect form element on the options page.
878 + $( '#auth_settings_access_public_pages' ).multiSelect({
879 + selectableOptgroup: true,
880 + selectableHeader: '<div class="custom-header">Private Pages</div>',
881 + selectionHeader: '<div class="custom-header">Public Pages</div>',
882 + });
1010 883
1011 - // Update the options in the database with this change.
1012 - updateAuthUser( caller, 'access_users_' + listName, user );
1013 - });
1014 - };
884 + // Switch to the first tab (or the tab indicated in sessionStorage, or the querystring).
885 + var tab = '';
886 + if ( querystring( 'tab' ).length > 0 ) {
887 + tab = querystring( 'tab' )[0];
888 + } else if ( sessionStorage.getItem( 'tab' ) ) {
889 + tab = sessionStorage.getItem( 'tab' );
890 + }
891 + if ( $.inArray( tab, [ 'access_lists', 'access_login', 'access_public', 'external', 'advanced' ] ) < 0 ) {
892 + tab = 'access_lists';
893 + }
894 + choose_tab( tab, animation_speed );
1015 895
1016 - // Remove user from list (multisite options page).
1017 - window.authMultisiteIgnoreUser = function( caller, listName ) {
1018 - var isMultisite = true;
896 + // Hide/show multisite settings based on override checkbox.
897 + $( 'input[name="auth_settings[multisite_override]"]' ).change( function() {
898 + hide_multisite_settings_if_disabled();
899 + });
900 + hide_multisite_settings_if_disabled();
1019 901
1020 - // Set default list if not provided.
1021 - listName = typeof listName !== 'undefined' ? listName : '';
902 +});
1022 903
1023 - window.authIgnoreUser( caller, listName, isMultisite );
1024 - };
1025 904
1026 - // Save Authorizer Settings (multisite).
1027 - // @calls php wp_ajax_save_auth_multisite_settings.
1028 - /* eslint-disable camelcase */
1029 - window.saveAuthMultisiteSettings = function( caller ) {
1030 - // Enable wait cursor.
1031 - $( 'html' ).addClass( 'busy' );
905 +/**
906 + * Portions below from Bootstrap
907 + * http://getbootstrap.com/getting-started/#download
908 + */
1032 909
1033 - // Disable button (prevent duplicate clicks).
1034 - $( caller ).attr( 'disabled', 'disabled' );
1035 910
1036 - // Enable spinner by element that triggered this event (caller).
1037 - var $spinner = $( '<span class="spinner is-active"></span>' ).css({
1038 - position: 'absolute',
1039 - top: $( caller ).position().top,
1040 - left: $( caller ).position().left + $( caller ).width() + 20,
1041 - });
1042 - $( caller ).after( $spinner );
911 +/*!
912 + * Bootstrap v3.1.1 (http://getbootstrap.com)
913 + * Copyright 2011-2014 Twitter, Inc.
914 + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
915 + */
1043 916
1044 - // Get form elements to save
1045 - var nonce_save_auth_settings = $( '#nonce_save_auth_settings' ).val();
917 +if ( typeof jQuery === 'undefined' ) { throw new Error( 'Bootstrap\'s JavaScript requires jQuery' ) }
1046 918
1047 - var multisite_override = $( '#auth_settings_multisite_override' ).is( ':checked' ) ? '1' : '';
919 +/* ========================================================================
920 + * Bootstrap: dropdown.js v3.1.1
921 + * http://getbootstrap.com/javascript/#dropdowns
922 + * ========================================================================
923 + * Copyright 2011-2014 Twitter, Inc.
924 + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
925 + * ======================================================================== */
1048 926
1049 - var access_who_can_login = $( 'form input[name="auth_settings[access_who_can_login]"]:checked' ).val();
1050 927
1051 - var access_who_can_view = $( 'form input[name="auth_settings[access_who_can_view]"]:checked' ).val();
928 ++function ($) {
929 + 'use strict';
1052 930
1053 - var access_users_approved = {};
1054 - $( '#list_auth_settings_access_users_approved li' ).each( function( index ) {
1055 - var user = {};
1056 - user.email = $( '.auth-email', this ).val();
1057 - user.role = $( '.auth-role', this ).val();
1058 - user.date_added = $( '.auth-date-added', this ).val();
1059 - user.local_user = $( '.auth-local-user', this ).length !== 0;
1060 - access_users_approved[index] = user;
1061 - });
931 + // DROPDOWN CLASS DEFINITION
932 + // =========================
1062 933
1063 - var access_default_role = $( '#auth_settings_access_default_role' ).val();
1064 -
1065 - var google = $( '#auth_settings_google' ).is( ':checked' ) ? '1' : '';
1066 - var google_clientid = $( '#auth_settings_google_clientid' ).val();
1067 - var google_clientsecret = $( '#auth_settings_google_clientsecret' ).val();
1068 - var google_hosteddomain = $( '#auth_settings_google_hosteddomain' ).val();
1069 -
1070 - var cas = $( '#auth_settings_cas' ).is( ':checked' ) ? '1' : '';
1071 - var cas_custom_label = $( '#auth_settings_cas_custom_label' ).val();
1072 - var cas_host = $( '#auth_settings_cas_host' ).val();
1073 - var cas_port = $( '#auth_settings_cas_port' ).val();
1074 - var cas_path = $( '#auth_settings_cas_path' ).val();
1075 - var cas_version = $( '#auth_settings_cas_version' ).val();
1076 - var cas_attr_email = $( '#auth_settings_cas_attr_email' ).val();
1077 - var cas_attr_first_name = $( '#auth_settings_cas_attr_first_name' ).val();
1078 - var cas_attr_last_name = $( '#auth_settings_cas_attr_last_name' ).val();
1079 - var cas_attr_update_on_login = $( '#auth_settings_cas_attr_update_on_login' ).is( ':checked' ) ? '1' : '';
1080 - var cas_auto_login = $( '#auth_settings_cas_auto_login' ).is( ':checked' ) ? '1' : '';
1081 - var cas_link_on_username = $( '#auth_settings_cas_link_on_username' ).is( ':checked' ) ? '1' : '';
1082 -
1083 - var ldap = $( '#auth_settings_ldap' ).is( ':checked' ) ? '1' : '';
1084 - var ldap_host = $( '#auth_settings_ldap_host' ).val();
1085 - var ldap_port = $( '#auth_settings_ldap_port' ).val();
1086 - var ldap_search_base = $( '#auth_settings_ldap_search_base' ).val();
1087 - var ldap_uid = $( '#auth_settings_ldap_uid' ).val();
1088 - var ldap_attr_email = $( '#auth_settings_ldap_attr_email' ).val();
1089 - var ldap_user = $( '#auth_settings_ldap_user' ).val();
1090 - var ldap_password = $( '#auth_settings_ldap_password' ).val();
1091 - var ldap_tls = $( '#auth_settings_ldap_tls' ).is( ':checked' ) ? '1' : '';
1092 - var ldap_lostpassword_url = $( '#auth_settings_ldap_lostpassword_url' ).val();
1093 - var ldap_attr_first_name = $( '#auth_settings_ldap_attr_first_name' ).val();
1094 - var ldap_attr_last_name = $( '#auth_settings_ldap_attr_last_name' ).val();
1095 - var ldap_attr_update_on_login = $( '#auth_settings_ldap_attr_update_on_login' ).is( ':checked' ) ? '1' : '';
1096 -
1097 - var advanced_lockouts = {
1098 - attempts_1: $( '#auth_settings_advanced_lockouts_attempts_1' ).val(),
1099 - duration_1: $( '#auth_settings_advanced_lockouts_duration_1' ).val(),
1100 - attempts_2: $( '#auth_settings_advanced_lockouts_attempts_2' ).val(),
1101 - duration_2: $( '#auth_settings_advanced_lockouts_duration_2' ).val(),
1102 - reset_duration: $( '#auth_settings_advanced_lockouts_reset_duration' ).val(),
1103 - };
1104 - var advanced_hide_wp_login = $( '#auth_settings_advanced_hide_wp_login' ).is( ':checked' ) ? '1' : '';
1105 - var advanced_disable_wp_login = $( '#auth_settings_advanced_disable_wp_login' ).is( ':checked' ) ? '1' : '';
1106 - var advanced_widget_enabled = $( '#auth_settings_advanced_widget_enabled' ).is( ':checked' ) ? '1' : '';
1107 - var advanced_users_per_page = $( '#auth_settings_advanced_users_per_page' ).val();
1108 - var advanced_users_sort_by = $( '#auth_settings_advanced_users_sort_by' ).val();
1109 - var advanced_users_sort_order = $( '#auth_settings_advanced_users_sort_order' ).val();
1110 -
1111 - $.post( ajaxurl, {
1112 - action: 'save_auth_multisite_settings',
1113 - nonce: nonce_save_auth_settings,
1114 - multisite_override: multisite_override,
1115 - access_who_can_login: access_who_can_login,
1116 - access_who_can_view: access_who_can_view,
1117 - access_users_approved: access_users_approved,
1118 - access_default_role: access_default_role,
1119 - google: google,
1120 - google_clientid: google_clientid,
1121 - google_clientsecret: google_clientsecret,
1122 - google_hosteddomain: google_hosteddomain,
1123 - cas: cas,
1124 - cas_custom_label: cas_custom_label,
1125 - cas_host: cas_host,
1126 - cas_port: cas_port,
1127 - cas_path: cas_path,
1128 - cas_version: cas_version,
1129 - cas_attr_email: cas_attr_email,
1130 - cas_attr_first_name: cas_attr_first_name,
1131 - cas_attr_last_name: cas_attr_last_name,
1132 - cas_attr_update_on_login: cas_attr_update_on_login,
1133 - cas_auto_login: cas_auto_login,
1134 - cas_link_on_username: cas_link_on_username,
1135 - ldap: ldap,
1136 - ldap_host: ldap_host,
1137 - ldap_port: ldap_port,
1138 - ldap_search_base: ldap_search_base,
1139 - ldap_uid: ldap_uid,
1140 - ldap_attr_email: ldap_attr_email,
1141 - ldap_user: ldap_user,
1142 - ldap_password: ldap_password,
1143 - ldap_tls: ldap_tls,
1144 - ldap_lostpassword_url: ldap_lostpassword_url,
1145 - ldap_attr_first_name: ldap_attr_first_name,
1146 - ldap_attr_last_name: ldap_attr_last_name,
1147 - ldap_attr_update_on_login: ldap_attr_update_on_login,
1148 - advanced_lockouts: advanced_lockouts,
1149 - advanced_hide_wp_login: advanced_hide_wp_login,
1150 - advanced_disable_wp_login: advanced_disable_wp_login,
1151 - advanced_users_per_page: advanced_users_per_page,
1152 - advanced_users_sort_by: advanced_users_sort_by,
1153 - advanced_users_sort_order: advanced_users_sort_order,
1154 - advanced_widget_enabled: advanced_widget_enabled,
1155 - }, function( response ) {
1156 - var succeeded = response === 'success';
1157 - var spinnerText = succeeded ? authL10n.saved + '.' : '<span class="attention">' + authL10n.failed + '.</span>';
1158 - var spinnerWait = succeeded ? 500 : 2000;
1159 - $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinnerText + '</span>' ).delay( spinnerWait ).hide( animationSpeed, function() {
1160 - $( this ).remove();
1161 - });
1162 - $( caller ).removeAttr( 'disabled' );
1163 -
1164 - // Disable wait cursor.
1165 - $( 'html' ).removeClass( 'busy' );
1166 - }).fail( function() {
1167 - // Fail fires if the server doesn't respond
1168 - var succeeded = false;
1169 - var spinnerText = succeeded ? authL10n.saved + '.' : '<span class="attention">' + authL10n.failed + '.</span>';
1170 - var spinnerWait = succeeded ? 500 : 2000;
1171 - $( 'form .spinner:not(:has(.spinner-text))' ).append( '<span class="spinner-text">' + spinnerText + '</span>' ).delay( spinnerWait ).hide( animationSpeed, function() {
1172 - $( this ).remove();
1173 - });
1174 - $( caller ).removeAttr( 'disabled' );
1175 -
1176 - // Disable wait cursor.
1177 - $( 'html' ).removeClass( 'busy' );
1178 - });
1179 - };
1180 - /* eslint-enable camelcase */
1181 -
1182 - // Fade in/out Authorizer logo in bottom right on Settings.
1183 - $(document).on( 'scroll', function () {
1184 - fadeLogo();
1185 - });
1186 -
1187 - function fadeLogo() {
1188 - if ( getScrollPercent() < 90 ) {
1189 - $( '#wpwrap' ).removeClass( 'not-faded' );
1190 - } else {
1191 - $( '#wpwrap' ).addClass( 'not-faded' );
1192 - }
934 + var backdrop = '.dropdown-backdrop'
935 + var toggle = '[data-toggle=dropdown]'
936 + var Dropdown = function ( element ) {
937 + $( element ).on( 'click.bs.dropdown', this.toggle)
1193 938 }
1194 939
1195 - function getScrollPercent() {
1196 - var h = document.documentElement,
1197 - b = document.body,
1198 - st = 'scrollTop',
1199 - sh = 'scrollHeight';
1200 - return ( h[st] || b[st] ) / ( ( h[sh] || b[sh] ) - h.clientHeight ) * 100;
1201 - }
940 + Dropdown.prototype.toggle = function ( e ) {
941 + var $this = $( this )
1202 942
1203 - /* ========================================================================
1204 - * Portions below from Bootstrap.
1205 - * ========================================================================
1206 - * Bootstrap: dropdown.js v3.1.1
1207 - * http://getbootstrap.com/javascript/#dropdowns
1208 - * ========================================================================
1209 - * Copyright 2011-2014 Twitter, Inc.
1210 - * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
1211 - * ======================================================================== */
943 + if ($this.is( '.disabled, :disabled' )) return
1212 944
1213 - // DROPDOWN CLASS DEFINITION
1214 - // =========================
1215 - var backdrop = '.dropdown-backdrop';
1216 - var toggle = '[data-toggle=dropdown]';
1217 - var Dropdown = function( element ) { // eslint-disable-line func-style
1218 - $( element ).on( 'click.bs.dropdown', this.toggle );
1219 - };
945 + var $parent = getParent($this)
946 + var isActive = $parent.hasClass( 'open' )
1220 947
1221 - Dropdown.prototype.toggle = function( event ) {
1222 - var $this = $( this );
948 + clearMenus()
1223 949
1224 - if ( $this.is( '.disabled, :disabled' ) ) {
1225 - return;
1226 - }
1227 -
1228 - var $parent = getParent( $this );
1229 - var isActive = $parent.hasClass( 'open' );
1230 -
1231 - clearMenus();
1232 -
1233 - if ( ! isActive ) {
1234 - if ( 'ontouchstart' in document.documentElement && ! $parent.closest( '.navbar-nav' ).length ) {
950 + if (!isActive) {
951 + if ( 'ontouchstart' in document.documentElement && !$parent.closest( '.navbar-nav' ).length) {
1235 952 // if mobile we use a backdrop because click events don't delegate
1236 - $( '<div class="dropdown-backdrop"/>' ).insertAfter( $( this ) ).on( 'click', clearMenus );
953 + $( '<div class="dropdown-backdrop"/>' ).insertAfter($( this )).on( 'click', clearMenus)
1237 954 }
1238 955
1239 - var relatedTarget = { relatedTarget: this };
1240 - $parent.trigger( event = $.Event( 'show.bs.dropdown', relatedTarget ) );
956 + var relatedTarget = { relatedTarget: this }
957 + $parent.trigger( e = $.Event( 'show.bs.dropdown', relatedTarget ) )
1241 958
1242 - if ( event.isDefaultPrevented() ) {
1243 - return;
1244 - }
959 + if (e.isDefaultPrevented()) return
1245 960
1246 961 $parent
1247 962 .toggleClass( 'open' )
1248 - .trigger( 'shown.bs.dropdown', relatedTarget);
963 + .trigger( 'shown.bs.dropdown', relatedTarget)
1249 964
1250 - $this.focus();
965 + $this.focus()
1251 966 }
1252 967
1253 - return false;
1254 - };
968 + return false
969 + }
1255 970
1256 - Dropdown.prototype.keydown = function( event ) {
1257 - if ( ! /(38|40|27)/.test( event.keyCode ) ) {
1258 - return;
1259 - }
971 + Dropdown.prototype.keydown = function (e) {
972 + if (!/(38|40|27)/.test(e.keyCode)) return
1260 973
1261 - var $this = $( this );
974 + var $this = $( this )
1262 975
1263 - event.preventDefault();
1264 - event.stopPropagation();
976 + e.preventDefault()
977 + e.stopPropagation()
1265 978
1266 - if ( $this.is( '.disabled, :disabled' ) ) {
1267 - return;
1268 - }
979 + if ($this.is( '.disabled, :disabled' )) return
1269 980
1270 - var $parent = getParent( $this );
1271 - var isActive = $parent.hasClass( 'open' );
981 + var $parent = getParent($this)
982 + var isActive = $parent.hasClass( 'open' )
1272 983
1273 - if ( ! isActive || ( isActive && event.keyCode === 27 ) ) {
1274 - if ( event.which === 27 ) {
1275 - $parent.find( toggle ).focus();
1276 - }
1277 - return $this.click();
984 + if (!isActive || (isActive && e.keyCode == 27)) {
985 + if (e.which == 27) $parent.find(toggle).focus()
986 + return $this.click()
1278 987 }
1279 988
1280 - var desc = ' li:not(.divider):visible a';
1281 - var $items = $parent.find( '[role=menu]' + desc + ', [role=listbox]' + desc);
989 + var desc = ' li:not(.divider):visible a'
990 + var $items = $parent.find( '[role=menu]' + desc + ', [role=listbox]' + desc)
1282 991
1283 - if ( ! $items.length ) {
1284 - return;
1285 - }
992 + if (!$items.length) return
1286 993
1287 - var index = $items.index($items.filter( ':focus' ));
994 + var index = $items.index($items.filter( ':focus' ))
1288 995
1289 - if ( event.keyCode === 38 && index > 0 ) {
1290 - index--; // up
1291 - }
1292 - if ( event.keyCode === 40 && index < $items.length - 1 ) {
1293 - index++; // down
1294 - }
1295 - if ( ! ~index ) {
1296 - index = 0;
1297 - }
996 + if (e.keyCode == 38 && index > 0) index-- // up
997 + if (e.keyCode == 40 && index < $items.length - 1) index++ // down
998 + if (!~index) index = 0
1298 999
1299 - $items.eq(index).focus();
1300 - };
1000 + $items.eq(index).focus()
1001 + }
1301 1002
1302 - function clearMenus( event ) {
1303 - $(backdrop).remove();
1304 - $(toggle).each( function() {
1305 - var $parent = getParent($( this ));
1306 - var relatedTarget = { relatedTarget: this };
1307 - if ( ! $parent.hasClass( 'open' ) ) {
1308 - return;
1309 - }
1310 - $parent.trigger( event = $.Event( 'hide.bs.dropdown', relatedTarget ) );
1311 - if ( event.isDefaultPrevented() ) {
1312 - return;
1313 - }
1314 - $parent.removeClass( 'open' ).trigger( 'hidden.bs.dropdown', relatedTarget);
1315 - });
1003 + function clearMenus(e) {
1004 + $(backdrop).remove()
1005 + $(toggle).each(function () {
1006 + var $parent = getParent($( this ))
1007 + var relatedTarget = { relatedTarget: this }
1008 + if (!$parent.hasClass( 'open' )) return
1009 + $parent.trigger(e = $.Event( 'hide.bs.dropdown', relatedTarget))
1010 + if (e.isDefaultPrevented()) return
1011 + $parent.removeClass( 'open' ).trigger( 'hidden.bs.dropdown', relatedTarget)
1012 + })
1316 1013 }
1317 1014
1318 - function getParent( $this ) {
1319 - var selector = $this.attr( 'data-target' );
1015 + function getParent($this) {
1016 + var selector = $this.attr( 'data-target' )
1320 1017
1321 - if ( ! selector ) {
1322 - selector = $this.attr( 'href' );
1323 - selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '' ); // strip for ie7
1018 + if (!selector) {
1019 + selector = $this.attr( 'href' )
1020 + selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '' ) //strip for ie7
1324 1021 }
1325 1022
1326 - var $parent = selector && $(selector);
1023 + var $parent = selector && $(selector)
1327 1024
1328 - return $parent && $parent.length ? $parent : $this.parent();
1025 + return $parent && $parent.length ? $parent : $this.parent()
1329 1026 }
1330 1027
1028 +
1331 1029 // DROPDOWN PLUGIN DEFINITION
1332 1030 // ==========================
1333 - var old = $.fn.dropdown;
1334 - $.fn.dropdown = function( option ) {
1335 - return this.each( function() {
1336 - var $this = $( this );
1337 - var data = $this.data( 'bs.dropdown' );
1338 1031
1339 - if ( ! data ) {
1340 - $this.data( 'bs.dropdown', ( data = new Dropdown( this ) ) );
1341 - }
1342 - if ( 'string' === typeof option ) {
1343 - data[option].call( $this );
1344 - }
1345 - });
1346 - };
1347 - $.fn.dropdown.Constructor = Dropdown;
1032 + var old = $.fn.dropdown
1348 1033
1034 + $.fn.dropdown = function (option) {
1035 + return this.each(function () {
1036 + var $this = $( this )
1037 + var data = $this.data( 'bs.dropdown' )
1038 +
1039 + if (!data) $this.data( 'bs.dropdown', (data = new Dropdown( this )))
1040 + if (typeof option == 'string' ) data[option].call($this)
1041 + })
1042 + }
1043 +
1044 + $.fn.dropdown.Constructor = Dropdown
1045 +
1046 +
1349 1047 // DROPDOWN NO CONFLICT
1350 1048 // ====================
1351 - $.fn.dropdown.noConflict = function() {
1352 - $.fn.dropdown = old;
1353 - return this;
1354 - };
1355 1049
1050 + $.fn.dropdown.noConflict = function () {
1051 + $.fn.dropdown = old
1052 + return this
1053 + }
1054 +
1055 +
1356 1056 // APPLY TO STANDARD DROPDOWN ELEMENTS
1357 1057 // ===================================
1058 +
1358 1059 $(document)
1359 1060 .on( 'click.bs.dropdown.data-api', clearMenus)
1360 - .on( 'click.bs.dropdown.data-api', '.dropdown form', function( event ) { event.stopPropagation(); })
1061 + .on( 'click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
1361 1062 .on( 'click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle)
1362 - .on( 'keydown.bs.dropdown.data-api', toggle + ', [role=menu], [role=listbox]', Dropdown.prototype.keydown);
1063 + .on( 'keydown.bs.dropdown.data-api', toggle + ', [role=menu], [role=listbox]', Dropdown.prototype.keydown)
1363 1064
1364 -} )( jQuery );
1065 +}(jQuery);