PluginProbe
WP-Memory-Usage / 2.2.0
WP-Memory-Usage v2.2.0
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 2.2.0, at wp-memory-usage.php

307 lines 13.9 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: 2.2.0
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 // Load translations early for both admin and frontend.
25 /*
26 function wpmu_load_textdomain() {
27 $mofile = plugin_dir_path( __FILE__ ) . 'languages/wp-memory-usage-' . get_locale() . '.mo';
28 load_textdomain( 'wp-memory-usage', $mofile );
29 }
30 add_action( 'plugins_loaded', 'wpmu_load_textdomain', 1 );
31 */
32 function wpmu_load_textdomain() {
33 load_plugin_textdomain( 'wp-memory-usage', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
34 }
35 add_action( 'init', 'wpmu_load_textdomain' );
36
37 const WPMU_LOG_FILE = "wpmu-log.cgi";
38 const WPMU_LOG_PATH = ABSPATH. '../logs/wpmu/';
39
40 class wp_memory_usage {
41 public $ipadr = "";
42 public $servername = "";
43 public $servernameout = "";
44 public $memory = array();
45
46
47 public function __construct() {
48 $this->get_ip_address();
49 #$this->load_textdomain();
50 #add_action( 'init', array( $this, 'load_textdomain' ) );
51 add_action( 'init', array ($this, 'check_limit') );
52 add_action( 'wp_dashboard_setup', array ($this, 'add_dashboard') );
53 if ( is_multisite() ) {
54 add_action( 'wp_network_dashboard_setup', array ($this, 'add_dashboard') );
55 }
56 add_filter( 'admin_footer_text', array ($this, 'add_footer') );
57 }
58
59 private function getinput($fieldkey, $default="") {
60 #var_Dump($_GET);
61 if (
62 ! isset($_GET['_wpnonce']) ||
63 ! wp_verify_nonce( sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'memusage' )
64 ) {
65 #echo "NONCE FAILED for GET $fieldkey<hr>";
66 return ""; #wp_die('Ung�ltiger Nonce � Sicherheits�berpr�fung fehlgeschlagen.');
67 }
68 #echo "NONCE OK for GET $fieldkey<hr>";
69 return sanitize_text_field(wp_unslash(($_GET[$fieldkey] ?? $default)));
70 }
71
72 private function getserverinput($fieldkey, $default="") {
73 return sanitize_text_field(wp_unslash(($_SERVER[$fieldkey] ?? $default)));
74 }
75
76 #public function load_textdomain() {
77 # load_plugin_textdomain( 'wp-memory-usage', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
78 #}
79
80 public function check_limit() {
81 $this->memory['phplimit'] = "";
82 $this->memory['phplimitunity'] = 'MB';
83 if (!is_null(ini_get('memory_limit'))) {
84 $phplimit_memory = ini_get('memory_limit');
85 if (preg_match("/G$/i", $phplimit_memory)) {
86 # set in gigabyte
87 $phplimit_memory = 1024 * preg_replace("/G$/i", "", $phplimit_memory);
88 }
89 $this->memory['phplimit'] = (int) $phplimit_memory;
90 }
91 $ret = $this->formatWP_MEMORY_LIMIT(WP_MEMORY_LIMIT);
92 $this->memory["wpmb"] = $ret["mb"] ?? '';
93 $this->memory["wpunity"] = $ret["unity"] ?? '';
94
95 $ret = $this->formatWP_MEMORY_LIMIT(WP_MAX_MEMORY_LIMIT);
96 $this->memory["wpmaxmb"] = $ret["mb"] ?? '';
97 $this->memory["wpmaxunity"] = $ret["unity"] ?? '';
98 }
99
100 public function check_memory_usage() {
101 $this->memory['usage'] = function_exists('memory_get_peak_usage') ? round(memory_get_peak_usage(true) / 1024 / 1024, 2) : 0;
102 if ( !empty($this->memory['usage'])) {
103 $this->memory['percent'] = -1;
104 if (!empty($this->memory['wpmb']) && ($this->memory['wpmb']!=0)) {
105 $this->memory['percent'] = round($this->memory['usage'] /$this->memory["wpmb"] * 100, 0);
106 }
107
108 $this->memory['percentphp'] = -1;
109 if (!empty($this->memory['phplimit']) && ($this->memory['phplimit']!=0)) {
110 $this->memory['percentphp'] = round ($this->memory['usage'] / $this->memory["phplimit"] * 100, 0);
111 }
112
113 //If the bar is too small we move the text outside
114 $this->memory['percent_pos'] = '';
115 //In case we are in our limits take the admin color
116 $this->memory['color'] = '';
117 if ($this->memory['percent'] > 80) $this->memory['color'] = 'background: #E66F00;';
118 if ($this->memory['percent'] > 95) $this->memory['color'] = 'background: red;';
119 if ($this->memory['percent'] < 10) $this->memory['percent_pos'] = 'margin-right: -30px; color: #444;';
120 $this->memory['percentwidth'] = $this->memory['percent'];
121 if ($this->memory['percent']>100) {
122 $this->memory['percentwidth'] = 100;
123 }
124 }
125 }
126
127 public function dashboard_output() {
128 $this->check_memory_usage();
129 ?>
130 <ul>
131 <li><strong><?php echo esc_html(__('PHP Version', 'wp-memory-usage')); ?>:</strong> <span><?php
132 echo esc_html(PHP_VERSION); ?>&nbsp;/&nbsp;<?php echo esc_html((PHP_INT_SIZE * 8) . __('Bit OS', 'wp-memory-usage'));
133
134 if (!is_null(ini_get('max_execution_time'))) {
135 $max_execution_time = (int) ini_get('max_execution_time'). __('sec', 'wp-memory-usage');
136 echo " / ".esc_html(__('Max execution time: ', 'wp-memory-usage')).' '.esc_html($max_execution_time);
137 }
138
139
140 ?></span></li>
141 <li><strong><?php echo esc_html(__('Memory limits', 'wp-memory-usage')); ?>:</strong> <span>
142 <?php
143 if (!empty($this->memory["wpmb"])) {
144 echo esc_html(__('Wordpress', 'wp-memory-usage').' '.$this->memory["wpmb"]. $this->memory["wpunity"])." / ";
145 }
146 if ($this->memory["wpmaxmb"]!="" && ($this->memory["wpmb"]!=$this->memory["wpmaxmb"])) {
147 echo esc_html(__('Wordpress-Admin', 'wp-memory-usage').' '.$this->memory["wpmaxmb"]. $this->memory["wpmaxunity"])." / ";
148 }
149 if ($this->memory['phplimit']!="") {
150 echo esc_html(__('PHP ', 'wp-memory-usage').' '.$this->memory['phplimit'].$this->memory['phplimitunity']);
151 }
152 ?>
153 </span></li>
154 <li><strong><?php
155 $mem = $this->memory['usage'] ?? 0;
156 echo esc_html(__('Current Memory usage', 'wp-memory-usage')); ?>:</strong> <span><?php echo esc_html($mem.__('MB', 'wp-memory-usage')); ?> </span><br>
157
158 <?php
159 if ($this->memory['percent']>=0) {
160 ?>
161 <div class="progressbar">
162 <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;">
163 <div class="button-primary" style="width: <?php echo esc_html($this->memory['percentwidth']); ?>%;<?php echo esc_html($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;">
164 <div style="padding:2px;<?php echo esc_html($this->memory['percent_pos']); ?>"><?php echo esc_html($this->memory['percent']); ?>%</div>
165 </div>
166 </div>
167 </div>
168 <?php }
169
170 $settings_url = admin_url( 'options-general.php?page=wpmu-memory-alerts' );
171 echo '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings & Monitor', 'wp-memory-usage' ) . '</a>';
172 #echo $settings_link;
173
174 // ── Neuester Digest ────────────────────────────────────────────
175 if ( class_exists( 'WPMU_Threshold_Alerts' ) ) {
176 $digest_files = glob( ABSPATH . '../logs/wpmu/digest_*.cgibak' ) ?: array();
177 if ( ! empty( $digest_files ) ) {
178 // neueste Datei
179 usort( $digest_files, fn( $a, $b ) => filemtime( $b ) - filemtime( $a ) );
180 $latest = $digest_files[0];
181 // phpcs:disable WordPress.WP.AlternativeFunctions
182 $fh = @fopen( $latest, 'rb' );
183 $digest_data = null;
184 if ( $fh ) {
185 flock( $fh, LOCK_SH );
186 $digest_data = json_decode( fgets( $fh ), true );
187 flock( $fh, LOCK_UN );
188 fclose( $fh );
189 }
190 // phpcs:enable WordPress.WP.AlternativeFunctions
191 if ( is_array( $digest_data ) && isset( $digest_data['status'] ) ) {
192 $dw = (int) ( $digest_data['status']['warn'] ?? 0 );
193 $dd = (int) ( $digest_data['status']['danger'] ?? 0 );
194 $dc = (int) ( $digest_data['status']['critical'] ?? 0 );
195 $fn_label = basename( $latest );
196 $raw_ts = str_replace( array( 'digest_', '.cgibak' ), '', $fn_label );
197 $dt_obj = DateTime::createFromFormat( 'Y-m-d-H-i-s', $raw_ts );
198 $ts_label = $dt_obj ? wp_date( get_option('date_format') . ', ' . get_option('time_format'), $dt_obj->getTimestamp() ) : $fn_label;
199 echo '<hr style="margin:8px 0;">';
200 echo '<strong>' . esc_html__( 'Latest Digest', 'wp-memory-usage' ) . ':</strong> <span style="font-size:11px;color:#888;">' . esc_html( $ts_label ) . '</span><br>';
201 #$any_alert = ( $dw + $dd + $dc ) > 0;
202 echo '<span style="display:inline-flex;gap:6px;margin-top:4px;flex-wrap:wrap;">';
203 echo '<span style="background:' . esc_attr( $dw > 0 ? '#f0ad4e' : '#e8e8e8' ) . ';color:' . esc_attr( $dw > 0 ? '#3d2b00' : '#888' ) . ';padding:2px 8px;border-radius:10px;font-size:12px;font-weight:700;">âš  ' . esc_html($dw) . '</span>';
204 echo '<span style="background:' . esc_attr( $dd > 0 ? '#d9534f' : '#e8e8e8' ) . ';color:' . esc_attr( $dd > 0 ? '#fff' : '#888' ) . ';padding:2px 8px;border-radius:10px;font-size:12px;font-weight:700;">🔴 ' . esc_html($dd) . '</span>';
205 echo '<span style="background:' . esc_attr( $dc > 0 ? '#8B0000' : '#e8e8e8' ) . ';color:' . esc_attr( $dc > 0 ? '#fff' : '#888' ) . ';padding:2px 8px;border-radius:10px;font-size:12px;font-weight:700;">🆘 ' . esc_html($dc) . '</span>';
206 echo '</span>';
207 $digest_url = admin_url( 'options-general.php?page=wpmu-memory-alerts&tab=digest' );
208 echo ' &nbsp;<a href="' . esc_url( $digest_url ) . '" style="font-size:11px;">' . esc_html__( '→ Digest', 'wp-memory-usage' ) . '</a>';
209 }
210 }
211 }
212 ?>
213
214
215
216 </li>
217 </ul>
218 <?php
219 }
220
221 public function add_dashboard() {
222 #$servertime = gmdate(__('d.m.Y, H:i:s', 'wp-memory-usage'));
223 $servertime = wp_date( get_option('date_format') . ', ' . get_option('time_format') );
224 wp_add_dashboard_widget( 'wp_memory_dashboard', __('Memory Overview', 'wp-memory-usage')."<br>".__('Servertime', 'wp-memory-usage').": ".$servertime, array ($this, 'dashboard_output') );
225 }
226
227 private function formatWP_MEMORY_LIMIT($valin) { #WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT come with size and unity
228 $valin = $valin ?? '';
229 if (empty($valin)) {
230 return $valin;
231 }
232 if (preg_match("/G$/i", $valin)) {
233 # set in gigabyte
234 $valinTmp = preg_replace("/G$/i", "", $valin);
235 $valinNo = 1024 * $valinTmp;
236 $valin = $valinNo."M";
237 }
238 $size = strtolower(substr($valin, -1));
239 $number = (int) substr($valin, 0, -1);
240 $ret = array();
241 if ($size=="k") { $ret["mb"] = ($number/1024); $ret["unity"] = "kB"; }
242 if ($size=="m") { $ret["mb"] = $number; $ret["unity"] = "MB"; }
243 if ($size=="g") { $ret["mb"] = ($number*1024); $ret["unity"] = "GB"; }
244 if ($size=="t") { $ret["mb"] = ($number*(1024*1024)); $ret["unity"] = "TB"; }
245 if ($size=="p") { $ret["mb"] = ($number*(1024*1024*1024)); $ret["unity"] = "PB"; }
246 return $ret;
247 }
248
249 private function get_ip_address() {
250 $get_SERVER_ADDR = $this->getserverinput('SERVER_ADDR');
251 #if (isset($_SERVER[ 'SERVER_ADDR' ]) && !empty($_SERVER[ 'SERVER_ADDR' ])) {
252 if (!empty($get_SERVER_ADDR)) {
253 $this->ipadr = $get_SERVER_ADDR;
254 #$this->ipadr = $_SERVER[ 'SERVER_ADDR' ];
255 }
256 $get_LOCAL_ADDR = $this->getserverinput('LOCAL_ADDR');
257 if (empty($this->ipadr) && !empty($get_LOCAL_ADDR)) {
258 #if (empty($this->ipadr) && isset($_SERVER[ 'LOCAL_ADDR' ]) && !empty($_SERVER[ 'LOCAL_ADDR' ])) {
259 $this->ipadr = $get_LOCAL_ADDR;
260 #$this->ipadr = $_SERVER[ 'LOCAL_ADDR' ];
261 }
262
263 $get_SERVER_NAME = $this->getserverinput('SERVER_NAME');
264 if (!empty($get_SERVER_NAME)) {
265 $this->servername = $get_SERVER_NAME;
266 $this->servernameout = " (".$get_SERVER_NAME.")";
267 }
268 }
269
270 public function add_footer($content) {
271 $this->check_memory_usage();
272 $content .= ' | '. __( 'WP Memory Limit:', 'wp-memory-usage'). ' ' . esc_html($this->memory['usage']) . ' ' . __( 'of', 'wp-memory-usage') . ' ' . esc_html($this->memory["wpmb"]).esc_html($this->memory["wpunity"]). " (".esc_html($this->memory['percent'])."%)";
273 $content .= ' | '. __( 'PHP Memory Limit:', 'wp-memory-usage'). ' ' . esc_html($this->memory['usage']) . ' ' . __( 'of', 'wp-memory-usage') . ' ' . esc_html($this->memory['phplimit']).esc_html($this->memory['phplimitunity']). " (".esc_html($this->memory['percentphp'])."%)";
274 $content .= ' | '. __( 'IP-Address', 'wp-memory-usage') . " " . esc_html($this->servernameout). ': '.esc_html($this->ipadr);
275 $content .= ' | '. __( 'PHP', 'wp-memory-usage') . ": " . esc_html(PHP_VERSION);
276 return $content;
277 }
278
279 }
280
281 // Start this plugin once all other plugins are fully loaded
282
283
284
285 if ( file_exists( plugin_dir_path( __FILE__ ) . 'includes/threshold-alerts.php' ) ) { #load WPMU_Threshold_Alerts
286 require_once plugin_dir_path( __FILE__ ) . 'includes/threshold-alerts.php';
287 if ( class_exists( 'WPMU_Threshold_Alerts' ) ) {
288 add_action( 'plugins_loaded', function() {
289 WPMU_Threshold_Alerts::init(25, 100, WPMU_LOG_PATH);
290 }, 2 ); // Priority 2: nach wpmu_load_textdomain (Priority 1)
291 }
292 }
293
294 if ( is_admin() ) {
295 function WP_Memory_Usage_action_plugins_loaded( $array ) {
296 return new wp_memory_usage();
297 };
298 add_action( 'plugins_loaded', 'WP_Memory_Usage_action_plugins_loaded', 10, 1 );
299 }
300
301 // Add Settings link in Plugins list
302 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function( $links ) { # insert link in plugin list
303 $settings_url = admin_url( 'options-general.php?page=wpmu-memory-alerts' );
304 $settings_link = '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings & Monitor', 'wp-memory-usage' ) . '</a>';
305 $links[] = $settings_link;
306 return $links;
307 } );