WordPress.org

Make WordPress Core

Ticket #21181: 21181.rework.diff

File 21181.rework.diff, 6.2 KB (added by westi, 7 years ago)

A much bigger rework of the upload space tracking code.

  • wp-includes/ms-functions.php

     
    14591459}
    14601460
    14611461/**
    1462  * Check whether a blog has used its allotted upload space.
    1463  *
    1464  * @since MU
    1465  * @uses get_dirsize()
    1466  *
    1467  * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
    1468  * @return int
    1469  */
    1470 function upload_is_user_over_quota( $echo = true ) {
    1471         if ( get_site_option( 'upload_space_check_disabled' ) )
    1472                 return false;
    1473 
    1474         $spaceAllowed = get_space_allowed();
    1475         if ( empty( $spaceAllowed ) || !is_numeric( $spaceAllowed ) )
    1476                 $spaceAllowed = 10;     // Default space allowed is 10 MB
    1477 
    1478         $size = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
    1479 
    1480         if ( ($spaceAllowed-$size) < 0 ) {
    1481                 if ( $echo )
    1482                         _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ); // No space left
    1483                 return true;
    1484         } else {
    1485                 return false;
    1486         }
    1487 }
    1488 
    1489 /**
    14901462 * Check an array of MIME types against a whitelist.
    14911463 *
    14921464 * WordPress ships with a set of allowed upload filetypes,
     
    15411513}
    15421514
    15431515/**
    1544  * Get the remaining upload space for this blog.
    1545  *
    1546  * @since MU
    1547  * @uses upload_is_user_over_quota()
    1548  * @uses get_space_allowed()
    1549  * @uses get_dirsize()
    1550  *
    1551  * @param int $size
    1552  * @return int
    1553  */
    1554 function fix_import_form_size( $size ) {
    1555         if ( upload_is_user_over_quota( false ) == true )
    1556                 return 0;
    1557 
    1558         $spaceAllowed = 1024 * 1024 * get_space_allowed();
    1559         $dirsize = get_dirsize( BLOGUPLOADDIR );
    1560         if ( $size > $spaceAllowed - $dirsize )
    1561                 return $spaceAllowed - $dirsize; // remaining space
    1562         else
    1563                 return $size; // default
    1564 }
    1565 
    1566 /**
    15671516 * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
    15681517 *
    15691518 * @since 3.0.0
  • wp-admin/includes/dashboard.php

     
    10971097                return true;
    10981098
    10991099        $quota = get_space_allowed();
    1100         $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
     1100        $used = get_space_used();
    11011101
    11021102        if ( $used > $quota )
    11031103                $percentused = '100';
  • wp-admin/includes/ms.php

     
    2525        if ( defined( 'WP_IMPORTING' ) )
    2626                return $file;
    2727
    28         $space_allowed = 1048576 * get_space_allowed();
    29         $space_used = get_dirsize( BLOGUPLOADDIR );
    30         $space_left = $space_allowed - $space_used;
     28        $space_left = get_upload_space_available();
     29
    3130        $file_size = filesize( $file['tmp_name'] );
    3231        if ( $space_left < $file_size )
    3332                $file['error'] = sprintf( __( 'Not enough space to upload. %1$s KB needed.' ), number_format( ($file_size - $space_left) /1024 ) );
     
    310309        if ( get_site_option( 'upload_space_check_disabled' ) )
    311310                return $space_allowed;
    312311
    313         $dir_name = trailingslashit( BLOGUPLOADDIR );
    314         if ( !( is_dir( $dir_name) && is_readable( $dir_name ) ) )
    315                 return $space_allowed;
     312        $space_used = get_space_used() * 1024 * 1024;
    316313
    317         $dir = dir( $dir_name );
    318         $size = 0;
     314        if ( ( $space_allowed - $space_used ) <= 0 )
     315                return 0;
    319316
    320         while ( $file = $dir->read() ) {
    321                 if ( $file != '.' && $file != '..' ) {
    322                         if ( is_dir( $dir_name . $file) ) {
    323                                 $size += get_dirsize( $dir_name . $file );
    324                         } else {
    325                                 $size += filesize( $dir_name . $file );
    326                         }
    327                 }
     317        return $space_allowed - $space_used;
     318}
     319
     320/**
     321 * Check whether a blog has used its allotted upload space.
     322 *
     323 * @since MU
     324 *
     325 * @param bool $echo Optional. If $echo is set and the quota is exceeded, a warning message is echoed. Default is true.
     326 * @return int
     327 */
     328function upload_is_user_over_quota( $echo = true ) {
     329        if ( get_site_option( 'upload_space_check_disabled' ) )
     330                return false;
     331
     332        $space_allowed = get_space_allowed();
     333        if ( empty( $space_allowed ) || !is_numeric( $space_allowed ) )
     334                $space_allowed = 10;// Default space allowed is 10 MB
     335
     336        $space_used = get_space_used();
     337
     338        if ( ($space_allowed - $space_used ) < 0 ) {
     339                if ( $echo )
     340                        _e( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' );
     341                return true;
     342        } else {
     343                return false;
    328344        }
    329         $dir->close();
     345}
    330346
    331         if ( ( $space_allowed - $size ) <= 0 )
    332                 return 0;
     347function get_space_used() {
     348        // Allow for an alternative way of tracking storage space used
     349        $space_used = apply_filters( 'pre_get_space_used', false );
     350        if ( false === $space_used )
     351                $space_used = get_dirsize( BLOGUPLOADDIR );
    333352
    334         return $space_allowed - $size;
     353        return $space_used;
    335354}
    336355
    337356/**
     
    352371}
    353372
    354373function display_space_usage() {
    355         $space = get_space_allowed();
    356         $used = get_dirsize( BLOGUPLOADDIR ) / 1024 / 1024;
     374        $space_allowed = get_space_allowed();
     375        $space_used = get_space_used();
    357376
    358         $percentused = ( $used / $space ) * 100;
     377        $percent_used = ( $space_used / $space_allowed ) * 100;
    359378
    360         if ( $space > 1000 ) {
    361                 $space = number_format( $space / 1024 );
     379        if ( $space_allowed > 1000 ) {
     380                $space = number_format( $space_allowed / 1024 );
    362381                /* translators: Gigabytes */
    363382                $space .= __( 'GB' );
    364383        } else {
     384                $space = number_format( $space_allowed );
    365385                /* translators: Megabytes */
    366386                $space .= __( 'MB' );
    367387        }
    368388        ?>
    369         <strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percentused ), $space ); ?></strong>
     389        <strong><?php printf( __( 'Used: %1s%% of %2s' ), number_format( $percent_used ), $space ); ?></strong>
    370390        <?php
    371391}
    372392
     393/**
     394 * Get the remaining upload space for this blog.
     395 *
     396 * @since MU
     397 * @uses upload_is_user_over_quota()
     398 * @uses get_space_allowed()
     399 * @uses get_upload_space_available()
     400 *
     401 * @param int $size Current max size in bytes
     402 * @return int Max size in bytes
     403 */
     404function fix_import_form_size( $size ) {
     405        if ( upload_is_user_over_quota( false ) == true )
     406                return 0;
     407
     408        $available = get_upload_space_available();
     409        return min( $size, $available);
     410}
     411
    373412// Edit blog upload space setting on Edit Blog page
    374413function upload_space_setting( $id ) {
    375414        $quota = get_blog_option( $id, 'blog_upload_space' );