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