| 1 |
jQuery( function( $ ) { |
| 2 |
var $container = $( '#fca_eoi_email_test' ); |
| 3 |
|
| 4 |
var $recommendation = |
| 5 |
$( '<div class="fca_eoi_email_test_recommendation"/>' ) |
| 6 |
.html( fca_eoi_email_test_recommendation ); |
| 7 |
|
| 8 |
var $invalid_address_error = |
| 9 |
$( '<span class="fca_eoi_email_test_error"/>') |
| 10 |
.text( 'Invalid email address.' ); |
| 11 |
|
| 12 |
if ( $container.length == 0 ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
var $text = $( '<span>Would you like to send a test email?</span>' ); |
| 17 |
var $buttons = $( '<div class="fca_eoi_email_test_buttons"/>' ); |
| 18 |
|
| 19 |
var remove_container = function() { |
| 20 |
$container.remove(); |
| 21 |
}; |
| 22 |
|
| 23 |
var disable_and_hide_prompt = function() { |
| 24 |
post( 'fca_eoi_email_test_disable', {}, remove_container ); |
| 25 |
}; |
| 26 |
|
| 27 |
var disable_prompt = function() { |
| 28 |
post( 'fca_eoi_email_test_disable' ); |
| 29 |
}; |
| 30 |
|
| 31 |
var highlight_recommendation = function() { |
| 32 |
$text.remove(); |
| 33 |
$buttons.remove(); |
| 34 |
$recommendation.addClass('highlighted').show(); |
| 35 |
}; |
| 36 |
|
| 37 |
var hide_invalid_address_error = function() { |
| 38 |
$invalid_address_error.fadeOut( function() { |
| 39 |
$( this ).remove(); |
| 40 |
} ); |
| 41 |
}; |
| 42 |
|
| 43 |
var show_invalid_address_error = function() { |
| 44 |
$invalid_address_error.finish().remove(); |
| 45 |
$buttons.append( $invalid_address_error.show() ); |
| 46 |
}; |
| 47 |
|
| 48 |
var post = function( action, data, completion_handler ) { |
| 49 |
$buttons.html( '<i class="fa fa-spinner fa-spin"></i>' ); |
| 50 |
|
| 51 |
data = data || {}; |
| 52 |
data['action'] = action; |
| 53 |
|
| 54 |
$.post( ajaxurl, data, completion_handler ); |
| 55 |
}; |
| 56 |
|
| 57 |
var $button_disable_test = $( '<span class="button">No</span>' ).click( disable_and_hide_prompt ); |
| 58 |
|
| 59 |
var $button_send_test = $( '<span class="button">Yes, send test email</span>' ).click( function() { |
| 60 |
var $input_email = $( '<input type="email" placeholder="Email address"/>' ) |
| 61 |
.keyup( hide_invalid_address_error ) |
| 62 |
.mouseup( hide_invalid_address_error ) |
| 63 |
.on( 'paste', hide_invalid_address_error ) |
| 64 |
.on( 'contextmenu', hide_invalid_address_error ) |
| 65 |
.keypress( function( event ) { |
| 66 |
if ( event.which == 13 ) { |
| 67 |
send_email(); |
| 68 |
event.preventDefault(); |
| 69 |
event.stopPropagation(); |
| 70 |
} |
| 71 |
} ); |
| 72 |
|
| 73 |
var send_email = function() { |
| 74 |
var email = $.trim( $input_email.val() ); |
| 75 |
|
| 76 |
if ( ! email || ! validateEmail( email ) ) { |
| 77 |
show_invalid_address_error(); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$text.text( 'Sending email...' ); |
| 82 |
|
| 83 |
post( 'fca_eoi_email_test_send', { |
| 84 |
sender_name: $( 'input[name="fca_eoi[optin_bait_fields][sender_name]"]' ).val() || "WordPress", |
| 85 |
sender_email: $( 'input[name="fca_eoi[optin_bait_fields][sender_email]"]' ).val() || 'wordpress@' + window.location.host, |
| 86 |
email: email, |
| 87 |
subject: $( 'input[name="fca_eoi[optin_bait_fields][subject]"]' ).val() || 'Test', |
| 88 |
message: $( 'textarea[name="fca_eoi[optin_bait_fields][message]"]' ).val() || 'Test message.' |
| 89 |
}, function( result ) { |
| 90 |
if ( JSON.parse( result ) ) { |
| 91 |
$text.text( 'Email sent. Dit it arrive?' ); |
| 92 |
|
| 93 |
$buttons.html( '' ).append( |
| 94 |
$( '<span class="button">Yes</span>' ).click( function() { |
| 95 |
if ( fca_eoi_email_test_has_active ) { |
| 96 |
disable_and_hide_prompt(); |
| 97 |
} else { |
| 98 |
disable_prompt(); |
| 99 |
highlight_recommendation(); |
| 100 |
} |
| 101 |
} ), |
| 102 |
$( '<span class="button">No</span>' ).click( function() { |
| 103 |
if ( fca_eoi_email_test_has_active ) { |
| 104 |
$text.html( fca_eoi_email_test_failure_message ); |
| 105 |
$buttons.remove(); |
| 106 |
$recommendation.remove(); |
| 107 |
} else { |
| 108 |
highlight_recommendation(); |
| 109 |
} |
| 110 |
} ) |
| 111 |
); |
| 112 |
} else { |
| 113 |
$text.html( 'Could not send email. ' + fca_eoi_email_test_failure_message ); |
| 114 |
$buttons.remove(); |
| 115 |
$recommendation.remove(); |
| 116 |
} |
| 117 |
} ); |
| 118 |
}; |
| 119 |
|
| 120 |
var $button_send = $( '<span class="button">Send test email</span>' ).click( send_email ); |
| 121 |
|
| 122 |
$buttons.html( '' ).append( $input_email, $button_send ); |
| 123 |
} ); |
| 124 |
|
| 125 |
$container.html( '' ).append( |
| 126 |
$text, |
| 127 |
$buttons.append( $button_disable_test, $button_send_test ) |
| 128 |
); |
| 129 |
|
| 130 |
if ( ! fca_eoi_email_test_has_active ) { |
| 131 |
$container.append( $recommendation ); |
| 132 |
} |
| 133 |
|
| 134 |
function validateEmail( email ) { |
| 135 |
var re = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i; |
| 136 |
return re.test( email ); |
| 137 |
} |
| 138 |
} ); |