| 1 |
(function( $ ) { |
| 2 |
|
| 3 |
var prefix = 'automatorwp-'; |
| 4 |
var _prefix = 'automatorwp_activecampaign_'; |
| 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 url = $('#' + _prefix + 'url').val(); |
| 14 |
var key = $('#' + _prefix + 'key').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( url.length === 0 || key.length === 0 ) { |
| 26 |
response_wrap.addClass( 'automatorwp-notice-error' ); |
| 27 |
response_wrap.html( 'All fields are required to connect with ActiveCampaign' ); |
| 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_activecampaign_authorize', |
| 45 |
nonce: automatorwp_activecampaign.nonce, |
| 46 |
url: url, |
| 47 |
key: key, |
| 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 |
|
| 75 |
// On click refresh button |
| 76 |
$('body').on('click', '.automatorwp_settings #' + _prefix + 'refresh', function(e) { |
| 77 |
e.preventDefault(); |
| 78 |
|
| 79 |
var button = $(this); |
| 80 |
var wrapper = button.parent(); |
| 81 |
|
| 82 |
var webhook = $('#' + _prefix + 'webhook').val(); |
| 83 |
|
| 84 |
// Check if response div exists |
| 85 |
var response_wrap = wrapper.find('#' + _prefix + 'response'); |
| 86 |
|
| 87 |
if( ! response_wrap.length ) { |
| 88 |
wrapper.append( '<div id="' + _prefix + 'response" style="display: none; margin-top: 10px;"></div>' ); |
| 89 |
response_wrap = wrapper.find('#' + _prefix + 'response'); |
| 90 |
} |
| 91 |
|
| 92 |
response_wrap.slideUp('fast'); |
| 93 |
response_wrap.attr('class', ''); |
| 94 |
|
| 95 |
// Show spinner |
| 96 |
$('<span class="spinner is-active" style="float: none;"></span>').insertAfter( button ); |
| 97 |
|
| 98 |
// Disable button |
| 99 |
button.prop('disabled', true); |
| 100 |
|
| 101 |
$.post( |
| 102 |
ajaxurl, |
| 103 |
{ |
| 104 |
action: 'automatorwp_activecampaign_refresh', |
| 105 |
nonce: automatorwp_activecampaign.nonce, |
| 106 |
webhook: webhook, |
| 107 |
}, |
| 108 |
function( response ) { |
| 109 |
|
| 110 |
// Add class automatorwp-notice-success on successful unlock, if not will add the class automatorwp-notice-error |
| 111 |
response_wrap.addClass( 'automatorwp-notice-' + ( response.success === true ? 'success' : 'error' ) ); |
| 112 |
response_wrap.html( ( response.data.message !== undefined ? response.data.message : response.data ) ); |
| 113 |
response_wrap.slideDown('fast'); |
| 114 |
|
| 115 |
// Hide spinner |
| 116 |
wrapper.find('.spinner').remove(); |
| 117 |
|
| 118 |
// Redirect on success |
| 119 |
if( response.success === true && response.data.redirect_url !== undefined ) { |
| 120 |
window.location = response.data.redirect_url; |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
// Enable button |
| 126 |
button.prop('disabled', false); |
| 127 |
|
| 128 |
} |
| 129 |
); |
| 130 |
|
| 131 |
}); |
| 132 |
|
| 133 |
// View webhook on triggers |
| 134 |
$('body').on('click', '.automatorwp-view-webhook', function(e) { |
| 135 |
e.preventDefault(); |
| 136 |
|
| 137 |
$(this).parent().next().slideDown('fast'); |
| 138 |
|
| 139 |
}); |
| 140 |
|
| 141 |
})( jQuery ); |