| 1 |
/** |
| 2 |
* Patchstack |
| 3 |
* https://patchstack.com |
| 4 |
*/ |
| 5 |
|
| 6 |
window.Patchstack = window.Patchstack || {}; |
| 7 |
|
| 8 |
( function( window, document, $, plugin ) { |
| 9 |
var $c = {}; |
| 10 |
var polling = null; |
| 11 |
var isPolling = false; |
| 12 |
var pollingDelay = 0; |
| 13 |
|
| 14 |
plugin.init = function() { |
| 15 |
plugin.cache(); |
| 16 |
plugin.bindEvents(); |
| 17 |
}; |
| 18 |
|
| 19 |
plugin.cache = function() { |
| 20 |
$c.window = $( window ); |
| 21 |
$c.body = $( document.body ); |
| 22 |
}; |
| 23 |
|
| 24 |
plugin.poller = function() { |
| 25 |
polling = setInterval(() => { |
| 26 |
if (!isPolling && pollingDelay == 0) { |
| 27 |
plugin.autoActivate(); |
| 28 |
pollingDelay = 5; |
| 29 |
} |
| 30 |
|
| 31 |
pollingDelay--; |
| 32 |
}, 1000); |
| 33 |
}; |
| 34 |
|
| 35 |
plugin.autoActivate = function() { |
| 36 |
isPolling = true; |
| 37 |
|
| 38 |
var postData = { |
| 39 |
action: 'patchstack_activate_auto', |
| 40 |
PatchstackNonce: PatchstackVars.nonce |
| 41 |
}; |
| 42 |
|
| 43 |
$.post( PatchstackVars.ajaxurl, postData, function( response ) { |
| 44 |
var postData = { |
| 45 |
action: 'patchstack_activation_status', |
| 46 |
PatchstackNonce: PatchstackVars.nonce |
| 47 |
}; |
| 48 |
|
| 49 |
$.post ( PatchstackVars.ajaxurl, postData, function( response ) { |
| 50 |
if ( response.activated ) { |
| 51 |
window.location = window.location.href.replace('&ps_autoa=1', '') + '&resync=1'; |
| 52 |
} |
| 53 |
|
| 54 |
isPolling = false; |
| 55 |
}); |
| 56 |
}); |
| 57 |
}; |
| 58 |
|
| 59 |
plugin.bindEvents = function() { |
| 60 |
$('input#patchstack-activate').on( 'click', function(e) { |
| 61 |
e.preventDefault(); |
| 62 |
var postData = { |
| 63 |
action: 'patchstack_activate_license', |
| 64 |
key: $('.patchstack-auto-activate').attr('style') == '' ? $( '#patchstack_api_key2' ).val() : $( '#patchstack_api_key' ).val(), |
| 65 |
PatchstackNonce: PatchstackVars.nonce |
| 66 |
}; |
| 67 |
|
| 68 |
if ( $.trim( postData.key ).length == 0 ) { |
| 69 |
alert( 'Please enter the API key.' ); |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
$( '.patchstack-resync' ).hide(); |
| 74 |
$( '#patchstack-activate' ).hide(); |
| 75 |
$( '.patchstack-loading' ).show(); |
| 76 |
$( '.is-polling-section' ).hide(); |
| 77 |
isPolling = true |
| 78 |
|
| 79 |
$.post( PatchstackVars.ajaxurl, postData, function( response ) { |
| 80 |
isPolling = false |
| 81 |
pollingDelay = 5 |
| 82 |
if ( response.result == 'error' ) { |
| 83 |
$( '#patchstack-activate, .is-polling-section' ).show(); |
| 84 |
$( '.patchstack-loading' ).hide(); |
| 85 |
if ( response.error_message ) { |
| 86 |
alert( response.error_message ); |
| 87 |
} else { |
| 88 |
alert( PatchstackVars.error_message ); |
| 89 |
} |
| 90 |
} else { |
| 91 |
window.location = window.location.href + '&resync=1'; |
| 92 |
} |
| 93 |
}); |
| 94 |
}); |
| 95 |
}; |
| 96 |
|
| 97 |
$( plugin.init ); |
| 98 |
}( window, document, jQuery, window.Patchstack ) ); |