| @@ -1,61 +1,299 @@ | ||
| 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 | + * VERSION: Increased timeouts for better user experience | |
| 6 | + */ | |
| 7 | +jQuery(document).ready(function($) { | |
| 8 | + // Get configuration from hidden fields | |
| 9 | + const AJAX_URL = $('#mxchat-ajax-url').val() || ajaxurl; | |
| 10 | + const NONCE = $('#mxchat-nonce').val(); | |
| 11 | + const API_URL = $('#mxchat-api-url').val() || 'https://mxchat.ai'; | |
| 12 | + | |
| 13 | + // Elements | |
| 14 | + const form = $('#mxchat-activation-form'); | |
| 15 | + const spinner = $('#mxchat-activation-spinner'); | |
| 16 | + const submitButton = $('#activate_license_button'); | |
| 17 | + const licenseStatus = $('#mxchat-license-status'); | |
| 18 | + | |
| 19 | + // Create or get status message element | |
| 20 | + var $statusMessage = $('<div id="mxchat-activation-status" style="margin-top: 12px; padding: 12px 16px; border-radius: 8px; font-size: 14px; display: none;"></div>'); | |
| 21 | + if ($('#mxchat-activation-status').length === 0) { | |
| 22 | + spinner.after($statusMessage); | |
| 23 | + } else { | |
| 24 | + $statusMessage = $('#mxchat-activation-status'); | |
| 25 | + } | |
| 6 | 26 | |
| 7 | - form.addEventListener('submit', function(e) { | |
| 8 | - e.preventDefault(); | |
| 27 | + function showStatus(message, type) { | |
| 28 | + var bgColor = type === 'success' ? 'rgba(16, 185, 129, 0.1)' : | |
| 29 | + type === 'error' ? 'rgba(239, 68, 68, 0.1)' : | |
| 30 | + 'rgba(120, 115, 245, 0.1)'; | |
| 31 | + var textColor = type === 'success' ? '#059669' : | |
| 32 | + type === 'error' ? '#dc2626' : | |
| 33 | + '#7873f5'; | |
| 34 | + var icon = type === 'success' ? '✓' : | |
| 35 | + type === 'error' ? '✗' : | |
| 36 | + '<span class="dashicons dashicons-update spin" style="font-size: 14px; width: 14px; height: 14px; margin-right: 4px;"></span>'; | |
| 9 | 37 | |
| 10 | - // Show spinner and disable the button to prevent multiple submissions | |
| 11 | - spinner.style.display = 'inline-block'; | |
| 12 | - submitButton.disabled = true; | |
| 38 | + $statusMessage.html(icon + ' ' + message) | |
| 39 | + .css({ | |
| 40 | + 'background': bgColor, | |
| 41 | + 'color': textColor, | |
| 42 | + 'display': 'block' | |
| 43 | + }); | |
| 44 | + } | |
| 13 | 45 | |
| 14 | - // Gather form data | |
| 15 | - var formData = new FormData(form); | |
| 16 | - formData.append('action', 'mxchat_activate_license'); | |
| 17 | - formData.append('security', mxchatData.nonce); | |
| 46 | + // Activation handling | |
| 47 | + if (form.length && licenseStatus.length && submitButton.length) { | |
| 48 | + function handleActivationResponse(response) { | |
| 49 | + if (response.success) { | |
| 50 | + licenseStatus.text('Active'); | |
| 51 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 18 | 52 | |
| 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'; | |
| 53 | + // Show linking status - keep spinner visible | |
| 54 | + showStatus('License verified! Linking domain...', 'loading'); | |
| 25 | 55 | |
| 26 | - if (xhr.status >= 200 && xhr.status < 400) { | |
| 27 | - var response = JSON.parse(xhr.responseText); | |
| 28 | - if (response.success) { | |
| 29 | - // Update the license status to active | |
| 30 | - licenseStatus.textContent = 'Active'; | |
| 31 | - licenseStatus.classList.remove('inactive'); | |
| 32 | - licenseStatus.classList.add('active'); | |
| 56 | + // Track domain BEFORE showing success | |
| 57 | + trackDomainActivation() | |
| 58 | + .done(function(trackResponse) { | |
| 59 | + console.log('Domain tracking successful:', trackResponse); | |
| 60 | + spinner.hide(); | |
| 61 | + showStatus('License activated and domain linked successfully!', 'success'); | |
| 33 | 62 | |
| 34 | - // Hide the activation form | |
| 35 | - form.style.display = 'none'; | |
| 36 | - } else { | |
| 37 | - // Re-enable the button | |
| 38 | - submitButton.disabled = false; | |
| 63 | + // Wait before reload | |
| 64 | + setTimeout(function() { | |
| 65 | + location.reload(); | |
| 66 | + }, 2000); | |
| 67 | + }) | |
| 68 | + .fail(function(jqXHR, textStatus, errorThrown) { | |
| 69 | + console.error('Domain tracking failed:', textStatus, errorThrown); | |
| 70 | + spinner.hide(); | |
| 39 | 71 | |
| 40 | - // Handle the error case | |
| 41 | - alert(response.data || 'There was an error activating the license.'); | |
| 72 | + // Still show success but warn about domain linking | |
| 73 | + showStatus('License activated! Domain linking failed - you can link it manually after reload.', 'success'); | |
| 74 | + | |
| 75 | + // Wait before reload | |
| 76 | + setTimeout(function() { | |
| 77 | + location.reload(); | |
| 78 | + }, 3000); | |
| 79 | + }); | |
| 80 | + } else { | |
| 81 | + spinner.hide(); | |
| 82 | + licenseStatus.text('Inactive'); | |
| 83 | + showStatus(response.data || 'Activation failed. Please check your input.', 'error'); | |
| 84 | + submitButton.prop('disabled', false); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | + function checkActivationStatus() { | |
| 89 | + const email = $('#mxchat_pro_email').val(); | |
| 90 | + const key = $('#mxchat_activation_key').val(); | |
| 91 | + | |
| 92 | + $.ajax({ | |
| 93 | + type: 'POST', | |
| 94 | + url: AJAX_URL, | |
| 95 | + data: { | |
| 96 | + action: 'mxchat_check_license_status', | |
| 97 | + email: email, | |
| 98 | + key: key, | |
| 99 | + security: NONCE | |
| 100 | + }, | |
| 101 | + timeout: 45000, // Increased from default to 45 seconds | |
| 102 | + success: function(response) { | |
| 103 | + if (response.is_active) { | |
| 104 | + licenseStatus.text('Active'); | |
| 105 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 106 | + form.hide(); | |
| 107 | + | |
| 108 | + // Try to track domain for timeout scenario | |
| 109 | + trackDomainActivation() | |
| 110 | + .done(function(trackResponse) { | |
| 111 | + console.log('Domain tracking completed after timeout check:', trackResponse); | |
| 112 | + alert('Your license has been activated and domain linked successfully!'); | |
| 113 | + setTimeout(function() { | |
| 114 | + location.reload(); | |
| 115 | + }, 3000); | |
| 116 | + }) | |
| 117 | + .fail(function() { | |
| 118 | + console.log('Domain tracking failed after timeout check'); | |
| 119 | + alert('Your license has been activated successfully!\n\nNote: Domain linking may have failed. Check the "Link Domain" button after reload if needed.'); | |
| 120 | + setTimeout(function() { | |
| 121 | + location.reload(); | |
| 122 | + }, 3000); | |
| 123 | + }); | |
| 124 | + } else { | |
| 125 | + alert('License activation timed out. Please try again or contact support if the problem persists.'); | |
| 126 | + submitButton.prop('disabled', false); | |
| 127 | + } | |
| 128 | + }, | |
| 129 | + error: function() { | |
| 130 | + alert('Unable to verify license status. Please refresh the page to check if activation was successful.'); | |
| 131 | + submitButton.prop('disabled', false); | |
| 42 | 132 | } |
| 43 | - } else { | |
| 44 | - // Re-enable the button | |
| 45 | - submitButton.disabled = false; | |
| 133 | + }); | |
| 134 | + } | |
| 46 | 135 | |
| 47 | - // Handle server error | |
| 48 | - alert('Server error. Please try again.'); | |
| 49 | - } | |
| 50 | - }; | |
| 136 | + function trackDomainActivation() { | |
| 137 | + const email = $('#mxchat_pro_email').val(); | |
| 138 | + const key = $('#mxchat_activation_key').val(); | |
| 139 | + const domain = $('#mxchat_domain').val(); | |
| 140 | + | |
| 141 | + console.log('Attempting to track domain:', domain); | |
| 142 | + | |
| 143 | + return $.ajax({ | |
| 144 | + type: 'POST', | |
| 145 | + url: API_URL + '/mxchat-api/link-domain', // Changed to use same endpoint as manual linking | |
| 146 | + data: { | |
| 147 | + email: email, // Changed parameter name to match manual linking | |
| 148 | + license_key: key, // Changed parameter name to match manual linking | |
| 149 | + domain: domain | |
| 150 | + }, | |
| 151 | + timeout: 30000, // Increased from 10 seconds to 30 seconds | |
| 152 | + xhrFields: { | |
| 153 | + withCredentials: false // Ensure CORS compatibility | |
| 154 | + }, | |
| 155 | + crossDomain: true | |
| 156 | + }); | |
| 157 | + } | |
| 51 | 158 | |
| 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 | - }; | |
| 159 | + form.on('submit', function(event) { | |
| 160 | + event.preventDefault(); | |
| 161 | + spinner.show(); | |
| 162 | + submitButton.prop('disabled', true); | |
| 163 | + showStatus('Validating license key...', 'loading'); | |
| 58 | 164 | |
| 59 | - xhr.send(formData); // Send the form data | |
| 165 | + var formData = { | |
| 166 | + action: 'mxchat_handle_activate_license', | |
| 167 | + mxchat_pro_email: $('#mxchat_pro_email').val(), | |
| 168 | + mxchat_activation_key: $('#mxchat_activation_key').val(), | |
| 169 | + security: NONCE | |
| 170 | + }; | |
| 171 | + | |
| 172 | + $.ajax({ | |
| 173 | + type: 'POST', | |
| 174 | + url: AJAX_URL, | |
| 175 | + data: formData, | |
| 176 | + timeout: 120000, // Increased from 70 seconds to 2 minutes (120 seconds) | |
| 177 | + success: function(response) { | |
| 178 | + handleActivationResponse(response); | |
| 179 | + }, | |
| 180 | + error: function(jqXHR, textStatus) { | |
| 181 | + // On any error, check if the license was actually saved successfully | |
| 182 | + // This handles cases where the response format caused a parse error | |
| 183 | + // but the activation actually worked | |
| 184 | + showStatus('Verifying activation status...', 'loading'); | |
| 185 | + checkActivationStatus(); | |
| 186 | + } | |
| 187 | + }); | |
| 188 | + }); | |
| 189 | + } | |
| 190 | + | |
| 191 | + // Domain linking for existing activations | |
| 192 | + $('#link-domain-button').on('click', function() { | |
| 193 | + const button = $(this); | |
| 194 | + const status = $('#domain-link-status'); | |
| 195 | + const originalText = button.text(); | |
| 196 | + | |
| 197 | + // Get stored values from the page | |
| 198 | + const email = $('input#mxchat_pro_email').val() || | |
| 199 | + $('p:contains("Email:")').text().split(':')[1]?.trim() || | |
| 200 | + ''; | |
| 201 | + const license_key = $('input#mxchat_activation_key').val() || | |
| 202 | + $('code:contains("-")').text().trim() || | |
| 203 | + ''; | |
| 204 | + const domain = $('#mxchat_domain').val(); | |
| 205 | + | |
| 206 | + if (!email || !license_key) { | |
| 207 | + status.html('<span style="color: red;">✗ Missing email or license key</span>'); | |
| 208 | + return; | |
| 209 | + } | |
| 210 | + | |
| 211 | + button.prop('disabled', true).text('Linking...'); | |
| 212 | + status.html('<span style="color: #666;">Processing...</span>'); | |
| 213 | + | |
| 214 | + $.ajax({ | |
| 215 | + type: 'POST', | |
| 216 | + url: API_URL + '/mxchat-api/link-domain', | |
| 217 | + data: { | |
| 218 | + email: email, | |
| 219 | + license_key: license_key, | |
| 220 | + domain: domain | |
| 221 | + }, | |
| 222 | + timeout: 30000, // Increased from 10 seconds to 30 seconds | |
| 223 | + success: function(response) { | |
| 224 | + if (response.success) { | |
| 225 | + status.html('<span style="color: green;">✓ Domain linked successfully!</span>'); | |
| 226 | + button.hide(); | |
| 227 | + | |
| 228 | + setTimeout(function() { | |
| 229 | + location.reload(); | |
| 230 | + }, 2000); | |
| 231 | + } else { | |
| 232 | + status.html('<span style="color: red;">✗ ' + (response.data || 'Failed to link domain') + '</span>'); | |
| 233 | + button.prop('disabled', false).text(originalText); | |
| 234 | + } | |
| 235 | + }, | |
| 236 | + error: function(jqXHR, textStatus, errorThrown) { | |
| 237 | + console.error('Domain linking error:', textStatus, errorThrown); | |
| 238 | + status.html('<span style="color: red;">✗ Connection error. Please try again.</span>'); | |
| 239 | + button.prop('disabled', false).text(originalText); | |
| 240 | + } | |
| 241 | + }); | |
| 60 | 242 | }); |
| 61 | -}); | |
| 243 | + | |
| 244 | + // License deactivation | |
| 245 | + $('#deactivate-license-button').on('click', function() { | |
| 246 | + const button = $(this); | |
| 247 | + const status = $('#deactivate-status'); | |
| 248 | + const originalText = button.text(); | |
| 249 | + | |
| 250 | + // Confirmation dialog | |
| 251 | + const confirmMessage = 'Are you sure you want to deactivate this license?\n\n' + | |
| 252 | + 'This will:\n' + | |
| 253 | + '• Disable pro features on this site\n' + | |
| 254 | + '• Unlink this domain from your account\n' + | |
| 255 | + '• Allow you to activate on a different site\n\n' + | |
| 256 | + 'You can reactivate anytime with the same license key.'; | |
| 257 | + | |
| 258 | + if (!confirm(confirmMessage)) { | |
| 259 | + return; | |
| 260 | + } | |
| 261 | + | |
| 262 | + button.prop('disabled', true).text('Deactivating...'); | |
| 263 | + status.html('<span style="color: #666;">Processing...</span>'); | |
| 264 | + | |
| 265 | + $.ajax({ | |
| 266 | + type: 'POST', | |
| 267 | + url: AJAX_URL, | |
| 268 | + data: { | |
| 269 | + action: 'mxchat_deactivate_license', | |
| 270 | + security: NONCE | |
| 271 | + }, | |
| 272 | + timeout: 45000, // Increased from 15 seconds to 45 seconds | |
| 273 | + success: function(response) { | |
| 274 | + if (response.success) { | |
| 275 | + status.html('<span style="color: green;">✓ ' + response.data.message + '</span>'); | |
| 276 | + | |
| 277 | + // Show success message | |
| 278 | + alert('License deactivated successfully!\n\nYou can now activate this license on another site.'); | |
| 279 | + | |
| 280 | + // Reload page to show deactivated state | |
| 281 | + setTimeout(function() { | |
| 282 | + location.reload(); | |
| 283 | + }, 2000); | |
| 284 | + } else { | |
| 285 | + status.html('<span style="color: red;">✗ ' + (response.data || 'Deactivation failed') + '</span>'); | |
| 286 | + button.prop('disabled', false).text(originalText); | |
| 287 | + } | |
| 288 | + }, | |
| 289 | + error: function(jqXHR, textStatus) { | |
| 290 | + if (textStatus === 'timeout') { | |
| 291 | + status.html('<span style="color: orange;">⚠ Deactivation timed out. Please check your account dashboard.</span>'); | |
| 292 | + } else { | |
| 293 | + status.html('<span style="color: red;">✗ Connection error. Please try again.</span>'); | |
| 294 | + } | |
| 295 | + button.prop('disabled', false).text(originalText); | |
| 296 | + } | |
| 297 | + }); | |
| 298 | + }); | |
| 299 | +}); | |