Make WordPress Core


Ignore:
Timestamp:
07/13/2015 12:06:39 AM (9 years ago)
Author:
jeremyfelt
Message:

Exclude individual site directories when calculating space used for the main site.

  • Add an $exclude parameter to recurse_dirsize().
  • Use this parameter in get_dirsize() to exclude /sites when on the main site.
  • Add tests for main site and switched site.

Props @earnjam, @jeremyfelt.
Fixes #30202.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-functions.php

    r33083 r33184  
    16871687 * @since MU
    16881688 *
    1689  * @param string $directory
    1690  * @return int
     1689 * @param string $directory Full path of a directory.
     1690 * @return int Size of the directory in MB.
    16911691 */
    16921692function get_dirsize( $directory ) {
     
    16981698        $dirsize = array();
    16991699
    1700     $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
     1700    // Exclude individual site directories from the total when checking the main site,
     1701    // as they are subdirectories and should not be counted.
     1702    if ( is_main_site() ) {
     1703        $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' );
     1704    } else {
     1705        $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
     1706    }
    17011707
    17021708    set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
     
    17111717 *
    17121718 * @since MU
    1713  *
    1714  * @param string $directory
    1715  * @return int|false
    1716  */
    1717 function recurse_dirsize( $directory ) {
     1719 * @since 4.3.0 $exclude parameter added.
     1720 *
     1721 * @param string $directory Full path of a directory.
     1722 * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
     1723 * @return int|false Size in MB if a valid directory. False if not.
     1724 */
     1725function recurse_dirsize( $directory, $exclude = null ) {
    17181726    $size = 0;
    17191727
    17201728    $directory = untrailingslashit( $directory );
    17211729
    1722     if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
     1730    if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
    17231731        return false;
     1732    }
    17241733
    17251734    if ($handle = opendir($directory)) {
     
    17301739                    $size += filesize($path);
    17311740                } elseif (is_dir($path)) {
    1732                     $handlesize = recurse_dirsize($path);
     1741                    $handlesize = recurse_dirsize( $path, $exclude );
    17331742                    if ($handlesize > 0)
    17341743                        $size += $handlesize;
Note: See TracChangeset for help on using the changeset viewer.