| 1 |
(function ( $ ) { |
| 2 |
|
| 3 |
var _prefix = 'automatorwp_mailchimp_'; |
| 4 |
|
| 5 |
// Save credentials |
| 6 |
$('body').on('click', '#automatorwp_save_mailchimp_oauth', function( e ) { |
| 7 |
|
| 8 |
e.preventDefault(); |
| 9 |
|
| 10 |
var button = $(this); |
| 11 |
var wrapper = button.parent(); |
| 12 |
|
| 13 |
// Check if response div exists |
| 14 |
var status_wrap = wrapper.find('#awp_mailchimp_oauth_status'); |
| 15 |
|
| 16 |
var data = { |
| 17 |
action: 'automatorwp_mailchimp_save_oauth_credentials', |
| 18 |
nonce: automatorwp_mailchimp.nonce, |
| 19 |
api_key: $('#' + _prefix + 'api_key').val(), |
| 20 |
server_prefix: $('#' + _prefix + 'server_prefix').val(), |
| 21 |
}; |
| 22 |
|
| 23 |
$.post(ajaxurl, data, function (response ) { |
| 24 |
if( response.success ) { |
| 25 |
status_wrap.css("width", "fit-content"); |
| 26 |
status_wrap.addClass( 'automatorwp-notice-success' ); |
| 27 |
status_wrap.html( 'Credentials Saved!' ); |
| 28 |
status_wrap.slideDown('fast'); |
| 29 |
} else { |
| 30 |
if ( data.api_key.length == 0 || data.server_prefix.length == 0 ) { |
| 31 |
status_wrap.css("width", "fit-content"); |
| 32 |
status_wrap.addClass( 'automatorwp-notice-error' ); |
| 33 |
status_wrap.html( 'All fields are required to connect with Mailchimp' ); |
| 34 |
status_wrap.slideDown('fast'); |
| 35 |
} else { |
| 36 |
status_wrap.css("width", "fit-content"); |
| 37 |
status_wrap.addClass( 'automatorwp-notice-error' ); |
| 38 |
status_wrap.html( 'Either Credentials are invalid or Mailchimp API is not accessible' ); |
| 39 |
status_wrap.slideDown('fast'); |
| 40 |
} |
| 41 |
} |
| 42 |
}); |
| 43 |
|
| 44 |
}); |
| 45 |
|
| 46 |
// Delete credentials |
| 47 |
$('body').on('click', '#automatorwp_remove_mailchimp_oauth', function( e ) { |
| 48 |
|
| 49 |
e.preventDefault(); |
| 50 |
|
| 51 |
$( '#awp_mailchimp_oauth_status' ).text( "Removing Credentials...(wait)" ); |
| 52 |
|
| 53 |
var data = { |
| 54 |
action: 'automatorwp_mailchimp_delete_oauth_credentials', |
| 55 |
nonce: automatorwp_mailchimp.nonce, |
| 56 |
}; |
| 57 |
|
| 58 |
$.post(ajaxurl, data, function (response ) { |
| 59 |
$( '#awp_mailchimp_oauth_status' ).text( "Credentials Removed! Refresh this page. " ); |
| 60 |
setTimeout(function () { location.reload(); }, 100); |
| 61 |
}); |
| 62 |
|
| 63 |
}); |
| 64 |
|
| 65 |
// On change audience |
| 66 |
$('body').on('change', '.automatorwp-action-mailchimp-user-add-tag .cmb2-id-audience select', function(e) { |
| 67 |
var list = $(this).closest('.cmb-row'); |
| 68 |
var tag_list = list.next('.cmb2-id-tags'); |
| 69 |
|
| 70 |
var list_id = $(this).val(); |
| 71 |
|
| 72 |
var first_change = list.hasClass('is-option-change'); |
| 73 |
|
| 74 |
if( list_id === 'any' || list_id === '' ) { |
| 75 |
// Hide the term selector |
| 76 |
if( first_change ) { |
| 77 |
tag_list.hide(); |
| 78 |
} else { |
| 79 |
tag_list.slideUp('fast'); |
| 80 |
} |
| 81 |
} else { |
| 82 |
var tag_selector = tag_list.find('select.select2-hidden-accessible'); |
| 83 |
|
| 84 |
// Remove Select2 element |
| 85 |
tag_selector.next('.select2').remove(); |
| 86 |
|
| 87 |
// Update the list (since we do not use the table attribute, lets to use it as list) |
| 88 |
tag_selector.data( 'table', list_id ); |
| 89 |
|
| 90 |
// Reset the selector |
| 91 |
tag_selector.removeAttr('data-select2-id'); |
| 92 |
|
| 93 |
// Init it again |
| 94 |
automatorwp_ajax_selector( tag_selector ); |
| 95 |
|
| 96 |
// Show the term selector |
| 97 |
if( first_change ) { |
| 98 |
tag_list.show(); |
| 99 |
} else { |
| 100 |
tag_list.slideDown('fast'); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
list.removeClass('is-option-change'); |
| 105 |
}); |
| 106 |
|
| 107 |
// On click on an option, check if form contains the list selector |
| 108 |
$('body').on('click', '.automatorwp-automation-item-label > .automatorwp-option', function(e) { |
| 109 |
|
| 110 |
var item = $(this).closest('.automatorwp-automation-item'); |
| 111 |
var option = $(this).data('option'); |
| 112 |
var option_form = item.find('.automatorwp-option-form-container[data-option="' + option + '"]'); |
| 113 |
var list_selector = option_form.find('.cmb2-id-audience'); |
| 114 |
|
| 115 |
if( list_selector !== undefined ) { |
| 116 |
list_selector.addClass('is-option-change'); |
| 117 |
list_selector.find('select.select2-hidden-accessible').trigger('change'); |
| 118 |
} |
| 119 |
|
| 120 |
}); |
| 121 |
|
| 122 |
})( jQuery ); |