| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Plugin Memory Usage |
| 4 |
Plugin URI: https://nett.pro/en/plugins/ |
| 5 |
Description: Display different plugins memory usage and impact on Wordpress. |
| 6 |
Version: 1.1.0 |
| 7 |
Author: NETT.PRO |
| 8 |
Author URI: https://nett.pro/en/ |
| 9 |
License: GPLv3 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Prevent direct access to this file |
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
function wpmem_wp_memory_usage_admin_menu() { |
| 21 |
add_menu_page( |
| 22 |
'Plugin Memory Usage', |
| 23 |
'Memory Usage', |
| 24 |
'manage_options', |
| 25 |
'wp_plugin_memory_usage', |
| 26 |
'wpmem_wp_memory_usage_admin_page', |
| 27 |
'dashicons-performance', |
| 28 |
100 |
| 29 |
); |
| 30 |
} |
| 31 |
add_action('admin_menu', 'wpmem_wp_memory_usage_admin_menu'); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
// Function to register the dashboard widget |
| 37 |
function wpmem_wp_memory_usage_widget() { |
| 38 |
wp_add_dashboard_widget( |
| 39 |
'wp_memory_usage', |
| 40 |
'Plugin Memory Usage', |
| 41 |
'wpmem_display_memory_usage_dashboard' |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
// Function to display content in the dashboard widget |
| 46 |
function wpmem_display_memory_usage_dashboard() { |
| 47 |
// Get PHP version |
| 48 |
$php_version = phpversion(); |
| 49 |
|
| 50 |
// Get WordPress memory limit |
| 51 |
$wp_memory_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 52 |
|
| 53 |
// Get PHP memory limit |
| 54 |
$php_memory_limit = ini_get('memory_limit'); |
| 55 |
|
| 56 |
// Get current memory usage |
| 57 |
$current_memory_usage = memory_get_usage(true); |
| 58 |
|
| 59 |
// Calculate percentage used |
| 60 |
$percentage_used = ($current_memory_usage / $wp_memory_limit) * 100; |
| 61 |
|
| 62 |
// Display the data |
| 63 |
echo '<p><strong>PHP Version:</strong> ' . esc_html($php_version) . '</p>'; |
| 64 |
echo '<p><strong>WordPress Memory Limit:</strong> ' . esc_html(size_format($wp_memory_limit)) . '</p>'; |
| 65 |
echo '<p><strong>PHP Memory Limit:</strong> ' . esc_html($php_memory_limit) . '</p>'; |
| 66 |
|
| 67 |
// Display memory usage |
| 68 |
echo '<h4>Current Memory Usage:</h4>'; |
| 69 |
echo '<p>' . esc_html(size_format($current_memory_usage)) . ' of ' . esc_html(size_format($wp_memory_limit)) . '</p>'; |
| 70 |
echo '<div style="width: 100%; background: #e0e0e0; height: 24px; border-radius: 10px; overflow: hidden;"> |
| 71 |
<div style="width: ' . esc_html(round($percentage_used, 1)) . '%; background: #76c7c0; height: 100%; text-align: center; line-height: 24px;" class="memory-bar-fill">' . esc_html(round($percentage_used, 1)) . '%</div> |
| 72 |
</div>'; |
| 73 |
|
| 74 |
// Message to increase memory limit if usage is high |
| 75 |
if ($current_memory_usage > $wp_memory_limit * 0.8) { |
| 76 |
echo 'You are using more than 80% of allocated memory. Please go to Memory Control Panel to try to increase memory limit '; |
| 77 |
echo '<p id="memory-increase-result"></p>'; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
// Add button to Plugin Memory Control Panel |
| 83 |
echo '<p><a href="' . esc_html(admin_url('admin.php?page=wp_plugin_memory_usage')) . '" class="button">Plugin Memory Control Panel</a></p>'; |
| 84 |
} |
| 85 |
|
| 86 |
// Hook to add the widget to the dashboard |
| 87 |
add_action('wp_dashboard_setup', 'wpmem_wp_memory_usage_widget'); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
function wpmem_wp_memory_usage_admin_page() { |
| 97 |
?> |
| 98 |
<div class="wrap"> |
| 99 |
<h1>Plugin Memory Usage - Control Panel</h1> |
| 100 |
|
| 101 |
<div class="card-container"> |
| 102 |
<div class="card-row"> |
| 103 |
<div class="card card-system-info"> |
| 104 |
<h2>System Information</h2> |
| 105 |
<table class="widefat"> |
| 106 |
<tr> |
| 107 |
<th>PHP Version</th> |
| 108 |
<td><?php echo esc_html(phpversion()); ?></td> |
| 109 |
</tr> |
| 110 |
<tr> |
| 111 |
<th>WordPress Memory Limit</th> |
| 112 |
<td><?php echo esc_html(WP_MEMORY_LIMIT); ?></td> |
| 113 |
</tr> |
| 114 |
<tr> |
| 115 |
<th>PHP Memory Limit</th> |
| 116 |
<td><?php echo esc_html(ini_get('memory_limit')); ?></td> |
| 117 |
</tr> |
| 118 |
<tr> |
| 119 |
<th>WordPress Version</th> |
| 120 |
<td><?php echo esc_html(get_bloginfo('version')); ?></td> |
| 121 |
</tr> |
| 122 |
</table> |
| 123 |
</div> |
| 124 |
|
| 125 |
<div class="card card-memory-usage"> |
| 126 |
<h2>Current Memory Usage</h2> |
| 127 |
<?php |
| 128 |
$wp_memory_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 129 |
$current_memory_usage = memory_get_usage(true); |
| 130 |
$percentage_used = ($current_memory_usage / $wp_memory_limit) * 100; |
| 131 |
?> |
| 132 |
<p>Using <span id="current-memory"><?php echo esc_html(size_format($current_memory_usage)); ?></span> of <?php echo esc_html(size_format($wp_memory_limit)); ?></p> |
| 133 |
<div class="memory-bar"> |
| 134 |
<div id="memory-bar-fill" class="memory-bar-fill" style="width: <?php echo esc_html(round($percentage_used, 1)); ?>%;"> |
| 135 |
<span id="memory-percentage"><?php echo esc_html(round($percentage_used, 1)); ?>%</span> |
| 136 |
</div> |
| 137 |
</div> |
| 138 |
|
| 139 |
<br><button id="refresh-memory" class="button">Refresh Memory Usage</button> |
| 140 |
<br><br>Memory fluctuate. Please press button once after entering panel. |
| 141 |
|
| 142 |
<?php |
| 143 |
if ($current_memory_usage > $wp_memory_limit * 0.8) { |
| 144 |
echo '<p style="color:red;">You are using more than 80% of allocated memory. Please click button below to try to increase memory limit.</p> '; |
| 145 |
echo '<p><button id="increase-memory-limit" class="button">Increase Memory Limit</button></p>'; |
| 146 |
echo '<p id="memory-increase-result"></p>'; |
| 147 |
} ?> |
| 148 |
</div> |
| 149 |
|
| 150 |
<div class="card card-memory-history"> |
| 151 |
<h2>Memory Usage History</h2> |
| 152 |
|
| 153 |
<ul id="memory-history-list"> |
| 154 |
|
| 155 |
|
| 156 |
<hr> |
| 157 |
<p>This plugin measures changes in WordPress memory usage as you activate and deactivate plugins. Here's how it works:</p> |
| 158 |
<ol> |
| 159 |
<li>It records the current memory usage of WordPress.</li> |
| 160 |
<li>When you activate or deactivate a plugin, it measures the memory usage again.</li> |
| 161 |
<li>The difference between these measurements gives an estimate of the plugin's memory impact.</li> |
| 162 |
<li>This process is repeated each time you toggle a plugin.</li> |
| 163 |
</ol> |
| 164 |
<p>Note: These measurements are estimates and may vary. Factors like caching, other active plugins, and WordPress itself can influence memory usage. For the most accurate results, consider testing in a controlled environment.</p> |
| 165 |
<p>To begin, try activating or deactivating plugins using the buttons provided. The memory usage history will appear here.</p>| |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
</ul> |
| 170 |
</div> |
| 171 |
</div> |
| 172 |
|
| 173 |
<div class="card card-plugins"> |
| 174 |
<h2>Plugins</h2> |
| 175 |
<?php |
| 176 |
$all_plugins = get_plugins(); |
| 177 |
$active_plugins = get_option('active_plugins'); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
echo '<ul class="plugin-list">'; |
| 184 |
foreach ($all_plugins as $plugin_path => $plugin_data) { |
| 185 |
$is_active = in_array($plugin_path, $active_plugins); |
| 186 |
$plugin_id = sanitize_title($plugin_data['Name']); |
| 187 |
$is_memory_usage_plugin = (strpos($plugin_path, 'plugin-memory-usage') !== false); |
| 188 |
$li_class = $is_active ? 'plugin-active' : 'plugin-inactive'; |
| 189 |
if ($is_memory_usage_plugin) { |
| 190 |
$li_class .= ' memory-usage-plugin'; |
| 191 |
} |
| 192 |
$avg_memory = wpmem_get_average_memory_usage($plugin_path); |
| 193 |
|
| 194 |
echo '<li class="' . esc_attr($li_class) . '">'; |
| 195 |
echo '<div class="plugin-info">'; |
| 196 |
echo '<span class="plugin-name">' . esc_html($plugin_data['Name']) . ' <span class="avg-memory" title="The average memory usage represents the typical amount of memory consumed by this plugin over time.">(' . esc_html($avg_memory) . ' MB)</span></span>'; |
| 197 |
echo '<div class="plugin-memory-bar" title="Absolute value of last memory change"><div class="plugin-memory-fill"><span class="plugin-memory-text"></span></div></div>'; |
| 198 |
echo '</div>'; |
| 199 |
echo '<div class="plugin-actions">'; |
| 200 |
if ($is_memory_usage_plugin) { |
| 201 |
echo '<button class="button" disabled>Active</button>'; |
| 202 |
} else { |
| 203 |
echo '<button class="button toggle-plugin" data-plugin="' . esc_attr($plugin_path) . '" data-action="' . ($is_active ? 'deactivate' : 'activate') . '">'; |
| 204 |
echo $is_active ? 'Deactivate' : 'Activate'; |
| 205 |
echo '</button>'; |
| 206 |
} |
| 207 |
echo '</div>'; |
| 208 |
echo '</li>'; |
| 209 |
} |
| 210 |
|
| 211 |
echo '</ul>'; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
?> |
| 218 |
</div> |
| 219 |
</div> |
| 220 |
</div> |
| 221 |
|
| 222 |
|
| 223 |
<?php |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
function wpmem_toggle_plugin() { |
| 231 |
check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 232 |
if (!current_user_can('activate_plugins')) { |
| 233 |
wp_send_json_error('Insufficient permissions'); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
$plugin = isset($_POST['plugin']) ? sanitize_text_field(wp_unslash($_POST['plugin'])) : ''; |
| 238 |
$action = isset($_POST['toggle_action']) ? sanitize_text_field(wp_unslash($_POST['toggle_action'])) : ''; |
| 239 |
|
| 240 |
if (empty($plugin) || empty($action)) { |
| 241 |
wp_send_json_error('Invalid plugin or action'); |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if ($action === 'activate') { |
| 248 |
$result = activate_plugin($plugin); |
| 249 |
} else { |
| 250 |
$result = deactivate_plugins($plugin); |
| 251 |
} |
| 252 |
|
| 253 |
if (is_wp_error($result)) { |
| 254 |
wp_send_json_error($result->get_error_message()); |
| 255 |
} else { |
| 256 |
wp_send_json_success(); |
| 257 |
} |
| 258 |
} |
| 259 |
add_action('wp_ajax_toggle_plugin', 'wpmem_toggle_plugin'); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
function wpmem_table_exists($table_name) { |
| 267 |
global $wpdb; |
| 268 |
$full_table_name = $wpdb->prefix . $table_name; |
| 269 |
|
| 270 |
// Create a unique cache key |
| 271 |
$cache_key = 'wpmem_table_exists_' . md5($full_table_name); |
| 272 |
|
| 273 |
// Try to get the result from cache |
| 274 |
$table_exists = wp_cache_get($cache_key); |
| 275 |
|
| 276 |
if (false === $table_exists) { |
| 277 |
// Cache miss, perform the database query |
| 278 |
// There isn't a reliable way to check if a table exists in WordPress without making a direct database call |
| 279 |
$result = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 280 |
$wpdb->prepare( |
| 281 |
"SHOW TABLES LIKE %s", |
| 282 |
$full_table_name |
| 283 |
) |
| 284 |
); |
| 285 |
|
| 286 |
$table_exists = ($result === $full_table_name); |
| 287 |
|
| 288 |
// Cache the result for future use (cache for 1 hour) |
| 289 |
wp_cache_set($cache_key, $table_exists, '', 3600); |
| 290 |
} |
| 291 |
|
| 292 |
return $table_exists; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
function wpmem_refresh_memory_usage() { |
| 300 |
check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 301 |
|
| 302 |
global $wpdb; |
| 303 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 304 |
|
| 305 |
// Check if the table exists |
| 306 |
if (!wpmem_table_exists('wpmem_plugin_history')) { |
| 307 |
wpmem_create_history_table(); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
$current_memory = memory_get_usage(true); |
| 312 |
$formatted_memory = size_format($current_memory, 2); |
| 313 |
$percentage = round(($current_memory / wp_convert_hr_to_bytes(WP_MEMORY_LIMIT)) * 100, 2); |
| 314 |
|
| 315 |
// Save the measurement if a plugin was toggled |
| 316 |
if (isset($_POST['plugin']) && isset($_POST['toggle_action'])) { |
| 317 |
$plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 318 |
$previous_memory = isset($_POST['previous_memory']) ? intval($_POST['previous_memory']) : 0; |
| 319 |
$memory_change = $current_memory - $previous_memory; |
| 320 |
wpmem_save_plugin_measurement($plugin, $memory_change); |
| 321 |
} |
| 322 |
|
| 323 |
wp_send_json_success(array( |
| 324 |
'current_memory' => $formatted_memory, |
| 325 |
'current_memory_bytes' => $current_memory, |
| 326 |
'percentage' => $percentage, |
| 327 |
)); |
| 328 |
} |
| 329 |
add_action('wp_ajax_refresh_memory_usage', 'wpmem_refresh_memory_usage'); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
function wp_memory_usage_enqueue_styles($hook) { |
| 335 |
// Only load on our plugin's admin page |
| 336 |
if ($hook != 'toplevel_page_wp_plugin_memory_usage') { |
| 337 |
return; |
| 338 |
} |
| 339 |
wp_enqueue_style('wp-memory-usage-styles', plugins_url('plugin-memory-usage.css', __FILE__)); |
| 340 |
} |
| 341 |
add_action('admin_enqueue_scripts', 'wp_memory_usage_enqueue_styles'); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
function wpmem_enqueue_scripts($hook) { |
| 348 |
if ($hook != 'toplevel_page_wp_plugin_memory_usage') { |
| 349 |
return; |
| 350 |
} |
| 351 |
wp_enqueue_script('wpmem-script', plugins_url('plugin-memory-usage.js', __FILE__), array(), '1.0', true); |
| 352 |
$nonce = wp_create_nonce('wp_memory_usage_nonce'); |
| 353 |
wp_localize_script('wpmem-script', 'wpmemData', array( |
| 354 |
'wpmem_ajaxurl' => admin_url('admin-ajax.php'), |
| 355 |
'nonce' => $nonce, |
| 356 |
'initialMemoryUsage' => memory_get_usage(true), |
| 357 |
'pluginHistory' => wpmem_get_all_plugin_history() |
| 358 |
)); |
| 359 |
} |
| 360 |
add_action('admin_enqueue_scripts', 'wpmem_enqueue_scripts'); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
function wpmem_create_history_table() { |
| 367 |
global $wpdb; |
| 368 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 369 |
$charset_collate = $wpdb->get_charset_collate(); |
| 370 |
|
| 371 |
$sql = "CREATE TABLE $table_name ( |
| 372 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 373 |
plugin_path varchar(255) NOT NULL, |
| 374 |
memory_change bigint(20) NOT NULL, |
| 375 |
zero_count int(11) NOT NULL DEFAULT 0, |
| 376 |
timestamp datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 377 |
PRIMARY KEY (id) |
| 378 |
) $charset_collate;"; |
| 379 |
|
| 380 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 381 |
$result = dbDelta($sql); |
| 382 |
|
| 383 |
if (!empty($wpdb->last_error)) { |
| 384 |
return false; |
| 385 |
} |
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
register_activation_hook(__FILE__, 'wpmem_create_history_table'); |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
function wpmem_save_plugin_measurement($plugin_path, $memory_change) { |
| 395 |
global $wpdb; |
| 396 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 397 |
|
| 398 |
// Convert memory_change to its absolute value |
| 399 |
$memory_change = abs($memory_change); |
| 400 |
|
| 401 |
if ($memory_change == 0) { |
| 402 |
// Increment zero count for the most recent entry |
| 403 |
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 404 |
$wpdb->prepare( |
| 405 |
"UPDATE `" . esc_sql($table_name) . "` SET zero_count = zero_count + 1 |
| 406 |
WHERE plugin_path = %s |
| 407 |
ORDER BY timestamp DESC |
| 408 |
LIMIT 1", |
| 409 |
$plugin_path |
| 410 |
) |
| 411 |
); |
| 412 |
} else { |
| 413 |
// Insert new non-zero measurement |
| 414 |
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 415 |
$table_name, |
| 416 |
array( |
| 417 |
'plugin_path' => $plugin_path, |
| 418 |
'memory_change' => $memory_change, |
| 419 |
'zero_count' => 0, |
| 420 |
) |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
// Clear caches |
| 425 |
wpmem_clear_plugin_history_cache($plugin_path); |
| 426 |
wpmem_clear_all_plugin_history_cache(); |
| 427 |
wpmem_clear_average_memory_cache($plugin_path); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
function wpmem_get_plugin_history($plugin_path) { |
| 433 |
global $wpdb; |
| 434 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 435 |
|
| 436 |
// Create a unique cache key for this query |
| 437 |
$cache_key = 'wpmem_plugin_history_' . md5($plugin_path); |
| 438 |
|
| 439 |
// Try to get the results from cache |
| 440 |
$results = wp_cache_get($cache_key); |
| 441 |
|
| 442 |
// If the results are not in cache, query the database |
| 443 |
if (false === $results) { |
| 444 |
$results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 445 |
$wpdb->prepare( |
| 446 |
"SELECT memory_change, timestamp FROM `" . esc_sql($table_name) . "` WHERE plugin_path = %s ORDER BY timestamp DESC LIMIT 5", |
| 447 |
$plugin_path |
| 448 |
) |
| 449 |
); |
| 450 |
|
| 451 |
// Cache the results for future use |
| 452 |
wp_cache_set($cache_key, $results, '', 3600); // Cache for 1 hour |
| 453 |
} |
| 454 |
|
| 455 |
return $results; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
function wpmem_clear_plugin_history_cache($plugin_path) { |
| 461 |
$cache_key = 'wpmem_plugin_history_' . md5($plugin_path); |
| 462 |
wp_cache_delete($cache_key); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
add_action('wp_ajax_get_plugin_history', 'wpmem_get_plugin_history_ajax'); |
| 468 |
function wpmem_get_plugin_history_ajax() { |
| 469 |
check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 470 |
if (!isset($_POST['plugin'])) { |
| 471 |
wp_send_json_error('Plugin not specified'); |
| 472 |
} |
| 473 |
|
| 474 |
$plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 475 |
$history = wpmem_get_plugin_history($plugin); |
| 476 |
|
| 477 |
wp_send_json_success($history); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
function wpmem_get_all_plugin_history() { |
| 488 |
global $wpdb; |
| 489 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 490 |
|
| 491 |
// Create a unique cache key for this query |
| 492 |
$cache_key = 'wpmem_all_plugin_history'; |
| 493 |
|
| 494 |
// Try to get the results from cache |
| 495 |
$results = wp_cache_get($cache_key); |
| 496 |
|
| 497 |
// If the results are not in cache, query the database |
| 498 |
if (false === $results) { |
| 499 |
$results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 500 |
$wpdb->prepare( |
| 501 |
"SELECT plugin_path, memory_change, timestamp |
| 502 |
FROM `" . esc_sql($table_name) . "` |
| 503 |
WHERE plugin_path IN (SELECT DISTINCT plugin_path FROM `" . esc_sql($table_name) . "`) |
| 504 |
ORDER BY timestamp DESC |
| 505 |
-- %d", |
| 506 |
1 |
| 507 |
) |
| 508 |
); |
| 509 |
|
| 510 |
// Cache the results for future use |
| 511 |
wp_cache_set($cache_key, $results, '', 3600); // Cache for 1 hour |
| 512 |
} |
| 513 |
|
| 514 |
// Process results... |
| 515 |
$history = array(); |
| 516 |
foreach ($results as $row) { |
| 517 |
if (!isset($history[$row->plugin_path])) { |
| 518 |
$history[$row->plugin_path] = array(); |
| 519 |
} |
| 520 |
if (count($history[$row->plugin_path]) < 5) { |
| 521 |
$history[$row->plugin_path][] = array( |
| 522 |
'memory_change' => $row->memory_change, |
| 523 |
'timestamp' => $row->timestamp |
| 524 |
); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
return $history; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
function wpmem_clear_all_plugin_history_cache() { |
| 535 |
wp_cache_delete('wpmem_all_plugin_history'); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
function wpmem_clear_table_exists_cache() { |
| 541 |
global $wpdb; |
| 542 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 543 |
$cache_key = 'wpmem_table_exists_' . $table_name; |
| 544 |
wp_cache_delete($cache_key); |
| 545 |
} |
| 546 |
|
| 547 |
register_activation_hook(__FILE__, 'wpmem_clear_table_exists_cache'); |
| 548 |
register_deactivation_hook(__FILE__, 'wpmem_clear_table_exists_cache'); |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
function wpmem_get_average_memory_usage($plugin_path) { |
| 557 |
global $wpdb; |
| 558 |
$table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 559 |
|
| 560 |
// Create a unique cache key |
| 561 |
$cache_key = 'wpmem_avg_memory_' . md5($plugin_path); |
| 562 |
|
| 563 |
// Try to get the result from cache |
| 564 |
$avg_memory = wp_cache_get($cache_key); |
| 565 |
|
| 566 |
if (false === $avg_memory) { |
| 567 |
// Cache miss, perform the database query |
| 568 |
$result = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 569 |
$wpdb->prepare( |
| 570 |
"SELECT SUM(memory_change) as total_change, |
| 571 |
COUNT(*) as non_zero_count, |
| 572 |
SUM(zero_count) as zero_count |
| 573 |
FROM `" . esc_sql($table_name) . "` |
| 574 |
WHERE plugin_path = %s", |
| 575 |
$plugin_path |
| 576 |
) |
| 577 |
); |
| 578 |
|
| 579 |
if ($result) { |
| 580 |
$total_count = $result->non_zero_count + $result->zero_count; |
| 581 |
$avg_memory = $total_count > 0 ? ($result->total_change / $total_count) / (1024 * 1024) : 0; |
| 582 |
$avg_memory = round($avg_memory, 2); // Round to 2 decimal places |
| 583 |
} else { |
| 584 |
$avg_memory = 0; |
| 585 |
} |
| 586 |
|
| 587 |
// Cache the result for future use (cache for 5 minutes) |
| 588 |
wp_cache_set($cache_key, $avg_memory, '', 300); |
| 589 |
} |
| 590 |
|
| 591 |
return $avg_memory; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
function wpmem_clear_average_memory_cache($plugin_path) { |
| 597 |
$cache_key = 'wpmem_avg_memory_' . md5($plugin_path); |
| 598 |
wp_cache_delete($cache_key); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
add_action('wp_ajax_update_average_memory', 'wpmem_update_average_memory'); |
| 604 |
add_action('wp_ajax_nopriv_update_average_memory', 'wpmem_update_average_memory'); |
| 605 |
|
| 606 |
function wpmem_update_average_memory() { |
| 607 |
// Check nonce for security |
| 608 |
if (!isset($_POST['nonce'])) { |
| 609 |
wp_send_json_error('Security token not provided'); |
| 610 |
} |
| 611 |
|
| 612 |
$nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); |
| 613 |
if (!wp_verify_nonce($nonce, 'wp_memory_usage_nonce')) { |
| 614 |
wp_send_json_error('Invalid security token'); |
| 615 |
} |
| 616 |
|
| 617 |
if (!isset($_POST['plugin_path'])) { |
| 618 |
wp_send_json_error('Plugin path not provided'); |
| 619 |
} |
| 620 |
|
| 621 |
$plugin_path = sanitize_text_field(wp_unslash($_POST['plugin_path'])); |
| 622 |
$avg_memory = wpmem_get_average_memory_usage($plugin_path); |
| 623 |
|
| 624 |
wp_send_json_success(array('avg_memory' => $avg_memory)); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
function wpmem_add_increase_memory_button($content) { |
| 633 |
$wp_memory_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 634 |
$current_memory_usage = memory_get_usage(true); |
| 635 |
|
| 636 |
if ($current_memory_usage > $wp_memory_limit) { |
| 637 |
$content .= '<button id="increase-memory-limit" class="button">Increase Memory Limit</button>'; |
| 638 |
$content .= '<span id="memory-increase-result"></span>'; |
| 639 |
} |
| 640 |
|
| 641 |
return $content; |
| 642 |
} |
| 643 |
add_filter('wpmem_memory_usage_content', 'wpmem_add_increase_memory_button'); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
function wpmem_increase_memory_limit() { |
| 650 |
check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 651 |
|
| 652 |
if (!current_user_can('manage_options')) { |
| 653 |
wp_send_json_error(array('message' => 'Insufficient permissions')); |
| 654 |
} |
| 655 |
|
| 656 |
$current_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 657 |
$new_limit = $current_limit * 2; // Double the current limit |
| 658 |
|
| 659 |
// Attempt to increase the limit |
| 660 |
if (wpmem_set_memory_limit($new_limit)) { |
| 661 |
wp_send_json_success(array('new_limit' => size_format($new_limit))); |
| 662 |
} else { |
| 663 |
wp_send_json_error(array('message' => 'Unable to increase memory limit. You may need to contact your hosting provider.')); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
add_action('wp_ajax_increase_memory_limit', 'wpmem_increase_memory_limit'); |
| 668 |
|
| 669 |
function wpmem_set_memory_limit($new_limit) { |
| 670 |
global $wp_filesystem; |
| 671 |
|
| 672 |
// Initialize the WP filesystem |
| 673 |
if (empty($wp_filesystem)) { |
| 674 |
require_once (ABSPATH . '/wp-admin/includes/file.php'); |
| 675 |
WP_Filesystem(); |
| 676 |
} |
| 677 |
|
| 678 |
$wp_config_file = ABSPATH . 'wp-config.php'; |
| 679 |
$config_content = $wp_filesystem->get_contents($wp_config_file); |
| 680 |
|
| 681 |
if ($config_content === false) { |
| 682 |
return false; |
| 683 |
} |
| 684 |
|
| 685 |
$new_limit_formatted = size_format($new_limit); |
| 686 |
|
| 687 |
if (preg_match("/define\(\s*'WP_MEMORY_LIMIT',\s*'.*?'\s*\);/", $config_content)) { |
| 688 |
// WP_MEMORY_LIMIT is already defined, so update it |
| 689 |
$new_content = preg_replace( |
| 690 |
"/define\(\s*'WP_MEMORY_LIMIT',\s*'.*?'\s*\);/", |
| 691 |
"define('WP_MEMORY_LIMIT', '$new_limit_formatted');", |
| 692 |
$config_content |
| 693 |
); |
| 694 |
} else { |
| 695 |
// WP_MEMORY_LIMIT is not defined, so add it |
| 696 |
$new_content = preg_replace( |
| 697 |
"/<\?php/", |
| 698 |
"<?php\ndefine('WP_MEMORY_LIMIT', '$new_limit_formatted');", |
| 699 |
$config_content, |
| 700 |
1 |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
if ($wp_filesystem->put_contents($wp_config_file, $new_content) === false) { |
| 705 |
return false; |
| 706 |
} |
| 707 |
|
| 708 |
return true; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
function wpmem_add_settings_link($links) { |
| 717 |
$settings_link = '<a href="' . admin_url('admin.php?page=wp_plugin_memory_usage') . '">Settings</a>'; |
| 718 |
array_unshift($links, $settings_link); |
| 719 |
return $links; |
| 720 |
} |
| 721 |
|
| 722 |
$plugin = plugin_basename(__FILE__); |
| 723 |
add_filter("plugin_action_links_$plugin", 'wpmem_add_settings_link'); |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
?> |