PluginProbe
WP-Memory-Usage / 1.2.9
WP-Memory-Usage v1.2.9
2.3.1 trunk 1.2.10 1.2.11 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.3.0
wp-memory-usage / wp-memory-usage.php

wp-memory-usage.php in WP-Memory-Usage 1.2.9, at wp-memory-usage.php

416 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.9
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 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.9' ); // current version number
27
28 function wp_memory_usage_i18n_init() {
29 $pd = dirname(
30 plugin_basename(__FILE__)
31 ).'/languages/';
32
33 $id = "wp-memory-usage";
34 $loaderrorlevel = load_plugin_textdomain($id, false, $pd);
35 }
36 add_action('plugins_loaded', 'wp_memory_usage_i18n_init');
37
38 class wp_memory_usage {
39 private $ipadr = "";
40 private $servername = "";
41 private $memory = array();
42
43 public function __construct() {
44 $this->get_ip_adress();
45 add_action( 'init', array (&$this, 'check_limit') );
46 add_action( 'wp_dashboard_setup', array (&$this, 'add_dashboard') );
47 if ( is_multisite() ) {
48 add_action( 'wp_network_dashboard_setup', array (&$this, 'add_dashboard') );
49 }
50 add_filter( 'admin_footer_text', array (&$this, 'add_footer') );
51 }
52
53 public function check_limit() {
54 $this->memory['phplimit'] = "";
55 $this->memory['phplimitunity'] = 'MB';
56 if (!is_null(ini_get('memory_limit'))) {
57 $phplimit_memory = ini_get('memory_limit');
58 if (preg_match("/G$/i", $phplimit_memory)) {
59 # set in gigabyte
60 $phplimit_memory = 1024 * preg_replace("/G$/i", "", $phplimit_memory);
61 }
62 $this->memory['phplimit'] = (int) $phplimit_memory;
63 }
64 $ret = $this->formatWP_MEMORY_LIMIT(WP_MEMORY_LIMIT);
65 $this->memory["wpmb"] = $ret["mb"] ?? '';
66 $this->memory["wpunity"] = $ret["unity"] ?? '';
67
68 $ret = $this->formatWP_MEMORY_LIMIT(WP_MAX_MEMORY_LIMIT);
69 $this->memory["wpmaxmb"] = $ret["mb"] ?? '';
70 $this->memory["wpmaxunity"] = $ret["unity"] ?? '';
71 }
72
73 private function check_memory_usage() {
74 $this->memory['usage'] = function_exists('memory_get_peak_usage') ? round(memory_get_peak_usage(true) / 1024 / 1024, 2) : 0;
75 if ( !empty($this->memory['usage'])) {
76 $this->memory['percent'] = -1;
77 if (!empty($this->memory['wpmb']) && ($this->memory['wpmb']!=0)) {
78 $this->memory['percent'] = round($this->memory['usage'] /$this->memory["wpmb"] * 100, 0);
79 }
80
81 $this->memory['percentphp'] = -1;
82 if (!empty($this->memory['phplimit']) && ($this->memory['phplimit']!=0)) {
83 $this->memory['percentphp'] = round ($this->memory['usage'] / $this->memory["phplimit"] * 100, 0);
84 }
85
86 //If the bar is tp small we move the text outside
87 $this->memory['percent_pos'] = '';
88 //In case we are in our limits take the admin color
89 $this->memory['color'] = '';
90 if ($this->memory['percent'] > 80) $this->memory['color'] = 'background: #E66F00;';
91 if ($this->memory['percent'] > 95) $this->memory['color'] = 'background: red;';
92 if ($this->memory['percent'] < 10) $this->memory['percent_pos'] = 'margin-right: -30px; color: #444;';
93 $this->memory['percentwidth'] = $this->memory['percent'];
94 if ($this->memory['percent']>100) {
95 $this->memory['percentwidth'] = 100;
96 }
97 }
98 }
99
100 public function dashboard_output() {
101 $this->check_memory_usage();
102 ?>
103 <ul>
104 <li><strong><?php echo __('PHP Version', 'wp-memory-usage'); ?>:</strong> <span><?php
105 echo PHP_VERSION; ?>&nbsp;/&nbsp;<?php echo (PHP_INT_SIZE * 8) . __('Bit OS', 'wp-memory-usage');
106
107 if (!is_null(ini_get('max_execution_time'))) {
108 $max_execution_time = (int) ini_get('max_execution_time'). __('sec', 'wp-memory-usage');
109 echo " / ".__('Max execution time: ', 'wp-memory-usage').' '.$max_execution_time;
110 }
111
112
113 ?></span></li>
114 <li><strong><?php echo __('Memory limits', 'wp-memory-usage'); ?>:</strong> <span>
115 <?php
116 if (!empty($this->memory["wpmb"])) {
117 echo __('Wordpress', 'wp-memory-usage').' '.$this->memory["wpmb"]. $this->memory["wpunity"]." / ";
118 }
119 if ($this->memory["wpmaxmb"]!="" && ($this->memory["wpmb"]!=$this->memory["wpmaxmb"])) {
120 echo __('Wordpress-Admin', 'wp-memory-usage').' '.$this->memory["wpmaxmb"]. $this->memory["wpmaxunity"]." / ";
121 }
122 if ($this->memory['phplimit']!="") {
123 echo __('PHP ', 'wp-memory-usage').' '.$this->memory['phplimit'].$this->memory['phplimitunity'];
124 }
125 ?>
126 </span></li>
127 <li><strong><?php
128 $mem = $this->memory['usage'] ?? 0;
129 echo __('Current Memory usage', 'wp-memory-usage'); ?>:</strong> <span><?php echo $mem.__('MB', 'wp-memory-usage'); ?> </span><br>
130
131 <?PHP
132 if ($this->memory['percent']>=0) {
133 ?>
134 <div class="progressbar">
135 <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;">
136 <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;">
137 <div style="padding:2px;<?php echo $this->memory['percent_pos']; ?>"><?php echo $this->memory['percent']; ?>%</div>
138 </div>
139 </div>
140 </div>
141 <?php } ?>
142
143 <!-- START measure memory -->
144 <hr>
145 <?PHP
146 echo '<strong>'.__('Multiple Memory Measurement: Reload page and measure memory', 'wp-memory-usage').'</strong><br>';
147 echo __('You might switch off some plugins to check, which plugin consumes significant memory.', 'wp-memory-usage')."<br>";
148 $cst = $_GET["c"] ?? '';
149 $cst = htmlentities($cst);
150 $ac = $_GET["ac"] ?? '';
151 $ac = htmlentities($ac);
152
153 $wpmemoryusage_settings_str = get_option('wpmemoryusage_settings') ?? '';
154 $wpmemoryusage_settings = json_decode($wpmemoryusage_settings_str, TRUE);
155
156 $nonceCheck = wp_verify_nonce( ($_REQUEST['memusage'] ?? ''), "memusage" );
157 if ($nonceCheck || is_null($wpmemoryusage_settings)) {
158 $nomeas = $_GET["nomeas"] ?? '';
159 $validatedValue_nomeas = filter_input(INPUT_GET, 'nomeas', FILTER_VALIDATE_INT);
160 if ($validatedValue_nomeas && $nomeas>0) {
161 $wpmemoryusage_settings["nomeas"] = $nomeas;
162 } else if ($cst!="st" && $ac!="stop") {
163 $wpmemoryusage_settings["nomeas"] = 2;
164 }
165 if ($wpmemoryusage_settings["nomeas"]>2000) {
166 $wpmemoryusage_settings["nomeas"] = 2000;
167 }
168
169 $secrel = $_GET["secrel"] ?? '';
170 $validatedValue_secrel = filter_input(INPUT_GET, 'secrel', FILTER_VALIDATE_INT);
171 if ($validatedValue_secrel && $secrel>0) {
172 $wpmemoryusage_settings["secrel"] = $secrel;
173 } else if ($cst!="st" && $ac!="stop") {
174 $wpmemoryusage_settings["secrel"] = 1000;
175 }
176 $memOptSave = update_option('wpmemoryusage_settings', json_encode($wpmemoryusage_settings));
177 }
178
179 echo "<form action=".admin_url().">";
180 wp_nonce_field( "memusage", "memusage" );
181 echo __('Number of measuring points', 'wp-memory-usage').': <input type=number name=nomeas value="'.htmlentities($wpmemoryusage_settings["nomeas"]).'"><br>';
182 echo __('Milliseconds between page reloads', 'wp-memory-usage').': <input type=number name=secrel value="'.htmlentities($wpmemoryusage_settings["secrel"]).'"><br>';
183 echo '<input type=submit value="'.__('Store settings', 'wp-memory-usage').'">';
184 echo "</form><hr>";
185
186 # for test only
187 #$this->memory["wpmb"]= 30;
188
189 $z = 1;
190 $validatedValue_z = filter_input(INPUT_GET, 'meme', FILTER_VALIDATE_INT);
191 if ($validatedValue_z && $validatedValue_z>0) {
192 $z = $_GET["meme"];
193 }
194
195 if ($z==1) {
196 $memOptSave = update_option('wpmemoryusage_emopt', array());
197 }
198 $memOpt = get_option('wpmemoryusage_emopt', array());
199 $anzpoints = count($memOpt);
200 $now = time();
201 $summb = 0;
202 $anz = 0;
203
204 $ha = array();
205 if ($anzpoints>0) {
206 krsort($memOpt);
207 foreach ($memOpt as $key => $value) {
208 $value = intval($value + 0.5);
209 $ha[$value] = $ha[$value] ?? 0;
210 $ha[$value]++;
211 $summb += $value;
212 $anz++;
213 }
214 $av = $summb/$anz;
215 }
216
217 $colgreen = "#d9ead3";
218 $colred = "#f4cccc";
219
220 if ($cst=="st" && $wpmemoryusage_settings["nomeas"]==$anz) {
221 echo '<a href='.admin_url().'?c=st>'.__('Restart Measurement', 'wp-memory-usage').'</a><hr>';
222 } else if ($cst=="st" && $wpmemoryusage_settings["nomeas"]!=$anz) {
223 echo '<a href='.admin_url().'?ac=stop>'.__('Stop Measurement', 'wp-memory-usage').'</a><hr>';
224 } else {
225 echo '<a href='.admin_url().'?c=st>'.__('Start Measurement', 'wp-memory-usage').'</a><hr>';
226 }
227
228 $error_WP_MEMORY_LIMIT_not_set = __('WP_MEMORY_LIMIT not set', 'wp-memory-usage');
229 $error_WP_MAX_MEMORY_LIMIT_not_set = __('WP_MAX_MEMORY_LIMIT not set', 'wp-memory-usage');
230
231 if ($anz>0) {
232 echo "<table border=1 width=100%>";
233 $mp = round($anz/$wpmemoryusage_settings["nomeas"]*1000)/10;
234 if ($mp<100) { $colli = $colred; } else { $colli = $colgreen; }
235 echo "<tr bgcolor=$colli><td colspan=3>";
236 echo __('Number of measuring points', 'wp-memory-usage')." ";
237 if ($wpmemoryusage_settings["nomeas"]>0) {
238 echo $mp.__('% (', 'wp-memory-usage');
239 }
240 echo $anz;
241 echo " ".__('of', 'wp-memory-usage')." ";
242 echo $wpmemoryusage_settings["nomeas"];
243 if ($wpmemoryusage_settings["nomeas"]>0) {
244 echo __(')', 'wp-memory-usage');
245 }
246 echo "</td></tr>";
247
248 echo "<tr bgcolor=#cfe2f3><td>";
249 echo __('Used MB', 'wp-memory-usage');
250 echo "</td><td>";
251 if (empty($this->memory["wpmb"])) {
252 echo $error_WP_MEMORY_LIMIT_not_set;
253 } else {
254 echo sprintf(__('MB (max. %s)', 'wp-memory-usage'),$this->memory["wpmb"]. $this->memory["wpunity"]);
255 }
256 echo "</td><td>";
257 echo __('%MB', 'wp-memory-usage');
258 echo "</td></tr>";
259
260 $val = round($summb/$anz);
261 $valproz = -1;
262 if ($this->memory["wpmb"]>0) {
263 $valproz = round($val/$this->memory["wpmb"]*1000)/10;
264 }
265 if ($valproz>0 && $valproz<100) { $colli = $colgreen; } else { $colli = $colred; }
266 echo "<tr bgcolor=$colli><td>";
267 echo __('Average MB', 'wp-memory-usage');
268 echo "</td><td>";
269 echo $val;
270 echo "</td><td>";
271 if ($valproz>0) {
272 echo $valproz.__('%', 'wp-memory-usage');
273 } else {
274 echo $error_WP_MEMORY_LIMIT_not_set;
275 }
276 echo "</td></tr>";
277
278 $val = min($memOpt);
279 $valproz = -1;
280 if ($this->memory["wpmb"]>0) {
281 $valproz = round($val/$this->memory["wpmb"]*1000)/10;
282 }
283 if ($valproz>0 && $valproz<100) { $colli = $colgreen; } else { $colli = $colred; }
284 echo "<tr bgcolor=$colli><td>";
285 echo __('Min MB', 'wp-memory-usage');
286 echo "</td><td>";
287 echo $val;
288 echo "</td><td>";
289 if ($valproz>0) {
290 echo $valproz.__('%', 'wp-memory-usage');
291 } else {
292 echo $error_WP_MEMORY_LIMIT_not_set;
293 }
294 echo "</td></tr>";
295
296 $val = max($memOpt);
297 $valproz = -1;
298 if ($this->memory["wpmb"]>0) {
299 $valproz = round($val/$this->memory["wpmb"]*1000)/10;
300 }
301 if ($valproz>0 && $valproz<100) { $colli = $colgreen; } else { $colli = $colred; }
302 if ($valproz<100) { $colli = $colgreen; } else { $colli = $colred; }
303 echo "<tr bgcolor=$colli><td>";
304 echo __('Max MB', 'wp-memory-usage');
305 echo "</td><td>";
306 echo $val;
307 echo "</td><td>";
308 if ($valproz>0) {
309 echo $valproz.__('%', 'wp-memory-usage');
310 } else {
311 echo $error_WP_MEMORY_LIMIT_not_set;
312 }
313 echo "</td></tr>";
314
315 if (count($ha)>0) {
316 krsort($ha);
317 echo "<tr bgcolor=#cfe2f3><td>";
318 echo __('MB', 'wp-memory-usage');
319 echo "</td><td colspan=2>";
320 echo __('Occurrence', 'wp-memory-usage');
321 echo "</td></tr>";
322 foreach ($ha as $key => $value) {
323 $valproz = -1;
324 if ($this->memory["wpmb"]>0) {
325 $valproz = round($key/$this->memory["wpmb"]*1000)/10;
326 }
327 if ($valproz>0 && $valproz<100) { $colli = $colgreen; } else { $colli = $colred; }
328 echo "<tr bgcolor=$colli><td>";
329 echo $key;
330 echo "</td><td colspan=2>";
331 echo $value;
332 echo "</td></tr>";
333 }
334 }
335 echo "</table>";
336 }
337 if ($mem>0) {
338 $memOpt[$now] = $mem;
339 krsort($memOpt);
340 $memOpt = array_slice($memOpt, 0, $wpmemoryusage_settings["nomeas"], TRUE);
341 $memOptSave = update_option('wpmemoryusage_emopt', $memOpt);
342 }
343
344 if ($anzpoints < $wpmemoryusage_settings["nomeas"] && $cst=="st") {
345 $next = 1+$z;
346 echo "<script>";
347 echo 'setTimeout(function() {window.location.assign("'.admin_url().'?c=st&meme='.$next.'");}, '.$wpmemoryusage_settings["secrel"].');';
348 echo "</script>";
349 #echo '<a href="javascript:newDoc()">jump '.$next.'</a><hr>';
350 }
351
352 ?>
353 </span>
354 <!-- END measure memory -->
355 </li>
356 </ul>
357 <?php
358 }
359
360 public function add_dashboard() {
361 $servertime = date(__('d.m.Y, H:i:s', 'wp-memory-usage'));
362 wp_add_dashboard_widget( 'wp_memory_dashboard', __('Memory Overview', 'wp-memory-usage')."<br>".__('Servertime', 'wp-memory-usage').": ".$servertime, array (&$this, 'dashboard_output') );
363 }
364
365 private function formatWP_MEMORY_LIMIT($valin) { #WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT come with size and unity
366 $valin = $valin ?? '';
367 if (empty($valin)) {
368 return $valin;
369 }
370 if (preg_match("/G$/i", $valin)) {
371 # set in gigabyte
372 $valinTmp = preg_replace("/G$/i", "", $valin);
373 $valinNo = 1024 * $valinTmp;
374 $valin = $valinNo."M";
375 }
376 $size = strtolower(substr($valin, -1));
377 $number = (int) substr($valin, 0, -1);
378 $ret = Array();
379 if ($size=="k") { $ret["mb"] = ($number/1024); $ret["unity"] = "kB"; }
380 if ($size=="m") { $ret["mb"] = $number; $ret["unity"] = "MB"; }
381 if ($size=="g") { $ret["mb"] = ($number*1024); $ret["unity"] = "GB"; }
382 if ($size=="t") { $ret["mb"] = ($number*(1024*1024)); $ret["unity"] = "TB"; }
383 if ($size=="p") { $ret["mb"] = ($number*(1024*1024*1024)); $ret["unity"] = "PB"; }
384 return $ret;
385 }
386
387 private function get_ip_adress() {
388 if (isset($_SERVER[ 'SERVER_ADDR' ]) && !empty($_SERVER[ 'SERVER_ADDR' ])) {
389 $this->ipadr = $_SERVER[ 'SERVER_ADDR' ];
390 }
391 if (empty($this->ipadr) && isset($_SERVER[ 'LOCAL_ADDR' ]) && !empty($_SERVER[ 'LOCAL_ADDR' ])) {
392 $this->ipadr = $_SERVER[ 'LOCAL_ADDR' ];
393 }
394
395 if (!empty($_SERVER['SERVER_NAME'])) {
396 $this->servername = " (".$_SERVER['SERVER_NAME'].")";
397 }
398 }
399
400 public function add_footer($content) {
401 $this->check_memory_usage();
402 $content .= ' | '. __( 'WP Memory Limit:', 'wp-memory-usage'). ' ' . $this->memory['usage'] . ' ' . __( 'of', 'wp-memory-usage') . ' ' . $this->memory["wpmb"].$this->memory["wpunity"]. " (".$this->memory['percent']."%)";
403 $content .= ' | '. __( 'PHP Memory Limit:', 'wp-memory-usage'). ' ' . $this->memory['usage'] . ' ' . __( 'of', 'wp-memory-usage') . ' ' . $this->memory['phplimit'].$this->memory['phplimitunity']. " (".$this->memory['percentphp']."%)";
404 $content .= ' | '. __( 'IP-Address', 'wp-memory-usage') . " " . $this->servername. ': '.$this->ipadr;
405 $content .= ' | '. __( 'PHP', 'wp-memory-usage') . ": " . PHP_VERSION;
406 return $content;
407 }
408
409 }
410
411 // Start this plugin once all other plugins are fully loaded
412 function WP_Memory_Usage_action_plugins_loaded( $array ) {
413 return new wp_memory_usage();
414 };
415 add_action( 'plugins_loaded', 'WP_Memory_Usage_action_plugins_loaded', 10, 1 );
416 }