Geen omschrijving

wpforms.js 68KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  1. /* global wpforms_settings, grecaptcha, hcaptcha, wpformsRecaptchaCallback, wpforms_validate, wpforms_datepicker, wpforms_timepicker, Mailcheck, Choices, WPFormsPasswordField, WPFormsEntryPreview, punycode, tinyMCE */
  2. 'use strict';
  3. var wpforms = window.wpforms || ( function( document, window, $ ) {
  4. var app = {
  5. /**
  6. * Start the engine.
  7. *
  8. * @since 1.2.3
  9. */
  10. init: function() {
  11. // Document ready.
  12. $( app.ready );
  13. // Page load.
  14. $( window ).on( 'load', function() {
  15. // In the case of jQuery 3.+, we need to wait for a ready event first.
  16. if ( typeof $.ready.then === 'function' ) {
  17. $.ready.then( app.load );
  18. } else {
  19. app.load();
  20. }
  21. } );
  22. app.bindUIActions();
  23. app.bindOptinMonster();
  24. },
  25. /**
  26. * Document ready.
  27. *
  28. * @since 1.2.3
  29. */
  30. ready: function() {
  31. // Clear URL - remove wpforms_form_id.
  32. app.clearUrlQuery();
  33. // Set user identifier.
  34. app.setUserIndentifier();
  35. app.loadValidation();
  36. app.loadDatePicker();
  37. app.loadTimePicker();
  38. app.loadInputMask();
  39. app.loadSmartPhoneField();
  40. app.loadPayments();
  41. app.loadMailcheck();
  42. app.loadChoicesJS();
  43. // Randomize elements.
  44. $( '.wpforms-randomize' ).each( function() {
  45. var $list = $( this ),
  46. $listItems = $list.children();
  47. while ( $listItems.length ) {
  48. $list.append( $listItems.splice( Math.floor( Math.random() * $listItems.length ), 1 )[0] );
  49. }
  50. } );
  51. // Unlock pagebreak navigation.
  52. $( '.wpforms-page-button' ).prop( 'disabled', false );
  53. $( document ).trigger( 'wpformsReady' );
  54. },
  55. /**
  56. * Page load.
  57. *
  58. * @since 1.2.3
  59. */
  60. load: function() {
  61. },
  62. //--------------------------------------------------------------------//
  63. // Initializing
  64. //--------------------------------------------------------------------//
  65. /**
  66. * Remove wpforms_form_id from URL.
  67. *
  68. * @since 1.5.2
  69. */
  70. clearUrlQuery: function() {
  71. var loc = window.location,
  72. query = loc.search;
  73. if ( query.indexOf( 'wpforms_form_id=' ) !== -1 ) {
  74. query = query.replace( /([&?]wpforms_form_id=[0-9]*$|wpforms_form_id=[0-9]*&|[?&]wpforms_form_id=[0-9]*(?=#))/, '' );
  75. history.replaceState( {}, null, loc.origin + loc.pathname + query );
  76. }
  77. },
  78. /**
  79. * Load jQuery Validation.
  80. *
  81. * @since 1.2.3
  82. */
  83. loadValidation: function() {
  84. // Only load if jQuery validation library exists.
  85. if ( typeof $.fn.validate !== 'undefined' ) {
  86. // jQuery Validation library will not correctly validate
  87. // fields that do not have a name attribute, so we use the
  88. // `wpforms-input-temp-name` class to add a temporary name
  89. // attribute before validation is initialized, then remove it
  90. // before the form submits.
  91. $( '.wpforms-input-temp-name' ).each( function( index, el ) {
  92. var random = Math.floor( Math.random() * 9999 ) + 1;
  93. $( this ).attr( 'name', 'wpf-temp-' + random );
  94. } );
  95. // Prepend URL field contents with http:// if user input doesn't contain a schema.
  96. $( '.wpforms-validate input[type=url]' ).change( function() {
  97. var url = $( this ).val();
  98. if ( ! url ) {
  99. return false;
  100. }
  101. if ( url.substr( 0, 7 ) !== 'http://' && url.substr( 0, 8 ) !== 'https://' ) {
  102. $( this ).val( 'http://' + url );
  103. }
  104. } );
  105. $.validator.messages.required = wpforms_settings.val_required;
  106. $.validator.messages.url = wpforms_settings.val_url;
  107. $.validator.messages.email = wpforms_settings.val_email;
  108. $.validator.messages.number = wpforms_settings.val_number;
  109. // Payments: Validate method for Credit Card Number.
  110. if ( typeof $.fn.payment !== 'undefined' ) {
  111. $.validator.addMethod( 'creditcard', function( value, element ) {
  112. //var type = $.payment.cardType(value);
  113. var valid = $.payment.validateCardNumber( value );
  114. return this.optional( element ) || valid;
  115. }, wpforms_settings.val_creditcard );
  116. // @todo validate CVC and expiration
  117. }
  118. // Validate method for file extensions.
  119. $.validator.addMethod( 'extension', function( value, element, param ) {
  120. param = 'string' === typeof param ? param.replace( /,/g, '|' ) : 'png|jpe?g|gif';
  121. return this.optional( element ) || value.match( new RegExp( '\\.(' + param + ')$', 'i' ) );
  122. }, wpforms_settings.val_fileextension );
  123. // Validate method for file size.
  124. $.validator.addMethod( 'maxsize', function( value, element, param ) {
  125. var maxSize = param,
  126. optionalValue = this.optional( element ),
  127. i, len, file;
  128. if ( optionalValue ) {
  129. return optionalValue;
  130. }
  131. if ( element.files && element.files.length ) {
  132. i = 0;
  133. len = element.files.length;
  134. for ( ; i < len; i++ ) {
  135. file = element.files[i];
  136. if ( file.size > maxSize ) {
  137. return false;
  138. }
  139. }
  140. }
  141. return true;
  142. }, wpforms_settings.val_filesize );
  143. // Validate email addresses.
  144. $.validator.methods.email = function( value, element ) {
  145. // Test email on the multiple @ and spaces:
  146. // - no spaces allowed in the local and domain parts
  147. // - only one @ after the local part allowed
  148. var structureTest = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test( value );
  149. // Test emails on the multiple dots:
  150. // - start and finish with dot not allowed
  151. // - two dots in a row not allowed
  152. var dotsTest = /^(?!\.)(?!.*?\.\.).*[^.]$/.test( value );
  153. return this.optional( element ) || ( structureTest && dotsTest );
  154. };
  155. // Validate email by allowlist/blocklist.
  156. $.validator.addMethod( 'restricted-email', function( value, element ) {
  157. var validator = this,
  158. $el = $( element ),
  159. $field = $el.closest( '.wpforms-field' ),
  160. $form = $el.closest( '.wpforms-form' ),
  161. isValid = 'pending';
  162. if ( ! $el.val().length ) {
  163. return true;
  164. }
  165. this.startRequest( element );
  166. $.post( {
  167. url: wpforms_settings.ajaxurl,
  168. type: 'post',
  169. async: false,
  170. data: {
  171. 'action': 'wpforms_restricted_email',
  172. 'form_id': $form.data( 'formid' ),
  173. 'field_id': $field.data( 'field-id' ),
  174. 'email': $el.val(),
  175. },
  176. dataType: 'json',
  177. success: function( response ) {
  178. var errors = {};
  179. isValid = response.success && response.data;
  180. if ( isValid ) {
  181. validator.resetInternals();
  182. validator.toHide = validator.errorsFor( element );
  183. validator.showErrors();
  184. } else {
  185. errors[ element.name ] = wpforms_settings.val_email_restricted;
  186. validator.showErrors( errors );
  187. }
  188. validator.stopRequest( element, isValid );
  189. },
  190. } );
  191. return isValid;
  192. }, wpforms_settings.val_email_restricted );
  193. // Validate confirmations.
  194. $.validator.addMethod( 'confirm', function( value, element, param ) {
  195. return value === $( element ).closest( '.wpforms-field' ).find( 'input:first-child' ).val();
  196. }, wpforms_settings.val_confirm );
  197. // Validate required payments.
  198. $.validator.addMethod( 'required-payment', function( value, element ) {
  199. return app.amountSanitize( value ) > 0;
  200. }, wpforms_settings.val_requiredpayment );
  201. // Validate 12-hour time.
  202. $.validator.addMethod( 'time12h', function( value, element ) {
  203. return this.optional( element ) || /^((0?[1-9]|1[012])(:[0-5]\d){1,2}(\ ?[AP]M))$/i.test( value );
  204. }, wpforms_settings.val_time12h );
  205. // Validate 24-hour time.
  206. $.validator.addMethod( 'time24h', function( value, element ) {
  207. return this.optional( element ) || /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(\ ?[AP]M)?$/i.test( value );
  208. }, wpforms_settings.val_time24h );
  209. // Validate time limits.
  210. $.validator.addMethod( 'time-limit', function( value, element ) { // eslint-disable-line complexity
  211. var $input = $( element ),
  212. minTime = $input.data( 'min-time' ),
  213. maxTime = $input.data( 'max-time' ),
  214. isRequired = $input.prop( 'required' ),
  215. isLimited = typeof minTime !== 'undefined';
  216. if ( ! isLimited ) {
  217. return true;
  218. }
  219. if ( ! isRequired && app.empty( value ) ) {
  220. return true;
  221. }
  222. if ( app.compareTimesGreaterThan( maxTime, minTime ) ) {
  223. return app.compareTimesGreaterThan( value, minTime ) && app.compareTimesGreaterThan( maxTime, value );
  224. }
  225. return ( app.compareTimesGreaterThan( value, minTime ) && app.compareTimesGreaterThan( value, maxTime ) ) ||
  226. ( app.compareTimesGreaterThan( minTime, value ) && app.compareTimesGreaterThan( maxTime, value ) );
  227. }, function( params, element ) {
  228. var $input = $( element ),
  229. minTime = $input.data( 'min-time' ),
  230. maxTime = $input.data( 'max-time' );
  231. // Replace `00:**pm` with `12:**pm`.
  232. minTime = minTime.replace( /^00:([0-9]{2})pm$/, '12:$1pm' );
  233. maxTime = maxTime.replace( /^00:([0-9]{2})pm$/, '12:$1pm' );
  234. // Properly format time: add space before AM/PM, make uppercase.
  235. minTime = minTime.replace( /(am|pm)/g, ' $1' ).toUpperCase();
  236. maxTime = maxTime.replace( /(am|pm)/g, ' $1' ).toUpperCase();
  237. return wpforms_settings.val_time_limit
  238. .replace( '{minTime}', minTime )
  239. .replace( '{maxTime}', maxTime );
  240. } );
  241. // Validate checkbox choice limit.
  242. $.validator.addMethod( 'check-limit', function( value, element ) {
  243. var $ul = $( element ).closest( 'ul' ),
  244. $checked = $ul.find( 'input[type="checkbox"]:checked' ),
  245. choiceLimit = parseInt( $ul.attr( 'data-choice-limit' ) || 0, 10 );
  246. if ( 0 === choiceLimit ) {
  247. return true;
  248. }
  249. return $checked.length <= choiceLimit;
  250. }, function( params, element ) {
  251. var choiceLimit = parseInt( $( element ).closest( 'ul' ).attr( 'data-choice-limit' ) || 0, 10 );
  252. return wpforms_settings.val_checklimit.replace( '{#}', choiceLimit );
  253. } );
  254. // Validate Smart Phone Field.
  255. if ( typeof $.fn.intlTelInput !== 'undefined' ) {
  256. $.validator.addMethod( 'smart-phone-field', function( value, element ) {
  257. if ( value.match( /[^\d()\-+\s]/ ) ) {
  258. return false;
  259. }
  260. return this.optional( element ) || $( element ).intlTelInput( 'isValidNumber' );
  261. }, wpforms_settings.val_phone );
  262. }
  263. // Validate Input Mask minimum length.
  264. $.validator.addMethod( 'empty-blanks', function( value, element ) {
  265. if ( typeof $.fn.inputmask === 'undefined' ) {
  266. return true;
  267. }
  268. return ! ( value.indexOf( element.inputmask.opts.placeholder ) + 1 );
  269. }, wpforms_settings.val_empty_blanks );
  270. // Validate Payment item value on zero.
  271. $.validator.addMethod( 'required-positive-number', function( value, element ) {
  272. return app.amountSanitize( value ) > 0;
  273. }, wpforms_settings.val_number_positive );
  274. // Validate US Phone Field.
  275. $.validator.addMethod( 'us-phone-field', function( value, element ) {
  276. if ( value.match( /[^\d()\-+\s]/ ) ) {
  277. return false;
  278. }
  279. return this.optional( element ) || value.replace( /[^\d]/g, '' ).length === 10;
  280. }, wpforms_settings.val_phone );
  281. // Validate International Phone Field.
  282. $.validator.addMethod( 'int-phone-field', function( value, element ) {
  283. if ( value.match( /[^\d()\-+\s]/ ) ) {
  284. return false;
  285. }
  286. return this.optional( element ) || value.replace( /[^\d]/g, '' ).length > 0;
  287. }, wpforms_settings.val_phone );
  288. // Validate password strength.
  289. $.validator.addMethod( 'password-strength', function( value, element ) {
  290. return WPFormsPasswordField.passwordStrength( value, element ) >= Number( $( element ).data( 'password-strength-level' ) );
  291. }, wpforms_settings.val_password_strength );
  292. // Finally load jQuery Validation library for our forms.
  293. $( '.wpforms-validate' ).each( function() {
  294. var form = $( this ),
  295. formID = form.data( 'formid' ),
  296. properties;
  297. // TODO: cleanup this BC with wpforms_validate.
  298. if ( typeof window['wpforms_' + formID] !== 'undefined' && window['wpforms_' + formID].hasOwnProperty( 'validate' ) ) {
  299. properties = window['wpforms_' + formID].validate;
  300. } else if ( typeof wpforms_validate !== 'undefined' ) {
  301. properties = wpforms_validate;
  302. } else {
  303. properties = {
  304. errorClass: 'wpforms-error',
  305. validClass: 'wpforms-valid',
  306. ignore: ':hidden:not(textarea.wp-editor-area), .wpforms-conditional-hide textarea.wp-editor-area',
  307. errorPlacement: function( error, element ) {
  308. if ( app.isLikertScaleField( element ) ) {
  309. element.closest( 'table' ).hasClass( 'single-row' ) ?
  310. element.closest( '.wpforms-field' ).append( error ) :
  311. element.closest( 'tr' ).find( 'th' ).append( error );
  312. } else if ( app.isWrappedField( element ) ) {
  313. element.closest( '.wpforms-field' ).append( error );
  314. } else if ( app.isDateTimeField( element ) ) {
  315. app.dateTimeErrorPlacement( element, error );
  316. } else if ( app.isFieldInColumn( element ) ) {
  317. element.parent().append( error );
  318. } else {
  319. error.insertAfter( element );
  320. }
  321. },
  322. highlight: function( element, errorClass, validClass ) {
  323. var $element = $( element ),
  324. $field = $element.closest( '.wpforms-field' ),
  325. inputName = $element.attr( 'name' );
  326. if ( 'radio' === $element.attr( 'type' ) || 'checkbox' === $element.attr( 'type' ) ) {
  327. $field.find( 'input[name="' + inputName + '"]' ).addClass( errorClass ).removeClass( validClass );
  328. } else {
  329. $element.addClass( errorClass ).removeClass( validClass );
  330. }
  331. $field.addClass( 'wpforms-has-error' );
  332. },
  333. unhighlight: function( element, errorClass, validClass ) {
  334. var $element = $( element ),
  335. $field = $element.closest( '.wpforms-field' ),
  336. inputName = $element.attr( 'name' );
  337. if ( 'radio' === $element.attr( 'type' ) || 'checkbox' === $element.attr( 'type' ) ) {
  338. $field.find( 'input[name="' + inputName + '"]' ).addClass( validClass ).removeClass( errorClass );
  339. } else {
  340. $element.addClass( validClass ).removeClass( errorClass );
  341. }
  342. $field.removeClass( 'wpforms-has-error' );
  343. },
  344. submitHandler: function( form ) {
  345. var $form = $( form ),
  346. $submit = $form.find( '.wpforms-submit' ),
  347. altText = $submit.data( 'alt-text' ),
  348. recaptchaID = $submit.get( 0 ).recaptchaID;
  349. if ( $form.data( 'token' ) && 0 === $( '.wpforms-token', $form ).length ) {
  350. $( '<input type="hidden" class="wpforms-token" name="wpforms[token]" />' )
  351. .val( $form.data( 'token' ) )
  352. .appendTo( $form );
  353. }
  354. $submit.prop( 'disabled', true );
  355. $form.find( '#wpforms-field_recaptcha-error' ).remove();
  356. // Display processing text.
  357. if ( altText ) {
  358. $submit.text( altText );
  359. }
  360. if ( ! app.empty( recaptchaID ) || recaptchaID === 0 ) {
  361. // Form contains invisible reCAPTCHA.
  362. grecaptcha.execute( recaptchaID ).then( null, function( reason ) {
  363. reason = ( null === reason ) ? '' : '<br>' + reason;
  364. $form.find( '.wpforms-recaptcha-container' ).append( '<label id="wpforms-field_recaptcha-error" class="wpforms-error"> ' + wpforms_settings.val_recaptcha_fail_msg + reason + '</label>' );
  365. $submit.prop( 'disabled', false );
  366. } );
  367. return false;
  368. }
  369. // Remove name attributes if needed.
  370. $( '.wpforms-input-temp-name' ).removeAttr( 'name' );
  371. app.formSubmit( $form );
  372. },
  373. invalidHandler: function( event, validator ) {
  374. if ( typeof validator.errorList[0] !== 'undefined' ) {
  375. app.scrollToError( $( validator.errorList[0].element ) );
  376. }
  377. },
  378. onkeyup: function( element, event ) {
  379. // This code is copied from JQuery Validate 'onkeyup' method with only one change: 'wpforms-novalidate-onkeyup' class check.
  380. var excludedKeys = [ 16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144, 225 ];
  381. if ( $( element ).hasClass( 'wpforms-novalidate-onkeyup' ) ) {
  382. return; // Disable onkeyup validation for some elements (e.g. remote calls).
  383. }
  384. if ( 9 === event.which && '' === this.elementValue( element ) || $.inArray( event.keyCode, excludedKeys ) !== -1 ) {
  385. return;
  386. } else if ( element.name in this.submitted || element.name in this.invalid ) {
  387. this.element( element );
  388. }
  389. },
  390. onfocusout: function( element ) {
  391. // This code is copied from JQuery Validate 'onfocusout' method with only one change: 'wpforms-novalidate-onkeyup' class check.
  392. var validate = false;
  393. if ( $( element ).hasClass( 'wpforms-novalidate-onkeyup' ) && ! element.value ) {
  394. validate = true; // Empty value error handling for elements with onkeyup validation disabled.
  395. }
  396. if ( ! this.checkable( element ) && ( element.name in this.submitted || ! this.optional( element ) ) ) {
  397. validate = true;
  398. }
  399. if ( validate ) {
  400. this.element( element );
  401. }
  402. },
  403. onclick: function( element ) {
  404. var validate = false,
  405. type = ( element || {} ).type,
  406. $el = $( element );
  407. if ( [ 'checkbox', 'radio' ].indexOf( type ) > -1 ) {
  408. if ( $el.hasClass( 'wpforms-likert-scale-option' ) ) {
  409. $el = $el.closest( 'tr' );
  410. } else {
  411. $el = $el.closest( '.wpforms-field' );
  412. }
  413. $el.find( 'label.wpforms-error' ).remove();
  414. validate = true;
  415. }
  416. if ( validate ) {
  417. this.element( element );
  418. }
  419. },
  420. };
  421. }
  422. form.validate( properties );
  423. } );
  424. }
  425. },
  426. /**
  427. * Is field inside column.
  428. *
  429. * @since 1.6.3
  430. *
  431. * @param {jQuery} element current form element.
  432. *
  433. * @returns {boolean} true/false.
  434. */
  435. isFieldInColumn: function( element ) {
  436. return element.parent().hasClass( 'wpforms-one-half' ) ||
  437. element.parent().hasClass( 'wpforms-two-fifths' ) ||
  438. element.parent().hasClass( 'wpforms-one-fifth' );
  439. },
  440. /**
  441. * Is datetime field.
  442. *
  443. * @since 1.6.3
  444. *
  445. * @param {jQuery} element current form element.
  446. *
  447. * @returns {boolean} true/false.
  448. */
  449. isDateTimeField: function( element ) {
  450. return element.hasClass( 'wpforms-timepicker' ) ||
  451. element.hasClass( 'wpforms-datepicker' ) ||
  452. ( element.is( 'select' ) && element.attr( 'class' ).match( /date-month|date-day|date-year/ ) );
  453. },
  454. /**
  455. * Is field wrapped in some container.
  456. *
  457. * @since 1.6.3
  458. *
  459. * @param {jQuery} element current form element.
  460. *
  461. * @returns {boolean} true/false.
  462. */
  463. isWrappedField: function( element ) { // eslint-disable-line complexity
  464. return 'checkbox' === element.attr( 'type' ) ||
  465. 'radio' === element.attr( 'type' ) ||
  466. 'range' === element.attr( 'type' ) ||
  467. 'select' === element.is( 'select' ) ||
  468. element.parent().hasClass( 'iti' ) ||
  469. element.hasClass( 'wpforms-validation-group-member' ) ||
  470. element.hasClass( 'choicesjs-select' ) ||
  471. element.hasClass( 'wpforms-net-promoter-score-option' );
  472. },
  473. /**
  474. * Is likert scale field.
  475. *
  476. * @since 1.6.3
  477. *
  478. * @param {jQuery} element current form element.
  479. *
  480. * @returns {boolean} true/false.
  481. */
  482. isLikertScaleField: function( element ) {
  483. return element.hasClass( 'wpforms-likert-scale-option' );
  484. },
  485. /**
  486. * Print error message into date time fields.
  487. *
  488. * @since 1.6.3
  489. *
  490. * @param {jQuery} element current form element.
  491. * @param {string} error Error message.
  492. */
  493. dateTimeErrorPlacement: function( element, error ) {
  494. var $wrapper = element.closest( '.wpforms-field-row-block, .wpforms-field-date-time' );
  495. if ( $wrapper.length ) {
  496. if ( ! $wrapper.find( 'label.wpforms-error' ).length ) {
  497. $wrapper.append( error );
  498. }
  499. } else {
  500. element.closest( '.wpforms-field' ).append( error );
  501. }
  502. },
  503. /**
  504. * Load jQuery Date Picker.
  505. *
  506. * @since 1.2.3
  507. */
  508. loadDatePicker: function() {
  509. // Only load if jQuery datepicker library exists.
  510. if ( typeof $.fn.flatpickr !== 'undefined' ) {
  511. $( '.wpforms-datepicker-wrap' ).each( function() {
  512. var element = $( this ),
  513. $input = element.find( 'input' ),
  514. form = element.closest( '.wpforms-form' ),
  515. formID = form.data( 'formid' ),
  516. fieldID = element.closest( '.wpforms-field' ).data( 'field-id' ),
  517. properties;
  518. if ( typeof window['wpforms_' + formID + '_' + fieldID] !== 'undefined' && window['wpforms_' + formID + '_' + fieldID].hasOwnProperty( 'datepicker' ) ) {
  519. properties = window['wpforms_' + formID + '_' + fieldID].datepicker;
  520. } else if ( typeof window['wpforms_' + formID] !== 'undefined' && window['wpforms_' + formID].hasOwnProperty( 'datepicker' ) ) {
  521. properties = window['wpforms_' + formID].datepicker;
  522. } else if ( typeof wpforms_datepicker !== 'undefined' ) {
  523. properties = wpforms_datepicker;
  524. } else {
  525. properties = {
  526. disableMobile: true,
  527. };
  528. }
  529. // Redefine locale only if user doesn't do that manually and we have the locale.
  530. if (
  531. ! properties.hasOwnProperty( 'locale' ) &&
  532. typeof wpforms_settings !== 'undefined' &&
  533. wpforms_settings.hasOwnProperty( 'locale' )
  534. ) {
  535. properties.locale = wpforms_settings.locale;
  536. }
  537. properties.wrap = true;
  538. properties.dateFormat = $input.data( 'date-format' );
  539. if ( $input.data( 'disable-past-dates' ) === 1 ) {
  540. properties.minDate = 'today';
  541. }
  542. var limitDays = $input.data( 'limit-days' ),
  543. weekDays = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
  544. if ( limitDays && limitDays !== '' ) {
  545. limitDays = limitDays.split( ',' );
  546. properties.disable = [ function( date ) {
  547. var limitDay;
  548. for ( var i in limitDays ) {
  549. limitDay = weekDays.indexOf( limitDays[ i ] );
  550. if ( limitDay === date.getDay() ) {
  551. return false;
  552. }
  553. }
  554. return true;
  555. } ];
  556. }
  557. // Toggle clear date icon.
  558. properties.onChange = function( selectedDates, dateStr, instance ) {
  559. var display = dateStr === '' ? 'none' : 'block';
  560. element.find( '.wpforms-datepicker-clear' ).css( 'display', display );
  561. };
  562. element.flatpickr( properties );
  563. } );
  564. }
  565. },
  566. /**
  567. * Load jQuery Time Picker.
  568. *
  569. * @since 1.2.3
  570. */
  571. loadTimePicker: function() {
  572. // Only load if jQuery timepicker library exists.
  573. if ( typeof $.fn.timepicker !== 'undefined' ) {
  574. $( '.wpforms-timepicker' ).each( function() {
  575. var element = $( this ),
  576. form = element.closest( '.wpforms-form' ),
  577. formID = form.data( 'formid' ),
  578. fieldID = element.closest( '.wpforms-field' ).data( 'field-id' ),
  579. properties;
  580. if (
  581. typeof window['wpforms_' + formID + '_' + fieldID] !== 'undefined' &&
  582. window['wpforms_' + formID + '_' + fieldID].hasOwnProperty( 'timepicker' )
  583. ) {
  584. properties = window['wpforms_' + formID + '_' + fieldID].timepicker;
  585. } else if (
  586. typeof window['wpforms_' + formID] !== 'undefined' &&
  587. window['wpforms_' + formID].hasOwnProperty( 'timepicker' )
  588. ) {
  589. properties = window['wpforms_' + formID].timepicker;
  590. } else if ( typeof wpforms_timepicker !== 'undefined' ) {
  591. properties = wpforms_timepicker;
  592. } else {
  593. properties = {
  594. scrollDefault: 'now',
  595. forceRoundTime: true,
  596. };
  597. }
  598. element.timepicker( properties );
  599. } );
  600. }
  601. },
  602. /**
  603. * Load jQuery input masks.
  604. *
  605. * @since 1.2.3
  606. */
  607. loadInputMask: function() {
  608. // Only load if jQuery input mask library exists.
  609. if ( typeof $.fn.inputmask === 'undefined' ) {
  610. return;
  611. }
  612. $( '.wpforms-masked-input' ).inputmask();
  613. },
  614. /**
  615. * Load smart phone field.
  616. *
  617. * @since 1.5.2
  618. */
  619. loadSmartPhoneField: function() {
  620. // Only load if library exists.
  621. if ( typeof $.fn.intlTelInput === 'undefined' ) {
  622. return;
  623. }
  624. var inputOptions = {};
  625. // Determine the country by IP if no GDPR restrictions enabled.
  626. if ( ! wpforms_settings.gdpr ) {
  627. inputOptions.geoIpLookup = app.currentIpToCountry;
  628. }
  629. // Try to kick in an alternative solution if GDPR restrictions are enabled.
  630. if ( wpforms_settings.gdpr ) {
  631. var lang = this.getFirstBrowserLanguage(),
  632. countryCode = lang.indexOf( '-' ) > -1 ? lang.split( '-' ).pop() : '';
  633. }
  634. // Make sure the library recognizes browser country code to avoid console error.
  635. if ( countryCode ) {
  636. var countryData = window.intlTelInputGlobals.getCountryData();
  637. countryData = countryData.filter( function( country ) {
  638. return country.iso2 === countryCode.toLowerCase();
  639. } );
  640. countryCode = countryData.length ? countryCode : '';
  641. }
  642. // Set default country.
  643. inputOptions.initialCountry = wpforms_settings.gdpr && countryCode ? countryCode : 'auto';
  644. $( '.wpforms-smart-phone-field' ).each( function( i, el ) {
  645. var $el = $( el );
  646. // Hidden input allows to include country code into submitted data.
  647. inputOptions.hiddenInput = $el.closest( '.wpforms-field-phone' ).data( 'field-id' );
  648. inputOptions.utilsScript = wpforms_settings.wpforms_plugin_url + 'pro/assets/js/vendor/jquery.intl-tel-input-utils.js';
  649. $el.intlTelInput( inputOptions );
  650. // For proper validation, we should preserve the name attribute of the input field.
  651. // But we need to modify original input name not to interfere with a hidden input.
  652. $el.attr( 'name', 'wpf-temp-' + $el.attr( 'name' ) );
  653. // Add special class to remove name attribute before submitting.
  654. // So, only the hidden input value will be submitted.
  655. $el.addClass( 'wpforms-input-temp-name' );
  656. // Instantly update a hidden form input with a correct data.
  657. // Previously "blur" only was used, which is broken in case Enter was used to submit the form.
  658. $el.on( 'blur input', function() {
  659. if ( $el.intlTelInput( 'isValidNumber' ) || ! app.empty( window.WPFormsEditEntry ) ) {
  660. $el.siblings( 'input[type="hidden"]' ).val( $el.intlTelInput( 'getNumber' ) );
  661. }
  662. } );
  663. } );
  664. // Update hidden input of the `Smart` phone field to be sure the latest value will be submitted.
  665. $( '.wpforms-form' ).on( 'wpformsBeforeFormSubmit', function() {
  666. $( this ).find( '.wpforms-smart-phone-field' ).trigger( 'input' );
  667. } );
  668. },
  669. /**
  670. * Payments: Do various payment-related tasks on load.
  671. *
  672. * @since 1.2.6
  673. */
  674. loadPayments: function() {
  675. // Update Total field(s) with latest calculation.
  676. $( '.wpforms-payment-total' ).each( function( index, el ) {
  677. app.amountTotal( this );
  678. } );
  679. // Credit card validation.
  680. if ( typeof $.fn.payment !== 'undefined' ) {
  681. $( '.wpforms-field-credit-card-cardnumber' ).payment( 'formatCardNumber' );
  682. $( '.wpforms-field-credit-card-cardcvc' ).payment( 'formatCardCVC' );
  683. }
  684. },
  685. /**
  686. * Load mailcheck.
  687. *
  688. * @since 1.5.3
  689. */
  690. loadMailcheck: function() {
  691. // Skip loading if `wpforms_mailcheck_enabled` filter return false.
  692. if ( ! wpforms_settings.mailcheck_enabled ) {
  693. return;
  694. }
  695. // Only load if library exists.
  696. if ( typeof $.fn.mailcheck === 'undefined' ) {
  697. return;
  698. }
  699. if ( wpforms_settings.mailcheck_domains.length > 0 ) {
  700. Mailcheck.defaultDomains = Mailcheck.defaultDomains.concat( wpforms_settings.mailcheck_domains );
  701. }
  702. if ( wpforms_settings.mailcheck_toplevel_domains.length > 0 ) {
  703. Mailcheck.defaultTopLevelDomains = Mailcheck.defaultTopLevelDomains.concat( wpforms_settings.mailcheck_toplevel_domains );
  704. }
  705. // Mailcheck suggestion.
  706. $( document ).on( 'blur', '.wpforms-field-email input', function() {
  707. var $input = $( this ),
  708. id = $input.attr( 'id' );
  709. $input.mailcheck( {
  710. suggested: function( $el, suggestion ) {
  711. if ( suggestion.address.match( /^xn--/ ) ) {
  712. suggestion.full = punycode.toUnicode( decodeURI( suggestion.full ) );
  713. var parts = suggestion.full.split( '@' );
  714. suggestion.address = parts[0];
  715. suggestion.domain = parts[1];
  716. }
  717. if ( suggestion.domain.match( /^xn--/ ) ) {
  718. suggestion.domain = punycode.toUnicode( decodeURI( suggestion.domain ) );
  719. }
  720. var address = decodeURI( suggestion.address ).replaceAll( /[<>'"()/\\|:;=@%&\s]/ig, '' ).substr( 0, 64 ),
  721. domain = decodeURI( suggestion.domain ).replaceAll( /[<>'"()/\\|:;=@%&+_\s]/ig, '' );
  722. suggestion = '<a href="#" class="mailcheck-suggestion" data-id="' + id + '" title="' + wpforms_settings.val_email_suggestion_title + '">' + address + '@' + domain + '</a>';
  723. suggestion = wpforms_settings.val_email_suggestion.replace( '{suggestion}', suggestion );
  724. $el.closest( '.wpforms-field' ).find( '#' + id + '_suggestion' ).remove();
  725. $el.parent().append( '<label class="wpforms-error mailcheck-error" id="' + id + '_suggestion">' + suggestion + '</label>' );
  726. },
  727. empty: function() {
  728. $( '#' + id + '_suggestion' ).remove();
  729. },
  730. } );
  731. } );
  732. // Apply Mailcheck suggestion.
  733. $( document ).on( 'click', '.wpforms-field-email .mailcheck-suggestion', function( e ) {
  734. var $suggestion = $( this ),
  735. $field = $suggestion.closest( '.wpforms-field' ),
  736. id = $suggestion.data( 'id' );
  737. e.preventDefault();
  738. $field.find( '#' + id ).val( $suggestion.text() );
  739. $suggestion.parent().remove();
  740. } );
  741. },
  742. /**
  743. * Load Choices.js library for all Modern style Dropdown fields (<select>).
  744. *
  745. * @since 1.6.1
  746. */
  747. loadChoicesJS: function() {
  748. // Loads if function exists.
  749. if ( typeof window.Choices !== 'function' ) {
  750. return;
  751. }
  752. $( '.wpforms-field-select-style-modern .choicesjs-select, .wpforms-field-payment-select .choicesjs-select' ).each( function( idx, el ) {
  753. var args = window.wpforms_choicesjs_config || {},
  754. searchEnabled = $( el ).data( 'search-enabled' );
  755. args.searchEnabled = 'undefined' !== typeof searchEnabled ? searchEnabled : true;
  756. args.callbackOnInit = function() {
  757. var self = this,
  758. $element = $( self.passedElement.element ),
  759. $input = $( self.input.element ),
  760. sizeClass = $element.data( 'size-class' );
  761. // Remove hidden attribute and hide `<select>` like a screen-reader text.
  762. // It's important for field validation.
  763. $element
  764. .removeAttr( 'hidden' )
  765. .addClass( self.config.classNames.input + '--hidden' );
  766. // Add CSS-class for size.
  767. if ( sizeClass ) {
  768. $( self.containerOuter.element ).addClass( sizeClass );
  769. }
  770. /**
  771. * If a multiple select has selected choices - hide a placeholder input.
  772. * We use custom styles like `.screen-reader-text` for it,
  773. * because it avoids an issue with closing a dropdown.
  774. */
  775. if ( $element.prop( 'multiple' ) ) {
  776. // On init event.
  777. if ( self.getValue( true ).length ) {
  778. $input.addClass( self.config.classNames.input + '--hidden' );
  779. }
  780. }
  781. // On change event.
  782. $element.on( 'change', function() {
  783. var validator;
  784. // Listen if multiple select has choices.
  785. if ( $element.prop( 'multiple' ) ) {
  786. self.getValue( true ).length > 0 ? $input.addClass( self.config.classNames.input + '--hidden' ) : $input.removeClass( self.config.classNames.input + '--hidden' );
  787. }
  788. validator = $element.closest( 'form' ).data( 'validator' );
  789. if ( ! validator ) {
  790. return;
  791. }
  792. validator.element( $element );
  793. } );
  794. };
  795. args.callbackOnCreateTemplates = function() {
  796. var self = this,
  797. $element = $( self.passedElement.element );
  798. return {
  799. // Change default template for option.
  800. option: function( item ) {
  801. var opt = Choices.defaults.templates.option.call( this, item );
  802. // Add a `.placeholder` class for placeholder option - it needs for WPForm CL.
  803. if ( 'undefined' !== typeof item.placeholder && true === item.placeholder ) {
  804. opt.classList.add( 'placeholder' );
  805. }
  806. // Add a `data-amount` attribute for payment dropdown.
  807. // It will be copy from a Choices.js `data-custom-properties` attribute.
  808. if ( $element.hasClass( 'wpforms-payment-price' ) && 'undefined' !== typeof item.customProperties && null !== item.customProperties ) {
  809. opt.dataset.amount = item.customProperties;
  810. }
  811. return opt;
  812. },
  813. };
  814. };
  815. // Save choicesjs instance for future access.
  816. $( el ).data( 'choicesjs', new Choices( el, args ) );
  817. } );
  818. },
  819. //--------------------------------------------------------------------//
  820. // Binds.
  821. //--------------------------------------------------------------------//
  822. /**
  823. * Element bindings.
  824. *
  825. * @since 1.2.3
  826. */
  827. bindUIActions: function() {
  828. // Pagebreak navigation.
  829. $( document ).on( 'click', '.wpforms-page-button', function( event ) {
  830. event.preventDefault();
  831. app.pagebreakNav( this );
  832. } );
  833. // Payments: Update Total field(s) when latest calculation.
  834. $( document ).on( 'change input', '.wpforms-payment-price', function() {
  835. app.amountTotal( this, true );
  836. } );
  837. // Payments: Restrict user input payment fields.
  838. $( document ).on( 'input', '.wpforms-payment-user-input', function() {
  839. var $this = $( this ),
  840. amount = $this.val();
  841. $this.val( amount.replace( /[^0-9.,]/g, '' ) );
  842. } );
  843. // Payments: Sanitize/format user input amounts.
  844. $( document ).on( 'focusout', '.wpforms-payment-user-input', function() {
  845. var $this = $( this ),
  846. amount = $this.val();
  847. if ( ! amount ) {
  848. return amount;
  849. }
  850. var sanitized = app.amountSanitize( amount ),
  851. formatted = app.amountFormat( sanitized );
  852. $this.val( formatted );
  853. } );
  854. // Payments: Update Total field(s) when conditionals are processed.
  855. $( document ).on( 'wpformsProcessConditionals', function( e, el ) {
  856. app.amountTotal( el, true );
  857. } );
  858. // Rating field: hover effect.
  859. $( '.wpforms-field-rating-item' ).hover(
  860. function() {
  861. $( this ).parent().find( '.wpforms-field-rating-item' ).removeClass( 'selected hover' );
  862. $( this ).prevAll().addBack().addClass( 'hover' );
  863. },
  864. function() {
  865. $( this ).parent().find( '.wpforms-field-rating-item' ).removeClass( 'selected hover' );
  866. $( this ).parent().find( 'input:checked' ).parent().prevAll().addBack().addClass( 'selected' );
  867. }
  868. );
  869. // Rating field: toggle selected state.
  870. $( document ).on( 'change', '.wpforms-field-rating-item input', function() {
  871. var $this = $( this ),
  872. $wrap = $this.closest( '.wpforms-field-rating-items' ),
  873. $items = $wrap.find( '.wpforms-field-rating-item' );
  874. $items.removeClass( 'hover selected' );
  875. $this.parent().prevAll().addBack().addClass( 'selected' );
  876. } );
  877. // Rating field: preselect the selected rating (from dynamic/fallback population).
  878. $( function() {
  879. $( '.wpforms-field-rating-item input:checked' ).change();
  880. } );
  881. // Checkbox/Radio/Payment checkbox: make labels keyboard-accessible.
  882. $( document ).on( 'keypress', '.wpforms-image-choices-item label', function( event ) {
  883. var $this = $( this ),
  884. $field = $this.closest( '.wpforms-field' );
  885. if ( $field.hasClass( 'wpforms-conditional-hide' ) ) {
  886. event.preventDefault();
  887. return false;
  888. }
  889. // Cause the input to be clicked when clicking the label.
  890. if ( 13 === event.which ) {
  891. $( '#' + $this.attr( 'for' ) ).click();
  892. }
  893. } );
  894. // IE: Click on the `image choice` image should trigger the click event on the input (checkbox or radio) field.
  895. if ( window.document.documentMode ) {
  896. $( document ).on( 'click', '.wpforms-image-choices-item img', function() {
  897. $( this ).closest( 'label' ).find( 'input' ).click();
  898. } );
  899. }
  900. $( document ).on( 'change', '.wpforms-field-checkbox input, .wpforms-field-radio input, .wpforms-field-payment-multiple input, .wpforms-field-payment-checkbox input, .wpforms-field-gdpr-checkbox input', function( event ) {
  901. var $this = $( this ),
  902. $field = $this.closest( '.wpforms-field' );
  903. if ( $field.hasClass( 'wpforms-conditional-hide' ) ) {
  904. event.preventDefault();
  905. return false;
  906. }
  907. switch ( $this.attr( 'type' ) ) {
  908. case 'radio':
  909. $this.closest( 'ul' ).find( 'li' ).removeClass( 'wpforms-selected' ).find( 'input[type=radio]' ).removeProp( 'checked' );
  910. $this
  911. .prop( 'checked', true )
  912. .closest( 'li' ).addClass( 'wpforms-selected' );
  913. break;
  914. case 'checkbox':
  915. if ( $this.is( ':checked' ) ) {
  916. $this.closest( 'li' ).addClass( 'wpforms-selected' );
  917. $this.prop( 'checked', true );
  918. } else {
  919. $this.closest( 'li' ).removeClass( 'wpforms-selected' );
  920. $this.prop( 'checked', false );
  921. }
  922. break;
  923. }
  924. } );
  925. // Upload fields: Check combined file size.
  926. $( document ).on( 'change', '.wpforms-field-file-upload input[type=file]:not(".dropzone-input")', function() {
  927. var $this = $( this ),
  928. $uploads = $this.closest( 'form.wpforms-form' ).find( '.wpforms-field-file-upload input:not(".dropzone-input")' ),
  929. totalSize = 0,
  930. postMaxSize = Number( wpforms_settings.post_max_size ),
  931. errorMsg = '<div class="wpforms-error-container-post_max_size">' + wpforms_settings.val_post_max_size + '</div>',
  932. errorCntTpl = '<div class="wpforms-error-container">{errorMsg}</span></div>',
  933. $submitCnt = $this.closest( 'form.wpforms-form' ).find( '.wpforms-submit-container' ),
  934. $submitBtn = $submitCnt.find( 'button.wpforms-submit' ),
  935. $errorCnt = $submitCnt.prev();
  936. // Calculating totalSize.
  937. $uploads.each( function() {
  938. var $upload = $( this ),
  939. i = 0,
  940. len = $upload[0].files.length;
  941. for ( ; i < len; i++ ) {
  942. totalSize += $upload[0].files[i].size;
  943. }
  944. } );
  945. // Checking totalSize.
  946. if ( totalSize > postMaxSize ) {
  947. // Convert sizes to Mb.
  948. totalSize = Number( ( totalSize / 1048576 ).toFixed( 3 ) );
  949. postMaxSize = Number( ( postMaxSize / 1048576 ).toFixed( 3 ) );
  950. // Preparing error message.
  951. errorMsg = errorMsg.replace( /{totalSize}/, totalSize ).replace( /{maxSize}/, postMaxSize );
  952. // Output error message.
  953. if ( $errorCnt.hasClass( 'wpforms-error-container' ) ) {
  954. $errorCnt.find( '.wpforms-error-container-post_max_size' ).remove();
  955. $errorCnt.append( errorMsg );
  956. } else {
  957. $submitCnt.before( errorCntTpl.replace( /{errorMsg}/, errorMsg ) );
  958. }
  959. // Disable submit button.
  960. $submitBtn.prop( 'disabled', true );
  961. } else {
  962. // Remove error and release submit button.
  963. $errorCnt.find( '.wpforms-error-container-post_max_size' ).remove();
  964. $submitBtn.prop( 'disabled', false );
  965. }
  966. } );
  967. // Number Slider field: update hints.
  968. $( document ).on( 'change input', '.wpforms-field-number-slider input[type=range]', function( event ) {
  969. var hintEl = $( event.target ).siblings( '.wpforms-field-number-slider-hint' );
  970. hintEl.html( hintEl.data( 'hint' ).replace( '{value}', '<b>' + event.target.value + '</b>' ) );
  971. } );
  972. // Enter key event.
  973. $( document ).on( 'keydown', '.wpforms-form input', function( e ) {
  974. if ( e.keyCode !== 13 ) {
  975. return;
  976. }
  977. var $t = $( this ),
  978. $page = $t.closest( '.wpforms-page' );
  979. if ( $page.length === 0 ) {
  980. return;
  981. }
  982. if ( [ 'text', 'tel', 'number', 'email', 'url', 'radio', 'checkbox' ].indexOf( $t.attr( 'type' ) ) < 0 ) {
  983. return;
  984. }
  985. if ( $t.hasClass( 'wpforms-datepicker' ) ) {
  986. $t.flatpickr( 'close' );
  987. }
  988. e.preventDefault();
  989. if ( $page.hasClass( 'last' ) ) {
  990. $page.closest( '.wpforms-form' ).find( '.wpforms-submit' ).click();
  991. return;
  992. }
  993. $page.find( '.wpforms-page-next' ).click();
  994. } );
  995. // Allow only numbers, minus and decimal point to be entered into the Numbers field.
  996. $( document ).on( 'keypress', '.wpforms-field-number input', function( e ) {
  997. return /^[-0-9.]+$/.test( String.fromCharCode( e.keyCode || e.which ) );
  998. } );
  999. },
  1000. /**
  1001. * Entry preview field callback for a page changing.
  1002. *
  1003. * @since 1.6.9
  1004. * @deprecated 1.7.0
  1005. *
  1006. * @param {Event} event Event.
  1007. * @param {int} currentPage Current page.
  1008. * @param {jQuery} $form Current form.
  1009. */
  1010. entryPreviewFieldPageChange: function( event, currentPage, $form ) {
  1011. console.warn( 'WARNING! Obsolete function called. Function wpforms.entryPreviewFieldPageChange has been deprecated, please use the WPFormsEntryPreview.pageChange function instead!' );
  1012. WPFormsEntryPreview.pageChange( event, currentPage, $form );
  1013. },
  1014. /**
  1015. * Update the entry preview fields on the page.
  1016. *
  1017. * @since 1.6.9
  1018. * @deprecated 1.7.0
  1019. *
  1020. * @param {int} currentPage Current page.
  1021. * @param {jQuery} $form Current form.
  1022. */
  1023. entryPreviewFieldUpdate: function( currentPage, $form ) {
  1024. console.warn( 'WARNING! Obsolete function called. Function wpforms.entryPreviewFieldUpdate has been deprecated, please use the WPFormsEntryPreview.update function instead!' );
  1025. WPFormsEntryPreview.update( currentPage, $form );
  1026. },
  1027. /**
  1028. * Scroll to and focus on the field with error.
  1029. *
  1030. * @since 1.5.8
  1031. *
  1032. * @param {jQuery} $el Form, container or input element jQuery object.
  1033. */
  1034. scrollToError: function( $el ) {
  1035. if ( $el.length === 0 ) {
  1036. return;
  1037. }
  1038. // Look for a field with an error inside an $el.
  1039. var $field = $el.find( '.wpforms-field.wpforms-has-error' );
  1040. // Look outside in not found inside.
  1041. if ( $field.length === 0 ) {
  1042. $field = $el.closest( '.wpforms-field' );
  1043. }
  1044. if ( $field.length === 0 ) {
  1045. return;
  1046. }
  1047. var offset = $field.offset();
  1048. if ( typeof offset === 'undefined' ) {
  1049. return;
  1050. }
  1051. app.animateScrollTop( offset.top - 75, 750 ).done( function() {
  1052. var $error = $field.find( '.wpforms-error' ).first();
  1053. if ( typeof $error.focus === 'function' ) {
  1054. $error.focus();
  1055. }
  1056. } );
  1057. },
  1058. /**
  1059. * Update Pagebreak navigation.
  1060. *
  1061. * @since 1.2.2
  1062. *
  1063. * @param {jQuery} el jQuery element object.
  1064. */
  1065. pagebreakNav: function( el ) {
  1066. var $this = $( el ),
  1067. valid = true,
  1068. action = $this.data( 'action' ),
  1069. page = $this.data( 'page' ),
  1070. page2 = page,
  1071. next = page + 1,
  1072. prev = page - 1,
  1073. $form = $this.closest( '.wpforms-form' ),
  1074. $page = $form.find( '.wpforms-page-' + page ),
  1075. $submit = $form.find( '.wpforms-submit-container' ),
  1076. $indicator = $form.find( '.wpforms-page-indicator' ),
  1077. $reCAPTCHA = $form.find( '.wpforms-recaptcha-container' ),
  1078. pageScroll = false;
  1079. app.saveTinyMCE();
  1080. // Page scroll.
  1081. // TODO: cleanup this BC with wpform_pageScroll.
  1082. if ( false === window.wpforms_pageScroll ) {
  1083. pageScroll = false;
  1084. } else if ( ! app.empty( window.wpform_pageScroll ) ) {
  1085. pageScroll = window.wpform_pageScroll;
  1086. } else {
  1087. pageScroll = $indicator.data( 'scroll' ) !== 0 ? 75 : false;
  1088. }
  1089. // Toggling between the pages.
  1090. if ( 'next' === action ) {
  1091. // Validate.
  1092. if ( typeof $.fn.validate !== 'undefined' ) {
  1093. $page.find( ':input' ).each( function( index, el ) {
  1094. // Skip input fields without `name` attribute, which could have fields.
  1095. // E.g. `Placeholder` input for Modern dropdown.
  1096. if ( ! $( el ).attr( 'name' ) ) {
  1097. return;
  1098. }
  1099. if ( ! $( el ).valid() ) {
  1100. valid = false;
  1101. }
  1102. } );
  1103. // Scroll to first/top error on page.
  1104. app.scrollToError( $page );
  1105. }
  1106. // Move to the next page.
  1107. if ( valid ) {
  1108. page2 = next;
  1109. $this.trigger( 'wpformsBeforePageChange', [ page2, $form ] );
  1110. $page.hide();
  1111. var $nextPage = $form.find( '.wpforms-page-' + next );
  1112. $nextPage.show();
  1113. if ( $nextPage.hasClass( 'last' ) ) {
  1114. $reCAPTCHA.show();
  1115. $submit.show();
  1116. }
  1117. if ( pageScroll ) {
  1118. // Scroll to top of the form.
  1119. app.animateScrollTop( $form.offset().top - pageScroll, 750 );
  1120. }
  1121. $this.trigger( 'wpformsPageChange', [ page2, $form ] );
  1122. }
  1123. } else if ( 'prev' === action ) {
  1124. // Move to the prev page.
  1125. page2 = prev;
  1126. $this.trigger( 'wpformsBeforePageChange', [ page2, $form ] );
  1127. $page.hide();
  1128. $form.find( '.wpforms-page-' + prev ).show();
  1129. $reCAPTCHA.hide();
  1130. $submit.hide();
  1131. if ( pageScroll ) {
  1132. // Scroll to the top of the form.
  1133. app.animateScrollTop( $form.offset().top - pageScroll );
  1134. }
  1135. $this.trigger( 'wpformsPageChange', [ page2, $form ] );
  1136. }
  1137. if ( $indicator ) {
  1138. var theme = $indicator.data( 'indicator' ),
  1139. color = $indicator.data( 'indicator-color' );
  1140. if ( 'connector' === theme || 'circles' === theme ) {
  1141. $indicator.find( '.wpforms-page-indicator-page' ).removeClass( 'active' );
  1142. $indicator.find( '.wpforms-page-indicator-page-' + page2 ).addClass( 'active' );
  1143. $indicator.find( '.wpforms-page-indicator-page-number' ).removeAttr( 'style' );
  1144. $indicator.find( '.active .wpforms-page-indicator-page-number' ).css( 'background-color', color );
  1145. if ( 'connector' === theme ) {
  1146. $indicator.find( '.wpforms-page-indicator-page-triangle' ).removeAttr( 'style' );
  1147. $indicator.find( '.active .wpforms-page-indicator-page-triangle' ).css( 'border-top-color', color );
  1148. }
  1149. } else if ( 'progress' === theme ) {
  1150. var $pageTitle = $indicator.find( '.wpforms-page-indicator-page-title' ),
  1151. $pageSep = $indicator.find( '.wpforms-page-indicator-page-title-sep' ),
  1152. totalPages = $form.find( '.wpforms-page' ).length,
  1153. width = ( page2 / totalPages ) * 100;
  1154. $indicator.find( '.wpforms-page-indicator-page-progress' ).css( 'width', width + '%' );
  1155. $indicator.find( '.wpforms-page-indicator-steps-current' ).text( page2 );
  1156. if ( $pageTitle.data( 'page-' + page2 + '-title' ) ) {
  1157. $pageTitle.css( 'display', 'inline' ).text( $pageTitle.data( 'page-' + page2 + '-title' ) );
  1158. $pageSep.css( 'display', 'inline' );
  1159. } else {
  1160. $pageTitle.css( 'display', 'none' );
  1161. $pageSep.css( 'display', 'none' );
  1162. }
  1163. }
  1164. }
  1165. },
  1166. /**
  1167. * OptinMonster compatibility.
  1168. *
  1169. * Re-initialize after OptinMonster loads to accommodate changes that
  1170. * have occurred to the DOM.
  1171. *
  1172. * @since 1.5.0
  1173. */
  1174. bindOptinMonster: function() {
  1175. // OM v5.
  1176. document.addEventListener( 'om.Campaign.load', function( event ) {
  1177. app.ready();
  1178. app.optinMonsterRecaptchaReset( event.detail.Campaign.data.id );
  1179. } );
  1180. // OM Legacy.
  1181. $( document ).on( 'OptinMonsterOnShow', function( event, data, object ) {
  1182. app.ready();
  1183. app.optinMonsterRecaptchaReset( data.optin );
  1184. } );
  1185. },
  1186. /**
  1187. * Reset/recreate hCaptcha/reCAPTCHA v2 inside OptinMonster.
  1188. *
  1189. * @since 1.5.0
  1190. * @since 1.6.4 Added hCaptcha support.
  1191. *
  1192. * @param {string} optinId OptinMonster ID.
  1193. */
  1194. optinMonsterRecaptchaReset: function( optinId ) {
  1195. var $form = $( '#om-' + optinId ).find( '.wpforms-form' ),
  1196. $captchaContainer = $form.find( '.wpforms-recaptcha-container' ),
  1197. $captcha = $form.find( '.g-recaptcha' );
  1198. if ( $form.length && $captcha.length ) {
  1199. var captchaSiteKey = $captcha.attr( 'data-sitekey' ),
  1200. captchaID = 'recaptcha-' + Date.now(),
  1201. apiVar = $captchaContainer.hasClass( 'wpforms-is-hcaptcha' ) ? hcaptcha : grecaptcha;
  1202. $captcha.remove();
  1203. $captchaContainer.prepend( '<div class="g-recaptcha" id="' + captchaID + '" data-sitekey="' + captchaSiteKey + '"></div>' );
  1204. apiVar.render(
  1205. captchaID,
  1206. {
  1207. sitekey: captchaSiteKey,
  1208. callback: function() {
  1209. wpformsRecaptchaCallback( $( '#' + captchaID ) );
  1210. },
  1211. }
  1212. );
  1213. }
  1214. },
  1215. //--------------------------------------------------------------------//
  1216. // Other functions.
  1217. //--------------------------------------------------------------------//
  1218. /**
  1219. * Payments: Run amount calculation and update the Total field value.
  1220. *
  1221. * @since 1.2.3
  1222. * @since 1.5.1 Added support for payment-checkbox field.
  1223. *
  1224. * @param {object} el jQuery DOM object.
  1225. * @param {boolean} validate Whether to validate or not.
  1226. */
  1227. amountTotal: function( el, validate ) {
  1228. validate = validate || false;
  1229. var $form = $( el ).closest( '.wpforms-form' ),
  1230. currency = app.getCurrency(),
  1231. total = app.amountTotalCalc( $form ),
  1232. totalFormatted,
  1233. totalFormattedSymbol;
  1234. totalFormatted = app.amountFormat( total );
  1235. if ( 'left' === currency.symbol_pos ) {
  1236. totalFormattedSymbol = currency.symbol + ' ' + totalFormatted;
  1237. } else {
  1238. totalFormattedSymbol = totalFormatted + ' ' + currency.symbol;
  1239. }
  1240. $form.find( '.wpforms-payment-total' ).each( function( index, el ) {
  1241. if ( 'hidden' === $( this ).attr( 'type' ) || 'text' === $( this ).attr( 'type' ) ) {
  1242. $( this ).val( totalFormattedSymbol );
  1243. if ( 'text' === $( this ).attr( 'type' ) && validate && $form.data( 'validator' ) ) {
  1244. $( this ).valid();
  1245. }
  1246. } else {
  1247. $( this ).text( totalFormattedSymbol );
  1248. }
  1249. } );
  1250. },
  1251. /**
  1252. * Payments: Calculate a total amount without formatting.
  1253. *
  1254. * @since 1.6.7.1
  1255. *
  1256. * @param {jQuery} $form Form element.
  1257. *
  1258. * @returns {number} Total amount.
  1259. */
  1260. amountTotalCalc: function( $form ) {
  1261. var total = 0;
  1262. $( '.wpforms-payment-price', $form ).each( function() {
  1263. var amount = 0,
  1264. $this = $( this ),
  1265. type = $this.attr( 'type' );
  1266. if ( $this.closest( '.wpforms-field-payment-single' ).hasClass( 'wpforms-conditional-hide' ) ) {
  1267. return;
  1268. }
  1269. if ( type === 'text' || type === 'hidden' ) {
  1270. amount = $this.val();
  1271. } else if ( ( type === 'radio' || type === 'checkbox' ) && $this.is( ':checked' ) ) {
  1272. amount = $this.data( 'amount' );
  1273. } else if ( $this.is( 'select' ) && $this.find( 'option:selected' ).length > 0 ) {
  1274. amount = $this.find( 'option:selected' ).data( 'amount' );
  1275. }
  1276. if ( ! app.empty( amount ) ) {
  1277. amount = app.amountSanitize( amount );
  1278. total = Number( total ) + Number( amount );
  1279. }
  1280. } );
  1281. return total;
  1282. },
  1283. /**
  1284. * Sanitize amount and convert to standard format for calculations.
  1285. *
  1286. * @since 1.2.6
  1287. *
  1288. * @param {string} amount Amount to sanitize.
  1289. *
  1290. * @returns {string} Sanitized amount.
  1291. */
  1292. amountSanitize: function( amount ) {
  1293. var currency = app.getCurrency();
  1294. amount = amount.toString().replace( /[^0-9.,]/g, '' );
  1295. if ( currency.decimal_sep === ',' ) {
  1296. if ( currency.thousands_sep === '.' && amount.indexOf( currency.thousands_sep ) !== -1 ) {
  1297. amount = amount.replace( new RegExp( '\\' + currency.thousands_sep, 'g' ), '' );
  1298. } else if ( currency.thousands_sep === '' && amount.indexOf( '.' ) !== -1 ) {
  1299. amount = amount.replace( /\./g, '' );
  1300. }
  1301. amount = amount.replace( currency.decimal_sep, '.' );
  1302. } else if ( currency.thousands_sep === ',' && ( amount.indexOf( currency.thousands_sep ) !== -1 ) ) {
  1303. amount = amount.replace( new RegExp( '\\' + currency.thousands_sep, 'g' ), '' );
  1304. }
  1305. return app.numberFormat( amount, currency.decimals, '.', '' );
  1306. },
  1307. /**
  1308. * Format amount.
  1309. *
  1310. * @since 1.2.6
  1311. *
  1312. * @param {string|number} amount Amount to format.
  1313. *
  1314. * @returns {string} Formatted amount.
  1315. */
  1316. amountFormat: function( amount ) {
  1317. var currency = app.getCurrency();
  1318. amount = String( amount );
  1319. // Format the amount
  1320. if ( ',' === currency.decimal_sep && ( amount.indexOf( currency.decimal_sep ) !== -1 ) ) {
  1321. var sepFound = amount.indexOf( currency.decimal_sep ),
  1322. whole = amount.substr( 0, sepFound ),
  1323. part = amount.substr( sepFound + 1, amount.length - 1 );
  1324. amount = whole + '.' + part;
  1325. }
  1326. // Strip , from the amount (if set as the thousands separator)
  1327. if ( ',' === currency.thousands_sep && ( amount.indexOf( currency.thousands_sep ) !== -1 ) ) {
  1328. amount = amount.replace( /,/g, '' );
  1329. }
  1330. if ( app.empty( amount ) ) {
  1331. amount = 0;
  1332. }
  1333. return app.numberFormat( amount, currency.decimals, currency.decimal_sep, currency.thousands_sep );
  1334. },
  1335. /**
  1336. * Get site currency settings.
  1337. *
  1338. * @since 1.2.6
  1339. *
  1340. * @returns {object} Currency data object.
  1341. */
  1342. getCurrency: function() {
  1343. var currency = {
  1344. code: 'USD',
  1345. thousands_sep: ',',
  1346. decimals: 2,
  1347. decimal_sep: '.',
  1348. symbol: '$',
  1349. symbol_pos: 'left',
  1350. };
  1351. // Backwards compatibility.
  1352. if ( typeof wpforms_settings.currency_code !== 'undefined' ) {
  1353. currency.code = wpforms_settings.currency_code;
  1354. }
  1355. if ( typeof wpforms_settings.currency_thousands !== 'undefined' ) {
  1356. currency.thousands_sep = wpforms_settings.currency_thousands;
  1357. }
  1358. if ( typeof wpforms_settings.currency_decimals !== 'undefined' ) {
  1359. currency.decimals = wpforms_settings.currency_decimals;
  1360. }
  1361. if ( typeof wpforms_settings.currency_decimal !== 'undefined' ) {
  1362. currency.decimal_sep = wpforms_settings.currency_decimal;
  1363. }
  1364. if ( typeof wpforms_settings.currency_symbol !== 'undefined' ) {
  1365. currency.symbol = wpforms_settings.currency_symbol;
  1366. }
  1367. if ( typeof wpforms_settings.currency_symbol_pos !== 'undefined' ) {
  1368. currency.symbol_pos = wpforms_settings.currency_symbol_pos;
  1369. }
  1370. return currency;
  1371. },
  1372. /**
  1373. * Format number.
  1374. *
  1375. * @see http://locutus.io/php/number_format/
  1376. *
  1377. * @since 1.2.6
  1378. *
  1379. * @param {string} number Number to format.
  1380. * @param {number} decimals How many decimals should be there.
  1381. * @param {string} decimalSep What is the decimal separator.
  1382. * @param {string} thousandsSep What is the thousands separator.
  1383. *
  1384. * @returns {string} Formatted number.
  1385. */
  1386. numberFormat: function( number, decimals, decimalSep, thousandsSep ) {
  1387. number = ( number + '' ).replace( /[^0-9+\-Ee.]/g, '' );
  1388. var n = ! isFinite( +number ) ? 0 : +number;
  1389. var prec = ! isFinite( +decimals ) ? 0 : Math.abs( decimals );
  1390. var sep = ( 'undefined' === typeof thousandsSep ) ? ',' : thousandsSep;
  1391. var dec = ( 'undefined' === typeof decimalSep ) ? '.' : decimalSep;
  1392. var s;
  1393. var toFixedFix = function( n, prec ) {
  1394. var k = Math.pow( 10, prec );
  1395. return '' + ( Math.round( n * k ) / k ).toFixed( prec );
  1396. };
  1397. // @todo: for IE parseFloat(0.55).toFixed(0) = 0;
  1398. s = ( prec ? toFixedFix( n, prec ) : '' + Math.round( n ) ).split( '.' );
  1399. if ( s[0].length > 3 ) {
  1400. s[0] = s[0].replace( /\B(?=(?:\d{3})+(?!\d))/g, sep );
  1401. }
  1402. if ( ( s[1] || '' ).length < prec ) {
  1403. s[1] = s[1] || '';
  1404. s[1] += new Array( prec - s[1].length + 1 ).join( '0' );
  1405. }
  1406. return s.join( dec );
  1407. },
  1408. /**
  1409. * Empty check similar to PHP.
  1410. *
  1411. * @see http://locutus.io/php/empty/
  1412. *
  1413. * @since 1.2.6
  1414. *
  1415. * @param {mixed} mixedVar Variable to check.
  1416. *
  1417. * @returns {boolean} Whether the var is empty or not.
  1418. */
  1419. empty: function( mixedVar ) {
  1420. var undef;
  1421. var key;
  1422. var i;
  1423. var len;
  1424. var emptyValues = [ undef, null, false, 0, '', '0' ];
  1425. for ( i = 0, len = emptyValues.length; i < len; i++ ) {
  1426. if ( mixedVar === emptyValues[i] ) {
  1427. return true;
  1428. }
  1429. }
  1430. if ( 'object' === typeof mixedVar ) {
  1431. for ( key in mixedVar ) {
  1432. if ( mixedVar.hasOwnProperty( key ) ) {
  1433. return false;
  1434. }
  1435. }
  1436. return true;
  1437. }
  1438. return false;
  1439. },
  1440. /**
  1441. * Set cookie container user UUID.
  1442. *
  1443. * @since 1.3.3
  1444. */
  1445. setUserIndentifier: function() {
  1446. if ( ( ( ! window.hasRequiredConsent && typeof wpforms_settings !== 'undefined' && wpforms_settings.uuid_cookie ) || ( window.hasRequiredConsent && window.hasRequiredConsent() ) ) && ! app.getCookie( '_wpfuuid' ) ) {
  1447. // Generate UUID - http://stackoverflow.com/a/873856/1489528
  1448. var s = new Array( 36 ),
  1449. hexDigits = '0123456789abcdef',
  1450. uuid;
  1451. for ( var i = 0; i < 36; i++ ) {
  1452. s[i] = hexDigits.substr( Math.floor( Math.random() * 0x10 ), 1 );
  1453. }
  1454. s[14] = '4';
  1455. s[19] = hexDigits.substr( ( s[19] & 0x3 ) | 0x8, 1 );
  1456. s[8] = s[13] = s[18] = s[23] = '-';
  1457. uuid = s.join( '' );
  1458. app.createCookie( '_wpfuuid', uuid, 3999 );
  1459. }
  1460. },
  1461. /**
  1462. * Create cookie.
  1463. *
  1464. * @since 1.3.3
  1465. *
  1466. * @param {string} name Cookie name.
  1467. * @param {string} value Cookie value.
  1468. * @param {string} days Whether it should expire and when.
  1469. */
  1470. createCookie: function( name, value, days ) {
  1471. var expires = '';
  1472. var secure = '';
  1473. if ( wpforms_settings.is_ssl ) {
  1474. secure = ';secure';
  1475. }
  1476. // If we have a days value, set it in the expiry of the cookie.
  1477. if ( days ) {
  1478. // If -1 is our value, set a session-based cookie instead of a persistent cookie.
  1479. if ( '-1' === days ) {
  1480. expires = '';
  1481. } else {
  1482. var date = new Date();
  1483. date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
  1484. expires = ';expires=' + date.toGMTString();
  1485. }
  1486. } else {
  1487. expires = ';expires=Thu, 01 Jan 1970 00:00:01 GMT';
  1488. }
  1489. // Write the cookie.
  1490. document.cookie = name + '=' + value + expires + ';path=/;samesite=strict' + secure;
  1491. },
  1492. /**
  1493. * Retrieve cookie.
  1494. *
  1495. * @since 1.3.3
  1496. *
  1497. * @param {string} name Cookie name.
  1498. *
  1499. * @returns {string|null} Cookie value or null when it doesn't exist.
  1500. */
  1501. getCookie: function( name ) {
  1502. var nameEQ = name + '=',
  1503. ca = document.cookie.split( ';' );
  1504. for ( var i = 0; i < ca.length; i++ ) {
  1505. var c = ca[i];
  1506. while ( ' ' === c.charAt( 0 ) ) {
  1507. c = c.substring( 1, c.length );
  1508. }
  1509. if ( 0 === c.indexOf( nameEQ ) ) {
  1510. return c.substring( nameEQ.length, c.length );
  1511. }
  1512. }
  1513. return null;
  1514. },
  1515. /**
  1516. * Delete cookie.
  1517. *
  1518. * @since 1.3.3
  1519. *
  1520. * @param {string} name Cookie name.
  1521. */
  1522. removeCookie: function( name ) {
  1523. app.createCookie( name, '', -1 );
  1524. },
  1525. /**
  1526. * Get user browser preferred language.
  1527. *
  1528. * @since 1.5.2
  1529. *
  1530. * @returns {string} Language code.
  1531. */
  1532. getFirstBrowserLanguage: function() {
  1533. var nav = window.navigator,
  1534. browserLanguagePropertyKeys = [ 'language', 'browserLanguage', 'systemLanguage', 'userLanguage' ],
  1535. i,
  1536. language;
  1537. // Support for HTML 5.1 "navigator.languages".
  1538. if ( Array.isArray( nav.languages ) ) {
  1539. for ( i = 0; i < nav.languages.length; i++ ) {
  1540. language = nav.languages[ i ];
  1541. if ( language && language.length ) {
  1542. return language;
  1543. }
  1544. }
  1545. }
  1546. // Support for other well known properties in browsers.
  1547. for ( i = 0; i < browserLanguagePropertyKeys.length; i++ ) {
  1548. language = nav[ browserLanguagePropertyKeys[ i ] ];
  1549. if ( language && language.length ) {
  1550. return language;
  1551. }
  1552. }
  1553. return '';
  1554. },
  1555. /**
  1556. * Asynchronously fetches country code using current IP
  1557. * and executes a callback provided with a country code parameter.
  1558. *
  1559. * @since 1.5.2
  1560. *
  1561. * @param {Function} callback Executes once the fetch is completed.
  1562. */
  1563. currentIpToCountry: function( callback ) {
  1564. var fallback = function() {
  1565. $.get( 'https://ipapi.co/jsonp', function() {}, 'jsonp' )
  1566. .always( function( resp ) {
  1567. var countryCode = ( resp && resp.country ) ? resp.country : '';
  1568. if ( ! countryCode ) {
  1569. var lang = app.getFirstBrowserLanguage();
  1570. countryCode = lang.indexOf( '-' ) > -1 ? lang.split( '-' ).pop() : '';
  1571. }
  1572. callback( countryCode );
  1573. } );
  1574. };
  1575. $.get( 'https://geo.wpforms.com/v3/geolocate/json' )
  1576. .done( function( resp ) {
  1577. if ( resp && resp.country_iso ) {
  1578. callback( resp.country_iso );
  1579. } else {
  1580. fallback();
  1581. }
  1582. } )
  1583. .fail( function( resp ) {
  1584. fallback();
  1585. } );
  1586. },
  1587. /**
  1588. * Form submit.
  1589. *
  1590. * @since 1.5.3
  1591. *
  1592. * @param {jQuery} $form Form element.
  1593. */
  1594. formSubmit: function( $form ) {
  1595. // Form element was passed from vanilla JavaScript.
  1596. if ( ! ( $form instanceof jQuery ) ) {
  1597. $form = $( $form );
  1598. }
  1599. app.saveTinyMCE();
  1600. $form.trigger( 'wpformsBeforeFormSubmit' );
  1601. if ( $form.hasClass( 'wpforms-ajax-form' ) && typeof FormData !== 'undefined' ) {
  1602. app.formSubmitAjax( $form );
  1603. } else {
  1604. app.formSubmitNormal( $form );
  1605. }
  1606. },
  1607. /**
  1608. * Normal form submit with page reload.
  1609. *
  1610. * @since 1.5.3
  1611. *
  1612. * @param {jQuery} $form Form element.
  1613. */
  1614. formSubmitNormal: function( $form ) {
  1615. if ( ! $form.length ) {
  1616. return;
  1617. }
  1618. var $submit = $form.find( '.wpforms-submit' ),
  1619. recaptchaID = $submit.get( 0 ).recaptchaID;
  1620. if ( ! app.empty( recaptchaID ) || recaptchaID === 0 ) {
  1621. $submit.get( 0 ).recaptchaID = false;
  1622. }
  1623. $form.get( 0 ).submit();
  1624. },
  1625. /**
  1626. * Reset form captcha.
  1627. *
  1628. * @since 1.5.3
  1629. * @since 1.6.4 Added hCaptcha support.
  1630. *
  1631. * @param {jQuery} $form Form element.
  1632. */
  1633. resetFormRecaptcha: function( $form ) {
  1634. if ( ! $form || ! $form.length ) {
  1635. return;
  1636. }
  1637. if ( typeof hcaptcha === 'undefined' && typeof grecaptcha === 'undefined' ) {
  1638. return;
  1639. }
  1640. var $captchaContainer = $form.find( '.wpforms-recaptcha-container' ),
  1641. apiVar = $captchaContainer.hasClass( 'wpforms-is-hcaptcha' ) ? hcaptcha : grecaptcha,
  1642. recaptchaID;
  1643. // Check for invisible recaptcha first.
  1644. recaptchaID = $form.find( '.wpforms-submit' ).get( 0 ).recaptchaID;
  1645. // Check for hcaptcha/recaptcha v2, if invisible recaptcha is not found.
  1646. if ( app.empty( recaptchaID ) && recaptchaID !== 0 ) {
  1647. recaptchaID = $form.find( '.g-recaptcha' ).data( 'recaptcha-id' );
  1648. }
  1649. // Reset captcha.
  1650. if ( ! app.empty( recaptchaID ) || recaptchaID === 0 ) {
  1651. apiVar.reset( recaptchaID );
  1652. }
  1653. },
  1654. /**
  1655. * Console log AJAX error.
  1656. *
  1657. * @since 1.5.3
  1658. *
  1659. * @param {string} error Error text (optional).
  1660. */
  1661. consoleLogAjaxError: function( error ) {
  1662. if ( error ) {
  1663. console.error( 'WPForms AJAX submit error:\n%s', error ); // eslint-disable-line no-console
  1664. } else {
  1665. console.error( 'WPForms AJAX submit error' ); // eslint-disable-line no-console
  1666. }
  1667. },
  1668. /**
  1669. * Display form AJAX errors.
  1670. *
  1671. * @since 1.5.3
  1672. *
  1673. * @param {jQuery} $form Form element.
  1674. * @param {object} errors Errors in format { general: { generalErrors }, field: { fieldErrors } }.
  1675. */
  1676. displayFormAjaxErrors: function( $form, errors ) {
  1677. if ( 'string' === typeof errors ) {
  1678. app.displayFormAjaxGeneralErrors( $form, errors );
  1679. return;
  1680. }
  1681. errors = errors && ( 'errors' in errors ) ? errors.errors : null;
  1682. if ( app.empty( errors ) || ( app.empty( errors.general ) && app.empty( errors.field ) ) ) {
  1683. app.consoleLogAjaxError();
  1684. return;
  1685. }
  1686. if ( ! app.empty( errors.general ) ) {
  1687. app.displayFormAjaxGeneralErrors( $form, errors.general );
  1688. }
  1689. if ( ! app.empty( errors.field ) ) {
  1690. app.displayFormAjaxFieldErrors( $form, errors.field );
  1691. }
  1692. },
  1693. /**
  1694. * Display form AJAX general errors that cannot be displayed using jQuery Validation plugin.
  1695. *
  1696. * @since 1.5.3
  1697. *
  1698. * @param {jQuery} $form Form element.
  1699. * @param {object} errors Errors in format { errorType: errorText }.
  1700. */
  1701. displayFormAjaxGeneralErrors: function( $form, errors ) {
  1702. if ( ! $form || ! $form.length ) {
  1703. return;
  1704. }
  1705. if ( app.empty( errors ) ) {
  1706. return;
  1707. }
  1708. // Safety net for random errors thrown by a third-party code. Should never be used intentionally.
  1709. if ( 'string' === typeof errors ) {
  1710. $form.find( '.wpforms-submit-container' ).before( '<div class="wpforms-error-container">' + errors + '</div>' );
  1711. return;
  1712. }
  1713. $.each( errors, function( type, html ) {
  1714. switch ( type ) {
  1715. case 'header':
  1716. $form.prepend( html );
  1717. break;
  1718. case 'footer':
  1719. $form.find( '.wpforms-submit-container' ).before( html );
  1720. break;
  1721. case 'recaptcha':
  1722. $form.find( '.wpforms-recaptcha-container' ).append( html );
  1723. break;
  1724. }
  1725. } );
  1726. },
  1727. /**
  1728. * Clear forms AJAX general errors that cannot be cleared using jQuery Validation plugin.
  1729. *
  1730. * @since 1.5.3
  1731. *
  1732. * @param {jQuery} $form Form element.
  1733. */
  1734. clearFormAjaxGeneralErrors: function( $form ) {
  1735. $form.find( '.wpforms-error-container' ).remove();
  1736. $form.find( '#wpforms-field_recaptcha-error' ).remove();
  1737. },
  1738. /**
  1739. * Display form AJAX field errors using jQuery Validation plugin.
  1740. *
  1741. * @since 1.5.3
  1742. *
  1743. * @param {jQuery} $form Form element.
  1744. * @param {object} errors Errors in format { fieldName: errorText }.
  1745. */
  1746. displayFormAjaxFieldErrors: function( $form, errors ) {
  1747. if ( ! $form || ! $form.length ) {
  1748. return;
  1749. }
  1750. if ( app.empty( errors ) ) {
  1751. return;
  1752. }
  1753. var validator = $form.data( 'validator' );
  1754. if ( ! validator ) {
  1755. return;
  1756. }
  1757. validator.showErrors( errors );
  1758. validator.focusInvalid();
  1759. },
  1760. /**
  1761. * Submit a form using AJAX.
  1762. *
  1763. * @since 1.5.3
  1764. *
  1765. * @param {jQuery} $form Form element.
  1766. *
  1767. * @returns {JQueryXHR|JQueryDeferred} Promise like object for async callbacks.
  1768. */
  1769. formSubmitAjax: function( $form ) {
  1770. if ( ! $form.length ) {
  1771. return $.Deferred().reject(); // eslint-disable-line new-cap
  1772. }
  1773. var $container = $form.closest( '.wpforms-container' ),
  1774. $spinner = $form.find( '.wpforms-submit-spinner' ),
  1775. $confirmationScroll,
  1776. formData,
  1777. args;
  1778. $container.css( 'opacity', 0.6 );
  1779. $spinner.show();
  1780. app.clearFormAjaxGeneralErrors( $form );
  1781. formData = new FormData( $form.get( 0 ) );
  1782. formData.append( 'action', 'wpforms_submit' );
  1783. formData.append( 'page_url', window.location.href );
  1784. args = {
  1785. type : 'post',
  1786. dataType : 'json',
  1787. url : wpforms_settings.ajaxurl,
  1788. data : formData,
  1789. cache : false,
  1790. contentType: false,
  1791. processData: false,
  1792. };
  1793. args.success = function( json ) {
  1794. if ( ! json ) {
  1795. app.consoleLogAjaxError();
  1796. return;
  1797. }
  1798. if ( json.data && json.data.action_required ) {
  1799. $form.trigger( 'wpformsAjaxSubmitActionRequired', json );
  1800. return;
  1801. }
  1802. if ( ! json.success ) {
  1803. app.resetFormRecaptcha( $form );
  1804. app.displayFormAjaxErrors( $form, json.data );
  1805. $form.trigger( 'wpformsAjaxSubmitFailed', json );
  1806. return;
  1807. }
  1808. $form.trigger( 'wpformsAjaxSubmitSuccess', json );
  1809. if ( ! json.data ) {
  1810. return;
  1811. }
  1812. if ( json.data.redirect_url ) {
  1813. $form.trigger( 'wpformsAjaxSubmitBeforeRedirect', json );
  1814. window.location = json.data.redirect_url;
  1815. return;
  1816. }
  1817. if ( json.data.confirmation ) {
  1818. $container.html( json.data.confirmation );
  1819. $confirmationScroll = $container.find( 'div.wpforms-confirmation-scroll' );
  1820. $container.trigger( 'wpformsAjaxSubmitSuccessConfirmation', json );
  1821. if ( $confirmationScroll.length ) {
  1822. app.animateScrollTop( $confirmationScroll.offset().top - 100 );
  1823. }
  1824. }
  1825. };
  1826. args.error = function( jqHXR, textStatus, error ) {
  1827. app.consoleLogAjaxError( error );
  1828. $form.trigger( 'wpformsAjaxSubmitError', [ jqHXR, textStatus, error ] );
  1829. };
  1830. args.complete = function( jqHXR, textStatus ) {
  1831. // Do not make form active if the action is required.
  1832. if ( jqHXR.responseJSON && jqHXR.responseJSON.data && jqHXR.responseJSON.data.action_required ) {
  1833. return;
  1834. }
  1835. var $submit = $form.find( '.wpforms-submit' ),
  1836. submitText = $submit.data( 'submit-text' );
  1837. if ( submitText ) {
  1838. $submit.text( submitText );
  1839. }
  1840. $submit.prop( 'disabled', false );
  1841. $container.css( 'opacity', '' );
  1842. $spinner.hide();
  1843. $form.trigger( 'wpformsAjaxSubmitCompleted', [ jqHXR, textStatus ] );
  1844. };
  1845. $form.trigger( 'wpformsAjaxBeforeSubmit' );
  1846. return $.ajax( args );
  1847. },
  1848. /**
  1849. * Scroll to position with animation.
  1850. *
  1851. * @since 1.5.3
  1852. *
  1853. * @param {number} position Position (in pixels) to scroll to,
  1854. * @param {number} duration Animation duration.
  1855. * @param {Function} complete Function to execute after animation is complete.
  1856. *
  1857. * @returns {JQueryPromise} Promise object for async callbacks.
  1858. */
  1859. animateScrollTop: function( position, duration, complete ) {
  1860. duration = duration || 1000;
  1861. complete = typeof complete === 'function' ? complete : function() {};
  1862. return $( 'html, body' ).animate( { scrollTop: parseInt( position, 10 ) }, { duration: duration, complete: complete } ).promise();
  1863. },
  1864. /**
  1865. * Save tinyMCE.
  1866. *
  1867. * @since 1.7.0
  1868. */
  1869. saveTinyMCE: function() {
  1870. if ( typeof tinyMCE !== 'undefined' ) {
  1871. tinyMCE.triggerSave();
  1872. }
  1873. },
  1874. /**
  1875. * Check if object is a function.
  1876. *
  1877. * @deprecated 1.6.7
  1878. *
  1879. * @since 1.5.8
  1880. *
  1881. * @param {mixed} object Object to check if it is function.
  1882. *
  1883. * @returns {boolean} True if object is a function.
  1884. */
  1885. isFunction: function( object ) {
  1886. return !! ( object && object.constructor && object.call && object.apply );
  1887. },
  1888. /**
  1889. * Compare times.
  1890. *
  1891. * @since 1.7.1
  1892. *
  1893. * @param {string} time1 Time 1.
  1894. * @param {string} time2 Time 2.
  1895. *
  1896. * @returns {boolean} True if time1 is greater than time2.
  1897. */
  1898. compareTimesGreaterThan: function( time1, time2 ) {
  1899. // Properly format time: add space before AM/PM, make uppercase.
  1900. time1 = time1.replace( /(am|pm)/g, ' $1' ).toUpperCase();
  1901. time2 = time2.replace( /(am|pm)/g, ' $1' ).toUpperCase();
  1902. var time1Date = Date.parse( '01 Jan 2021 ' + time1 ),
  1903. time2Date = Date.parse( '01 Jan 2021 ' + time2 );
  1904. return time1Date >= time2Date;
  1905. },
  1906. };
  1907. return app;
  1908. }( document, window, jQuery ) );
  1909. // Initialize.
  1910. wpforms.init();