| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-Memory-Usage |
| 4 |
Plugin URI: https://www.json-content-importer.com |
| 5 |
Description: Show up memory limits, current memory usage, IP-Address, PHP-Version in the dashboard and admin footer |
| 6 |
Author: Bernhard Kux |
| 7 |
Version: 1.2.4 |
| 8 |
Author URI: https://www.json-content-importer.com |
| 9 |
Text Domain: wp-memory-usage |
| 10 |
Domain Path: /languages/ |
| 11 |
License: GPLv3 |
| 12 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html |
| 13 |
|
| 14 |
Copyright 2009-2013 by Alex Rabe, 2022- by Bernhard Kux |
| 15 |
*/ |
| 16 |
|
| 17 |
/* block direct requests */ |
| 18 |
if ( !function_exists( 'add_action' ) ) { |
| 19 |
echo 'Hello, this is a plugin: You must not call me directly.'; |
| 20 |
exit; |
| 21 |
} |
| 22 |
defined('ABSPATH') OR exit; |
| 23 |
|
| 24 |
if ( is_admin() ) { |
| 25 |
|
| 26 |
define( 'WPMEMORYUSAGEVERSION', '1.2.4' ); // current version number |
| 27 |
|
| 28 |
function wp_memory_usage_i18n_init() { |
| 29 |
$pd = dirname( |
| 30 |
plugin_basename(__FILE__) |
| 31 |
).'/languages/'; |
| 32 |
load_plugin_textdomain('wp_memory_usage', false, $pd); |
| 33 |
} |
| 34 |
add_action('plugins_loaded', 'wp_memory_usage_i18n_init'); |
| 35 |
|
| 36 |
class wp_memory_usage { |
| 37 |
private $ipadr = ""; |
| 38 |
private $servername = ""; |
| 39 |
private $memory = array(); |
| 40 |
|
| 41 |
public function wp_memory_usage() { |
| 42 |
return $this->__construct(); |
| 43 |
} |
| 44 |
|
| 45 |
public function __construct() { |
| 46 |
$this->get_ip_adress(); |
| 47 |
add_action( 'init', array (&$this, 'check_limit') ); |
| 48 |
add_action( 'wp_dashboard_setup', array (&$this, 'add_dashboard') ); |
| 49 |
add_filter( 'admin_footer_text', array (&$this, 'add_footer') ); |
| 50 |
} |
| 51 |
|
| 52 |
public function check_limit() { |
| 53 |
$this->memory['phplimit'] = ""; |
| 54 |
$this->memory['phplimitunity'] = 'MB'; |
| 55 |
if (!is_null(ini_get('memory_limit'))) { |
| 56 |
$this->memory['phplimit'] = (int) ini_get('memory_limit'); |
| 57 |
} |
| 58 |
$ret = $this->formatWP_MEMORY_LIMIT(WP_MEMORY_LIMIT); |
| 59 |
$this->memory["wpmb"] = $ret["mb"]; |
| 60 |
$this->memory["wpunity"] = $ret["unity"]; |
| 61 |
|
| 62 |
$ret = $this->formatWP_MEMORY_LIMIT(WP_MAX_MEMORY_LIMIT); |
| 63 |
$this->memory["wpmaxmb"] = $ret["mb"]; |
| 64 |
$this->memory["wpmaxunity"] = $ret["unity"]; |
| 65 |
} |
| 66 |
|
| 67 |
private function check_memory_usage() { |
| 68 |
$this->memory['usage'] = function_exists('memory_get_peak_usage') ? round(memory_get_peak_usage(true) / 1024 / 1024, 2) : 0; |
| 69 |
if ( !empty($this->memory['usage'])) { |
| 70 |
$this->memory['percent'] = -1; |
| 71 |
if (!empty($this->memory['wpmb']) && ($this->memory['wpmb']!=0)) { |
| 72 |
$this->memory['percent'] = round($this->memory['usage'] /$this->memory["wpmb"] * 100, 0); |
| 73 |
} |
| 74 |
|
| 75 |
$this->memory['percentphp'] = -1; |
| 76 |
if (!empty($this->memory['phplimit']) && ($this->memory['phplimit']!=0)) { |
| 77 |
$this->memory['percentphp'] = round ($this->memory['usage'] / $this->memory["phplimit"] * 100, 0); |
| 78 |
} |
| 79 |
|
| 80 |
//If the bar is tp small we move the text outside |
| 81 |
$this->memory['percent_pos'] = ''; |
| 82 |
//In case we are in our limits take the admin color |
| 83 |
$this->memory['color'] = ''; |
| 84 |
if ($this->memory['percent'] > 80) $this->memory['color'] = 'background: #E66F00;'; |
| 85 |
if ($this->memory['percent'] > 95) $this->memory['color'] = 'background: red;'; |
| 86 |
if ($this->memory['percent'] < 10) $this->memory['percent_pos'] = 'margin-right: -30px; color: #444;'; |
| 87 |
$this->memory['percentwidth'] = $this->memory['percent']; |
| 88 |
if ($this->memory['percent']>100) { |
| 89 |
$this->memory['percentwidth'] = 100; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public function dashboard_output() { |
| 95 |
$this->check_memory_usage(); |
| 96 |
?> |
| 97 |
<ul> |
| 98 |
<li><strong><?php echo __('PHP Version', 'wp_memory_usage'); ?>:</strong> <span><?php |
| 99 |
echo PHP_VERSION; ?> / <?php echo (PHP_INT_SIZE * 8) . __('Bit OS', 'wp_memory_usage'); |
| 100 |
|
| 101 |
if (!is_null(ini_get('max_execution_time'))) { |
| 102 |
$max_execution_time = (int) ini_get('max_execution_time'). __('sec', 'wp_memory_usage'); |
| 103 |
echo " / ".__('Max execution time: ', 'wp_memory_usage').$max_execution_time; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
?></span></li> |
| 108 |
<li><strong><?php echo __('Memory limits', 'wp_memory_usage'); ?></strong> : <span> |
| 109 |
<?php |
| 110 |
if ($this->memory["wpmb"]!="") { |
| 111 |
echo __('Wordpress', 'wp_memory_usage').' '.$this->memory["wpmb"]. $this->memory["wpunity"]." / "; |
| 112 |
} |
| 113 |
#if ($this->memory["wpmaxmb"]!="") { |
| 114 |
# echo __('Wordpress-Admin', 'wp_memory_usage').' '.$this->memory["wpmaxmb"]. $this->memory["wpmaxunity"]." / "; |
| 115 |
#} |
| 116 |
if ($this->memory['phplimit']!="") { |
| 117 |
echo __('PHP ', 'wp_memory_usage').' '.$this->memory['phplimit'].$this->memory['phplimitunity']; |
| 118 |
} |
| 119 |
?> |
| 120 |
</span></li> |
| 121 |
<li><strong><?php echo __('Memory usage', 'wp_memory_usage'); ?></strong> : <span><?php echo $this->memory['usage']."MB"; ?> |
| 122 |
<!-- <?PHP #echo "<strong>".__( 'IP-Address', 'wp_memory_usage') . " " . $this->servername. ':</strong> '.$this->ipadr; ?>--> |
| 123 |
</span></li> |
| 124 |
</ul> |
| 125 |
<?php if (!empty($this->memory['percent'])) : ?> |
| 126 |
<div class="progressbar"> |
| 127 |
<div style="border:1px solid #DDDDDD; background-color:#F9F9F9; border-color: rgb(223, 223, 223); box-shadow: 0px 1px 0px rgb(255, 255, 255) inset; border-radius: 3px;"> |
| 128 |
<div class="button-primary" style="width: <?php echo $this->memory['percentwidth']; ?>%;<?php echo $this->memory['color'];?>padding: 0px;border-width:0px; color:#FFFFFF;text-align:right; border-color: rgb(223, 223, 223); box-shadow: 0px 1px 0px rgb(255, 255, 255) inset; border-radius: 3px; margin-top: -1px;"> |
| 129 |
<div style="padding:2px;<?php echo $this->memory['percent_pos']; ?>"><?php echo $this->memory['percent']; ?>%</div> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<?php endif; ?> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
public function add_dashboard() { |
| 138 |
wp_add_dashboard_widget( 'wp_memory_dashboard', __('Memory Overview', 'wp_memory_usage'), array (&$this, 'dashboard_output') ); |
| 139 |
} |
| 140 |
|
| 141 |
private function formatWP_MEMORY_LIMIT($valin) { #WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT come with size and unity |
| 142 |
$size = strtolower(substr($valin, -1)); |
| 143 |
$number = (int) substr($valin, 0, -1); |
| 144 |
$ret = Array(); |
| 145 |
if ($size=="k") { $ret["mb"] = ($number/1024); $ret["unity"] = "kB"; } |
| 146 |
if ($size=="m") { $ret["mb"] = $number; $ret["unity"] = "MB"; } |
| 147 |
if ($size=="g") { $ret["mb"] = ($number*1024); $ret["unity"] = "GB"; } |
| 148 |
if ($size=="t") { $ret["mb"] = ($number*(1024*1024)); $ret["unity"] = "TB"; } |
| 149 |
if ($size=="p") { $ret["mb"] = ($number*(1024*1024*1024)); $ret["unity"] = "PB"; } |
| 150 |
return $ret; |
| 151 |
} |
| 152 |
|
| 153 |
private function get_ip_adress() { |
| 154 |
if (isset($_SERVER[ 'SERVER_ADDR' ]) && !empty($_SERVER[ 'SERVER_ADDR' ])) { |
| 155 |
$this->ipadr = $_SERVER[ 'SERVER_ADDR' ]; |
| 156 |
} |
| 157 |
if (empty($this->ipadr) && isset($_SERVER[ 'LOCAL_ADDR' ]) && !empty($_SERVER[ 'LOCAL_ADDR' ])) { |
| 158 |
$this->ipadr = $_SERVER[ 'LOCAL_ADDR' ]; |
| 159 |
} |
| 160 |
|
| 161 |
if (!empty($_SERVER['SERVER_NAME'])) { |
| 162 |
$this->servername = " (".$_SERVER['SERVER_NAME'].")"; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public function add_footer($content) { |
| 167 |
$this->check_memory_usage(); |
| 168 |
$content .= ' | '. __( 'WP Memory Limit:', 'wp_memory_usage'). ' ' . $this->memory['usage'] . ' ' . __( 'of', 'wp_memory_usage') . ' ' . $this->memory["wpmb"].$this->memory["wpunity"]. " (".$this->memory['percent']."%)"; |
| 169 |
$content .= ' | '. __( 'PHP Memory Limit:', 'wp_memory_usage'). ' ' . $this->memory['usage'] . ' ' . __( 'of', 'wp_memory_usage') . ' ' . $this->memory['phplimit'].$this->memory['phplimitunity']. " (".$this->memory['percentphp']."%)"; |
| 170 |
$content .= ' | '. __( 'IP-Address', 'wp_memory_usage') . " " . $this->servername. ': '.$this->ipadr; |
| 171 |
$content .= ' | '. __( 'PHP', 'wp_memory_usage') . ": " . PHP_VERSION; |
| 172 |
return $content; |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
// Start this plugin once all other plugins are fully loaded |
| 178 |
function WP_Memory_Usage_action_plugins_loaded( $array ) { |
| 179 |
return new wp_memory_usage(); |
| 180 |
}; |
| 181 |
add_action( 'plugins_loaded', 'WP_Memory_Usage_action_plugins_loaded', 10, 1 ); |
| 182 |
} |