Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multisite: Show upload space usage for a subsite #8303

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions src/wp-admin/includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -1628,38 +1628,45 @@ function wp_dashboard_primary_output( $widget_id, $feeds ) {
}

/**
* Displays file upload quota on dashboard.
* Displays storage space information of a blog on the WordPress dashboard.
*
* Runs on the {@see 'activity_box_end'} hook in wp_dashboard_right_now().
*
* This function checks the space used and the space allowed for uploads on the site,
* then displays this information in the dashboard with a link to manage uploads.
* It also includes a visual warning if the space usage exceeds 70% of the allowed quota.
*
* @since 3.0.0
*
* @return true|void True if not multisite, user can't upload files, or the space check option is disabled.
* @return true|void True if not multisite, user can't upload files; else it outputs the storage space information directly in the dashboard.
*
* @see get_space_usage() for retrieving the current space usage information in formatted string.
*/
function wp_dashboard_quota() {
if ( ! is_multisite() || ! current_user_can( 'upload_files' )
|| get_site_option( 'upload_space_check_disabled' )
) {
if ( ! is_multisite() || ! current_user_can( 'upload_files' ) ) {
return true;
}

$quota = get_space_allowed();
$used = get_space_used();
$used = get_space_used();

if ( $used > $quota ) {
$percentused = '100';
} else {
$percentused = ( $used / $quota ) * 100;
$used_class = '';
if ( ! get_site_option( 'upload_space_check_disabled' ) ) {
$quota = get_space_allowed();
if ( $used > $quota ) {
$percentused = '100';
} else {
$percentused = ( $used / $quota ) * 100;
$used_class = ( $percentused >= 70 ) ? ' warning' : '';
$percentused = number_format( $percentused );
}
}

$used_class = ( $percentused >= 70 ) ? ' warning' : '';
$used = round( $used, 2 );
$percentused = number_format( $percentused );

$used = round( $used, 2 );
?>
<h3 class="mu-storage"><?php _e( 'Storage Space' ); ?></h3>
<div class="mu-storage">
<ul>
<ul>
<?php if ( isset( $quota ) ) : ?>
<li class="storage-count">
<?php
$text = sprintf(
Expand All @@ -1675,18 +1682,14 @@ function wp_dashboard_quota() {
__( 'Manage Uploads' )
);
?>
</li><li class="storage-count <?php echo $used_class; ?>">
</li>
<?php endif; ?>
<li class="storage-count <?php echo $used_class; ?>">
<?php
$text = sprintf(
/* translators: 1: Number of megabytes, 2: Percentage. */
__( '%1$s MB (%2$s%%) Space Used' ),
number_format_i18n( $used, 2 ),
$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,
get_space_usage(),
/* translators: Hidden accessibility text. */
__( 'Manage Uploads' )
);
Expand Down
2 changes: 2 additions & 0 deletions src/wp-admin/includes/ms-admin-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

// Media hooks.
add_filter( 'wp_handle_upload_prefilter', 'check_upload_size' );
add_action( 'post-upload-ui', 'display_space_usage' );
add_action( 'load-upload.php', 'upload_page_notice_display_space_usage_notice' );

// User hooks.
add_action( 'user_admin_notices', 'new_user_email_admin_notice' );
Expand Down
60 changes: 45 additions & 15 deletions src/wp-admin/includes/ms.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,52 @@ function upload_is_user_over_quota( $display_message = true ) {
}

/**
* Displays the amount of disk space used by the current site. Not used in core.
* Displays the space usage details by the current site.
*
* This function echoes the space usage information retrieved from the
* `get_space_usage` function, wrapped in a `<strong>` HTML tag for emphasis.
*
* @return void This function does not return a value; it outputs the space usage directly.
*
* @see get_space_usage() for the function that retrieves the space usage information.
*
* @since MU (3.0.0)
*/
function display_space_usage() {
$space_allowed = get_space_allowed();
$space_used = get_space_used();
echo '<strong>' . get_space_usage() . '</strong>';
}

$percent_used = ( $space_used / $space_allowed ) * 100;
/**
* Displays a notice in the WordPress admin area showing the current storage space usage.
*
* This function outputs an HTML notice containing the storage space usage information.
* The notice is styled as a success message and is dismissible. It uses the `display_space_usage`
* function to retrieve and display the actual storage usage details.
*
* @see get_space_usage() for the function that retrieves the space usage information.
*
* @hook admin_notices
*
* @return void This function does not return a value; it directly outputs HTML.
*/
function display_space_usage_notice() {
echo '<div class="notice notice-success is-dismissible">';
echo '<p><strong>' . esc_html__( 'Storage Space' ) . ':</strong> ';
echo get_space_usage();
echo '</p></div>';
}

$space = size_format( $space_allowed * MB_IN_BYTES );
?>
<strong>
<?php
/* translators: Storage space that's been used. 1: Percentage of used space, 2: Total space allowed in megabytes or gigabytes. */
printf( __( 'Used: %1$s%% of %2$s' ), number_format( $percent_used ), $space );
?>
</strong>
<?php
/**
* Hooks into the `load-upload.php` action to add the
* `display_space_usage_notice` function to the `admin_notices` hook.
*
* @hook load-upload.php
*
* @return void This function does not return a value; it only adds an action to the `admin_notices` hook.
*/
function upload_page_notice_display_space_usage_notice() {
// Add the admin_notices action only for the upload.php page
add_action( 'admin_notices', 'display_space_usage_notice' );
}

/**
Expand Down Expand Up @@ -295,7 +322,6 @@ function fix_import_form_size( $size ) {
function upload_space_setting( $id ) {
switch_to_blog( $id );
$quota = get_option( 'blog_upload_space' );
restore_current_blog();

if ( ! $quota ) {
$quota = '';
Expand All @@ -313,10 +339,14 @@ function upload_space_setting( $id ) {
/* translators: Hidden accessibility text. */
_e( 'Size in megabytes' );
?>
</span> <?php _e( 'MB (Leave blank for network default)' ); ?></span>
</span><?php printf( '(Leave blank for network default: %s MB)', get_site_option( 'blog_upload_space' ) ); ?></span>
<p class="description blog-upload-space-count">
<?php echo get_space_usage(); ?>
</p>
</td>
</tr>
<?php
restore_current_blog();
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/wp-includes/ms-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,41 @@ function upload_size_limit_filter( $size ) {
return min( $size, $max_fileupload_in_bytes, get_upload_space_available() );
}

/**
* Retrieves the space usage details for the site.
*
* This function calculates and returns the space used for uploads in the site.
* If the upload space check is disabled, it returns a simple message showing the
* space used. Otherwise, it provides more detailed information including the
* percentage of space used and the total space allowed.
*
* @return string Formatted string indicating space usage. The format depends on
* whether the upload space check is enabled or not.
*/
function get_space_usage() {
$space_used = get_space_used();

if ( '1' === get_site_option( 'upload_space_check_disabled' ) ) {
return sprintf(
/* translators: 1: Number of megabytes */
__( '%s MB Space Used' ),
number_format_i18n( $space_used, 2 )
);
} else {
$space_allowed = get_space_allowed();
$percent_used = ( $space_used / $space_allowed ) * 100;
$space = size_format( $space_allowed * MB_IN_BYTES );

/* translators: Storage space that's been used. 1: Used space in MB 2: Percentage of used space, 3: Total space allowed in megabytes or gigabytes. */
return sprintf(
__( 'Used: %1$s (%2$s%%) of %3$s' ),
number_format_i18n( $space_used, 2 ),
number_format( $percent_used ),
$space
);
}
}

/**
* Determines whether or not we have a large network.
*
Expand Down
Loading