| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /* |
| 3 | 3 | Plugin Name: Plugin Memory Usage |
| 4 | 4 | Plugin URI: https://nett.pro/en/plugins/ |
| 5 | 5 | Description: Display different plugins memory usage and impact on Wordpress. |
| 6 | -Version: 1.3.0 | |
| 6 | +Version: 1.2.3 | |
| 7 | 7 | Author: NETT.PRO |
| 8 | 8 | Author URI: https://nett.pro/en/ |
| 9 | 9 | License: GPLv3 |
| 10 | 10 | */ |
| @@ -13,39 +13,39 @@ | ||
| 13 | 13 | if (!defined('ABSPATH')) { |
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | -define('PLUGINMEMORYUSAGE_VERSION', '1.3.0'); | |
| 17 | +define('PLUGINMEMORYUSAGE_VERSION', '1.2.3'); | |
| 18 | 18 | |
| 19 | 19 | |
| 20 | -function pmusage_memory_usage_admin_menu() { | |
| 20 | +function wpmem_memory_usage_admin_menu() { | |
| 21 | 21 | add_menu_page( |
| 22 | 22 | 'Plugin Memory Usage', |
| 23 | 23 | 'Memory Usage', |
| 24 | 24 | 'manage_options', |
| 25 | 25 | 'wp_plugin_memory_usage', |
| 26 | - 'pmusage_memory_usage_admin_page', | |
| 26 | + 'wpmem_memory_usage_admin_page', | |
| 27 | 27 | 'dashicons-performance', |
| 28 | 28 | 100 |
| 29 | 29 | ); |
| 30 | 30 | } |
| 31 | -add_action('admin_menu', 'pmusage_memory_usage_admin_menu'); | |
| 31 | +add_action('admin_menu', 'wpmem_memory_usage_admin_menu'); | |
| 32 | 32 | |
| 33 | 33 | |
| 34 | 34 | |
| 35 | 35 | |
| 36 | 36 | // Function to register the dashboard widget |
| 37 | -function pmusage_memory_usage_widget() { | |
| 37 | +function pluginmemoryusage_memory_usage_widget() { | |
| 38 | 38 | |
| 39 | 39 | wp_add_dashboard_widget( |
| 40 | 40 | 'wp_memory_usage', |
| 41 | 41 | 'Plugin Memory Usage <span class="wpmem-version">'.PLUGINMEMORYUSAGE_VERSION.'</span>', |
| 42 | - 'pmusage_display_memory_usage_dashboard' | |
| 42 | + 'pluginmemoryusage_display_memory_usage_dashboard' | |
| 43 | 43 | ); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // Hook to add the widget to the dashboard |
| 47 | -add_action('wp_dashboard_setup', 'pmusage_memory_usage_widget', 1); | |
| 47 | +add_action('wp_dashboard_setup', 'pluginmemoryusage_memory_usage_widget', 1); | |
| 48 | 48 | |
| 49 | 49 | |
| 50 | 50 | |
| 51 | 51 | |
| @@ -52,10 +52,10 @@ | ||
| 52 | 52 | |
| 53 | 53 | |
| 54 | 54 | |
| 55 | 55 | // Get supported PHP versions with caching |
| 56 | -function pmusage_get_supported_php_versions() { | |
| 57 | - $supported_versions = get_transient('pmusage_supported_php_versions'); | |
| 56 | +function wpmem_get_supported_php_versions() { | |
| 57 | + $supported_versions = get_transient('wpmem_supported_php_versions'); | |
| 58 | 58 | |
| 59 | 59 | if (false === $supported_versions) { |
| 60 | 60 | $response = wp_remote_get('https://www.php.net/supported-versions.php', array( |
| 61 | 61 | 'sslverify' => false, |
| @@ -63,10 +63,10 @@ | ||
| 63 | 63 | )); |
| 64 | 64 | |
| 65 | 65 | if (!is_wp_error($response) && 200 === wp_remote_retrieve_response_code($response)) { |
| 66 | 66 | $html = wp_remote_retrieve_body($response); |
| 67 | - $supported_versions = pmusage_parse_supported_versions($html); | |
| 68 | - set_transient('pmusage_supported_php_versions', $supported_versions, WEEK_IN_SECONDS); | |
| 67 | + $supported_versions = wpmem_parse_supported_versions($html); | |
| 68 | + set_transient('wpmem_supported_php_versions', $supported_versions, WEEK_IN_SECONDS); | |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | return is_array($supported_versions) ? $supported_versions : []; |
| @@ -71,12 +71,10 @@ | ||
| 71 | 71 | |
| 72 | 72 | return is_array($supported_versions) ? $supported_versions : []; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - | |
| 76 | - | |
| 77 | 75 | // Parse HTML response from PHP.net |
| 78 | -function pmusage_parse_supported_versions($html) { | |
| 76 | +function wpmem_parse_supported_versions($html) { | |
| 79 | 77 | $versions = []; |
| 80 | 78 | $dom = new DOMDocument(); |
| 81 | 79 | @$dom->loadHTML($html); |
| 82 | 80 | $tables = $dom->getElementsByTagName('table'); |
| @@ -106,82 +104,20 @@ | ||
| 106 | 104 | |
| 107 | 105 | |
| 108 | 106 | |
| 109 | 107 | |
| 110 | -function pmusage_get_php_status($current_version) { | |
| 111 | - $supported_versions = pmusage_get_supported_php_versions(); | |
| 108 | +function wpmem_get_php_status($current_version) { | |
| 109 | + $supported_versions = wpmem_get_supported_php_versions(); | |
| 112 | 110 | $current_branch = preg_replace('/^(\d+\.\d+).*$/', '$1', $current_version); |
| 113 | - | |
| 114 | - if ( isset($supported_versions[$current_branch]) && $supported_versions[$current_branch]['status'] === 'supported' ) { | |
| 115 | - return 'supported'; | |
| 116 | - } | |
| 117 | - return 'eol'; | |
| 118 | -} | |
| 119 | - | |
| 120 | - | |
| 121 | - | |
| 122 | - | |
| 123 | - | |
| 124 | -/** | |
| 125 | - * Get latest WordPress version from WordPress.org API with extensive debugging | |
| 126 | - */ | |
| 127 | -function pmusage_get_latest_wp_version() { | |
| 128 | - | |
| 129 | - // Try to get from cache first | |
| 130 | - $latest_version = get_transient('pmusage_latest_wp_version'); | |
| 131 | 111 | |
| 132 | - if (false !== $latest_version) { | |
| 133 | - return $latest_version; | |
| 112 | + // Check if current branch is supported | |
| 113 | + foreach ($supported_versions as $version => $data) { | |
| 114 | + if (version_compare($current_branch, $version, '>=') && $data['status'] === 'supported') { | |
| 115 | + return 'supported'; | |
| 116 | + } | |
| 134 | 117 | } |
| 135 | 118 | |
| 136 | - | |
| 137 | - // Use version 1.7 which returns JSON format | |
| 138 | - $url = 'https://api.wordpress.org/core/version-check/1.7/'; | |
| 139 | - | |
| 140 | - | |
| 141 | - $response = wp_remote_get($url, array( | |
| 142 | - 'timeout' => 10, | |
| 143 | - 'sslverify' => true, | |
| 144 | - 'user-agent' => 'WordPress/' . get_bloginfo('version') . '; ' . get_bloginfo('url') | |
| 145 | - )); | |
| 146 | - | |
| 147 | - // Check for errors | |
| 148 | - if (is_wp_error($response)) { | |
| 149 | - return 'Unknown'; | |
| 150 | - } | |
| 151 | - | |
| 152 | - $response_code = wp_remote_retrieve_response_code($response); | |
| 153 | - | |
| 154 | - if (200 !== $response_code) { | |
| 155 | - return 'Unknown'; | |
| 156 | - } | |
| 157 | - | |
| 158 | - $body = wp_remote_retrieve_body($response); | |
| 159 | - | |
| 160 | - if (empty($body)) { | |
| 161 | - return 'Unknown'; | |
| 162 | - } | |
| 163 | - | |
| 164 | - $data = json_decode($body, true); | |
| 165 | - | |
| 166 | - if (json_last_error() !== JSON_ERROR_NONE) { | |
| 167 | - return 'Unknown'; | |
| 168 | - } | |
| 169 | - | |
| 170 | - | |
| 171 | - if (isset($data['offers']) && is_array($data['offers']) && !empty($data['offers'])) { | |
| 172 | - // Get the first offer which is the latest stable version | |
| 173 | - $latest_version = $data['offers'][0]['version'] ?? 'Unknown'; | |
| 174 | - } else { | |
| 175 | - return 'Unknown'; | |
| 176 | - } | |
| 177 | - | |
| 178 | - // Only cache if we got a valid version | |
| 179 | - if ($latest_version !== 'Unknown' && preg_match('/^\d+\.\d+/', $latest_version)) { | |
| 180 | - set_transient('pmusage_latest_wp_version', $latest_version, 12 * HOUR_IN_SECONDS); | |
| 181 | - } | |
| 182 | - | |
| 183 | - return $latest_version; | |
| 119 | + return 'eol'; | |
| 184 | 120 | } |
| 185 | 121 | |
| 186 | 122 | |
| 187 | 123 | |
| @@ -187,60 +123,15 @@ | ||
| 187 | 123 | |
| 188 | 124 | |
| 189 | 125 | |
| 190 | 126 | |
| 191 | - | |
| 192 | - | |
| 193 | -function pmusage_get_cpu_usage() { | |
| 194 | - if ( ! function_exists('sys_getloadavg') ) { | |
| 195 | - return null; | |
| 196 | - } | |
| 197 | - $load = sys_getloadavg(); | |
| 198 | - $cores = 1; | |
| 199 | - if ( is_readable('/proc/cpuinfo') ) { | |
| 200 | - $cpuinfo = file_get_contents('/proc/cpuinfo'); | |
| 201 | - preg_match_all('/^processor/m', $cpuinfo, $matches); | |
| 202 | - $cores = max(1, count($matches[0])); | |
| 203 | - } | |
| 204 | - return round(($load[0] / $cores) * 100, 1); | |
| 205 | -} | |
| 206 | - | |
| 207 | -function pmusage_get_disk_usage() { | |
| 208 | - | |
| 209 | - $path = defined('ABSPATH') ? ABSPATH : '/'; | |
| 210 | - $total = @disk_total_space($path); | |
| 211 | - $free = @disk_free_space($path); | |
| 212 | - if ( ! $total || ! $free ) { | |
| 213 | - return null; | |
| 214 | - } | |
| 215 | - $used = $total - $free; | |
| 216 | - $percent = round(($used / $total) * 100, 1); | |
| 217 | - return [ | |
| 218 | - 'used' => $used, | |
| 219 | - 'total' => $total, | |
| 220 | - 'free' => $free, | |
| 221 | - 'percent' => $percent, | |
| 222 | - ]; | |
| 223 | - | |
| 224 | -} | |
| 225 | - | |
| 226 | - | |
| 227 | - | |
| 228 | - | |
| 229 | - | |
| 230 | - | |
| 231 | - | |
| 232 | - | |
| 233 | - | |
| 234 | - | |
| 235 | - | |
| 236 | 127 | // to be shown in several places |
| 237 | -function pmusage_render_system_info() { | |
| 128 | +function pluginmemoryusage_render_system_info() { | |
| 238 | 129 | global $wpdb; |
| 239 | 130 | |
| 240 | 131 | // Get PHP version and status |
| 241 | 132 | $php_version = phpversion(); |
| 242 | - $php_status = pmusage_get_php_status($php_version); | |
| 133 | + $php_status = wpmem_get_php_status($php_version); | |
| 243 | 134 | |
| 244 | 135 | // Dashicons for PHP version status |
| 245 | 136 | $icons = [ |
| 246 | 137 | 'supported' => '<span class="dashicons dashicons-yes-alt" style="color:#46b450; margin-left:5px;" title="PHP version is supported"></span>', |
| @@ -246,77 +137,23 @@ | ||
| 246 | 137 | 'supported' => '<span class="dashicons dashicons-yes-alt" style="color:#46b450; margin-left:5px;" title="PHP version is supported"></span>', |
| 247 | 138 | 'eol' => '<span class="dashicons dashicons-warning" style="color:#dc3232; margin-left:5px;" title="PHP version is end-of-life"></span>' |
| 248 | 139 | ]; |
| 249 | 140 | |
| 250 | - | |
| 251 | - | |
| 252 | - // Fetch latest PHP version with error handling | |
| 253 | - $latest_php_version = get_transient('pmusage_latest_php_version'); | |
| 254 | - if (false === $latest_php_version) { | |
| 255 | - $response = wp_remote_get('https://www.php.net/releases/?json', array( | |
| 256 | - 'sslverify' => false, | |
| 257 | - 'timeout' => 5 | |
| 258 | - )); | |
| 259 | - | |
| 260 | - if (!is_wp_error($response) && 200 === wp_remote_retrieve_response_code($response)) { | |
| 261 | - $data = json_decode(wp_remote_retrieve_body($response), true); | |
| 262 | - if (is_array($data) && !empty($data)) { | |
| 263 | - // Extract versions | |
| 264 | - $versions = array(); | |
| 265 | - foreach ($data as $major_version => $release_info) { | |
| 266 | - if (isset($release_info['version'])) { | |
| 267 | - $versions[] = $release_info['version']; | |
| 268 | - } | |
| 269 | - } | |
| 270 | - | |
| 271 | - usort($versions, 'version_compare'); | |
| 272 | - $latest_php_version = end($versions); | |
| 273 | - set_transient('pmusage_latest_php_version', $latest_php_version, 12 * HOUR_IN_SECONDS); | |
| 274 | - } | |
| 275 | - } | |
| 276 | - } | |
| 277 | - | |
| 278 | - // Validation | |
| 279 | - if (empty($latest_php_version) || !preg_match('/^\d+\.\d+(\.\d+)?$/', $latest_php_version)) { | |
| 141 | + // Get latest PHP version (from transient) | |
| 142 | + $latest_php_version = get_transient('wpmem_latest_php_version'); | |
| 143 | + if (!$latest_php_version) { | |
| 280 | 144 | $latest_php_version = 'Unknown'; |
| 281 | 145 | } |
| 282 | - | |
| 283 | - | |
| 284 | 146 | $is_latest = ($latest_php_version !== 'Unknown') ? version_compare($php_version, $latest_php_version, '>=') : false; |
| 285 | 147 | |
| 286 | - | |
| 287 | - | |
| 288 | - // Get MySQL version and status | |
| 289 | - $mysql_version = wp_cache_get('pmusage_mysql_version'); | |
| 148 | + // Get MySQL version (with caching) | |
| 149 | + $mysql_version = wp_cache_get('wpmem_mysql_version'); | |
| 290 | 150 | if (false === $mysql_version) { |
| 291 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Getting MySQL version requires direct query, no WordPress alternative available | |
| 151 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- No WP API for MySQL version; safe, read-only query and result is cached. | |
| 292 | 152 | $mysql_version = $wpdb->get_var("SELECT VERSION()"); |
| 293 | - wp_cache_set('pmusage_mysql_version', $mysql_version, '', 12 * HOUR_IN_SECONDS); | |
| 153 | + wp_cache_set('wpmem_mysql_version', $mysql_version, '', 12 * HOUR_IN_SECONDS); | |
| 294 | 154 | } |
| 295 | 155 | |
| 296 | - $mysql_status = pmusage_get_mysql_status($mysql_version); | |
| 297 | - $latest_mysql_version = pmusage_get_mysql_latest_version($mysql_version); | |
| 298 | - | |
| 299 | - // Enhanced icons for MySQL status with tooltips | |
| 300 | - $mysql_icons = [ | |
| 301 | - 'supported' => '<span class="dashicons dashicons-yes-alt" style="color: #46b450;" title="MySQL/MariaDB version is currently supported"></span>', | |
| 302 | - 'eol' => '<span class="dashicons dashicons-warning" style="color: #dc3232;" title="MySQL/MariaDB version has reached end-of-life - upgrade recommended"></span>', | |
| 303 | - 'innovation' => '<span class="dashicons dashicons-lightbulb" style="color: #ffb900;" title="MySQL Innovation Release - shorter support cycle"></span>', | |
| 304 | - ' unknown' => '<span class="dashicons dashicons-editor-help" style="color: #666;" title="Unable to determine MySQL/MariaDB version support status"></span>' | |
| 305 | - ]; | |
| 306 | - | |
| 307 | - // Check if current version is latest | |
| 308 | - $current_version_number = ''; | |
| 309 | - preg_match('/(\d+\.\d+\.\d+)/', $mysql_version, $matches); | |
| 310 | - if (!empty($matches[1])) { | |
| 311 | - $current_version_number = $matches[1]; | |
| 312 | - } | |
| 313 | - $is_latest_mysql = ($latest_mysql_version !== 'Unknown' && !empty($current_version_number)) ? | |
| 314 | - version_compare($current_version_number, $latest_mysql_version, '>=') : false; | |
| 315 | - | |
| 316 | - | |
| 317 | - | |
| 318 | - | |
| 319 | 156 | // Get max upload size |
| 320 | 157 | $max_upload = ini_get('upload_max_filesize'); |
| 321 | 158 | $max_post = ini_get('post_max_size'); |
| 322 | 159 | $memory_limit = ini_get('memory_limit'); |
| @@ -330,56 +167,14 @@ | ||
| 330 | 167 | $wp_memory_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 331 | 168 | $php_memory_limit = ini_get('memory_limit'); |
| 332 | 169 | |
| 333 | 170 | |
| 334 | - | |
| 335 | - | |
| 336 | - | |
| 337 | - | |
| 338 | - // Get current and latest WordPress version | |
| 339 | - $wp_version = get_bloginfo('version'); | |
| 340 | - | |
| 341 | - | |
| 342 | - $latest_wp_version = pmusage_get_latest_wp_version(); | |
| 343 | - | |
| 344 | - | |
| 345 | - // Check if current version is latest | |
| 346 | - $is_wp_latest = ($latest_wp_version !== 'Unknown') ? version_compare($wp_version, $latest_wp_version, '>=') : null; | |
| 347 | - | |
| 348 | - | |
| 349 | - // Display WordPress version with status | |
| 350 | - echo '<p class="pluginmemoryusage-system-info-line"><strong>WordPress Version:</strong> ' . esc_html($wp_version); | |
| 351 | - | |
| 352 | - // Add badge if version is up to date | |
| 353 | - if ($is_wp_latest === true) { | |
| 354 | - echo ' <span class="dashicons dashicons-yes-alt" style="color: #46b450; margin-left: 5px;" title="WordPress is up to date"></span>'; | |
| 355 | - } elseif ($is_wp_latest === false) { | |
| 356 | - echo ' <span class="dashicons dashicons-warning" style="color: #dc3232; margin-left: 5px;" title="WordPress update available"></span>'; | |
| 357 | - } | |
| 358 | - | |
| 359 | - // Show latest version info | |
| 360 | - if ('Unknown' !== $latest_wp_version) { | |
| 361 | - $label = $is_wp_latest ? 'Latest' : 'Latest: ' . esc_html($latest_wp_version); | |
| 362 | - $class = $is_wp_latest ? 'pluginmemoryusage_version-latest' : 'pluginmemoryusage_version-outdated'; | |
| 363 | - echo ' <span class="' . esc_attr($class) . '">' . esc_html($label) . '</span>'; | |
| 364 | - } else { | |
| 365 | - echo ' <span class="pluginmemoryusage_version-unknown">Version check failed</span>'; | |
| 366 | - } | |
| 367 | - | |
| 368 | - echo '</p>'; | |
| 369 | - | |
| 370 | - | |
| 371 | - | |
| 372 | - | |
| 373 | - | |
| 374 | - | |
| 375 | - | |
| 376 | - | |
| 171 | + echo '<p class="pluginmemoryusage-system-info-line"><strong>WordPress Version:</strong> ' . esc_html(get_bloginfo('version')) . '</p>'; | |
| 377 | 172 | echo '<p class="pluginmemoryusage-system-info-line"><strong>PHP Version:</strong> ' . esc_html($php_version); |
| 378 | 173 | echo wp_kses_post($icons[$php_status]); |
| 379 | 174 | if ('Unknown' !== $latest_php_version) { |
| 380 | 175 | $label = $is_latest ? 'Latest' : 'Latest: ' . esc_html($latest_php_version); |
| 381 | - $class = $is_latest ? 'pluginmemoryusage_version-latest' : 'pluginmemoryusage_version-outdated'; | |
| 176 | + $class = $is_latest ? 'php-version-latest' : 'php-version-outdated'; | |
| 382 | 177 | echo ' <span class="' . esc_attr($class) . '">' . esc_html($label) . '</span>'; |
| 383 | 178 | } else { |
| 384 | 179 | echo ' <span class="php-version-outdated">(Version check failed)</span>'; |
| 385 | 180 | } |
| @@ -384,62 +179,13 @@ | ||
| 384 | 179 | echo ' <span class="php-version-outdated">(Version check failed)</span>'; |
| 385 | 180 | } |
| 386 | 181 | echo '</p>'; |
| 387 | 182 | |
| 388 | - // Display MySQL version with status | |
| 389 | - echo '<strong>MySQL Version:</strong> ' . esc_html($mysql_version) . ' '; | |
| 390 | - echo wp_kses_post($mysql_icons[$mysql_status]); | |
| 391 | - | |
| 392 | - // Show latest version info (similar to PHP version display) | |
| 393 | - if ('Unknown' !== $latest_mysql_version) { | |
| 394 | - $label = $is_latest_mysql ? 'Latest' : 'Latest: ' . esc_html($latest_mysql_version); | |
| 395 | - $class = $is_latest_mysql ? 'pluginmemoryusage_version-latest' : 'pluginmemoryusage_version-outdated'; | |
| 396 | - echo ' <span class="' . esc_attr($class) . '">' . esc_html($label) . '</span>'; | |
| 397 | - | |
| 398 | - } else { | |
| 399 | - echo ' <span class="pluginmemoryusage_version-unknown">(Version check failed)</span>'; | |
| 400 | - } | |
| 401 | - | |
| 402 | - echo '</p>'; | |
| 403 | - | |
| 404 | - | |
| 183 | + echo '<p class="pluginmemoryusage-system-info-line"><strong>MySQL Version:</strong> ' . esc_html($mysql_version) . '</p>'; | |
| 405 | 184 | echo '<p class="pluginmemoryusage-system-info-line"><strong>Max Upload Size:</strong> ' . esc_html(size_format($upload_mb)) . '</p>'; |
| 406 | 185 | echo '<p class="pluginmemoryusage-system-info-line"><strong>WordPress Memory Limit:</strong> ' . esc_html(size_format($wp_memory_limit)) . '</p>'; |
| 407 | 186 | echo '<p class="pluginmemoryusage-system-info-line"><strong>PHP Memory Limit:</strong> ' . esc_html($php_memory_limit) . '</p>'; |
| 408 | 187 | |
| 409 | - | |
| 410 | - // CPU Usage | |
| 411 | - $cpu_usage = pmusage_get_cpu_usage(); | |
| 412 | - if ( $cpu_usage !== null ) { | |
| 413 | - if ( $cpu_usage >= 90 ) $cpu_class = 'pmusage-bar-critical'; | |
| 414 | - elseif ( $cpu_usage >= 75 ) $cpu_class = 'pmusage-bar-warning'; | |
| 415 | - elseif ( $cpu_usage >= 50 ) $cpu_class = 'pmusage-bar-moderate'; | |
| 416 | - else $cpu_class = 'pmusage-bar-good'; | |
| 417 | - | |
| 418 | - echo '<p class="pluginmemoryusage-system-info-line"><strong>CPU Load (1 min avg):</strong> ' . esc_html($cpu_usage) . '%</p>'; | |
| 419 | - echo '<div class="pmusage-mini-bar"><div class="pmusage-mini-bar-fill ' . esc_attr($cpu_class) . '" style="width:' . esc_attr(min($cpu_usage, 100)) . '%"></div></div>'; | |
| 420 | - } else { | |
| 421 | - echo '<p class="pluginmemoryusage-system-info-line"><strong>CPU Load:</strong> <span class="pluginmemoryusage_version-unknown">Unavailable</span></p>'; | |
| 422 | - } | |
| 423 | - | |
| 424 | - // Disk Usage | |
| 425 | - $disk = pmusage_get_disk_usage(); | |
| 426 | - if ( $disk !== null ) { | |
| 427 | - if ( $disk['percent'] >= 90 ) $disk_class = 'pmusage-bar-critical'; | |
| 428 | - elseif ( $disk['percent'] >= 75 ) $disk_class = 'pmusage-bar-warning'; | |
| 429 | - elseif ( $disk['percent'] >= 50 ) $disk_class = 'pmusage-bar-moderate'; | |
| 430 | - else $disk_class = 'pmusage-bar-good'; | |
| 431 | - | |
| 432 | - echo '<p class="pluginmemoryusage-system-info-line"><strong>Disk Usage:</strong> ' . esc_html(size_format($disk['used'])) . ' of ' . esc_html(size_format($disk['total'])) . ' (' . esc_html($disk['percent']) . '%)</p>'; | |
| 433 | - echo '<div class="pmusage-mini-bar"><div class="pmusage-mini-bar-fill ' . esc_attr($disk_class) . '" style="width:' . esc_attr($disk['percent']) . '%"></div></div>'; | |
| 434 | - } else { | |
| 435 | - echo '<p class="pluginmemoryusage-system-info-line"><strong>Disk Usage:</strong> <span class="pluginmemoryusage_version-unknown">Unavailable</span></p>'; | |
| 436 | - } | |
| 437 | - | |
| 438 | - | |
| 439 | - | |
| 440 | - | |
| 441 | - | |
| 442 | 188 | } |
| 443 | 189 | |
| 444 | 190 | |
| 445 | 191 | |
| @@ -449,9 +195,9 @@ | ||
| 449 | 195 | |
| 450 | 196 | |
| 451 | 197 | |
| 452 | 198 | // Function to display content in the dashboard widget |
| 453 | -function pmusage_display_memory_usage_dashboard() { | |
| 199 | +function pluginmemoryusage_display_memory_usage_dashboard() { | |
| 454 | 200 | global $wpdb; |
| 455 | 201 | |
| 456 | 202 | |
| 457 | 203 | // Get WordPress memory limit |
| @@ -465,9 +211,9 @@ | ||
| 465 | 211 | |
| 466 | 212 | |
| 467 | 213 | |
| 468 | 214 | // Display the data - In seperate function for reusability |
| 469 | - pmusage_render_system_info(); | |
| 215 | + pluginmemoryusage_render_system_info(); | |
| 470 | 216 | |
| 471 | 217 | // Display memory usage |
| 472 | 218 | echo '<p>Current Memory Usage: '; |
| 473 | 219 | echo esc_html(size_format($current_memory_usage)) . ' of ' . esc_html(size_format($wp_memory_limit)) . '</p>'; |
| @@ -494,9 +240,9 @@ | ||
| 494 | 240 | |
| 495 | 241 | |
| 496 | 242 | |
| 497 | 243 | |
| 498 | -function pmusage_memory_usage_admin_page() { | |
| 244 | +function wpmem_memory_usage_admin_page() { | |
| 499 | 245 | ?> |
| 500 | 246 | <div class="wrap"> |
| 501 | 247 | <h1>Plugin Memory Usage - Control Panel</h1> |
| 502 | 248 | |
| @@ -503,9 +249,9 @@ | ||
| 503 | 249 | <div class="card-container"> |
| 504 | 250 | <div class="card-row"> |
| 505 | 251 | <div class="card card-system-info"> |
| 506 | 252 | <h2>System Information</h2> |
| 507 | - <?php pmusage_render_system_info(); ?> | |
| 253 | + <?php pluginmemoryusage_render_system_info(); ?> | |
| 508 | 254 | </div> |
| 509 | 255 | |
| 510 | 256 | <div class="card card-memory-usage"> |
| 511 | 257 | <h2>Current Memory Usage</h2> |
| @@ -573,9 +319,9 @@ | ||
| 573 | 319 | $li_class = $is_active ? 'plugin-active' : 'plugin-inactive'; |
| 574 | 320 | if ($is_memory_usage_plugin) { |
| 575 | 321 | $li_class .= ' memory-usage-plugin'; |
| 576 | 322 | } |
| 577 | - $avg_memory = pmusage_get_average_memory_usage($plugin_path); | |
| 323 | + $avg_memory = wpmem_get_average_memory_usage($plugin_path); | |
| 578 | 324 | |
| 579 | 325 | echo '<li class="' . esc_attr($li_class) . '">'; |
| 580 | 326 | echo '<div class="plugin-info">'; |
| 581 | 327 | 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>'; |
| @@ -611,9 +357,9 @@ | ||
| 611 | 357 | |
| 612 | 358 | |
| 613 | 359 | |
| 614 | 360 | |
| 615 | -function pmusage_toggle_plugin() { | |
| 361 | +function wpmem_toggle_plugin() { | |
| 616 | 362 | check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 617 | 363 | if (!current_user_can('activate_plugins')) { |
| 618 | 364 | wp_send_json_error('Insufficient permissions'); |
| 619 | 365 | } |
| @@ -640,9 +386,9 @@ | ||
| 640 | 386 | } else { |
| 641 | 387 | wp_send_json_success(); |
| 642 | 388 | } |
| 643 | 389 | } |
| 644 | -add_action('wp_ajax_pmusage_toggle_plugin', 'pmusage_toggle_plugin'); | |
| 390 | +add_action('wp_ajax_toggle_plugin', 'wpmem_toggle_plugin'); | |
| 645 | 391 | |
| 646 | 392 | |
| 647 | 393 | |
| 648 | 394 | |
| @@ -647,14 +393,14 @@ | ||
| 647 | 393 | |
| 648 | 394 | |
| 649 | 395 | |
| 650 | 396 | |
| 651 | -function pmusage_table_exists($table_name) { | |
| 397 | +function wpmem_table_exists($table_name) { | |
| 652 | 398 | global $wpdb; |
| 653 | 399 | $full_table_name = $wpdb->prefix . $table_name; |
| 654 | 400 | |
| 655 | 401 | // Create a unique cache key |
| 656 | - $cache_key = 'pmusage_table_exists_' . md5($full_table_name); | |
| 402 | + $cache_key = 'wpmem_table_exists_' . md5($full_table_name); | |
| 657 | 403 | |
| 658 | 404 | // Try to get the result from cache |
| 659 | 405 | $table_exists = wp_cache_get($cache_key); |
| 660 | 406 | |
| @@ -680,9 +426,9 @@ | ||
| 680 | 426 | |
| 681 | 427 | |
| 682 | 428 | |
| 683 | 429 | |
| 684 | -function pmusage_refresh_memory_usage() { | |
| 430 | +function wpmem_refresh_memory_usage() { | |
| 685 | 431 | check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 686 | 432 | |
| 687 | 433 | global $wpdb; |
| 688 | 434 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| @@ -687,10 +433,10 @@ | ||
| 687 | 433 | global $wpdb; |
| 688 | 434 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 689 | 435 | |
| 690 | 436 | // Check if the table exists |
| 691 | - if (!pmusage_table_exists('wpmem_plugin_history')) { | |
| 692 | - pmusage_create_history_table(); | |
| 437 | + if (!wpmem_table_exists('wpmem_plugin_history')) { | |
| 438 | + wpmem_create_history_table(); | |
| 693 | 439 | } |
| 694 | 440 | |
| 695 | 441 | |
| 696 | 442 | $current_memory = memory_get_usage(true); |
| @@ -701,9 +447,9 @@ | ||
| 701 | 447 | if (isset($_POST['plugin']) && isset($_POST['toggle_action'])) { |
| 702 | 448 | $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 703 | 449 | $previous_memory = isset($_POST['previous_memory']) ? intval($_POST['previous_memory']) : 0; |
| 704 | 450 | $memory_change = $current_memory - $previous_memory; |
| 705 | - pmusage_save_plugin_measurement($plugin, $memory_change); | |
| 451 | + wpmem_save_plugin_measurement($plugin, $memory_change); | |
| 706 | 452 | } |
| 707 | 453 | |
| 708 | 454 | wp_send_json_success(array( |
| 709 | 455 | 'current_memory' => $formatted_memory, |
| @@ -710,18 +456,18 @@ | ||
| 710 | 456 | 'current_memory_bytes' => $current_memory, |
| 711 | 457 | 'percentage' => $percentage, |
| 712 | 458 | )); |
| 713 | 459 | } |
| 714 | -add_action('wp_ajax_pmusage_refresh_memory_usage', 'pmusage_refresh_memory_usage'); | |
| 460 | +add_action('wp_ajax_refresh_memory_usage', 'wpmem_refresh_memory_usage'); | |
| 715 | 461 | |
| 716 | 462 | |
| 717 | 463 | |
| 718 | 464 | |
| 719 | -add_action('admin_enqueue_scripts', 'pmusage_enqueue_styles'); | |
| 465 | +add_action('admin_enqueue_scripts', 'wp_memory_usage_enqueue_styles'); | |
| 720 | 466 | |
| 721 | -function pmusage_enqueue_styles($hook) { | |
| 467 | +function wp_memory_usage_enqueue_styles($hook) { | |
| 722 | 468 | // Load on all admin pages where the dashboard widget appears |
| 723 | - wp_enqueue_style('pmusage-styles', | |
| 469 | + wp_enqueue_style('wp-memory-usage-styles', | |
| 724 | 470 | plugins_url('plugin-memory-usage.css', __FILE__), |
| 725 | 471 | array(), |
| 726 | 472 | filemtime(plugin_dir_path(__FILE__) . 'plugin-memory-usage.css') |
| 727 | 473 | ); |
| @@ -729,28 +475,28 @@ | ||
| 729 | 475 | |
| 730 | 476 | |
| 731 | 477 | |
| 732 | 478 | |
| 733 | -function pmusage_enqueue_scripts($hook) { | |
| 479 | +function wpmem_enqueue_scripts($hook) { | |
| 734 | 480 | if ($hook != 'toplevel_page_wp_plugin_memory_usage') { |
| 735 | 481 | return; |
| 736 | 482 | } |
| 737 | - wp_enqueue_script('pmusage-script', plugins_url('plugin-memory-usage.js', __FILE__), array(), '1.0', true); | |
| 483 | + wp_enqueue_script('wpmem-script', plugins_url('plugin-memory-usage.js', __FILE__), array(), '1.0', true); | |
| 738 | 484 | $nonce = wp_create_nonce('wp_memory_usage_nonce'); |
| 739 | - wp_localize_script('pmusage-script', 'pmusageData', array( | |
| 740 | - 'pmusage_ajaxurl' => admin_url('admin-ajax.php'), | |
| 485 | + wp_localize_script('wpmem-script', 'wpmemData', array( | |
| 486 | + 'wpmem_ajaxurl' => admin_url('admin-ajax.php'), | |
| 741 | 487 | 'nonce' => $nonce, |
| 742 | 488 | 'initialMemoryUsage' => memory_get_usage(true), |
| 743 | - 'pluginHistory' => pmusage_get_all_plugin_history() | |
| 489 | + 'pluginHistory' => wpmem_get_all_plugin_history() | |
| 744 | 490 | )); |
| 745 | 491 | } |
| 746 | -add_action('admin_enqueue_scripts', 'pmusage_enqueue_scripts'); | |
| 492 | +add_action('admin_enqueue_scripts', 'wpmem_enqueue_scripts'); | |
| 747 | 493 | |
| 748 | 494 | |
| 749 | 495 | |
| 750 | 496 | |
| 751 | 497 | |
| 752 | -function pmusage_create_history_table() { | |
| 498 | +function wpmem_create_history_table() { | |
| 753 | 499 | global $wpdb; |
| 754 | 500 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 755 | 501 | $charset_collate = $wpdb->get_charset_collate(); |
| 756 | 502 | |
| @@ -772,13 +518,13 @@ | ||
| 772 | 518 | return true; |
| 773 | 519 | } |
| 774 | 520 | |
| 775 | 521 | |
| 776 | -register_activation_hook(__FILE__, 'pmusage_create_history_table'); | |
| 522 | +register_activation_hook(__FILE__, 'wpmem_create_history_table'); | |
| 777 | 523 | |
| 778 | 524 | |
| 779 | 525 | |
| 780 | -function pmusage_save_plugin_measurement($plugin_path, $memory_change) { | |
| 526 | +function wpmem_save_plugin_measurement($plugin_path, $memory_change) { | |
| 781 | 527 | global $wpdb; |
| 782 | 528 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 783 | 529 | |
| 784 | 530 | // Convert memory_change to its absolute value |
| @@ -807,16 +553,16 @@ | ||
| 807 | 553 | ); |
| 808 | 554 | } |
| 809 | 555 | |
| 810 | 556 | // Clear caches |
| 811 | - pmusage_clear_plugin_history_cache($plugin_path); | |
| 812 | - pmusage_clear_all_plugin_history_cache(); | |
| 813 | - pmusage_clear_average_memory_cache($plugin_path); | |
| 557 | + wpmem_clear_plugin_history_cache($plugin_path); | |
| 558 | + wpmem_clear_all_plugin_history_cache(); | |
| 559 | + wpmem_clear_average_memory_cache($plugin_path); | |
| 814 | 560 | } |
| 815 | 561 | |
| 816 | 562 | |
| 817 | 563 | |
| 818 | -function pmusage_get_plugin_history($plugin_path) { | |
| 564 | +function wpmem_get_plugin_history($plugin_path) { | |
| 819 | 565 | global $wpdb; |
| 820 | 566 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 821 | 567 | |
| 822 | 568 | // Create a unique cache key for this query |
| @@ -842,9 +588,9 @@ | ||
| 842 | 588 | } |
| 843 | 589 | |
| 844 | 590 | |
| 845 | 591 | |
| 846 | -function pmusage_clear_plugin_history_cache($plugin_path) { | |
| 592 | +function wpmem_clear_plugin_history_cache($plugin_path) { | |
| 847 | 593 | $cache_key = 'wpmem_plugin_history_' . md5($plugin_path); |
| 848 | 594 | wp_cache_delete($cache_key); |
| 849 | 595 | } |
| 850 | 596 | |
| @@ -849,10 +595,10 @@ | ||
| 849 | 595 | } |
| 850 | 596 | |
| 851 | 597 | |
| 852 | 598 | |
| 853 | -add_action('wp_ajax_pmusage_get_plugin_history', 'pmusage_get_plugin_history_ajax'); | |
| 854 | -function pmusage_get_plugin_history_ajax() { | |
| 599 | +add_action('wp_ajax_get_plugin_history', 'wpmem_get_plugin_history_ajax'); | |
| 600 | +function wpmem_get_plugin_history_ajax() { | |
| 855 | 601 | check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 856 | 602 | if (!isset($_POST['plugin'])) { |
| 857 | 603 | wp_send_json_error('Plugin not specified'); |
| 858 | 604 | } |
| @@ -857,9 +603,9 @@ | ||
| 857 | 603 | wp_send_json_error('Plugin not specified'); |
| 858 | 604 | } |
| 859 | 605 | |
| 860 | 606 | $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 861 | - $history = pmusage_get_plugin_history($plugin); | |
| 607 | + $history = wpmem_get_plugin_history($plugin); | |
| 862 | 608 | |
| 863 | 609 | wp_send_json_success($history); |
| 864 | 610 | } |
| 865 | 611 | |
| @@ -869,14 +615,14 @@ | ||
| 869 | 615 | |
| 870 | 616 | |
| 871 | 617 | |
| 872 | 618 | |
| 873 | -function pmusage_get_all_plugin_history() { | |
| 619 | +function wpmem_get_all_plugin_history() { | |
| 874 | 620 | global $wpdb; |
| 875 | 621 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 876 | 622 | |
| 877 | 623 | // Create a unique cache key for this query |
| 878 | - $cache_key = 'pmusage_all_plugin_history'; | |
| 624 | + $cache_key = 'wpmem_all_plugin_history'; | |
| 879 | 625 | |
| 880 | 626 | // Try to get the results from cache |
| 881 | 627 | $results = wp_cache_get($cache_key); |
| 882 | 628 | |
| @@ -916,36 +662,36 @@ | ||
| 916 | 662 | |
| 917 | 663 | |
| 918 | 664 | |
| 919 | 665 | |
| 920 | -function pmusage_clear_all_plugin_history_cache() { | |
| 921 | - wp_cache_delete('pmusage_all_plugin_history'); | |
| 666 | +function wpmem_clear_all_plugin_history_cache() { | |
| 667 | + wp_cache_delete('wpmem_all_plugin_history'); | |
| 922 | 668 | } |
| 923 | 669 | |
| 924 | 670 | |
| 925 | 671 | |
| 926 | -function pmusage_clear_table_exists_cache() { | |
| 672 | +function wpmem_clear_table_exists_cache() { | |
| 927 | 673 | global $wpdb; |
| 928 | 674 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 929 | - $cache_key = 'pmusage_table_exists_' . $table_name; | |
| 675 | + $cache_key = 'wpmem_table_exists_' . $table_name; | |
| 930 | 676 | wp_cache_delete($cache_key); |
| 931 | 677 | } |
| 932 | 678 | |
| 933 | 679 | |
| 934 | 680 | // Reset table verification cache when changing plugin state |
| 935 | -register_activation_hook(__FILE__, 'pmusage_clear_table_exists_cache'); // Pre-activation check | |
| 936 | -register_deactivation_hook(__FILE__, 'pmusage_clear_table_exists_cache'); // Post-deactivation cleanup | |
| 681 | +register_activation_hook(__FILE__, 'wpmem_clear_table_exists_cache'); // Pre-activation check | |
| 682 | +register_deactivation_hook(__FILE__, 'wpmem_clear_table_exists_cache'); // Post-deactivation cleanup | |
| 937 | 683 | |
| 938 | 684 | |
| 939 | 685 | |
| 940 | 686 | |
| 941 | 687 | |
| 942 | -function pmusage_get_average_memory_usage($plugin_path) { | |
| 688 | +function wpmem_get_average_memory_usage($plugin_path) { | |
| 943 | 689 | global $wpdb; |
| 944 | 690 | $table_name = $wpdb->prefix . 'wpmem_plugin_history'; |
| 945 | 691 | |
| 946 | 692 | // Create a unique cache key |
| 947 | - $cache_key = 'pmusage_avg_memory_' . md5($plugin_path); | |
| 693 | + $cache_key = 'wpmem_avg_memory_' . md5($plugin_path); | |
| 948 | 694 | |
| 949 | 695 | // Try to get the result from cache |
| 950 | 696 | $avg_memory = wp_cache_get($cache_key); |
| 951 | 697 | |
| @@ -978,19 +724,19 @@ | ||
| 978 | 724 | } |
| 979 | 725 | |
| 980 | 726 | |
| 981 | 727 | |
| 982 | -function pmusage_clear_average_memory_cache($plugin_path) { | |
| 983 | - $cache_key = 'pmusage_avg_memory_' . md5($plugin_path); | |
| 728 | +function wpmem_clear_average_memory_cache($plugin_path) { | |
| 729 | + $cache_key = 'wpmem_avg_memory_' . md5($plugin_path); | |
| 984 | 730 | wp_cache_delete($cache_key); |
| 985 | 731 | } |
| 986 | 732 | |
| 987 | 733 | |
| 988 | 734 | |
| 989 | -add_action('wp_ajax_pmusage_update_average_memory', 'pmusage_update_average_memory'); | |
| 990 | -add_action('wp_ajax_nopriv_update_average_memory', 'pmusage_update_average_memory'); | |
| 735 | +add_action('wp_ajax_update_average_memory', 'wpmem_update_average_memory'); | |
| 736 | +add_action('wp_ajax_nopriv_update_average_memory', 'wpmem_update_average_memory'); | |
| 991 | 737 | |
| 992 | -function pmusage_update_average_memory() { | |
| 738 | +function wpmem_update_average_memory() { | |
| 993 | 739 | // Check nonce for security |
| 994 | 740 | if (!isset($_POST['nonce'])) { |
| 995 | 741 | wp_send_json_error('Security token not provided'); |
| 996 | 742 | } |
| @@ -1004,9 +750,9 @@ | ||
| 1004 | 750 | wp_send_json_error('Plugin path not provided'); |
| 1005 | 751 | } |
| 1006 | 752 | |
| 1007 | 753 | $plugin_path = sanitize_text_field(wp_unslash($_POST['plugin_path'])); |
| 1008 | - $avg_memory = pmusage_get_average_memory_usage($plugin_path); | |
| 754 | + $avg_memory = wpmem_get_average_memory_usage($plugin_path); | |
| 1009 | 755 | |
| 1010 | 756 | wp_send_json_success(array('avg_memory' => $avg_memory)); |
| 1011 | 757 | } |
| 1012 | 758 | |
| @@ -1014,9 +760,9 @@ | ||
| 1014 | 760 | |
| 1015 | 761 | |
| 1016 | 762 | |
| 1017 | 763 | |
| 1018 | -function pmusage_add_increase_memory_button($content) { | |
| 764 | +function wpmem_add_increase_memory_button($content) { | |
| 1019 | 765 | $wp_memory_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 1020 | 766 | $current_memory_usage = memory_get_usage(true); |
| 1021 | 767 | |
| 1022 | 768 | if ($current_memory_usage > $wp_memory_limit) { |
| @@ -1025,14 +771,14 @@ | ||
| 1025 | 771 | } |
| 1026 | 772 | |
| 1027 | 773 | return $content; |
| 1028 | 774 | } |
| 1029 | -add_filter('pmusage_memory_usage_content', 'pmusage_add_increase_memory_button'); | |
| 775 | +add_filter('wpmem_memory_usage_content', 'wpmem_add_increase_memory_button'); | |
| 1030 | 776 | |
| 1031 | 777 | |
| 1032 | 778 | |
| 1033 | 779 | |
| 1034 | -function pmusage_increase_memory_limit() { | |
| 780 | +function wpmem_increase_memory_limit() { | |
| 1035 | 781 | check_ajax_referer('wp_memory_usage_nonce', 'nonce'); |
| 1036 | 782 | |
| 1037 | 783 | if (!current_user_can('manage_options')) { |
| 1038 | 784 | wp_send_json_error(array('message' => 'Insufficient permissions')); |
| @@ -1053,19 +799,19 @@ | ||
| 1053 | 799 | $new_limit = $max_limit; |
| 1054 | 800 | } |
| 1055 | 801 | |
| 1056 | 802 | // Attempt to increase the limit |
| 1057 | - if (pmusage_set_memory_limit($new_limit)) { | |
| 803 | + if (wpmem_set_memory_limit($new_limit)) { | |
| 1058 | 804 | wp_send_json_success(array('new_limit' => size_format($new_limit))); |
| 1059 | 805 | } else { |
| 1060 | 806 | wp_send_json_error(array('message' => 'Unable to increase memory limit. You may need to contact your hosting provider.')); |
| 1061 | 807 | } |
| 1062 | 808 | } |
| 1063 | -add_action('wp_ajax_pmusage_increase_memory_limit', 'pmusage_increase_memory_limit'); | |
| 809 | +add_action('wp_ajax_increase_memory_limit', 'wpmem_increase_memory_limit'); | |
| 1064 | 810 | |
| 1065 | 811 | |
| 1066 | 812 | |
| 1067 | -function pmusage_set_memory_limit($new_limit) { | |
| 813 | +function wpmem_set_memory_limit($new_limit) { | |
| 1068 | 814 | global $wp_filesystem; |
| 1069 | 815 | |
| 1070 | 816 | // Initialize the WP filesystem |
| 1071 | 817 | if (empty($wp_filesystem)) { |
| @@ -1110,9 +856,9 @@ | ||
| 1110 | 856 | |
| 1111 | 857 | |
| 1112 | 858 | |
| 1113 | 859 | |
| 1114 | -function pmusage_add_settings_link($links) { | |
| 860 | +function wpmem_add_settings_link($links) { | |
| 1115 | 861 | $settings_link = '<a href="' . admin_url('admin.php?page=wp_plugin_memory_usage') . '">Settings</a>'; |
| 1116 | 862 | array_unshift($links, $settings_link); |
| 1117 | 863 | return $links; |
| 1118 | 864 | } |
| @@ -1117,170 +863,29 @@ | ||
| 1117 | 863 | return $links; |
| 1118 | 864 | } |
| 1119 | 865 | |
| 1120 | 866 | $plugin = plugin_basename(__FILE__); |
| 1121 | -add_filter("plugin_action_links_$plugin", 'pmusage_add_settings_link'); | |
| 867 | +add_filter("plugin_action_links_$plugin", 'wpmem_add_settings_link'); | |
| 1122 | 868 | |
| 1123 | 869 | |
| 1124 | 870 | |
| 1125 | 871 | |
| 1126 | 872 | |
| 1127 | -function pmusage_add_memory_to_admin_bar($wp_admin_bar) { | |
| 873 | +function wpmem_add_memory_to_admin_bar($wp_admin_bar) { | |
| 1128 | 874 | $wp_memory_limit = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT); |
| 1129 | 875 | $current_memory_usage = memory_get_usage(true); |
| 1130 | 876 | $percentage_used = round(($current_memory_usage / $wp_memory_limit) * 100, 1); |
| 1131 | 877 | |
| 1132 | - | |
| 1133 | - // Determine color class based on memory usage | |
| 1134 | - if ($percentage_used >= 90) { | |
| 1135 | - $color_class = 'wpmem-critical'; // Red | |
| 1136 | - } elseif ($percentage_used >= 75) { | |
| 1137 | - $color_class = 'wpmem-warning'; // Orange | |
| 1138 | - } elseif ($percentage_used >= 50) { | |
| 1139 | - $color_class = 'wpmem-moderate'; // Yellow | |
| 1140 | - } else { | |
| 1141 | - $color_class = 'wpmem-good'; // Green | |
| 1142 | - } | |
| 1143 | - | |
| 1144 | 878 | $wp_admin_bar->add_node(array( |
| 1145 | - 'id' => 'pmusage_memory_usage', | |
| 1146 | - 'title' => '<span class="' . esc_attr($color_class) . '">MEM: ' . $percentage_used . '%</span>', | |
| 1147 | - 'href' => admin_url('admin.php?page=wp_plugin_memory_usage'), | |
| 1148 | - 'meta' => array( | |
| 1149 | - 'title' => 'Current memory usage: ' . size_format($current_memory_usage) . ' of ' . size_format($wp_memory_limit), | |
| 879 | + 'id' => 'wpmem_memory_usage', | |
| 880 | + 'title' => 'MEM: ' . $percentage_used . '%', | |
| 881 | + 'href' => admin_url('admin.php?page=wp_plugin_memory_usage'), | |
| 882 | + 'meta' => array( | |
| 883 | + 'title' => 'Current memory usage', | |
| 1150 | 884 | ), |
| 1151 | 885 | )); |
| 1152 | 886 | } |
| 1153 | -add_action('admin_bar_menu', 'pmusage_add_memory_to_admin_bar', 100); | |
| 1154 | - | |
| 1155 | - | |
| 1156 | - | |
| 1157 | - | |
| 1158 | - | |
| 1159 | -function pmusage_get_mysql_versions_from_api() { | |
| 1160 | - $mysql_versions = get_transient('pmusage_mysql_eol_data'); | |
| 1161 | - if (false === $mysql_versions) { | |
| 1162 | - // Get MySQL data | |
| 1163 | - $mysql_response = wp_remote_get('https://endoflife.date/api/mysql.json', array( | |
| 1164 | - 'sslverify' => false, | |
| 1165 | - 'timeout' => 5 | |
| 1166 | - )); | |
| 1167 | - | |
| 1168 | - // Get MariaDB data | |
| 1169 | - $mariadb_response = wp_remote_get('https://endoflife.date/api/mariadb.json', array( | |
| 1170 | - 'sslverify' => false, | |
| 1171 | - 'timeout' => 5 | |
| 1172 | - )); | |
| 1173 | - | |
| 1174 | - $versions_data = ['mysql' => [], 'mariadb' => []]; | |
| 1175 | - | |
| 1176 | - if (!is_wp_error($mysql_response) && 200 === wp_remote_retrieve_response_code($mysql_response)) { | |
| 1177 | - $mysql_data = json_decode(wp_remote_retrieve_body($mysql_response), true); | |
| 1178 | - if (is_array($mysql_data)) { | |
| 1179 | - foreach ($mysql_data as $version_info) { | |
| 1180 | - $cycle = $version_info['cycle']; | |
| 1181 | - $eol_date = $version_info['eol'] ?? null; | |
| 1182 | - | |
| 1183 | - if ($eol_date && $eol_date !== false) { | |
| 1184 | - $versions_data['mysql'][$cycle] = [ | |
| 1185 | - 'eol_date' => $eol_date, | |
| 1186 | - 'latest' => $version_info['latest'] ?? '', | |
| 1187 | - 'support_end' => $version_info['support'] ?? $eol_date | |
| 1188 | - ]; | |
| 1189 | - } | |
| 1190 | - } | |
| 1191 | - } | |
| 1192 | - } | |
| 1193 | - | |
| 1194 | - if (!is_wp_error($mariadb_response) && 200 === wp_remote_retrieve_response_code($mariadb_response)) { | |
| 1195 | - $mariadb_data = json_decode(wp_remote_retrieve_body($mariadb_response), true); | |
| 1196 | - if (is_array($mariadb_data)) { | |
| 1197 | - foreach ($mariadb_data as $version_info) { | |
| 1198 | - $cycle = $version_info['cycle']; | |
| 1199 | - $eol_date = $version_info['eol'] ?? null; | |
| 1200 | - | |
| 1201 | - if ($eol_date && $eol_date !== false) { | |
| 1202 | - $versions_data['mariadb'][$cycle] = [ | |
| 1203 | - 'eol_date' => $eol_date, | |
| 1204 | - 'latest' => $version_info['latest'] ?? '', // This is the key addition | |
| 1205 | - 'lts' => $version_info['lts'] ?? false | |
| 1206 | - ]; | |
| 1207 | - } | |
| 1208 | - } | |
| 1209 | - } | |
| 1210 | - } | |
| 1211 | - | |
| 1212 | - // Cache for one week | |
| 1213 | - set_transient('pmusage_mysql_eol_data', $versions_data, WEEK_IN_SECONDS); | |
| 1214 | - $mysql_versions = $versions_data; | |
| 1215 | - } | |
| 1216 | - | |
| 1217 | - return $mysql_versions; | |
| 1218 | -} | |
| 1219 | - | |
| 1220 | - | |
| 1221 | -function pmusage_get_mysql_latest_version($current_version) { | |
| 1222 | - $version_data = pmusage_get_mysql_versions_from_api(); | |
| 1223 | - $is_mariadb = stripos($current_version, 'mariadb') !== false; | |
| 1224 | - | |
| 1225 | - // Extract version number | |
| 1226 | - preg_match('/(\d+\.\d+)/', $current_version, $matches); | |
| 1227 | - $version_number = $matches[1] ?? ''; | |
| 1228 | - | |
| 1229 | - if (empty($version_number)) { | |
| 1230 | - return 'Unknown'; | |
| 1231 | - } | |
| 1232 | - | |
| 1233 | - $database_type = $is_mariadb ? 'mariadb' : 'mysql'; | |
| 1234 | - $versions = $version_data[$database_type] ?? []; | |
| 1235 | - | |
| 1236 | - if (isset($versions[$version_number]) && !empty($versions[$version_number]['latest'])) { | |
| 1237 | - return $versions[$version_number]['latest']; | |
| 1238 | - } | |
| 1239 | - | |
| 1240 | - return 'Unknown'; | |
| 1241 | -} | |
| 1242 | - | |
| 1243 | - | |
| 1244 | - | |
| 1245 | - | |
| 1246 | - | |
| 1247 | -function pmusage_get_mysql_status($current_version) { | |
| 1248 | - $version_data = pmusage_get_mysql_versions_from_api(); | |
| 1249 | - $is_mariadb = stripos($current_version, 'mariadb') !== false; | |
| 1250 | - | |
| 1251 | - // Extract version number | |
| 1252 | - preg_match('/(\d+\.\d+)/', $current_version, $matches); | |
| 1253 | - $version_number = $matches[1] ?? ''; | |
| 1254 | - | |
| 1255 | - if (empty($version_number)) { | |
| 1256 | - return 'unknown'; | |
| 1257 | - } | |
| 1258 | - | |
| 1259 | - $database_type = $is_mariadb ? 'mariadb' : 'mysql'; | |
| 1260 | - $versions = $version_data[$database_type] ?? []; | |
| 1261 | - | |
| 1262 | - if (isset($versions[$version_number])) { | |
| 1263 | - $version_info = $versions[$version_number]; | |
| 1264 | - $eol_date_str = $version_info['eol_date']; | |
| 1265 | - | |
| 1266 | - // Handle different date formats from API | |
| 1267 | - if ($eol_date_str === true || $eol_date_str === false) { | |
| 1268 | - return $eol_date_str === false ? 'supported' : 'eol'; | |
| 1269 | - } | |
| 1270 | - | |
| 1271 | - try { | |
| 1272 | - $eol_date = new DateTime($eol_date_str); | |
| 1273 | - $now = new DateTime(); | |
| 1274 | - | |
| 1275 | - return ($now < $eol_date) ? 'supported' : 'eol'; | |
| 1276 | - } catch (Exception $e) { | |
| 1277 | - return 'unknown'; | |
| 1278 | - } | |
| 1279 | - } | |
| 1280 | - | |
| 1281 | - return 'unknown'; | |
| 1282 | -} | |
| 887 | +add_action('admin_bar_menu', 'wpmem_add_memory_to_admin_bar', 100); | |
| 1283 | 888 | |
| 1284 | 889 | |
| 1285 | 890 | |
| 1286 | 891 | |