/** * TigerStyle Heat Admin JavaScript */ // Tab switching functionality - Available immediately window.switchTab = function(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tab-content"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].classList.remove("active"); } tablinks = document.getElementsByClassName("nav-tab"); for (i = 0; i < tablinks.length; i++) { tablinks[i].classList.remove("nav-tab-active"); } document.getElementById(tabName).classList.add("active"); evt.currentTarget.classList.add("nav-tab-active"); }; jQuery(document).ready(function($) { // SEO Health Checker functionality window.runSEOHealthCheck = function() { const button = document.getElementById('seo-health-check-btn'); if (!button) return; const originalText = button.textContent; // Update button state button.textContent = tigerstyleSEO.strings.runningAnalysis; button.disabled = true; // Show loading indicator const loadingDiv = document.createElement('div'); loadingDiv.id = 'seo-loading'; loadingDiv.innerHTML = '

Analyzing SEO health...

'; const resultsContainer = document.getElementById('seo-health-results'); if (resultsContainer) { resultsContainer.innerHTML = ''; resultsContainer.appendChild(loadingDiv); } // Make AJAX request $.ajax({ url: tigerstyleSEO.ajaxurl, type: 'POST', data: { action: 'seo_health_check', nonce: tigerstyleSEO.nonce }, success: function(response) { button.textContent = originalText; button.disabled = false; if (response.success && resultsContainer) { displaySEOResults(response.data); } else { showError(tigerstyleSEO.strings.analysisFailed); } }, error: function() { button.textContent = originalText; button.disabled = false; showError(tigerstyleSEO.strings.networkError); } }); }; function displaySEOResults(data) { const resultsContainer = document.getElementById('seo-health-results'); if (!resultsContainer) return; let html = '
'; html += '
'; html += '

SEO Health Score: ' + Math.round((data.score / data.max_score) * 100) + '%

'; html += '
'; if (data.recommendations && data.recommendations.length > 0) { html += '

Recommendations

'; data.recommendations.forEach(function(item) { html += createSEOItem(item); }); html += '
'; } html += '
'; resultsContainer.innerHTML = html; } function createSEOItem(item) { let html = '
'; html += '

' + item.title + '

'; html += '

' + item.description + '

'; if (item.action && item.action.url) { html += '' + item.action.text + ''; } html += '
'; return html; } function showError(message) { const resultsContainer = document.getElementById('seo-health-results'); if (resultsContainer) { resultsContainer.innerHTML = '

' + message + '

'; } } // Cache Analysis functionality $('#analyze-cache-btn').on('click', function() { const button = $(this); const originalText = button.text(); const loadingDiv = $('#cache-analysis-loading'); const resultsDiv = $('#cache-analysis-results'); // Update button state button.text('Analyzing...').prop('disabled', true); loadingDiv.show(); resultsDiv.hide().empty(); // Make AJAX request $.ajax({ url: tigerstyleSEO.ajaxurl, type: 'POST', data: { action: 'tigerstyle_analyze_cache', nonce: tigerstyleSEO.cache_analysis_nonce }, success: function(response) { button.text(originalText).prop('disabled', false); loadingDiv.hide(); if (response.success) { displayCacheAnalysisResults(response.data); resultsDiv.show(); } else { showCacheAnalysisError('Analysis failed. Please try again.'); } }, error: function() { button.text(originalText).prop('disabled', false); loadingDiv.hide(); showCacheAnalysisError('Network error. Please check your connection and try again.'); } }); }); function displayCacheAnalysisResults(data) { const resultsDiv = $('#cache-analysis-results'); let html = '
'; // Summary section html += '
'; html += '

📊 Analysis Summary

'; html += '
'; html += '
Total Content Size:
' + data.total_size_formatted + '
'; html += '
Potential Savings:
' + data.potential_savings_formatted + '
'; html += '
Compression Ratio:
' + data.savings_percentage + '%
'; html += '
'; // Compression estimates if (data.compression_estimates) { html += '
'; html += '

🎯 Compression Method Comparison

'; html += '
'; Object.values(data.compression_estimates).forEach(function(estimate) { html += '
'; html += '' + estimate.method + '
'; html += 'Compression: ' + estimate.ratio + '%
'; html += 'Saves: ' + estimate.savings_formatted + ''; html += '
'; }); html += '
'; } // Pages analysis if (data.pages && data.pages.length > 0) { html += '
'; html += '

📄 Page Analysis

'; html += '
'; html += ''; html += ''; html += ''; data.pages.forEach(function(page) { html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; }); html += '
PageSizeCompression RatioPotential Savings
' + page.title + '
' + page.url + '
' + page.size_formatted + '' + page.compression_ratio + '%' + page.savings_formatted + '
'; } // Assets analysis if (data.assets && data.assets.length > 0) { html += '
'; html += '

🗂️ Static Assets Analysis

'; html += '
'; html += ''; html += ''; html += ''; data.assets.forEach(function(asset) { html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; }); html += '
FileTypeSizeCompression RatioPotential Savings
' + asset.filename + '' + asset.type + '' + asset.size_formatted + '' + asset.compression_ratio + '%' + asset.savings_formatted + '
'; } // Recommendations html += '
'; html += '

💡 Recommendations

'; html += '
'; html += '
'; resultsDiv.html(html); } function showCacheAnalysisError(message) { const resultsDiv = $('#cache-analysis-results'); resultsDiv.html('

' + message + '

').show(); } // AI Provider functionality $('.test-api-key').on('click', function() { const button = $(this); const provider = button.data('provider'); const originalText = button.text(); const resultsDiv = $('#provider-results-' + provider); button.text('Testing...').prop('disabled', true); resultsDiv.html('

Testing API key...

'); $.ajax({ url: tigerstyleSEO.ajaxurl, type: 'POST', data: { action: 'tigerstyle_test_api_key', provider_name: provider, nonce: tigerstyleSEO.ai_nonce || tigerstyleSEO.nonce }, success: function(response) { button.text(originalText).prop('disabled', false); if (response.success) { resultsDiv.html('

' + response.message + '

'); // Refresh the page to show updated models setTimeout(function() { location.reload(); }, 2000); } else { resultsDiv.html('

Error: ' + response.error + '

'); } }, error: function() { button.text(originalText).prop('disabled', false); resultsDiv.html('

Network error. Please try again.

'); } }); }); $('.refresh-models').on('click', function() { const button = $(this); const provider = button.data('provider'); const originalText = button.text(); const resultsDiv = $('#provider-results-' + provider); button.text('Refreshing...').prop('disabled', true); resultsDiv.html('

Refreshing models...

'); $.ajax({ url: tigerstyleSEO.ajaxurl, type: 'POST', data: { action: 'tigerstyle_refresh_models', provider_name: provider, nonce: tigerstyleSEO.ai_nonce || tigerstyleSEO.nonce }, success: function(response) { button.text(originalText).prop('disabled', false); if (response.success) { resultsDiv.html('

' + response.message + '

'); // Refresh the page to show updated models setTimeout(function() { location.reload(); }, 2000); } else { resultsDiv.html('

Error: ' + response.error + '

'); } }, error: function() { button.text(originalText).prop('disabled', false); resultsDiv.html('

Network error. Please try again.

'); } }); }); $('.delete-provider').on('click', function() { const button = $(this); const provider = button.data('provider'); if (!confirm('Are you sure you want to delete the "' + provider + '" provider? This action cannot be undone.')) { return; } const originalText = button.text(); button.text('Deleting...').prop('disabled', true); $.ajax({ url: tigerstyleSEO.ajaxurl, type: 'POST', data: { action: 'tigerstyle_delete_api_key', provider_name: provider, nonce: tigerstyleSEO.ai_nonce || tigerstyleSEO.nonce }, success: function(response) { if (response.success) { // Remove the provider card from the page button.closest('.provider-card').fadeOut(function() { $(this).remove(); }); } else { button.text(originalText).prop('disabled', false); alert('Error: ' + response.message); } }, error: function() { button.text(originalText).prop('disabled', false); alert('Network error. Please try again.'); } }); }); $('.model-toggle').on('change', function() { const checkbox = $(this); const provider = checkbox.data('provider'); const model = checkbox.data('model'); const enabled = checkbox.is(':checked'); // Create a hidden form and submit it const form = $('
'); form.append(''); form.append(''); form.append(''); form.append(''); if (enabled) { form.append(''); } form.append(''); $('body').append(form); form.submit(); }); // AI Chat Interface functionality $('#ai-chat-provider').on('change', function() { const hasProvider = $(this).val() !== ''; $('#ai-chat-send').prop('disabled', !hasProvider); if (hasProvider) { $('#ai-chat-status').text('Ready to chat with ' + $(this).find('option:selected').text()); } else { $('#ai-chat-status').text(''); } }); $('#ai-chat-clear').on('click', function() { $('#ai-chat-messages').html('
💡 Ask me anything about SEO, content optimization, meta tags, or website improvement!
'); }); $('#ai-chat-send').on('click', function() { sendChatMessage(); }); $('#ai-chat-input').on('keypress', function(e) { if (e.which === 13 && e.ctrlKey) { // Ctrl+Enter sendChatMessage(); } }); function sendChatMessage() { const provider = $('#ai-chat-provider').val(); const message = $('#ai-chat-input').val().trim(); if (!provider || !message) { return; } const [providerName, modelId] = provider.split('|'); // Add user message to chat addChatMessage('user', message); $('#ai-chat-input').val(''); // Show loading const loadingId = 'loading_' + Date.now(); addChatMessage('assistant', '
Thinking...
'); // Send AJAX request $.ajax({ url: tigerstyleSEO.ajaxurl, type: 'POST', data: { action: 'tigerstyle_ai_chat', provider_name: providerName, model_id: modelId, message: message, nonce: tigerstyleSEO.ai_nonce || tigerstyleSEO.nonce }, success: function(response) { $('#' + loadingId).closest('.chat-message').remove(); if (response.success) { addChatMessage('assistant', response.data.response); } else { addChatMessage('error', 'Error: ' + (response.data || 'Failed to get response')); } }, error: function() { $('#' + loadingId).closest('.chat-message').remove(); addChatMessage('error', 'Network error. Please try again.'); } }); } function addChatMessage(sender, content) { const timestamp = new Date().toLocaleTimeString(); let messageClass = 'chat-message'; let senderIcon = ''; let senderLabel = ''; switch(sender) { case 'user': messageClass += ' user-message'; senderIcon = '🙋‍♀️'; senderLabel = 'You'; break; case 'assistant': messageClass += ' assistant-message'; senderIcon = '🤖'; senderLabel = 'AI Assistant'; break; case 'error': messageClass += ' error-message'; senderIcon = '⚠️'; senderLabel = 'Error'; break; } const messageHtml = '
' + '
' + '' + senderIcon + ' ' + senderLabel + ' ' + timestamp + '' + '
' + '
' + content + '
' + '
'; $('#ai-chat-messages').append(messageHtml); $('#ai-chat-messages').scrollTop($('#ai-chat-messages')[0].scrollHeight); } });