PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.7
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.7
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.7, at Inc/dashboard-widgets/Adminify_Memory_Usage.php

214 lines 5.8 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 $topitems['Entire Site'] = $this->jltwp_adminify_memory_usage_dir_size_display([$this->jltwp_adminify_memory_usage_dir_size(ABSPATH)]);
102 } else {
103 // WP_CONTENT_DIR outside ABSPATH
104 $topitems['Entire Site'] = $this->jltwp_adminify_memory_usage_dir_size_display([$this->jltwp_adminify_memory_usage_dir_size(ABSPATH), $this->jltwp_adminify_memory_usage_dir_size(WP_CONTENT_DIR)]);
105 }
106
107 foreach ($topitems as $name => $value) {
108 echo '<p class="halfspace"><span class="spacedark">' . $name . '</span>: ' . $value . '</p>';
109 }
110
111 echo '<div class="halfspace">
112 <p><span class="spacedark">' . __('Memory Used by ', 'adminify') . '</span></p>';
113
114 $uploads = wp_get_upload_dir(); // Get upload directory array without creating it
115
116 // WP Content and selected subfolders
117 $contents = array(
118 "wp-content" => WP_CONTENT_DIR,
119 "plugins" => WP_PLUGIN_DIR,
120 "themes" => get_theme_root(),
121 "uploads" => $uploads['basedir']
122 );
123
124 foreach ($contents as $name => $value) {
125
126 $name = __($name, 'adminify'); // Make translatable
127 if (false === (get_transient($value)))
128 echo '<span class="spacedark">' . $name . '</span>: ' . $this->jltwp_adminify_memory_usage_dir_size_display([$this->jltwp_adminify_memory_usage_dir_size($value)]) . '<br />';
129 else
130 echo '<span class="spacedark">' . $name . '</span>: ' . size_format(get_transient($value), 2) . '<br />';
131 }
132
133 echo '</div>';
134
135 // WordPress Admin and Includes folders
136 $wpadmin = ABSPATH . 'wp-admin/';
137 $wpincludes = ABSPATH . 'wp-includes/';
138
139 echo '<div class="halfspace">
140 <p><span class="spacedark">Other WP Folders</span></p>';
141
142 // wp-admin and wp-includes folders
143 $folders = array(
144 "wp-admin" => $wpadmin,
145 "wp-includes" => $wpincludes
146 );
147
148 foreach ($folders as $name => $value) {
149
150 $name = __($name, 'adminify'); // Make translatable
151
152 if (false === (get_transient($value)))
153 echo '<span class="spacedark">' . $name . '</span>: ' . $this->jltwp_adminify_memory_usage_dir_size_display([$this->jltwp_adminify_memory_usage_dir_size($value)]) . '<br />';
154 else
155 echo '<span class="spacedark">' . $name . '</span>: ' . size_format(get_transient($value), 2) . '<br />';
156 }
157
158 echo '</div>';
159 }
160
161 public function jltwp_adminify_memory_usage_dir_size_display($sizes = [])
162 {
163 $found_error = '';
164 $total_size = 0;
165
166 foreach ($sizes as $size) {
167 if (is_numeric($size)) {
168 $total_size += $size;
169 } else {
170 $found_error = $size;
171 break;
172 }
173 }
174
175 if (!empty($found_error)) {
176 if ($found_error == 'error_not_readable') {
177 return __('Failed to read', 'adminify');
178 }
179 }
180
181 return size_format($total_size, 2);
182 }
183
184 public function jltwp_adminify_memory_usage_dir_size($path)
185 {
186
187 // Add trailing slash to path if missing
188 if (substr($path, -1) != '/') $path .= '/';
189
190 $readable = @is_readable($path);
191
192 if ($readable) {
193
194 if (false === ($total_size = get_transient($path))) {
195
196 $total_size = 0;
197 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::FOLLOW_SYMLINKS)) as $file) {
198 $total_size += $file->getSize();
199 }
200
201 // Set transient, expires in 1 hour
202 set_transient($path, $total_size, 1 * HOUR_IN_SECONDS);
203
204 return $total_size;
205 } else {
206
207 return $total_size;
208 }
209 } else {
210 return 'error_not_readable';
211 }
212 }
213 }
214