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