var wpmem_ajaxurl = wpmemData.wpmem_ajaxurl; var wpmemNonce = wpmemData.nonce; var lastMemoryUsage = wpmemData.initialMemoryUsage; console.log('inside js '); var pluginMemoryUsage = {}; document.addEventListener('DOMContentLoaded', function() { var toggleButtons = document.querySelectorAll('.toggle-plugin'); toggleButtons.forEach(function(button) { button.addEventListener('click', function() { var plugin = this.dataset.plugin; var action = this.dataset.action; wpmem_togglePlugin(plugin, action, this); }); }); document.getElementById('refresh-memory').addEventListener('click', function() { wpmem_refreshMemoryUsage(null, null); }); wpmem_displayAllPluginHistory(); }); function wpmem_togglePlugin(plugin, action, button) { var xhr = new XMLHttpRequest(); xhr.open('POST', wpmem_ajaxurl, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { console.log('Raw AJAX response:', xhr.responseText); if (xhr.status === 200) { try { var response = JSON.parse(xhr.responseText); if (response.success) { button.textContent = action === 'activate' ? 'Deactivate' : 'Activate'; button.dataset.action = action === 'activate' ? 'deactivate' : 'activate'; button.closest('li').classList.toggle('plugin-active'); button.closest('li').classList.toggle('plugin-inactive'); wpmem_refreshMemoryUsage(plugin, action); wpmem_updateAverageMemory(plugin); } else { console.error('Error toggling plugin:', response.data); alert('Error toggling plugin: ' + response.data); } } catch (e) { console.error('Error parsing JSON:', e); console.log('Response that caused the error:', xhr.responseText); alert('Error parsing server response'); } } else { console.error('AJAX request failed with status:', xhr.status); console.log('Response text:', xhr.responseText); alert('AJAX request failed'); } } }; xhr.send('action=toggle_plugin&plugin=' + encodeURIComponent(plugin) + '&toggle_action=' + action + '&nonce=' + wpmemNonce); } function wpmem_updateMemoryHistory(plugin, action, memoryDifference) { var historyList = document.getElementById('memory-history-list'); var listItem = document.createElement('li'); listItem.textContent = wpmem_createMemoryHistoryEntry(plugin, action, memoryDifference); historyList.insertBefore(listItem, historyList.firstChild); } function wpmem_updatePluginMemoryUsage(plugin, memoryDifference) { var absDifference = Math.abs(memoryDifference); pluginMemoryUsage[plugin] = (pluginMemoryUsage[plugin] || 0) + absDifference; var pluginItem = document.querySelector('.plugin-list li .toggle-plugin[data-plugin="' + plugin + '"]').closest('li'); var memoryBar = pluginItem.querySelector('.plugin-memory-fill'); var memoryText = pluginItem.querySelector('.plugin-memory-text'); var avgMemorySpan = pluginItem.querySelector('.avg-memory'); var percentage = (absDifference / lastMemoryUsage) * 100; percentage = Math.max(0, Math.min(100, percentage)); // Ensure percentage is between 0 and 100 memoryBar.style.width = percentage + '%'; memoryText.textContent = wpmem_formatMemoryDifference(absDifference); // Update average memory var avgMemory = pluginMemoryUsage[plugin] / (1024 * 1024); // Convert to MB avgMemorySpan.textContent = '(' + avgMemory.toFixed(2) + ' MB)'; } function wpmem_createMemoryHistoryEntry(pluginName, action, memoryDifference) { var timestamp = new Date().toLocaleString(); if (action === 'increase_memory') { return `${timestamp} - Memory limit increased to ${memoryDifference}`; } else if (action === 'increase_memory_failed') { return `${timestamp} - Failed to increase memory limit: ${memoryDifference}`; } else if (action === 'increase_memory_error') { return `${timestamp} - Error while trying to increase memory limit: ${memoryDifference}`; } else { return `${timestamp} - ${pluginName} (${action}): ${wpmem_formatMemoryDifference(memoryDifference)}`; } } function wpmem_formatMemoryDifference(diffBytes) { var absValue = Math.abs(diffBytes); var sign = diffBytes >= 0 ? '+' : '-'; var mbValue = absValue / (1024 * 1024); // Convert to MB return `${sign}${mbValue.toFixed(2)} MB`; } function wpmem_refreshMemoryUsage(plugin, action) { var refreshButton = document.getElementById('refresh-memory'); refreshButton.textContent = 'Refreshing...'; refreshButton.disabled = true; var xhr = new XMLHttpRequest(); xhr.open('POST', wpmem_ajaxurl, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { console.log('Raw AJAX response (refresh):', xhr.responseText); if (xhr.status === 200) { try { var response = JSON.parse(xhr.responseText); if (response.success) { refreshButton.textContent = 'Refresh Memory Usage'; refreshButton.disabled = false; document.getElementById('current-memory').textContent = response.data.current_memory; document.getElementById('memory-bar-fill').style.width = response.data.percentage + '%'; document.getElementById('memory-percentage').textContent = response.data.percentage + '%'; var memoryDifference = response.data.current_memory_bytes - lastMemoryUsage; lastMemoryUsage = response.data.current_memory_bytes; if (plugin && action) { wpmem_updateMemoryHistory(plugin, action, memoryDifference); wpmem_updatePluginMemoryUsage(plugin, memoryDifference); wpmem_fetchAndDisplayPluginHistory(plugin); wpmem_updateAverageMemory(plugin); } } else { console.error('Error refreshing memory usage:', response.data); } } catch (e) { console.error('Error parsing JSON:', e); console.log('Response that caused the error:', xhr.responseText); } } else { console.error('AJAX request failed with status:', xhr.status); console.log('Response text:', xhr.responseText); } refreshButton.textContent = 'Refresh Memory Usage'; refreshButton.disabled = false; } }; var data = 'action=refresh_memory_usage&nonce=' + wpmemNonce; if (plugin && action) { data += '&plugin=' + encodeURIComponent(plugin) + '&toggle_action=' + action + '&previous_memory=' + lastMemoryUsage; } xhr.send(data); } function wpmem_fetchAndDisplayPluginHistory(plugin) { var xhr = new XMLHttpRequest(); xhr.open('POST', wpmem_ajaxurl, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { var response = JSON.parse(xhr.responseText); if (response.success) { wpmem_displayPluginHistory(plugin, response.data); } else { console.error('Error fetching plugin history:', response.data); } } }; xhr.send('action=get_plugin_history&plugin=' + encodeURIComponent(plugin) + '&nonce=' + wpmemNonce); } function wpmem_displayPluginHistory(plugin, history) { var pluginItem = document.querySelector('.plugin-list li .toggle-plugin[data-plugin="' + plugin + '"]'); if (!pluginItem) return; var historyContainer = pluginItem.closest('li').querySelector('.plugin-history-container'); if (!historyContainer) { historyContainer = document.createElement('div'); historyContainer.className = 'plugin-history-container'; pluginItem.closest('li').appendChild(historyContainer); } historyContainer.innerHTML = ''; history.slice(0, 3).forEach(function(entry, index) { var historyBar = document.createElement('div'); historyBar.className = 'plugin-history-bar'; var percentage = (Math.abs(entry.memory_change) / lastMemoryUsage) * 100; percentage = Math.max(0, Math.min(100, percentage)); historyBar.style.width = percentage + '%'; historyBar.style.backgroundColor = wpmem_getHistoryColor(index); historyBar.title = wpmem_formatMemoryDifference(entry.memory_change) + ' on ' + entry.timestamp; historyContainer.appendChild(historyBar); }); // Ensure the history container is visible historyContainer.style.display = 'block'; } function wpmem_getHistoryColor(index) { var colors = ['#4CAF50', '#8BC34A', '#CDDC39']; return colors[index] || colors[colors.length - 1]; } function wpmem_displayAllPluginHistory() { var pluginHistory = wpmemData.pluginHistory; for (var plugin in pluginHistory) { if (pluginHistory.hasOwnProperty(plugin)) { wpmem_displayPluginHistory(plugin, pluginHistory[plugin]); } } } function wpmem_updateAverageMemory(plugin) { jQuery.ajax({ url: wpmem_ajaxurl, type: 'POST', data: { action: 'update_average_memory', plugin_path: plugin, nonce: wpmemNonce }, success: function(response) { if (response.success) { var avgMemorySpan = jQuery('.plugin-list li .toggle-plugin[data-plugin="' + plugin + '"]') .closest('li') .find('.avg-memory'); avgMemorySpan.text('(' + response.data.avg_memory + ' MB)'); } else { console.error('Error updating average memory:', response.data); } }, error: function(xhr, status, error) { console.error('AJAX request failed:', status, error); } }); } jQuery(document).ready(function($) { $('#increase-memory-limit').on('click', function() { $.ajax({ url: wpmemData.wpmem_ajaxurl, type: 'POST', data: { action: 'increase_memory_limit', nonce: wpmemData.nonce }, success: function(response) { if (response.success) { $('#memory-increase-result').text('Memory limit increased to ' + response.data.new_limit); // Add to memory history wpmem_updateMemoryHistory('System', 'increase_memory', response.data.new_limit); // Refresh the memory usage display // Refresh the page after a short delay setTimeout(function() { location.reload(); }, 2500); } else { $('#memory-increase-result').text('Failed to increase memory limit: ' + response.data.message); // Add failure to memory history wpmem_updateMemoryHistory('System', 'increase_memory_failed', response.data.message); } }, error: function(xhr, status, error) { console.error('AJAX request failed:', status, error); // Add error to memory history wpmem_updateMemoryHistory('System', 'increase_memory_error', 'AJAX request failed'); } }); }); });