/**
* 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 = '
';
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 = '';
}
}
// 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 += '| Page | Size | Compression Ratio | Potential Savings |
';
html += '';
data.pages.forEach(function(page) {
html += '';
html += '' + page.title + ' ' + page.url + ' | ';
html += '' + page.size_formatted + ' | ';
html += '' + page.compression_ratio + '% | ';
html += '' + page.savings_formatted + ' | ';
html += '
';
});
html += '
';
}
// Assets analysis
if (data.assets && data.assets.length > 0) {
html += '
';
html += '
🗂️ Static Assets Analysis
';
html += '
';
html += '
';
html += '| File | Type | Size | Compression Ratio | Potential Savings |
';
html += '';
data.assets.forEach(function(asset) {
html += '';
html += '| ' + asset.filename + ' | ';
html += '' + asset.type + ' | ';
html += '' + asset.size_formatted + ' | ';
html += '' + asset.compression_ratio + '% | ';
html += '' + asset.savings_formatted + ' | ';
html += '
';
});
html += '
';
}
// Recommendations
html += '
';
html += '
💡 Recommendations
';
html += '
';
html += '- Enable Compression: Turn on the compression system above to automatically compress all content.
';
html += '- Use Auto Mode: The automatic Brotli + Gzip fallback provides the best compatibility and performance.
';
html += '- Monitor Performance: Check the statistics regularly to track your bandwidth savings.
';
if (data.savings_percentage > 60) {
html += '- Excellent Potential: Your site has high compression potential! Enable compression for significant performance gains.
';
}
html += '
';
html += '
';
resultsDiv.html(html);
}
function showCacheAnalysisError(message) {
const resultsDiv = $('#cache-analysis-results');
resultsDiv.html('').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('');
$.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('');
// 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('');
$.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('');
// 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 = $('