Created
April 18, 2017 16:10
-
-
Save panoslyrakis/1d7ea3e1c25bf3b2c37a70c2ecef0bd2 to your computer and use it in GitHub Desktop.
Displays allowed and used space in GB or MB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: WPMUDEV Dashboard Quota | |
| * Plugin URI: https://premium.wpmudev.org/ | |
| * Description: Displays allowed and used space in GB or MB | |
| * Version: 1.0.0 | |
| * Author: Panos Lyrakis @ WPMUDEV | |
| * Author URI: https://premium.wpmudev.org/profile/panoskatws | |
| * License: GPL-2.0+ | |
| * License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
| */ | |
| if( !class_exists( 'WPMUDEV_Dashboard_Quota' ) ){ | |
| class WPMUDEV_Dashboard_Quota{ | |
| private static $_instance = null; | |
| public static function get_instance() { | |
| if ( is_null( self::$_instance ) ) { | |
| self::$_instance = new WPMUDEV_Dashboard_Quota(); | |
| } | |
| return self::$_instance; | |
| } | |
| private function __construct() { | |
| add_filter( 'dashboard_glance_items', array( $this, 'dashboard_glance_items' ), 10 ); | |
| add_action( 'activity_box_end', array( $this, 'activity_box_end' ), 10 ); | |
| add_action( 'admin_head', array( $this, 'hide_default_quota' ), 10 ); | |
| } | |
| public function dashboard_glance_items( $elements ){ | |
| $elements[] = self::display_quota(); | |
| return $elements; | |
| } | |
| public function activity_box_end(){ | |
| echo self::display_quota(); | |
| } | |
| public static function display_quota(){ | |
| if ( !is_multisite() || !current_user_can( 'upload_files' ) || get_site_option( 'upload_space_check_disabled' ) ) | |
| return true; | |
| $quota = get_space_allowed(); | |
| $used = get_space_used(); | |
| $quota_str = self::format_space( $quota ); | |
| $used_str = self::format_space( $used ); | |
| if ( $used > $quota ) | |
| $percentused = '100'; | |
| else | |
| $percentused = ( $used / $quota ) * 100; | |
| $used_class = ( $percentused >= 70 ) ? ' warning' : ''; | |
| $used = round( $used, 2 ); | |
| $percentused = number_format( $percentused ); | |
| ob_start(); | |
| ?> | |
| <h3 class="wpmudev_mu-storage"><?php _e( 'Storage Space' ); ?></h3> | |
| <div class="wpmudev_mu-storage"> | |
| <ul> | |
| <li class="storage-count"> | |
| <?php $text = sprintf( | |
| /* translators: number of megabytes */ | |
| __( '%s Space Allowed' ), | |
| $quota_str | |
| ); | |
| printf( | |
| '<a href="%1$s">%2$s <span class="screen-reader-text">(%3$s)</span></a>', | |
| esc_url( admin_url( 'upload.php' ) ), | |
| $text, | |
| __( 'Manage Uploads' ) | |
| ); ?> | |
| </li><li class="storage-count <?php echo $used_class; ?>"> | |
| <?php $text = sprintf( | |
| /* translators: 1: number of megabytes, 2: percentage */ | |
| __( '%1$s (%2$s%%) Space Used' ), | |
| $used_str, | |
| $percentused | |
| ); | |
| printf( | |
| '<a href="%1$s" class="musublink">%2$s <span class="screen-reader-text">(%3$s)</span></a>', | |
| esc_url( admin_url( 'upload.php' ) ), | |
| $text, | |
| __( 'Manage Uploads' ) | |
| ); ?> | |
| </li> | |
| </ul> | |
| </div> | |
| <?php | |
| return ob_get_clean(); | |
| } | |
| public static function format_space( $space ){ | |
| $format = __( ' MB' ); | |
| if( $space > 1024 ){ | |
| $space = $space / 1024; | |
| $format = __( ' GB' ); | |
| } | |
| return number_format_i18n( $space, 2 ) . $format; | |
| } | |
| public function hide_default_quota(){ | |
| $screen = get_current_screen(); | |
| if( $screen->id != 'dashboard' ){ | |
| return; | |
| } | |
| ?> | |
| <style> | |
| .mu-storage{ | |
| display: none; | |
| } | |
| </style> | |
| <?php | |
| } | |
| } | |
| add_action( 'plugins_loaded', function(){ | |
| $GLOBALS['WPMUDEV_Dashboard_Quota'] = WPMUDEV_Dashboard_Quota::get_instance(); | |
| }, 10 ); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not recommended, it's mostly for experimenting