public function directory_filesize(\SplFileInfo $file)
{
// For performance reasons we cache the root.
if ($file->getRealPath() === PATH::get_root() && $this->excludes) {
$directory_sizes = get_transient('hmbkp_root_size');
if ($directory_sizes) {
return (int) $directory_sizes;
}
}
// If we haven't calculated the site size yet then kick it off in a thread.
$directory_sizes = $this->get_cached_filesizes();
if (!is_array($directory_sizes)) {
$this->rebuild_directory_filesizes();
// Intentionally return null so the caller can tell that the size is being calculated.
return null;
}
/*
* Ensure we only include files in the current path, the filepaths are stored in keys
* so we need to flip for use with preg_grep.
*/
$directory_sizes = array_flip(preg_grep('(' . wp_normalize_path($file->getRealPath()) . ')', array_flip($directory_sizes)));
if ($this->excludes) {
$excludes = implode('|', $this->excludes->get_excludes_for_regex());
if ($excludes) {
// Use PREG_GREP_INVERT to remove any filepaths which match an exclude rule
$directory_sizes = array_flip(preg_grep('(' . $excludes . ')', array_flip($directory_sizes), PREG_GREP_INVERT));
}
}
$directory_sizes = absint(array_sum($directory_sizes));
// For performance reasons we cache the root.
if ($file->getRealPath() === PATH::get_root() && $this->excludes) {
set_transient('hmbkp_root_size', $directory_sizes, DAY_IN_SECONDS);
}
// Directory size is now just a sum of all files across all sub directories.
return (int) $directory_sizes;
}