/** * Core JavaScript file for the s2Member plugin. * * This is the development version of the code. * Which ultimately produces s2member.min.js. * * Copyright: © 2009-2011 * {@link http://websharks-inc.com/ WebSharks, Inc.} * (coded in the USA) * * Released under the terms of the GNU General Public License. * You should have received a copy of the GNU General Public License, * along with this software. In the main directory, see: /licensing/ * If not, see: {@link http://www.gnu.org/licenses/}. * * @package s2Member * @since 3.0 */ jQuery(document) .ready(function($) { window.ws_plugin__s2member_skip_all_file_confirmations = window.ws_plugin__s2member_skip_all_file_confirmations || false; var runningBuddyPress = '', filesBaseDir = '', skipAllFileConfirmations = ws_plugin__s2member_skip_all_file_confirmations ? true : false, uniqueFilesDownloadedInPage = [/* Real-time counts in a single page/instance. */]; window.ws_plugin__s2member_passwordMinLength = function() { return parseInt(''); }; window.ws_plugin__s2member_passwordMinStrengthCode = function() { return ''; }; window.ws_plugin__s2member_passwordMinStrengthLabel = function() { return ''; }; window.ws_plugin__s2member_passwordMinStrengthScore = function() { return parseInt(''); }; window.ws_plugin__s2member_passwordStrengthMeter = function(password1, password2, scoreOnly) { var score = 0, // Initialize score. minLength = ws_plugin__s2member_passwordMinLength(); password1 = String(password1); password2 = String(password2); if(password1 != password2 && password2.length > 0) return 'mismatch'; else if(password1.length < 1) return 'empty'; else if(password1.length < minLength) return 'short'; if(password1.match(/[0-9]/)) score += 10; if(password1.match(/[a-z]/)) score += 10; if(password1.match(/[A-Z]/)) score += 10; if(password1.match(/[^0-9a-zA-Z]/)) score = score === 30 ? score + 20 : score + 10; if(scoreOnly) return score; if(score < 30) return 'weak'; if(score < 50) return 'good'; return 'strong'; // Default return value. }; window.ws_plugin__s2member_passwordStrength = function($username, $pass1, $pass2, $result) { if($username instanceof jQuery && $pass1 instanceof jQuery && $pass2 instanceof jQuery && $result instanceof jQuery) { var pwsL10n = { // Password strength meter translations. 'empty' : '', 'short' : '', 'weak' : '', 'good' : '', 'strong' : '', 'mismatch': '' }; $result.removeClass('ws-plugin--s2member-password-strength-short'); $result.removeClass('ws-plugin--s2member-password-strength-weak'); $result.removeClass('ws-plugin--s2member-password-strength-good'); $result.removeClass('ws-plugin--s2member-password-strength-strong'); $result.removeClass('ws-plugin--s2member-password-strength-mismatch'); $result.removeClass('ws-plugin--s2member-password-strength-empty'); var meterSays = ws_plugin__s2member_passwordStrengthMeter($pass1.val(), $pass2.val()); $result.addClass('ws-plugin--s2member-password-strength-' + meterSays).html(pwsL10n[meterSays]); } }; window.ws_plugin__s2member_validationErrors = function(label, field, context, required, expected) { if(typeof label === 'string' && label && typeof field === 'object' && typeof context === 'object') if(typeof field.tagName === 'string' && /^(input|textarea|select)$/i.test(field.tagName) && !field.disabled) { var tag = field.tagName.toLowerCase(), $field = $(field), type = $.trim($field.attr('type')).toLowerCase(), name = $.trim($field.attr('name')), value = $field.val(); required = ( typeof required === 'boolean') ? required : ($field.attr('aria-required') === 'true'), expected = ( typeof expected === 'string') ? expected : $.trim($field.attr('data-expected')); var forcePersonalEmails = ('' > 0); var nonPersonalEmailUsers = new RegExp('^()@', 'i'); if(tag === 'input' && type === 'checkbox' && /\[\]$/.test(name)) { if(typeof field.id === 'string' && /-0$/.test(field.id)) if(required && !$('input[name="' + ws_plugin__s2member_escjQAttr(name) + '"]:checked', context).length) return label + '\n' + ''; } else if(tag === 'input' && type === 'checkbox') { if(required && !field.checked) return label + '\n' + ''; } else if(tag === 'input' && type === 'radio') { if(typeof field.id === 'string' && /-0$/.test(field.id)) if(required && !$('input[name="' + ws_plugin__s2member_escjQAttr(name) + '"]:checked', context).length) return label + '\n' + ''; } else if(tag === 'select' && $field.attr('multiple')) { if(required && (!(value instanceof Array) || !value.length)) return label + '\n' + ''; } else if(typeof value !== 'string' || (required && !(value = $.trim(value)).length)) { return label + '\n' + ''; } else if((value = $.trim(value)).length && ((tag === 'input' && /^(text|password)$/i.test(type)) || tag === 'textarea') && typeof expected === 'string' && expected.length) { if(expected === 'numeric-wp-commas' && (!/^[0-9\.,]+$/.test(value) || isNaN(value.replace(/,/g, '')))) { return label + '\n' + ''; } else if(expected === 'numeric' && (!/^[0-9\.]+$/.test(value) || isNaN(value))) { return label + '\n' + ''; } else if(expected === 'integer' && (!/^[0-9]+$/.test(value) || isNaN(value))) { return label + '\n' + ''; } else if(expected === 'integer-gt-0' && (!/^[0-9]+$/.test(value) || isNaN(value) || value <= 0)) { return label + '\n' + ' 0 (whole number, no decimals, greater than 0).", "s2member-front", "s2member") /*b*/); ?>'; } else if(expected === 'float' && (!/^[0-9\.]+$/.test(value) || !/[0-9]/.test(value) || !/\./.test(value) || isNaN(value))) { return label + '\n' + ''; } else if(expected === 'float-gt-0' && (!/^[0-9\.]+$/.test(value) || !/[0-9]/.test(value) || !/\./.test(value) || isNaN(value) || value <= 0)) { return label + '\n' + ' 0 (floating point number, decimals required, greater than 0).", "s2member-front", "s2member") /*b*/); ?>'; } else if(expected === 'date' && !/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/.test(value)) { return label + '\n' + ''; } else if(expected === 'email' && !/^[a-zA-Z0-9_!#$%&*+=?`{}~|\/\^\'\-]+(?:\.?[a-zA-Z0-9_!#$%&*+=?`{}~|\/\^\'\-]+)*@[a-zA-Z0-9]+(?:\-*[a-zA-Z0-9]+)*(?:\.[a-zA-Z0-9]+(?:\-*[a-zA-Z0-9]+)*)*(?:\.[a-zA-Z][a-zA-Z0-9]+)?$/.test(value)) { return label + '\n' + ''; } else if(expected === 'email' && forcePersonalEmails && nonPersonalEmailUsers.test(value)) { return label + '\n' + $.sprintf(' are problematic.", "s2member-front", "s2member") /*b*/); ?>', value.split('@')[0]); } else if(expected === 'url' && !/^https?\:\/\/.+$/i.test(value)) { return label + '\n' + ''; } else if(expected === 'domain' && !/^[a-zA-Z0-9]+(?:\-*[a-zA-Z0-9]+)*(?:\.[a-zA-Z0-9]+(?:\-*[a-zA-Z0-9]+)*)*(?:\.[a-zA-Z][a-zA-Z0-9]+)?$/.test(value)) { return label + '\n' + ''; } else if(expected === 'phone' && (!/^[0-9 ()\-]+$/.test(value) || value.replace(/[^0-9]+/g, '').length !== 10)) { return label + '\n' + ''; } else if(expected === 'uszip' && !/^[0-9]{5}(?:\-[0-9]{4})?$/.test(value)) { return label + '\n' + ''; } else if(expected === 'cazip' && !/^[0-9A-Z]{3} ?[0-9A-Z]{3}$/i.test(value)) { return label + '\n' + ''; } else if(expected === 'uczip' && !/^[0-9]{5}(?:\-[0-9]{4})?$/.test(value) && !/^[0-9A-Z]{3} ?[0-9A-Z]{3}$/i.test(value)) { return label + '\n' + ''; } else if(expected === 'ukpostcode' && !/^(([a-zA-Z]{1,2}[0-9][a-zA-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][a-zA-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[a-zA-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/.test(value)) { return label + '\n' + ''; } else if(/^alphanumerics\-spaces\-punctuation\-[0-9]+(?:\-e)?$/.test(expected) && !/^[a-z 0-9\/\\\\,.?:;"\'{}[\]\^|+=_()*&%$#@!`~\-]+$/i.test(value)) { return label + '\n' + ''; } else if(/^alphanumerics\-spaces\-[0-9]+(?:\-e)?$/.test(expected) && !/^[a-z 0-9]+$/i.test(value)) { return label + '\n' + ''; } else if(/^alphanumerics\-punctuation\-[0-9]+(?:\-e)?$/.test(expected) && !/^[a-z0-9\/\\\\,.?:;"\'{}[\]\^|+=_()*&%$#@!`~\-]+$/i.test(value)) { return label + '\n' + ''; } else if(/^alphanumerics\-[0-9]+(?:\-e)?$/.test(expected) && !/^[a-z0-9]+$/i.test(value)) { return label + '\n' + ''; } else if(/^alphabetics\-[0-9]+(?:\-e)?$/.test(expected) && !/^[a-z]+$/i.test(value)) { return label + '\n' + ''; } else if(/^numerics\-[0-9]+(?:\-e)?$/.test(expected) && !/^[0-9]+$/i.test(value)) { return label + '\n' + ''; } else if(/^(?:any|alphanumerics\-spaces\-punctuation|alphanumerics\-spaces|alphanumerics\-punctuation|alphanumerics|alphabetics|numerics)\-[0-9]+(?:\-e)?$/.test(expected)) { var split = expected.split('-'), length = Number(split[1]), exactLength = (split.length > 2 && split[2] === 'e'); if(exactLength && value.length !== length/* An exact length is required? */) return label + '\n' + $.sprintf('', length, ((split[0] === 'numerics') ? ((length === 1) ? '' : '') : ((length === 1) ? '' : ''))); else if(value.length < length/* Otherwise, we interpret as the minimum length. */) return label + '\n' + $.sprintf('', length, ((split[0] === 'numerics') ? ((length === 1) ? '' : '') : ((length === 1) ? '' : ''))); } } } return ''; // No errors in this case. }; window.ws_plugin__s2member_animateProcessing = function($obj, reset) { if(reset) $($obj).removeClass('ws-plugin--s2member-animate-processing'); else $($obj).addClass('ws-plugin--s2member-animate-processing'); }; window.ws_plugin__s2member_escAttr = window.ws_plugin__s2member_escHtml = function(string) { if(/[&\<\>"']/.test(string = String(string))) string = string.replace(/&/g, '&').replace(//g, '>'), string = string.replace(/"/g, '"').replace(/'/g, '''); return string; }; window.ws_plugin__s2member_escjQAttr = function(string) { return String(string).replace(/([.:\[\]])/g, '\\$1'); }; if(!skipAllFileConfirmations && S2MEMBER_CURRENT_USER_IS_LOGGED_IN && S2MEMBER_CURRENT_USER_DOWNLOADS_CURRENTLY < S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED) { $('a[href*="s2member_file_download="], a[href*="/s2member-files/"], a[href^="s2member-files/"], a[href*="/' + filesBaseDir.replace(/([\:\.\[\]])/g, '\\$1') + '/"], a[href^="' + filesBaseDir.replace(/([\:\.\[\]])/g, '\\$1') + '/"]') .click(function() { if(!/s2member[_\-]file[_\-]download[_\-]key[\=\-].+/i.test(this.href)/* Do NOT prompt on downloads issued with a Key. */) { var c = '' + '\n\n'; c += $.sprintf('', S2MEMBER_CURRENT_USER_DOWNLOADS_CURRENTLY, ((S2MEMBER_CURRENT_USER_DOWNLOADS_CURRENTLY === 1) ? '' : ''), ((S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED_DAYS === 1) ? '' : $.sprintf('', S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED_DAYS))) + '\n\n'; c += (S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED_IS_UNLIMITED) ? '' : $.sprintf('', S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED, ((S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED === 1) ? '' : ''), ((S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED_DAYS === 1) ? '' : $.sprintf('', S2MEMBER_CURRENT_USER_DOWNLOADS_ALLOWED_DAYS))); if((/s2member[_\-]skip[_\-]confirmation/i.test(this.href) && !/s2member[_\-]skip[_\-]confirmation[\=\-](0|no|false)/i.test(this.href)) || confirm(c)) { if($.inArray(this.href, uniqueFilesDownloadedInPage) === -1) S2MEMBER_CURRENT_USER_DOWNLOADS_CURRENTLY++, uniqueFilesDownloadedInPage.push(this.href); return true; } return false; } return true; }); } if(!/\/wp-admin([\/?#]|$)/i.test(location.href)) { $('input#ws-plugin--s2member-profile-password1, input#ws-plugin--s2member-profile-password2') .on('keyup initialize.s2', function() { ws_plugin__s2member_passwordStrength( $('input#ws-plugin--s2member-profile-login'), $('input#ws-plugin--s2member-profile-password1'), $('input#ws-plugin--s2member-profile-password2'), $('div#ws-plugin--s2member-profile-password-strength') ); }).trigger('initialize.s2'); $('form#ws-plugin--s2member-profile') .submit(function(/* Validate Profile. */) { var context = this, label = '', error = '', errors = '', $password1 = $('input#ws-plugin--s2member-profile-password1', context), $password2 = $('input#ws-plugin--s2member-profile-password2', context); var $submissionButton = $('input#ws-plugin--s2member-profile-submit', context); $(':input', context) .each(function(/* Go through them all together. */) { var id = /* Remove numeric suffixes. */ $.trim($(this).attr('id')).replace(/---[0-9]+$/g, ''); if(id && (label = $.trim($('label[for="' + id + '"]', context).first().children('strong').first().text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += /* Collect errors. */ error + '\n\n'; } }); if(errors = $.trim(errors)) { alert('' + '\n\n' + errors); return false; } else if($.trim($password1.val()) && $.trim($password1.val()) !== $.trim($password2.val())) { alert('' + '\n\n' + ''); return false; } else if($.trim($password1.val()) && $.trim($password1.val()).length < ws_plugin__s2member_passwordMinLength()) { alert('' + '\n\n' + ''); return false; } else if($.trim($password1.val()) && ws_plugin__s2member_passwordStrengthMeter($.trim($password1.val()), $.trim($password2.val()), true) < ws_plugin__s2member_passwordMinStrengthScore()) { alert('' + '\n\n' + ''); return false; } ws_plugin__s2member_animateProcessing($submissionButton); return true; }); } if(/\/wp-signup\.php/i.test(location.href)) { $('div#content > div.mu_register > form#setupform') .submit(function() { var context = this, label = '', error = '', errors = '', $submissionButton = $('p.submit input[type="submit"]', context); $('input#user_email', context).attr('data-expected', 'email'); $('input#user_name, input#user_email, input#blogname, input#blog_title, input#captcha_code', context).attr({'aria-required': 'true'}); $(':input', context) .each(function(/* Go through them all together. */) { var id = $.trim($(this).attr('id')).replace(/---[0-9]+$/g, ''/* Remove numeric suffixes. */); if(id && (label = $.trim($('label[for="' + id + '"]', context).first().text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += error + '\n\n'/* Collect errors. */; } }); if(errors = $.trim(errors)) { alert('' + '\n\n' + errors); return false; } ws_plugin__s2member_animateProcessing($submissionButton); return true; }); } if(/\/wp-login\.php/i.test(location.href)) { $('div#login > form#registerform input#user_login').attr('tabindex', '10'); $('div#login > form#registerform input#user_email').attr('tabindex', '20'); $('div#login > form#registerform input#wp-submit').attr('tabindex', '1000'); $('input#ws-plugin--s2member-custom-reg-field-user-pass1, input#ws-plugin--s2member-custom-reg-field-user-pass2') .on('keyup initialize.s2', function() { ws_plugin__s2member_passwordStrength( $('input#user_login'), $('input#ws-plugin--s2member-custom-reg-field-user-pass1'), $('input#ws-plugin--s2member-custom-reg-field-user-pass2'), $('div#ws-plugin--s2member-custom-reg-field-user-pass-strength') ); }).trigger('initialize.s2'); $('div#login > form#registerform') .submit(function() { var context = this, label = '', error = '', errors = '', $pass1 = $('input#ws-plugin--s2member-custom-reg-field-user-pass1[aria-required="true"]', context), $pass2 = $('input#ws-plugin--s2member-custom-reg-field-user-pass2', context), $submissionButton = $('input#wp-submit', context/* Registration submission button. */); $('input#user_email', context).attr('data-expected', 'email'); $('input#user_login, input#user_email, input#captcha_code', context).attr({'aria-required': 'true'}); $(':input', context) .each(function(/* Go through them all together. */) { var id = $.trim($(this).attr('id')).replace(/---[0-9]+$/g, ''/* Remove numeric suffixes. */); if($.inArray(id, ['user_login', 'user_email', 'captcha_code']) !== -1/* No for="" attribute on these fields. */) { if((label = $.trim($(this).parent('label').text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += error + '\n\n'/* Collect errors. */; } } else if(id && (label = $.trim($('label[for="' + id + '"]', context).first().children('span').first().text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += error + '\n\n'/* Collect errors. */; } }); if(errors = $.trim(errors)) { alert('' + '\n\n' + errors); return false; } else if($pass1.length && $.trim($pass1.val()) !== $.trim($pass2.val())) { alert('' + '\n\n' + ''); return false; } else if($pass1.length && $.trim($pass1.val()).length < ws_plugin__s2member_passwordMinLength()) { alert('' + '\n\n' + ''); return false; } else if($pass1.length && ws_plugin__s2member_passwordStrengthMeter($.trim($pass1.val()), $.trim($pass2.val()), true) < ws_plugin__s2member_passwordMinStrengthScore()) { alert('' + '\n\n' + ''); return false; } ws_plugin__s2member_animateProcessing($submissionButton); return true; }); } if(/\/wp-admin\/(?:user\/)?profile\.php/i.test(location.href)) { $('form#your-profile') .submit(function(/* Validation. */) { var context = this, label = '', error = '', errors = ''; $('input#email', context).attr('data-expected', 'email'); $(':input[id^="ws-plugin--s2member-profile-"]', context) .each(function(/* Go through them all together. */) { var id = /* Remove numeric suffixes. */ $.trim($(this).attr('id')).replace(/---[0-9]+$/g, ''); if(id && (label = $.trim($('label[for="' + id + '"]', context).first().text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += error + '\n\n'/* Collect errors. */; } }); if(errors = $.trim(errors)) { alert('' + '\n\n' + errors); return false; } return true; }); } if(runningBuddyPress/* Attach form submission handler to `/register` for BuddyPress. */) { $('body.registration form div#ws-plugin--s2member-custom-reg-fields-4bp-section').closest('form') .submit(function() { var context = this, label = '', error = '', errors = ''; $('input#signup_email', context).attr('data-expected', 'email'); $('input#signup_username, input#signup_email, input#signup_password, input#field_1', context).attr({'aria-required': 'true'}); $(':input', context) .each(function(/* Go through them all together. */) { var id = $.trim($(this).attr('id')).replace(/---[0-9]+$/g, ''/* Remove numeric suffixes. */); if(id && (label = $.trim($('label[for="' + id + '"]', context).first().text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += error + '\n\n'/* Collect errors. */; } }); if(errors = $.trim(errors)) { alert('' + '\n\n' + errors); return false; } return true; }); $('body.logged-in.profile.profile-edit :input.ws-plugin--s2member-profile-field-4bp').closest('form') .submit(function() { var context = this, label = '', error = '', errors = ''; $('input#field_1', context).attr({'aria-required': 'true'}); $(':input', context) .each(function(/* Go through them all together. */) { var id = $.trim($(this).attr('id')).replace(/---[0-9]+$/g, ''/* Remove numeric suffixes. */); if(id && (label = $.trim($('label[for="' + id + '"]', context).first().text().replace(/[\r\n\t]+/g, ' ')))) { if(error = ws_plugin__s2member_validationErrors(label, this, context)) errors += error + '\n\n'/* Collect errors. */; } }); if(errors = $.trim(errors)) { alert('' + '\n\n' + errors); return false; } return true; }); } });