PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.9
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.9
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / dashboard-widgets / Adminify_Memory_Usage.php

Adminify_Memory_Usage.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.9, at Inc/dashboard-widgets/Adminify_Memory_Usage.php

225 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Inc\DashboardWidgets;
4
5 // no direct access allowed
6 if (!defined('ABSPATH')) exit;
7
8 /**
9 * System Info Dashboard Widget
10 *
11 * @return void
12 */
13 /**
14 * WPAdminify
15 *`
16 * @author Jewel Theme <support@jeweltheme.com>
17 */
18
19 class Adminify_Memory_Usage
20 {
21
22 public function __construct()
23 {
24
25 add_action('wp_dashboard_setup', [$this, 'jltwp_adminify_memory_usage_widget']);
26 add_action('admin_enqueue_scripts', [$this, 'jltwp_adminify_memory_usage_admin_css']);
27 }
28
29 // Register Memory Usage Dashboard Widget
30 public function jltwp_adminify_memory_usage_widget()
31 {
32 add_meta_box(
33 'jltwp_adminify_memory_usage',
34 __('WP Memory Usage - Adminify', 'adminify'),
35 [$this, 'jltwp_adminify_memory_usage_widget_details'],
36 'dashboard',
37 'side',
38 'high'
39 );
40 }
41
42 public function jltwp_adminify_memory_usage_admin_css()
43 {
44 $screen = get_current_screen();
45 if ($screen->id == 'dashboard') {
46 $my_site_space_css = '';
47 $my_site_space_css .= '';
48 echo '<style>' . wp_strip_all_tags($my_site_space_css) . '</style>';
49 }
50 }
51
52
53 public function jltwp_adminify_memory_usage_get_memory()
54 {
55
56 $memory['memory_limit'] = ini_get('memory_limit');
57 $memory['memory_usage'] = function_exists('memory_get_usage') ? round(memory_get_usage(), 2) : 0;
58
59 return $memory;
60 }
61
62 // Create the function to output the contents of our Dashboard Widget
63 public function jltwp_adminify_memory_usage_widget_details()
64 {
65
66 global $wpdb;
67 $dbname = $wpdb->dbname;
68
69 $phpversion = PHP_VERSION;
70
71 $memory = $this->jltwp_adminify_memory_usage_get_memory();
72 $memory_limit = $memory['memory_limit'];
73 $memory_usage = $memory['memory_usage'];
74
75 // Get Memory
76 if (!empty($memory_usage) && !empty($memory_limit)) {
77 $memory_percent = round((int)$memory_usage / (int)$memory_limit * 100, 0);
78 }
79
80 // Get Database Size
81 $result = $wpdb->get_results('SHOW TABLE STATUS', ARRAY_A);
82 $rows = count($result);
83 $dbsize = 0;
84
85 if ($wpdb->num_rows > 0) {
86 foreach ($result as $row) {
87 $dbsize += $row["Data_length"] + $row["Index_length"];
88 }
89 }
90
91 // PHP version, memory, database size and entire site usage (may include not WP items)
92 $topitems = array(
93 'PHP Version' => $phpversion . ' ' . (PHP_INT_SIZE * 8) . ' ' . __('Bit OS', 'adminify'),
94 'Memory' => size_format($memory_usage, 2) . __(' of ', 'adminify') . $memory_limit,
95 'Database' => size_format($dbsize, 2)
96 );
97
98 // Check if WP_CONTENT_DIR outside of base path (ABSPATH)
99 if (strpos(WP_CONTENT_DIR, ABSPATH) !== false) {
100 // WP_CONTENT_DIR in ABSPATH
101 $entire_site_size = $this->jltwp_adminify_memory_usage_dir_size(ABSPATH);
102 if ( $entire_site_size > 0 ) {
103 $topitems['Entire Site'] = $this->jltwp_adminify_memory_usage_dir_size_display([ $entire_site_size ]);
104 }
105 } else {
106 // WP_CONTENT_DIR outside ABSPATH
107 $entire_abs_size = $this->jltwp_adminify_memory_usage_dir_size(ABSPATH);
108 $entire_content_size = $this->jltwp_adminify_memory_usage_dir_size(WP_CONTENT_DIR);
109
110 if ( $entire_abs_size > 0 && $entire_content_size > 0 ) {
111 $topitems['Entire Site'] = $this->jltwp_adminify_memory_usage_dir_size_display([ $entire_abs_size, $entire_content_size ]);
112 }
113
114 }
115
116 foreach ($topitems as $name => $value) {
117 echo '<p class="halfspace"><span class="spacedark">' . $name . '</span>: ' . $value . '</p>';
118 }
119
120 echo '<div class="halfspace">
121 <p><span class="spacedark">' . __('Memory Used by ', 'adminify') . '</span></p>';
122
123 $uploads = wp_get_upload_dir(); // Get upload directory array without creating it
124
125 // WP Content and selected subfolders
126 $contents = array(
127 "wp-content" => WP_CONTENT_DIR,
128 "plugins" => WP_PLUGIN_DIR,
129 "themes" => get_theme_root(),
130 "uploads" => $uploads['basedir']
131 );
132
133 foreach ($contents as $name => $value) {
134
135 $name = __($name, 'adminify'); // Make translatable
136 if (false === (get_transient($value)))
137 echo '<span class="spacedark">' . $name . '</span>: ' . $this->jltwp_adminify_memory_usage_dir_size_display([$this->jltwp_adminify_memory_usage_dir_size($value)]) . '<br />';
138 else
139 echo '<span class="spacedark">' . $name . '</span>: ' . size_format(get_transient($value), 2) . '<br />';
140 }
141
142 echo '</div>';
143
144 // WordPress Admin and Includes folders
145 $wpadmin = ABSPATH . 'wp-admin/';
146 $wpincludes = ABSPATH . 'wp-includes/';
147
148 echo '<div class="halfspace">
149 <p><span class="spacedark">Other WP Folders</span></p>';
150
151 // wp-admin and wp-includes folders
152 $folders = array(
153 "wp-admin" => $wpadmin,
154 "wp-includes" => $wpincludes
155 );
156
157 foreach ($folders as $name => $value) {
158
159 $name = __($name, 'adminify'); // Make translatable
160
161 if (false === (get_transient($value)))
162 echo '<span class="spacedark">' . $name . '</span>: ' . $this->jltwp_adminify_memory_usage_dir_size_display([$this->jltwp_adminify_memory_usage_dir_size($value)]) . '<br />';
163 else
164 echo '<span class="spacedark">' . $name . '</span>: ' . size_format(get_transient($value), 2) . '<br />';
165 }
166
167 echo '</div>';
168 }
169
170 public function jltwp_adminify_memory_usage_dir_size_display($sizes = [])
171 {
172 $found_error = '';
173 $total_size = 0;
174
175 foreach ($sizes as $size) {
176 if (is_numeric($size)) {
177 $total_size += $size;
178 } else {
179 $found_error = $size;
180 break;
181 }
182 }
183
184 if (!empty($found_error)) {
185 if ($found_error == 'error_not_readable') {
186 return __('Failed to read', 'adminify');
187 }
188 }
189
190 return size_format($total_size, 2);
191 }
192
193 public function jltwp_adminify_memory_usage_dir_size($path)
194 {
195
196 // Add trailing slash to path if missing
197 if (substr($path, -1) != '/') $path .= '/';
198
199 $readable = @is_readable($path);
200
201 if ($readable) {
202
203 if (false === ($total_size = get_transient($path))) {
204
205 $total_size = 0;
206 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::FOLLOW_SYMLINKS)) as $file) {
207 if ( @is_readable($file) ) {
208 $total_size += $file->getSize();
209 }
210 }
211
212 // Set transient, expires in 1 hour
213 set_transient($path, $total_size, 1 * HOUR_IN_SECONDS);
214
215 return $total_size;
216 } else {
217
218 return $total_size;
219 }
220 } else {
221 return 'error_not_readable';
222 }
223 }
224 }
225