| @@ -6,8 +6,11 @@ | ||
| 6 | 6 | window.Patchstack = window.Patchstack || {}; |
| 7 | 7 | |
| 8 | 8 | ( function( window, document, $, plugin ) { |
| 9 | 9 | var $c = {}; |
| 10 | + var polling = null; | |
| 11 | + var isPolling = false; | |
| 12 | + var pollingDelay = 0; | |
| 10 | 13 | |
| 11 | 14 | plugin.init = function() { |
| 12 | 15 | plugin.cache(); |
| 13 | 16 | plugin.bindEvents(); |
| @@ -17,127 +20,79 @@ | ||
| 17 | 20 | $c.window = $( window ); |
| 18 | 21 | $c.body = $( document.body ); |
| 19 | 22 | }; |
| 20 | 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 | + | |
| 21 | 59 | plugin.bindEvents = function() { |
| 22 | 60 | $('input#patchstack-activate').on( 'click', function(e) { |
| 23 | 61 | e.preventDefault(); |
| 24 | 62 | var postData = { |
| 25 | - action: 'activate_license', | |
| 26 | - clientid: $( '#patchstack_api_client_id' ).val(), | |
| 27 | - secretkey: $( '#patchstack_api_client_secret_key' ).val(), | |
| 63 | + action: 'patchstack_activate_license', | |
| 64 | + key: $('.patchstack-auto-activate').attr('style') == '' ? $( '#patchstack_api_key2' ).val() : $( '#patchstack_api_key' ).val(), | |
| 28 | 65 | PatchstackNonce: PatchstackVars.nonce |
| 29 | 66 | }; |
| 30 | 67 | |
| 31 | - if ( $.trim( postData.clientid ).length == 0 || $.trim( postData.secretkey ).length == 0) { | |
| 32 | - alert( 'Please enter the site id and secret key.' ); | |
| 68 | + if ( $.trim( postData.key ).length == 0 ) { | |
| 69 | + alert( 'Please enter the API key.' ); | |
| 33 | 70 | return false; |
| 34 | 71 | } |
| 35 | 72 | |
| 73 | + $( '.patchstack-resync' ).hide(); | |
| 74 | + $( '#patchstack-activate' ).hide(); | |
| 75 | + $( '.patchstack-loading' ).show(); | |
| 76 | + $( '.is-polling-section' ).hide(); | |
| 77 | + isPolling = true | |
| 78 | + | |
| 36 | 79 | $.post( PatchstackVars.ajaxurl, postData, function( response ) { |
| 80 | + isPolling = false | |
| 81 | + pollingDelay = 5 | |
| 37 | 82 | if ( response.result == 'error' ) { |
| 38 | - alert( PatchstackVars.error_message ); | |
| 39 | - } else { | |
| 40 | - if (response.response && response.response.free === false) { | |
| 41 | - window.location.reload(); | |
| 83 | + $( '#patchstack-activate, .is-polling-section' ).show(); | |
| 84 | + $( '.patchstack-loading' ).hide(); | |
| 85 | + if ( response.error_message ) { | |
| 86 | + alert( response.error_message ); | |
| 42 | 87 | } else { |
| 43 | - $( '.patchstack-license-status' ).text( 'Active' ).css( { color: '#B2D675' } ); | |
| 44 | - $( '.patchstack-sub-type' ).text( 'Community (Free) '); | |
| 45 | - $( '.ps-b1, .ps-b3, .ps-p1' ).hide(); | |
| 46 | - $( '.ps-b2, .ps-b4, .ps-p2' ).show(); | |
| 88 | + alert( PatchstackVars.error_message ); | |
| 47 | 89 | } |
| 48 | - } | |
| 49 | - }); | |
| 50 | - }); | |
| 51 | - | |
| 52 | - $('#patchstack_send_mail_url').on( 'click', function(e) { | |
| 53 | - e.preventDefault(); | |
| 54 | - var postData = { | |
| 55 | - action: 'send_new_url_email', | |
| 56 | - PatchstackNonce: PatchstackVars.nonce | |
| 57 | - }; | |
| 58 | - $.post( PatchstackVars.ajaxurl, postData, function( response ) { | |
| 59 | - if ( response == 'fail') { | |
| 60 | - alert( PatchstackVars.error_message ); | |
| 61 | 90 | } else { |
| 62 | - alert( 'Email Sent!' ); | |
| 91 | + window.location = window.location.href + '&resync=1'; | |
| 63 | 92 | } |
| 64 | 93 | }); |
| 65 | 94 | }); |
| 66 | - | |
| 67 | - // Don't load this part if DataTables is not loaded. | |
| 68 | - if(typeof jQuery.fn.dataTable !== 'undefined' && window.location.href.indexOf('patchstack') !== -1){ | |
| 69 | - | |
| 70 | - // Initialize datatables. | |
| 71 | - $('.table-firewall-log').DataTable({ | |
| 72 | - "processing": true, | |
| 73 | - "serverSide": true, | |
| 74 | - "ajax": { | |
| 75 | - "url": PatchstackVars.ajaxurl, | |
| 76 | - "type": "POST", | |
| 77 | - "data": function(d){ | |
| 78 | - d.action = 'firewall_log_table'; | |
| 79 | - d.PatchstackNonce = $("meta[name=patchstack_nonce]").attr("value"); | |
| 80 | - } | |
| 81 | - }, | |
| 82 | - "responsive": true, | |
| 83 | - "columns": [ | |
| 84 | - { "data": "fid" }, | |
| 85 | - { "data": "referer" }, | |
| 86 | - { "data": "method" }, | |
| 87 | - { "data": "ip", render: $.fn.dataTable.render.text() }, | |
| 88 | - { "data": "log_date" } | |
| 89 | - ], | |
| 90 | - "order": [[0, "desc"]], | |
| 91 | - "searching": false, | |
| 92 | - "ordering": false, | |
| 93 | - "drawCallback": function( settings ) { | |
| 94 | - $('.titletip').tooltip(); | |
| 95 | - }, | |
| 96 | - "columnDefs": [ | |
| 97 | - { | |
| 98 | - "render": function ( data, type, row ) { | |
| 99 | - var title = (data ? data : 'Unknown'); | |
| 100 | - var description = (row.description ? row.description : 'No Explanation'); | |
| 101 | - | |
| 102 | - return '<span class=" titletip" title="' + description + '">' + title + '</span>'; | |
| 103 | - }, | |
| 104 | - "targets": 0 | |
| 105 | - }, | |
| 106 | - { | |
| 107 | - "render": function ( data, type, row ) { | |
| 108 | - return decodeURIComponent(data).replace(/</g,"<").replace(/>/g,">").replace('+', ' '); | |
| 109 | - }, | |
| 110 | - "targets": 1 | |
| 111 | - } | |
| 112 | - ] | |
| 113 | - }); | |
| 114 | - | |
| 115 | - $('.table-user-log').DataTable({ | |
| 116 | - "processing": true, | |
| 117 | - "serverSide": true, | |
| 118 | - "ajax": { | |
| 119 | - "url": PatchstackVars.ajaxurl, | |
| 120 | - "type": "POST", | |
| 121 | - "data": function(d){ | |
| 122 | - d.action = 'users_log_table'; | |
| 123 | - d.PatchstackNonce = $("meta[name=patchstack_nonce]").attr("value"); | |
| 124 | - } | |
| 125 | - }, | |
| 126 | - "responsive": true, | |
| 127 | - "columns": [ | |
| 128 | - { "data": "author" }, | |
| 129 | - { "data": "action", render: $.fn.dataTable.render.text() }, | |
| 130 | - { "data": "object", render: $.fn.dataTable.render.text() }, | |
| 131 | - { "data": "object_name", render: $.fn.dataTable.render.text() }, | |
| 132 | - { "data": "ip", render: $.fn.dataTable.render.text() }, | |
| 133 | - { "data": "date", render: $.fn.dataTable.render.text() } | |
| 134 | - ], | |
| 135 | - "order": [[0, "desc"]], | |
| 136 | - "searching": true, | |
| 137 | - "ordering": false | |
| 138 | - }); | |
| 139 | - } | |
| 140 | 95 | |
| 141 | 96 | if($("#patchstack_captcha_type").val() == "v2"){ |
| 142 | 97 | $("#patchstack_captcha_public_key_v3, #patchstack_captcha_private_key_v3, #patchstack_captcha_public_key_v3_new, #patchstack_captcha_private_key_v3_new").hide(); |
| 143 | 98 | $("#patchstack_captcha_public_key, #patchstack_captcha_private_key").show(); |