Make WordPress Core


Ignore:
Timestamp:
04/02/2019 11:32:31 PM (6 years ago)
Author:
azaozz
Message:

Site health:

  • Prevent fatal errors from timeouts on the Tools => Site Health => Info tab.
  • Use the get_dirsize() and recurse_dirsize() functions to calculate directory sizes. The results are cached.
  • Introduce "timeout protection" in recurse_dirsize().

Props pento, Clorith, xkon, afercia, jeremyfelt, azaozz.
Fixes #46645.

File:
1 edited

Legend:

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

    r44926 r45104  
    17791779
    17801780/**
    1781  * Get the size of a directory.
    1782  *
    1783  * A helper function that is used primarily to check whether
    1784  * a blog has exceeded its allowed upload space.
    1785  *
    1786  * @since MU (3.0.0)
    1787  *
    1788  * @param string $directory Full path of a directory.
    1789  * @return int Size of the directory in MB.
    1790  */
    1791 function get_dirsize( $directory ) {
    1792     $dirsize = get_transient( 'dirsize_cache' );
    1793     if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
    1794         return $dirsize[ $directory ]['size'];
    1795     }
    1796 
    1797     if ( ! is_array( $dirsize ) ) {
    1798         $dirsize = array();
    1799     }
    1800 
    1801     // Exclude individual site directories from the total when checking the main site,
    1802     // as they are subdirectories and should not be counted.
    1803     if ( is_main_site() ) {
    1804         $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites' );
    1805     } else {
    1806         $dirsize[ $directory ]['size'] = recurse_dirsize( $directory );
    1807     }
    1808 
    1809     set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
    1810     return $dirsize[ $directory ]['size'];
    1811 }
    1812 
    1813 /**
    1814  * Get the size of a directory recursively.
    1815  *
    1816  * Used by get_dirsize() to get a directory's size when it contains
    1817  * other directories.
    1818  *
    1819  * @since MU (3.0.0)
    1820  * @since 4.3.0 $exclude parameter added.
    1821  *
    1822  * @param string $directory Full path of a directory.
    1823  * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
    1824  * @return int|false Size in MB if a valid directory. False if not.
    1825  */
    1826 function recurse_dirsize( $directory, $exclude = null ) {
    1827     $size = 0;
    1828 
    1829     $directory = untrailingslashit( $directory );
    1830 
    1831     if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
    1832         return false;
    1833     }
    1834 
    1835     if ( $handle = opendir( $directory ) ) {
    1836         while ( ( $file = readdir( $handle ) ) !== false ) {
    1837             $path = $directory . '/' . $file;
    1838             if ( $file != '.' && $file != '..' ) {
    1839                 if ( is_file( $path ) ) {
    1840                     $size += filesize( $path );
    1841                 } elseif ( is_dir( $path ) ) {
    1842                     $handlesize = recurse_dirsize( $path, $exclude );
    1843                     if ( $handlesize > 0 ) {
    1844                         $size += $handlesize;
    1845                     }
    1846                 }
    1847             }
    1848         }
    1849         closedir( $handle );
    1850     }
    1851     return $size;
    1852 }
    1853 
    1854 /**
    18551781 * Check an array of MIME types against a whitelist.
    18561782 *
Note: See TracChangeset for help on using the changeset viewer.