| @@ -1,115 +1,226 @@ | ||
| 1 | -jQuery( document ).ready(function( $ ){ | |
| 1 | +/* jshint asi: true */ | |
| 2 | 2 | |
| 3 | - $( document ).on( 'submit', '.fca_eoi_form', function () { | |
| 3 | +jQuery( document ).ready(function($){ | |
| 4 | + | |
| 5 | + function is_email( email ) { | |
| 6 | + var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/ | |
| 7 | + return regex.test(email) | |
| 8 | + } | |
| 4 | 9 | |
| 10 | + function set_cookie( name, value, exdays ) { | |
| 11 | + if ( exdays === 0 ) { | |
| 12 | + document.cookie = name + "=" + value + "" + ";" + "path=/;" | |
| 13 | + } else { | |
| 14 | + var d = new Date() | |
| 15 | + d.setTime( d.getTime() + ( exdays*24*60*60*1000 ) ) | |
| 16 | + document.cookie = name + "=" + value + "" + ";" + "path=/;" + "expires=" + d.toUTCString() | |
| 17 | + } | |
| 18 | + } | |
| 19 | + | |
| 20 | + function get_cookie( name ) { | |
| 21 | + var value = "; " + document.cookie | |
| 22 | + var parts = value.split( "; " + name + "=" ) | |
| 23 | + | |
| 24 | + if ( parts.length === 2 ) { | |
| 25 | + return parts.pop().split(";").shift() | |
| 26 | + } else { | |
| 27 | + return false | |
| 28 | + } | |
| 29 | + } | |
| 30 | + | |
| 31 | + $( document ).on( 'submit', '.fca_eoi_form', function (e) { | |
| 32 | + | |
| 33 | + e.preventDefault() | |
| 34 | + | |
| 35 | + var $this = $( this ) | |
| 36 | + var $email_field = $( '[name=email]', $this ) | |
| 37 | + var $name_field = $( '[name=name]', $this ) | |
| 38 | + var name = $name_field.val() | |
| 39 | + var email = $email_field.val() | |
| 40 | + var list_id = $this.data( 'fca_eoi_list_id' ) | |
| 41 | + var $button = $( '[type=submit]', $this ) | |
| 42 | + var button_initial_val | |
| 43 | + var highlight_interval = false; | |
| 44 | + var thank_you_mode = $this.data( 'fca_eoi_thank_you_mode' ) | |
| 45 | + var thank_you_page = $this.data( 'fca_eoi_thank_you_page' ) | |
| 46 | + var cookie_duration = $this.data( 'fca_eoi_success_cookie_duration' ) | |
| 47 | + var subscribe_msg = $this.data( 'fca_eoi_sub_msg' ) | |
| 48 | + var has_error = false | |
| 49 | + var form_id = $( '[name=fca_eoi_form_id]', $this ).val() | |
| 50 | + | |
| 5 | 51 | // Attach tooltips |
| 6 | - $( '[name=email], [name=name]', $( this ) ).each( function() { | |
| 52 | + $this.find('.tooltipstered').tooltipster('hide') | |
| 53 | + $( '[name=email], [name=name]', $this ).not('.tooltipstered').each( function() { | |
| 7 | 54 | |
| 8 | - var $this = $( this ); | |
| 9 | - var tooltipWidth = $this.width() * .8; | |
| 55 | + var $this = $( this ) | |
| 56 | + var tooltipWidth = $this.width() * .8 | |
| 10 | 57 | |
| 11 | 58 | $this.tooltipster( { |
| 12 | - contentAsHTML: true | |
| 13 | - , fixedWidth: tooltipWidth | |
| 14 | - , minWidth: tooltipWidth | |
| 15 | - , maxWidth: tooltipWidth | |
| 16 | - , trigger: 'none' | |
| 17 | - } ); | |
| 18 | - } ); | |
| 59 | + contentAsHTML: true, | |
| 60 | + fixedWidth: tooltipWidth, | |
| 61 | + minWidth: tooltipWidth, | |
| 62 | + maxWidth: tooltipWidth, | |
| 63 | + trigger: 'none', | |
| 64 | + theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] | |
| 65 | + } ) | |
| 66 | + } ) | |
| 19 | 67 | |
| 20 | 68 | // Remove tooltip and tick on focus |
| 21 | 69 | $( '[name=email], [name=name]', $( this ) ).focus( function() { |
| 22 | - var $this = $( this ); | |
| 23 | - var $button = $( '[type=submit]', $this.closest( '.fca_eoi_form' ) ); | |
| 24 | - var button_initial_val = $button.data( 'fca_eoi_initial_val' ); | |
| 70 | + var $this = $( this ) | |
| 71 | + var $button = $( '[type=submit]', $this.closest( '.fca_eoi_form' ) ) | |
| 72 | + var button_initial_val = $button.data( 'fca_eoi_initial_val' ) | |
| 25 | 73 | |
| 26 | 74 | // Remove any previous icon |
| 27 | 75 | if( 'undefined' !== typeof( button_initial_val ) ) { |
| 28 | - $button.val( button_initial_val ); | |
| 76 | + $button.val( button_initial_val ) | |
| 29 | 77 | } |
| 30 | 78 | |
| 31 | 79 | // Hide tooltip |
| 32 | - $( this ).tooltipster( 'hide' ); | |
| 33 | - } ); | |
| 80 | + $( this ).tooltipster( 'hide' ) | |
| 81 | + } ) | |
| 34 | 82 | |
| 35 | - // Handle form submissions | |
| 36 | - //$( '.fca_eoi_form' ).submit( function( e ) { | |
| 83 | + // Save or save and get initial button value | |
| 84 | + if( $button.data( 'fca_eoi_initial_val' ) ) { | |
| 85 | + button_initial_val = $button.data( 'fca_eoi_initial_val' ) | |
| 86 | + } else { | |
| 87 | + button_initial_val = $button.val() | |
| 88 | + $button.data( 'fca_eoi_initial_val', button_initial_val ) | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Remove any previous icon | |
| 92 | + $button.val( button_initial_val ); | |
| 93 | + | |
| 94 | + // Get Error Messages from hidden fields | |
| 95 | + fcaEoiScriptData.invalid_email = $this.find('.fca_eoi_error_texts_email').val() | |
| 96 | + fcaEoiScriptData.field_required = $this.find('.fca_eoi_error_texts_required').val() | |
| 97 | + | |
| 98 | + // Check email address | |
| 99 | + if( ! email || ! is_email( email ) ) { | |
| 100 | + $email_field.tooltipster( 'content', email ? fcaEoiScriptData.invalid_email : fcaEoiScriptData.field_required ) | |
| 101 | + $email_field.tooltipster( 'show' ); | |
| 102 | + $button.val( '✗ ' + button_initial_val ); | |
| 103 | + has_error = true; | |
| 104 | + } | |
| 37 | 105 | |
| 38 | - var $this = $( this ); | |
| 39 | - var $email_field = $( '[name=email]', $this ); | |
| 40 | - var $name_field = $( '[name=name]', $this ); | |
| 41 | - var name = $name_field.val(); | |
| 42 | - var email = $email_field.val(); | |
| 43 | - var list_id = $this.data( 'fca_eoi_list_id' ); | |
| 44 | - var url = 'https://api.createsend.com/api/v3.1/subscribers/' + list_id + '.json'; | |
| 45 | - var $button = $( '[type=submit]', $this ); | |
| 46 | - var button_initial_val; | |
| 47 | - var highlight_interval = false; | |
| 48 | - var thank_you_page = $this.data( 'fca_eoi_thank_you_page' ); | |
| 49 | - var has_error = false; | |
| 50 | - var fca_eoi_form_id = $( '[name=fca_eoi_form_id]', $this ).val(); | |
| 51 | - | |
| 52 | - // Save or save and get initial button value | |
| 53 | - if( $button.data( 'fca_eoi_initial_val' ) ) { | |
| 54 | - button_initial_val = $button.data( 'fca_eoi_initial_val' ); | |
| 55 | - } else { | |
| 56 | - button_initial_val = $button.val(); | |
| 57 | - $button.data( 'fca_eoi_initial_val', button_initial_val ); | |
| 106 | + // Check name | |
| 107 | + if( $name_field.is(':visible') && !name ) { | |
| 108 | + $name_field.tooltipster( 'content', fcaEoiScriptData.field_required ); | |
| 109 | + $name_field.tooltipster( 'show' ); | |
| 110 | + $button.val( '✗ ' + button_initial_val ); | |
| 111 | + has_error = true; | |
| 112 | + } | |
| 113 | + | |
| 114 | + // Exit if honeypot is entered | |
| 115 | + if( $( '[name=i_agree]', $this ).prop( 'checked' ) ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + // Exit if there is any error | |
| 120 | + if( has_error ) { | |
| 121 | + return false; | |
| 122 | + } | |
| 123 | + | |
| 124 | + var gdpr_consent = 'unknown' | |
| 125 | + if( fcaEoiScriptData.gdpr_checkbox ) { | |
| 126 | + if( $this.find( '.fca_eoi_gdpr_consent' ).length === 0 ) { | |
| 127 | + $this.find('.fca_eoi_layout_field_wrapper').hide() | |
| 128 | + $this.find('.fca_eoi_layout_email_field_inner').hide() | |
| 129 | + $this.find('.fca_eoi_layout_name_field_inner').hide() | |
| 130 | + if ( $this.closest( '.fca_eoi_form_wrapper ' ).hasClass( 'fca_eoi_layout_banner_wrapper' ) ) { | |
| 131 | + $this.find('.fca_eoi_layout_headline_copy_wrapper')[0].style.setProperty( 'display', 'block', 'important' ) | |
| 132 | + $this.find('.fca_eoi_layout_headline_copy_wrapper').html( '<div style="font-size: 14px;">' + fcaEoiScriptData.consent_headline + '<br>' + '<label><input class="fca_eoi_gdpr_consent" value="" type="checkbox"></input>'+fcaEoiScriptData.consent_msg+'</label></div>') | |
| 133 | + } else { | |
| 134 | + $this.find('.fca_eoi_layout_description_copy_wrapper')[0].style.setProperty( 'display', 'block', 'important' ) | |
| 135 | + $this.find('.fca_eoi_layout_description_copy_wrapper').html( '<div style="text-align:left;">' + fcaEoiScriptData.consent_headline + '<br><br>' + '<label><input class="fca_eoi_gdpr_consent" value="" type="checkbox"></input>'+fcaEoiScriptData.consent_msg+'</label></div>') | |
| 136 | + } | |
| 137 | + return false | |
| 58 | 138 | } |
| 59 | 139 | |
| 60 | - // Remove any previous icon | |
| 61 | - $button.val( button_initial_val ); | |
| 62 | - | |
| 63 | - // Check email address | |
| 64 | - if( ! email || ! is_email( email ) ) { | |
| 65 | - | |
| 66 | - $email_field.tooltipster( 'content', email ? fca_eoi.invalid_email : fca_eoi.field_required ); | |
| 67 | - $email_field.tooltipster( 'show' ); | |
| 68 | - $button.val( '✗ ' + button_initial_val ); | |
| 69 | - has_error = true; | |
| 140 | + gdpr_consent = $this.find( '.fca_eoi_gdpr_consent' ).prop('checked') | |
| 141 | + | |
| 142 | + if ( gdpr_consent == false ) { | |
| 143 | + $this.find( '.fca_eoi_gdpr_consent' ).not('.tooltipstered').tooltipster( { | |
| 144 | + contentAsHTML: true, | |
| 145 | + trigger: 'none', | |
| 146 | + theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] | |
| 147 | + } ).tooltipster( 'content', fcaEoiScriptData.field_required ).tooltipster( 'show' ) | |
| 148 | + return false | |
| 70 | 149 | } |
| 150 | + | |
| 151 | + } | |
| 152 | + | |
| 153 | + $button.val( subscribe_msg ); | |
| 154 | + //DISABLE BUTTON CLICK EVENT | |
| 155 | + $button.prop('disabled', true); | |
| 71 | 156 | |
| 72 | - // Check name | |
| 73 | - if( $name_field.length && ! name ) { | |
| 74 | - | |
| 75 | - $name_field.tooltipster( 'content', fca_eoi.field_required ); | |
| 76 | - $name_field.tooltipster( 'show' ); | |
| 77 | - $button.val( '✗ ' + button_initial_val ); | |
| 78 | - has_error = true; | |
| 157 | + var tz = jstz.determine() | |
| 158 | + | |
| 159 | + $.ajax({ | |
| 160 | + type: 'POST', | |
| 161 | + url: fcaEoiScriptData.ajax_url, | |
| 162 | + data: { | |
| 163 | + 'email': email, | |
| 164 | + 'name': name, | |
| 165 | + 'action': 'fca_eoi_subscribe', | |
| 166 | + 'list_id': list_id, | |
| 167 | + 'form_id': form_id, | |
| 168 | + 'nonce': fcaEoiScriptData.nonce, | |
| 169 | + 'timezone': tz.name(), | |
| 170 | + 'consent_granted': gdpr_consent | |
| 79 | 171 | } |
| 80 | - | |
| 81 | - // Exit if there is any error | |
| 82 | - if( has_error ) { | |
| 83 | - return false; | |
| 172 | + }).done( function( data ) { | |
| 173 | + | |
| 174 | + // Stop highlighting and add an icon for success/failure | |
| 175 | + if ( data === '✓' ) { | |
| 176 | + $button.val( data + ' ' + button_initial_val ); | |
| 177 | + | |
| 178 | + set_cookie( 'fca_eoi_success_' + form_id, Math.floor(Date.now() / 1000), cookie_duration ) | |
| 179 | + | |
| 180 | + //REMOVE FROM ACTIVE | |
| 181 | + var activeOptins = JSON.parse( get_cookie( 'fca_eoi_active_optins' ) ) | |
| 182 | + if ( !Array.isArray( activeOptins ) ) { | |
| 183 | + activeOptins = [] | |
| 184 | + } | |
| 185 | + var index = activeOptins.indexOf( form_id ) | |
| 186 | + if ( index !== -1 ) { | |
| 187 | + activeOptins.splice( index, 1 ) | |
| 188 | + } | |
| 189 | + set_cookie ( 'fca_eoi_active_optins', JSON.stringify ( activeOptins ) ) | |
| 190 | + | |
| 191 | + } else if( data === '✗ Failed to add user - denied' ) { | |
| 192 | + $button.prop('disabled', false); | |
| 193 | + $button.val( button_initial_val ); | |
| 194 | + $this.find( '.fca_eoi_gdpr_consent' ).tooltipster( 'show' ) | |
| 195 | + } else { | |
| 196 | + $button.prop('disabled', false); | |
| 197 | + $button.val( data ); | |
| 84 | 198 | } |
| 85 | - | |
| 86 | - $( '.fca_eoi_form_button_element', $this ).attr( 'disabled', 'disabled' ); | |
| 87 | - | |
| 88 | - return true; | |
| 89 | - | |
| 90 | - // Start highlighting | |
| 91 | - highlight_interval = setInterval( function() { | |
| 92 | - $button.fadeTo( 200, 0.25 ).fadeTo( 200, 1.0 ); | |
| 93 | - }, 500 ); | |
| 94 | - | |
| 95 | - $.ajax( { | |
| 96 | - url: fca_eoi.ajax_url | |
| 97 | - , data: { 'email': email, 'name': name, 'action': 'fca_eoi_subscribe', 'list_id': list_id ,'form_id': fca_eoi_form_id} | |
| 98 | - , type: 'POST' | |
| 99 | - , datatype: 'text' | |
| 100 | - } ).done( function( data ) { | |
| 101 | - // Stop highlighting and add an icon for success/failure | |
| 102 | - $button.val( data + ' ' + button_initial_val ); | |
| 103 | - clearInterval( highlight_interval ); | |
| 199 | + | |
| 200 | + if ( thank_you_mode === 'ajax' && data === '✓' ) { | |
| 201 | + clearInterval( highlight_interval ) | |
| 104 | 202 | if ( thank_you_page && '✓' === data ) { |
| 105 | - window.location.href = thank_you_page; | |
| 203 | + tooltipWidth = $button.width() | |
| 204 | + $button.tooltipster( { | |
| 205 | + contentAsHTML: true, | |
| 206 | + trigger: 'none', | |
| 207 | + fixedWidth: tooltipWidth, | |
| 208 | + minWidth: tooltipWidth, | |
| 209 | + maxWidth: tooltipWidth, | |
| 210 | + arrow: false, | |
| 211 | + theme: ['tooltipster-borderless', 'tooltipster-optin-cat'] | |
| 212 | + } ); | |
| 213 | + | |
| 214 | + $button.tooltipster( 'content', thank_you_page ) | |
| 215 | + $button.tooltipster( 'show' ) | |
| 216 | + $email_field.prop('disabled', true) | |
| 217 | + $name_field.prop('disabled', true) | |
| 218 | + | |
| 219 | + $('.tooltipster-content').css("background-color", "#148544") | |
| 106 | 220 | } |
| 107 | - } ); | |
| 108 | - //} ); | |
| 109 | - } ); | |
| 110 | -} ); | |
| 111 | - | |
| 112 | -function is_email( email ) { | |
| 113 | - var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; | |
| 114 | - return regex.test(email); | |
| 115 | -} | |
| 221 | + } else if ( data === '✓' ) { | |
| 222 | + window.location.href = thank_you_page | |
| 223 | + } | |
| 224 | + }) | |
| 225 | + }) | |
| 226 | +}) | |