// Define loadTheme in the global scope so it can be called from inline scripts var loadTheme; jQuery(document).ready(function($) { // Function to show notification function showNotification(message, $element, isError = false) { // Remove any existing notifications $('.seoprostack-setting-notification').remove(); // Create notification element var $notification = $('' + message + ''); // If element is provided, show notification next to it if ($element && $element.length) { $element.after($notification); } else { // Fallback to header if no element provided $('.seoprostack-header h1').after($notification); } // Fade out after delay setTimeout(function() { $notification.fadeOut(300, function() { $(this).remove(); }); }, 2000); } // Handle option updates function updateOption(option, value) { return $.ajax({ url: wpSeoProStack.ajaxurl, type: 'POST', data: { action: 'wp_seoprostack_update_option', option: option, value: value, nonce: wpSeoProStack.nonce } }).then(function(response) { if (!response.success) { throw new Error(response.data || 'Error saving setting'); } return response; }); } // Toggle sections $('.seoprostack-toggle-header').on('click', function() { $(this).toggleClass('active'); $(this).next('.seoprostack-toggle-settings').slideToggle(300); }); // Tabs functionality (if not using WP default tabs) $('.seoprostack-tab-nav a').on('click', function(e) { e.preventDefault(); var targetTab = $(this).attr('href').substring(1); // Update active tab $('.seoprostack-tab-nav a').removeClass('active'); $(this).addClass('active'); // Show target tab content $('.seoprostack-tab-content').hide(); $('#' + targetTab).show(); // Update URL without refreshing if (history.pushState) { var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?page=seoprostack&tab=' + targetTab; window.history.pushState({path: newUrl}, '', newUrl); } }); // Pro Plugins Tab if ($('#pro-plugins').length) { // Category filter $('.seoprostack-category-filter a').on('click', function(e) { e.preventDefault(); var category = $(this).data('category'); // Update active filter $('.seoprostack-category-filter a').removeClass('active'); $(this).addClass('active'); // Show loading $('#seoprostack-plugins-grid').addClass('loading'); // Load plugins $.ajax({ url: wpSeoProStack.ajaxurl, type: 'POST', data: { action: 'wp_seoprostack_get_plugins', category: category, nonce: wpSeoProStack.nonce }, success: function(response) { $('#seoprostack-plugins-grid').removeClass('loading'); if (response.success) { renderPlugins(response.data.plugins); } else { $('#seoprostack-plugins-grid').html('
' + response.data.message + '
'); } }, error: function() { $('#seoprostack-plugins-grid').removeClass('loading'); $('#seoprostack-plugins-grid').html('
Error connecting to server
'); } }); }); // Activate plugin $(document).on('click', '.seoprostack-activate-plugin', function(e) { e.preventDefault(); var $button = $(this); var plugin = $button.data('plugin'); var $card = $button.closest('.seoprostack-plugin-card'); // Show loading $button.prop('disabled', true).text('Activating...'); // Send activation request $.ajax({ url: wpSeoProStack.ajaxurl, type: 'POST', data: { action: 'wp_seoprostack_activate_plugin', plugin: plugin, nonce: wpSeoProStack.nonce }, success: function(response) { if (response.success) { // Update UI $button.text('Activated').addClass('button-disabled'); $card.find('.plugin-status').removeClass('not-installed installed').addClass('active').text('Active'); } else { $button.prop('disabled', false).text('Activate'); alert(response.data.message); } }, error: function() { $button.prop('disabled', false).text('Activate'); alert('Error connecting to server'); } }); }); // Render plugins function renderPlugins(plugins) { var html = ''; if (plugins.length === 0) { html = '
No plugins found in this category
'; } else { plugins.forEach(function(plugin) { var statusClass = plugin.active ? 'active' : (plugin.status === 'installed' ? 'installed' : 'not-installed'); var statusText = plugin.active ? 'Active' : (plugin.status === 'installed' ? 'Installed' : 'Not Installed'); var buttonText = plugin.active ? 'Activated' : 'Activate'; var buttonDisabled = plugin.active ? ' button-disabled' : ''; html += '
'; html += '
'; html += '' + statusText + ''; html += '
'; html += '
'; html += '

' + plugin.name + ' v' + plugin.version + '

'; html += '

' + plugin.description + '

'; html += ''; html += '
'; html += '
'; }); } $('#seoprostack-plugins-grid').html(html); } // Load initial plugins $('.seoprostack-category-filter a.active').trigger('click'); } // Advanced Tab Form if ($('#seoprostack-advanced-settings-form').length) { $('#seoprostack-advanced-settings-form').on('submit', function(e) { e.preventDefault(); var $form = $(this); var $button = $('#seoprostack-save-advanced-settings'); var $spinner = $button.next('.spinner'); var $response = $('#advanced-settings-response'); // Show loading $button.prop('disabled', true); $spinner.css('visibility', 'visible'); // Get form data var formData = $form.serializeArray(); var data = { action: 'wp_seoprostack_save_advanced_settings', nonce: wpSeoProStack.nonce }; // Convert form data to proper format $.each(formData, function(i, field) { data[field.name] = field.value; }); // Add checkbox fields that might not be in formData $form.find('input[type="checkbox"]').each(function() { var name = $(this).attr('name'); if (data[name] === undefined) { data[name] = 'no'; } }); // Send AJAX request $.post(seoProStack.ajaxurl, data, function(response) { // Hide loading $button.prop('disabled', false); $spinner.css('visibility', 'hidden'); // Show response if (response.success) { $response.html('
' + response.data.message + '
'); } else { $response.html('
' + response.data.message + '
'); } // Hide response after delay setTimeout(function() { $response.find('.seoprostack-notice').fadeOut(500, function() { $(this).remove(); }); }, 3000); }).fail(function() { // Hide loading $button.prop('disabled', false); $spinner.css('visibility', 'hidden'); // Show error $response.html('
Error connecting to server
'); }); }); } // Tools Tab if ($('#tools').length) { // Database Optimization $('#seoprostack-optimize-db').on('click', function(e) { e.preventDefault(); var $button = $(this); var $spinner = $button.next('.spinner'); var $response = $('#db-optimize-response'); // Show loading $button.prop('disabled', true); $spinner.css('visibility', 'visible'); // Send AJAX request $.ajax({ url: wpSeoProStack.ajaxurl, type: 'POST', data: { action: 'wp_seoprostack_optimize_database', nonce: wpSeoProStack.nonce }, success: function(response) { // Hide loading $button.prop('disabled', false); $spinner.css('visibility', 'hidden'); // Show response if (response.success) { $response.html('
' + response.data.message + '
'); // Update stats if provided if (response.data.stats) { updateDbStats(response.data.stats); } } else { $response.html('
' + response.data.message + '
'); } // Hide response after delay setTimeout(function() { $response.find('.seoprostack-notice').fadeOut(500, function() { $(this).remove(); }); }, 5000); }, error: function() { // Hide loading $button.prop('disabled', false); $spinner.css('visibility', 'hidden'); // Show error $response.html('
Error connecting to server
'); } }); }); // Function to update database stats function updateDbStats(stats) { if (stats.total_cleaned) { $('#db-total-cleaned').text(stats.total_cleaned); } if (stats.db_size_before) { $('#db-size-before').text(stats.db_size_before); } if (stats.db_size_after) { $('#db-size-after').text(stats.db_size_after); } if (stats.savings_percentage) { $('#db-savings').text(stats.savings_percentage + '%'); } } // Generate Robots.txt $('#seoprostack-generate-robots').on('click', function(e) { e.preventDefault(); var $button = $(this); var $spinner = $button.next('.spinner'); var $response = $('#robots-response'); var $content = $('#robots-content'); // Show loading $button.prop('disabled', true); $spinner.css('visibility', 'visible'); // Send AJAX request $.ajax({ url: wpSeoProStack.ajaxurl, type: 'POST', data: { action: 'wp_seoprostack_generate_robots', nonce: wpSeoProStack.nonce }, success: function(response) { // Hide loading $button.prop('disabled', false); $spinner.css('visibility', 'hidden'); // Show response if (response.success) { $response.html('
' + response.data.message + '
'); // Display robots.txt content if (response.data.content) { $content.val(response.data.content); $content.closest('.seoprostack-setting-row').show(); } } else { $response.html('
' + response.data.message + '
'); } }, error: function() { // Hide loading $button.prop('disabled', false); $spinner.css('visibility', 'hidden'); // Show error $response.html('
Error connecting to server
'); } }); }); // Copy to Clipboard Functionality $('.seoprostack-copy-to-clipboard').on('click', function(e) { e.preventDefault(); var targetId = $(this).data('target'); var $target = $('#' + targetId); var $button = $(this); var originalText = $button.text(); // Copy to clipboard $target.select(); document.execCommand('copy'); // Update button text $button.text('Copied!'); // Reset button text setTimeout(function() { $button.text(originalText); }, 2000); }); } }); });