Multisite: Rework the upload space usage tracking code so as to be fully pluggable.

* Moves some admin only functions into wp-admin/includes/ms.php from wp-includes/ms-functions.php
 * Reworked the variable naming to be more in line with the Coding Standards
 * Introduced a new get_space_used() function instead of calculating it in multiple places.

Fixes #21181 props dllh and jkudish for inital work on this.


git-svn-id: http://core.svn.wordpress.org/trunk@21387 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2012-08-01 13:52:06 +00:00
parent 29f2b5b903
commit 4443baaab9
3 changed files with 68 additions and 80 deletions

View File

@ -1097,7 +1097,7 @@ function wp_dashboard_quota() {
return true;
$quota = get_space_allowed();
$used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
$used = get_space_used();
if ( $used > $quota )
$percentused = '100';

View File

@ -25,9 +25,8 @@ function check_upload_size( $file ) {
if ( defined( 'WP_IMPORTING' ) )
return $file;
$space_allowed = 1048576 * get_space_allowed();
$space_used = get_dirsize( BLOGUPLOADDIR );
$space_left = $space_allowed - $space_used;
$space_left = get_upload_space_available();
$file_size = filesize( $file['tmp_name'] );
if ( $space_left < $file_size )
$file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) );
@ -310,28 +309,48 @@ function get_upload_space_available() {
if ( get_site_option( 'upload_space_check_disabled' ) )
return $space_allowed;
$dir_name = trailingslashit( BLOGUPLOADDIR );
if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) )
return $space_allowed;
$space_used = get_space_used() * 1024 * 1024;
$dir = dir( $dir_name );
$size = 0;
while ( $file = $dir->read() ) {
if ( $file != '.' && $file != '..' ) {
if ( is_dir( $dir_name . $file) ) {
$size += get_dirsize( $dir_name . $file );
} else {
$size += filesize( $dir_name . $file );
}
}
}
$dir->close();
if ( ( $space_allowed - $size ) <= 0 )
if ( ( $space_allowed - $space_used ) <= 0 )
return 0;
return $space_allowed - $size;
return $space_allowed - $space_used;
}
/**
* Check whether a blog has used its allotted upload space.
*
* @since MU
*
* @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
* @return int
*/
function upload_is_user_over_quota( $echo = true ) {
if ( get_site_option( 'upload_space_check_disabled' ) )
return false;
$space_allowed = get_space_allowed();
if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
$space_allowed = 10;// Default space allowed is 10 MB
$space_used = get_space_used();
if ( ($space_allowed - $space_used ) < 0 ) {
if ( $echo )
_e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' );
return true;
} else {
return false;
}
}
function get_space_used() {
// Allow for an alternative way of tracking storage space used
$space_used = apply_filters( 'pre_get_space_used', false );
if ( false === $space_used )
$space_used = get_dirsize( BLOGUPLOADDIR );
return $space_used;
}
/**
@ -352,24 +371,44 @@ function get_space_allowed() {
}
function display_space_usage() {
$space = get_space_allowed();
$used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
$space_allowed = get_space_allowed();
$space_used = get_space_used();
$percentused = ( $used / $space ) * 100;
$percent_used = ( $space_used / $space_allowed ) * 100;
if ( $space > 1000 ) {
$space = number_format( $space / 1024 );
if ( $space_allowed > 1000 ) {
$space = number_format( $space_allowed / 1024 );
/* translators: Gigabytes */
$space .= __( 'GB' );
} else {
$space = number_format( $space_allowed );
/* translators: Megabytes */
$space .= __( 'MB' );
}
?>
<strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percentused ), $space ); ?></strong>
<strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percent_used ), $space ); ?></strong>
<?php
}
/**
* Get the remaining upload space for this blog.
*
* @since MU
* @uses upload_is_user_over_quota()
* @uses get_space_allowed()
* @uses get_upload_space_available()
*
* @param int $size Current max size in bytes
* @return int Max size in bytes
*/
function fix_import_form_size( $size ) {
if ( upload_is_user_over_quota( false ) == true )
return 0;
$available = get_upload_space_available();
return min( $size, $available);
}
// Edit blog upload space setting on Edit Blog page
function upload_space_setting( $id ) {
$quota = get_blog_option( $id, 'blog_upload_space' );

View File

@ -1458,34 +1458,6 @@ function recurse_dirsize( $directory ) {
return $size;
}
/**
* Check whether a blog has used its allotted upload space.
*
* @since MU
* @uses get_dirsize()
*
* @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
* @return int
*/
function upload_is_user_over_quota( $echo = true ) {
if ( get_site_option( 'upload_space_check_disabled' ) )
return false;
$spaceAllowed = get_space_allowed();
if ( empty( $spaceAllowed ) || !is_numeric( $spaceAllowed ) )
$spaceAllowed = 10; // Default space allowed is 10 MB
$size = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
if ( ($spaceAllowed-$size) < 0 ) {
if ( $echo )
_e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); // No space left
return true;
} else {
return false;
}
}
/**
* Check an array of MIME types against a whitelist.
*
@ -1540,29 +1512,6 @@ function wpmu_log_new_registrations( $blog_id, $user_id ) {
$wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
}
/**
* Get the remaining upload space for this blog.
*
* @since MU
* @uses upload_is_user_over_quota()
* @uses get_space_allowed()
* @uses get_dirsize()
*
* @param int $size
* @return int
*/
function fix_import_form_size( $size ) {
if ( upload_is_user_over_quota( false ) == true )
return 0;
$spaceAllowed = 1024 * 1024 * get_space_allowed();
$dirsize = get_dirsize( BLOGUPLOADDIR );
if ( $size > $spaceAllowed - $dirsize )
return $spaceAllowed - $dirsize; // remaining space
else
return $size; // default
}
/**
* Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
*