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

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