| @@ -1,61 +1,224 @@ | ||
| 1 | -document.addEventListener('DOMContentLoaded', function() { | |
| 2 | - var form = document.getElementById('mxchat-activation-form'); | |
| 3 | - var spinner = document.getElementById('mxchat-activation-spinner'); | |
| 4 | - var submitButton = document.getElementById('activate_license_button'); | |
| 5 | - var licenseStatus = document.getElementById('mxchat-license-status'); | |
| 1 | +/** | |
| 2 | + * Enhanced MxChat License Activation JS | |
| 3 | + * This handles BOTH local activation AND domain tracking | |
| 4 | + * Plus deactivation functionality | |
| 5 | + */ | |
| 6 | +jQuery(document).ready(function($) { | |
| 7 | + // Get configuration from hidden fields | |
| 8 | + const AJAX_URL = $('#mxchat-ajax-url').val() || ajaxurl; | |
| 9 | + const NONCE = $('#mxchat-nonce').val(); | |
| 10 | + const API_URL = $('#mxchat-api-url').val() || 'https://mxchat.ai'; | |
| 11 | + | |
| 12 | + // Elements | |
| 13 | + const form = $('#mxchat-activation-form'); | |
| 14 | + const spinner = $('#mxchat-activation-spinner'); | |
| 15 | + const submitButton = $('#activate_license_button'); | |
| 16 | + const licenseStatus = $('#mxchat-license-status'); | |
| 17 | + | |
| 18 | + // Activation handling | |
| 19 | + if (form.length && licenseStatus.length && submitButton.length) { | |
| 20 | + function handleActivationResponse(response) { | |
| 21 | + spinner.hide(); | |
| 22 | + if (response.success) { | |
| 23 | + licenseStatus.text('Active'); | |
| 24 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 25 | + form.hide(); | |
| 26 | + | |
| 27 | + alert('License activated successfully!'); | |
| 28 | + | |
| 29 | + // Track domain on your website | |
| 30 | + trackDomainActivation(); | |
| 31 | + | |
| 32 | + setTimeout(function() { | |
| 33 | + location.reload(); | |
| 34 | + }, 1500); | |
| 35 | + } else { | |
| 36 | + licenseStatus.text('Inactive'); | |
| 37 | + alert(response.data || 'Activation failed. Please check your input.'); | |
| 38 | + submitButton.prop('disabled', false); | |
| 39 | + } | |
| 40 | + } | |
| 6 | 41 | |
| 7 | - form.addEventListener('submit', function(e) { | |
| 8 | - e.preventDefault(); | |
| 42 | + function checkActivationStatus() { | |
| 43 | + const email = $('#mxchat_pro_email').val(); | |
| 44 | + const key = $('#mxchat_activation_key').val(); | |
| 45 | + | |
| 46 | + $.ajax({ | |
| 47 | + type: 'POST', | |
| 48 | + url: AJAX_URL, | |
| 49 | + data: { | |
| 50 | + action: 'mxchat_check_license_status', | |
| 51 | + email: email, | |
| 52 | + key: key, | |
| 53 | + security: NONCE | |
| 54 | + }, | |
| 55 | + success: function(response) { | |
| 56 | + if (response.is_active) { | |
| 57 | + licenseStatus.text('Active'); | |
| 58 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 59 | + form.hide(); | |
| 60 | + alert('Your license has been activated successfully!'); | |
| 61 | + | |
| 62 | + trackDomainActivation(); | |
| 63 | + | |
| 64 | + setTimeout(function() { | |
| 65 | + location.reload(); | |
| 66 | + }, 1500); | |
| 67 | + } else { | |
| 68 | + alert('License activation timed out. Please try again or contact support if the problem persists.'); | |
| 69 | + submitButton.prop('disabled', false); | |
| 70 | + } | |
| 71 | + }, | |
| 72 | + error: function() { | |
| 73 | + alert('Unable to verify license status. Please refresh the page to check if activation was successful.'); | |
| 74 | + submitButton.prop('disabled', false); | |
| 75 | + } | |
| 76 | + }); | |
| 77 | + } | |
| 9 | 78 | |
| 10 | - // Show spinner and disable the button to prevent multiple submissions | |
| 11 | - spinner.style.display = 'inline-block'; | |
| 12 | - submitButton.disabled = true; | |
| 79 | + function trackDomainActivation() { | |
| 80 | + $.ajax({ | |
| 81 | + type: 'POST', | |
| 82 | + url: API_URL + '/mxchat-api/activate-license', | |
| 83 | + data: { | |
| 84 | + mxchat_pro_email: $('#mxchat_pro_email').val(), | |
| 85 | + mxchat_activation_key: $('#mxchat_activation_key').val(), | |
| 86 | + domain: $('#mxchat_domain').val() | |
| 87 | + }, | |
| 88 | + success: function(response) { | |
| 89 | + //console.log('Domain tracking:', response.success ? 'Success' : 'Failed'); | |
| 90 | + }, | |
| 91 | + error: function() { | |
| 92 | + //console.log('Domain tracking: Connection error'); | |
| 93 | + } | |
| 94 | + }); | |
| 95 | + } | |
| 13 | 96 | |
| 14 | - // Gather form data | |
| 15 | - var formData = new FormData(form); | |
| 16 | - formData.append('action', 'mxchat_activate_license'); | |
| 17 | - formData.append('security', mxchatData.nonce); | |
| 18 | - | |
| 19 | - // Send the AJAX request | |
| 20 | - var xhr = new XMLHttpRequest(); | |
| 21 | - xhr.open('POST', mxchatData.ajax_url, true); | |
| 22 | - xhr.onload = function() { | |
| 23 | - // Hide the spinner | |
| 24 | - spinner.style.display = 'none'; | |
| 25 | - | |
| 26 | - if (xhr.status >= 200 && xhr.status < 400) { | |
| 27 | - var response = JSON.parse(xhr.responseText); | |
| 97 | + form.on('submit', function(event) { | |
| 98 | + event.preventDefault(); | |
| 99 | + spinner.show(); | |
| 100 | + submitButton.prop('disabled', true); | |
| 101 | + | |
| 102 | + var formData = { | |
| 103 | + action: 'mxchat_handle_activate_license', | |
| 104 | + mxchat_pro_email: $('#mxchat_pro_email').val(), | |
| 105 | + mxchat_activation_key: $('#mxchat_activation_key').val(), | |
| 106 | + security: NONCE | |
| 107 | + }; | |
| 108 | + | |
| 109 | + $.ajax({ | |
| 110 | + type: 'POST', | |
| 111 | + url: AJAX_URL, | |
| 112 | + data: formData, | |
| 113 | + timeout: 70000, | |
| 114 | + success: function(response) { | |
| 115 | + handleActivationResponse(response); | |
| 116 | + }, | |
| 117 | + error: function(jqXHR, textStatus) { | |
| 118 | + if (textStatus === 'timeout') { | |
| 119 | + spinner.hide(); | |
| 120 | + alert('Activation is taking longer than expected. Checking status...'); | |
| 121 | + checkActivationStatus(); | |
| 122 | + } else { | |
| 123 | + alert('Activation failed due to a server error: ' + (jqXHR.responseText || textStatus)); | |
| 124 | + spinner.hide(); | |
| 125 | + submitButton.prop('disabled', false); | |
| 126 | + } | |
| 127 | + } | |
| 128 | + }); | |
| 129 | + }); | |
| 130 | + } | |
| 131 | + | |
| 132 | + // Domain linking for existing activations | |
| 133 | + $('#link-domain-button').on('click', function() { | |
| 134 | + const button = $(this); | |
| 135 | + const status = $('#domain-link-status'); | |
| 136 | + const originalText = button.text(); | |
| 137 | + | |
| 138 | + button.prop('disabled', true).text('Linking...'); | |
| 139 | + status.html('<span style="color: #666;">Processing...</span>'); | |
| 140 | + | |
| 141 | + $.ajax({ | |
| 142 | + type: 'POST', | |
| 143 | + url: API_URL + '/mxchat-api/link-domain', | |
| 144 | + data: { | |
| 145 | + email: $('#mxchat_pro_email').val(), | |
| 146 | + license_key: $('#mxchat_activation_key').val(), | |
| 147 | + domain: $('#mxchat_domain').val() | |
| 148 | + }, | |
| 149 | + success: function(response) { | |
| 28 | 150 | if (response.success) { |
| 29 | - // Update the license status to active | |
| 30 | - licenseStatus.textContent = 'Active'; | |
| 31 | - licenseStatus.classList.remove('inactive'); | |
| 32 | - licenseStatus.classList.add('active'); | |
| 33 | - | |
| 34 | - // Hide the activation form | |
| 35 | - form.style.display = 'none'; | |
| 151 | + status.html('<span style="color: green;">✓ Domain linked successfully!</span>'); | |
| 152 | + button.hide(); | |
| 153 | + | |
| 154 | + setTimeout(function() { | |
| 155 | + location.reload(); | |
| 156 | + }, 3000); | |
| 36 | 157 | } else { |
| 37 | - // Re-enable the button | |
| 38 | - submitButton.disabled = false; | |
| 39 | - | |
| 40 | - // Handle the error case | |
| 41 | - alert(response.data || 'There was an error activating the license.'); | |
| 158 | + status.html('<span style="color: red;">✗ ' + (response.data || 'Failed to link domain') + '</span>'); | |
| 159 | + button.prop('disabled', false).text(originalText); | |
| 42 | 160 | } |
| 43 | - } else { | |
| 44 | - // Re-enable the button | |
| 45 | - submitButton.disabled = false; | |
| 46 | - | |
| 47 | - // Handle server error | |
| 48 | - alert('Server error. Please try again.'); | |
| 161 | + }, | |
| 162 | + error: function(jqXHR, textStatus) { | |
| 163 | + status.html('<span style="color: red;">✗ Connection error. Please try again.</span>'); | |
| 164 | + button.prop('disabled', false).text(originalText); | |
| 49 | 165 | } |
| 50 | - }; | |
| 51 | - | |
| 52 | - xhr.onerror = function() { | |
| 53 | - // Hide the spinner and re-enable the button on error | |
| 54 | - spinner.style.display = 'none'; | |
| 55 | - submitButton.disabled = false; | |
| 56 | - alert('Request failed. Please check your internet connection.'); | |
| 57 | - }; | |
| 58 | - | |
| 59 | - xhr.send(formData); // Send the form data | |
| 166 | + }); | |
| 60 | 167 | }); |
| 61 | -}); | |
| 168 | + | |
| 169 | + // License deactivation | |
| 170 | + $('#deactivate-license-button').on('click', function() { | |
| 171 | + const button = $(this); | |
| 172 | + const status = $('#deactivate-status'); | |
| 173 | + const originalText = button.text(); | |
| 174 | + | |
| 175 | + // Confirmation dialog | |
| 176 | + const confirmMessage = 'Are you sure you want to deactivate this license?\n\n' + | |
| 177 | + 'This will:\n' + | |
| 178 | + '• Disable pro features on this site\n' + | |
| 179 | + '• Unlink this domain from your account\n' + | |
| 180 | + '• Allow you to activate on a different site\n\n' + | |
| 181 | + 'You can reactivate anytime with the same license key.'; | |
| 182 | + | |
| 183 | + if (!confirm(confirmMessage)) { | |
| 184 | + return; | |
| 185 | + } | |
| 186 | + | |
| 187 | + button.prop('disabled', true).text('Deactivating...'); | |
| 188 | + status.html('<span style="color: #666;">Processing...</span>'); | |
| 189 | + | |
| 190 | + $.ajax({ | |
| 191 | + type: 'POST', | |
| 192 | + url: AJAX_URL, | |
| 193 | + data: { | |
| 194 | + action: 'mxchat_deactivate_license', | |
| 195 | + security: NONCE | |
| 196 | + }, | |
| 197 | + timeout: 15000, | |
| 198 | + success: function(response) { | |
| 199 | + if (response.success) { | |
| 200 | + status.html('<span style="color: green;">✓ ' + response.data.message + '</span>'); | |
| 201 | + | |
| 202 | + // Show success message | |
| 203 | + alert('License deactivated successfully!\n\nYou can now activate this license on another site.'); | |
| 204 | + | |
| 205 | + // Reload page to show deactivated state | |
| 206 | + setTimeout(function() { | |
| 207 | + location.reload(); | |
| 208 | + }, 2000); | |
| 209 | + } else { | |
| 210 | + status.html('<span style="color: red;">✗ ' + (response.data || 'Deactivation failed') + '</span>'); | |
| 211 | + button.prop('disabled', false).text(originalText); | |
| 212 | + } | |
| 213 | + }, | |
| 214 | + error: function(jqXHR, textStatus) { | |
| 215 | + if (textStatus === 'timeout') { | |
| 216 | + status.html('<span style="color: orange;">⚠ Deactivation timed out. Please check your account dashboard.</span>'); | |
| 217 | + } else { | |
| 218 | + status.html('<span style="color: red;">✗ Connection error. Please try again.</span>'); | |
| 219 | + } | |
| 220 | + button.prop('disabled', false).text(originalText); | |
| 221 | + } | |
| 222 | + }); | |
| 223 | + }); | |
| 224 | +}); | |