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