From c122491c7000d82c157cbe90ac437e5b4f163c84 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Mon, 13 Jul 2015 00:07:24 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@33184 git-svn-id: http://core.svn.wordpress.org/trunk@33156 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/ms-functions.php | 25 +++++++++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 487d8401e5..edbb984255 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -1686,8 +1686,8 @@ function get_most_recent_post_of_user( $user_id ) { * * @since MU * - * @param string $directory - * @return int + * @param string $directory Full path of a directory. + * @return int Size of the directory in MB. */ function get_dirsize( $directory ) { $dirsize = get_transient( 'dirsize_cache' ); @@ -1697,7 +1697,13 @@ function get_dirsize( $directory ) { if ( ! is_array( $dirsize ) ) $dirsize = array(); - $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory ); + // Exclude individual site directories from the total when checking the main site, + // as they are subdirectories and should not be counted. + if ( is_main_site() ) { + $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' ); + } else { + $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory ); + } set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS ); return $dirsize[ $directory ][ 'size' ]; @@ -1710,17 +1716,20 @@ function get_dirsize( $directory ) { * other directories. * * @since MU + * @since 4.3.0 $exclude parameter added. * - * @param string $directory - * @return int|false + * @param string $directory Full path of a directory. + * @param string $exclude Optional. Full path of a subdirectory to exclude from the total. + * @return int|false Size in MB if a valid directory. False if not. */ -function recurse_dirsize( $directory ) { +function recurse_dirsize( $directory, $exclude = null ) { $size = 0; $directory = untrailingslashit( $directory ); - if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) ) + if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) { return false; + } if ($handle = opendir($directory)) { while(($file = readdir($handle)) !== false) { @@ -1729,7 +1738,7 @@ function recurse_dirsize( $directory ) { if (is_file($path)) { $size += filesize($path); } elseif (is_dir($path)) { - $handlesize = recurse_dirsize($path); + $handlesize = recurse_dirsize( $path, $exclude ); if ($handlesize > 0) $size += $handlesize; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 5546462aa1..1cdc5c7ba8 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-beta2-33183'; +$wp_version = '4.3-beta2-33184'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.