| 1 |
(function( $ ) { |
| 2 |
|
| 3 |
var prefix = 'automatorwp-mailerlite-'; |
| 4 |
var _prefix = 'automatorwp_mailerlite_'; |
| 5 |
|
| 6 |
// On click authorize button |
| 7 |
$('body').on('click', '.automatorwp_settings #' + _prefix + 'authorize', function(e) { |
| 8 |
e.preventDefault(); |
| 9 |
|
| 10 |
var button = $(this); |
| 11 |
var wrapper = button.parent(); |
| 12 |
|
| 13 |
var token = $('#' + _prefix + 'token').val(); |
| 14 |
|
| 15 |
// Check if response div exists |
| 16 |
var response_wrap = wrapper.find('#' + _prefix + 'response'); |
| 17 |
|
| 18 |
if( ! response_wrap.length ) { |
| 19 |
wrapper.append( '<div id="' + _prefix + 'response" style="display: none; margin-top: 10px;"></div>' ); |
| 20 |
response_wrap = wrapper.find('#' + _prefix + 'response'); |
| 21 |
} |
| 22 |
|
| 23 |
// Show error message if not correctly configured |
| 24 |
if( token.length === 0 ) { |
| 25 |
response_wrap.addClass( 'automatorwp-notice-error' ); |
| 26 |
response_wrap.html( 'API token is required to connect with MailerLite' ); |
| 27 |
response_wrap.slideDown('fast'); |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
response_wrap.slideUp('fast'); |
| 32 |
response_wrap.attr('class', ''); |
| 33 |
|
| 34 |
// Show spinner |
| 35 |
wrapper.append('<span class="spinner is-active" style="float: none;"></span>'); |
| 36 |
|
| 37 |
// Disable button |
| 38 |
button.prop('disabled', true); |
| 39 |
|
| 40 |
$.post( |
| 41 |
ajaxurl, |
| 42 |
{ |
| 43 |
action: 'automatorwp_mailerlite_authorize', |
| 44 |
nonce: automatorwp_mailerlite.nonce, |
| 45 |
token: token, |
| 46 |
}, |
| 47 |
function( response ) { |
| 48 |
|
| 49 |
// Add class automatorwp-notice-success on successful unlock, if not will add the class automatorwp-notice-error |
| 50 |
response_wrap.addClass( 'automatorwp-notice-' + ( response.success === true ? 'success' : 'error' ) ); |
| 51 |
response_wrap.html( ( response.data.message !== undefined ? response.data.message : response.data ) ); |
| 52 |
response_wrap.slideDown('fast'); |
| 53 |
|
| 54 |
// Hide spinner |
| 55 |
wrapper.find('.spinner').remove(); |
| 56 |
|
| 57 |
// Redirect on success |
| 58 |
if( response.success === true && response.data.redirect_url !== undefined ) { |
| 59 |
window.location = response.data.redirect_url; |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
// Enable button |
| 65 |
button.prop('disabled', false); |
| 66 |
|
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
|
| 71 |
}); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
})( jQuery ); |