<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[45104] trunk/src: Site health:</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/45104">45104</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/45104","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-04-02 23:32:31 +0000 (Tue, 02 Apr 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/46645">#46645</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpdebugdataphp">trunk/src/wp-admin/includes/class-wp-debug-data.php</a></li>
<li><a href="#trunksrcwpincludesdefaultconstantsphp">trunk/src/wp-includes/default-constants.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpdebugdataphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-debug-data.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-debug-data.php       2019-04-02 22:21:19 UTC (rev 45103)
+++ trunk/src/wp-admin/includes/class-wp-debug-data.php 2019-04-02 23:32:31 UTC (rev 45104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -32,7 +32,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        static function debug_data( $locale = null ) {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wpdb;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( ! empty( $locale ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Change the language used for translations
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( function_exists( 'switch_to_locale' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -312,21 +311,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $size_db = WP_Debug_Data::get_database_size();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Go through the various installation directories and calculate their sizes.
</span><span class="cx" style="display: block; padding: 0 10px">                $uploads_dir = wp_upload_dir();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $inaccurate  = false;
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * We will be using the PHP max execution time to prevent the size calculations
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * from causing a timeout. We provide a default value of 30 seconds, as some
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * from causing a timeout. The default value is 30 seconds, and some
</ins><span class="cx" style="display: block; padding: 0 10px">                  * hosts do not allow you to read configuration values.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $max_execution_time   = 30;
-               $start_execution_time = microtime( true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $max_execution_time = 30;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( function_exists( 'ini_get' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $max_execution_time = ini_get( 'max_execution_time' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Here 20 seconds is a "sensible default" for how long to make the user wait for the directory size calculation.
+               // When testing 20 seconds seem enough in nearly all cases. The remaining edge cases are likely testing or development sites
+               // that have very large number of files, for example `node_modules` in plugins or themes, etc.
+               if ( $max_execution_time > 20  ) {
+                       $max_execution_time = 20;
+               } elseif ( $max_execution_time > 2 ) {
+                       // If the max_execution_time is set to lower than 20 seconds, reduce it a bit to prevent
+                       // edge-case timeouts that may happen after the size loop has finished running.
+                       $max_execution_time -= 1;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $size_directories = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'wordpress' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'path' => ABSPATH,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -346,36 +357,45 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $timeout = __( 'The directory size calculation has timed out. Usually caused by a very large number of sub-directories and files.' );
+               $inaccessible = __( 'The size cannot be calculated. The directory is not accessible. Usually caused by invalid permissions.' );
+               $size_total = 0;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Loop over all the directories we want to gather the sizes for.
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $size_directories as $size => $attributes ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        /*
-                        * We run a helper function with a RecursiveIterator, which
-                        * may throw an exception if it can't access directories.
-                        *
-                        * If a failure is detected we mark the result as inaccurate.
-                        */
-                       try {
-                               $calculated_size = WP_Debug_data::get_directory_size( $attributes['path'], $max_execution_time, $start_execution_time );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $dir_size = null; // Default to timeout.
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $size_directories[ $size ]['size'] = $calculated_size;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( microtime( true ) - WP_START_TIMESTAMP < $max_execution_time ) {
+                               $dir_size = get_dirsize( $attributes['path'], $max_execution_time );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                /*
-                                * If the size returned is -1, this means execution has
-                                * exceeded the maximum execution time, also denoting an
-                                * inaccurate value in the end.
-                                */
-                               if ( -1 === $calculated_size ) {
-                                       $inaccurate = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $dir_size === false ) {
+                               // Error reading
+                               $dir_size = $inaccessible;
+                               $size_total = null;
+                       } elseif ( $dir_size === null ) {
+                               // Timeout
+                               $dir_size = $timeout;
+                               $size_total = null;
+                       } else {
+                               $is_subdir = ( strpos( $size_directories[ $size ]['path'], ABSPATH ) === 0 );
+
+                               if ( $size_total !== null && ( $size === 'wordpress' || ! $is_subdir ) ) {
+                                       $size_total += $dir_size;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        } catch ( Exception $e ) {
-                               $inaccurate = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               $dir_size = size_format( $dir_size, 2 );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       $size_directories[ $size ]['size'] = $dir_size;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $size_db = WP_Debug_Data::get_database_size();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $size_total !== null && $size_db > 0 ) {
+                       $size_total = size_format( $size_total + $size_db, 2 );
+               } else {
+                       $size_total = __( 'Total size is not available. Some errors were encountered when determining the size of your installation.' );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $size_total = $size_directories['wordpress']['size'] + $size_db;
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $info['wp-paths-sizes']['fields'] = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Uploads Directory Location' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -383,7 +403,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Uploads Directory Size' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'value' => ( -1 === $size_directories['uploads']['size'] ? __( 'Unable to determine the size of this directory' ) : size_format( $size_directories['uploads']['size'], 2 ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'value' => $size_directories['uploads']['size'],
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Themes Directory Location' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -395,7 +415,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Themes Directory Size' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'value' => ( -1 === $size_directories['themes']['size'] ? __( 'Unable to determine the size of this directory' ) : size_format( $size_directories['themes']['size'], 2 ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'value' => $size_directories['themes']['size'],
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Plugins Directory Location' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -403,7 +423,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Plugins Directory Size' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'value' => ( -1 === $size_directories['plugins']['size'] ? __( 'Unable to determine the size of this directory' ) : size_format( $size_directories['plugins']['size'], 2 ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'value' => $size_directories['plugins']['size'],
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'WordPress Directory Location' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -411,7 +431,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'WordPress Directory Size' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'value' => size_format( $size_directories['wordpress']['size'], 2 ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'value' => $size_directories['wordpress']['size'],
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Database size' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -419,11 +439,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'label' => __( 'Total installation size' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'value' => sprintf(
-                                       '%s%s',
-                                       size_format( $size_total, 2 ),
-                                       ( false === $inaccurate ? '' : __( '- Some errors, likely caused by invalid permissions, were encountered when determining the size of your installation. This means the values represented may be inaccurate.' ) )
-                               ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'value' => $size_total,
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -935,36 +951,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Return the size of a directory, including all subdirectories.
-        *
-        * @since 5.2.0
-        *
-        * @param string     $path                 The directory to check.
-        * @param string|int $max_execution_time   How long a PHP script can run on this host.
-        * @param float      $start_execution_time When we started executing this section of the script.
-        *
-        * @return int The directory size, in bytes.
-        */
-       public static function get_directory_size( $path, $max_execution_time, $start_execution_time ) {
-               $size = 0;
-
-               foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) ) as $file ) {
-                       // Check if the maximum execution time is a value considered "infinite".
-                       if ( 0 !== $max_execution_time && -1 !== $max_execution_time ) {
-                               $runtime = ( microtime( true ) - $start_execution_time );
-
-                               // If the script has been running as long, or longer, as it is allowed, return a failure message.
-                               if ( $runtime >= $max_execution_time ) {
-                                       return -1;
-                               }
-                       }
-                       $size += $file->getSize();
-               }
-
-               return $size;
-       }
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * Fetch the total size of all the database tables for the active database user.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.2.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -982,6 +968,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return $size;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return (int) $size;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultconstantsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-constants.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-constants.php       2019-04-02 22:21:19 UTC (rev 45103)
+++ trunk/src/wp-includes/default-constants.php 2019-04-02 23:32:31 UTC (rev 45104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -29,6 +29,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
</span><span class="cx" style="display: block; padding: 0 10px">        /**#@-*/
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Start of run timestamp.
+       if ( ! defined( 'WP_START_TIMESTAMP' ) ) {
+               define( 'WP_START_TIMESTAMP', microtime( true ) );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $current_limit     = @ini_get( 'memory_limit' );
</span><span class="cx" style="display: block; padding: 0 10px">        $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php       2019-04-02 22:21:19 UTC (rev 45103)
+++ trunk/src/wp-includes/functions.php 2019-04-02 23:32:31 UTC (rev 45104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7006,3 +7006,103 @@
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px">        echo '</p>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Get the size of a directory.
+ *
+ * A helper function that is used primarily to check whether
+ * a blog has exceeded its allowed upload space.
+ *
+ * @since MU (3.0.0)
+ *
+ * @param string $directory Full path of a directory.
+ * @param int    $max_execution_time Maximum time to run before giving up. In seconds.
+ *                                   The timeout is global and is measured from the moment WordPress started to load.
+ * @return int|false|null Size in MB if a valid directory. False if not. Null if timeout.
+ */
+function get_dirsize( $directory, $max_execution_time = null ) {
+       $dirsize = get_transient( 'dirsize_cache' );
+
+       if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
+               return $dirsize[ $directory ]['size'];
+       }
+
+       if ( ! is_array( $dirsize ) ) {
+               $dirsize = array();
+       }
+
+       // Exclude individual site directories from the total when checking the main site of a network
+       // as they are subdirectories and should not be counted.
+       if ( is_multisite() && is_main_site() ) {
+               $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
+       } else {
+               $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, null, $max_execution_time );
+       }
+
+       set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
+       return $dirsize[ $directory ]['size'];
+}
+
+/**
+ * Get the size of a directory recursively.
+ *
+ * Used by get_dirsize() to get a directory's size when it contains
+ * other directories.
+ *
+ * @since MU (3.0.0)
+ * @since 4.3.0 $exclude parameter added.
+ *
+ * @param string $directory Full path of a directory.
+ * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
+ * @param int    $max_execution_time Maximum time to run before giving up. In seconds.
+ *                                   The timeout is global and is measured from the moment WordPress started to load.
+ * @return int|false|null Size in MB if a valid directory. False if not. Null if timeout.
+ */
+function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) {
+       $size = 0;
+
+       $directory = untrailingslashit( $directory );
+
+       if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
+               return false;
+       }
+
+       if ( ! $max_execution_time ) {
+               // Keep the previous behavior but attempt to prevent fatal errors from timeout.
+               if ( function_exists( 'ini_get' ) ) {
+                       $max_execution_time = ini_get( 'max_execution_time' );
+               } else {
+                       // Use PHP default.
+                       $max_execution_time = 30;
+               }
+
+               // Leave 1 second "buffer" for other operations if $max_execution_time has reasonable value.
+               if ( $max_execution_time > 10 ) {
+                       $max_execution_time -= 1;
+               }
+       }
+
+       if ( $handle = opendir( $directory ) ) {
+               while ( ( $file = readdir( $handle ) ) !== false ) {
+                       $path = $directory . '/' . $file;
+                       if ( $file != '.' && $file != '..' ) {
+                               if ( is_file( $path ) ) {
+                                       $size += filesize( $path );
+                               } elseif ( is_dir( $path ) ) {
+                                       $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time );
+                                       if ( $handlesize > 0 ) {
+                                               $size += $handlesize;
+                                       }
+                               }
+
+                               if ( microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
+                                       // Time exceeded. Give up instead of risking a fatal timeout.
+                                       $size = null;
+                                       break;
+                               }
+                       }
+               }
+               closedir( $handle );
+       }
+       return $size;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-functions.php    2019-04-02 22:21:19 UTC (rev 45103)
+++ trunk/src/wp-includes/ms-functions.php      2019-04-02 23:32:31 UTC (rev 45104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1778,80 +1778,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Misc functions
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get the size of a directory.
- *
- * A helper function that is used primarily to check whether
- * a blog has exceeded its allowed upload space.
- *
- * @since MU (3.0.0)
- *
- * @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' );
-       if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
-               return $dirsize[ $directory ]['size'];
-       }
-
-       if ( ! is_array( $dirsize ) ) {
-               $dirsize = array();
-       }
-
-       // 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'];
-}
-
-/**
- * Get the size of a directory recursively.
- *
- * Used by get_dirsize() to get a directory's size when it contains
- * other directories.
- *
- * @since MU (3.0.0)
- * @since 4.3.0 $exclude parameter added.
- *
- * @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, $exclude = null ) {
-       $size = 0;
-
-       $directory = untrailingslashit( $directory );
-
-       if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
-               return false;
-       }
-
-       if ( $handle = opendir( $directory ) ) {
-               while ( ( $file = readdir( $handle ) ) !== false ) {
-                       $path = $directory . '/' . $file;
-                       if ( $file != '.' && $file != '..' ) {
-                               if ( is_file( $path ) ) {
-                                       $size += filesize( $path );
-                               } elseif ( is_dir( $path ) ) {
-                                       $handlesize = recurse_dirsize( $path, $exclude );
-                                       if ( $handlesize > 0 ) {
-                                               $size += $handlesize;
-                                       }
-                               }
-                       }
-               }
-               closedir( $handle );
-       }
-       return $size;
-}
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px">  * Check an array of MIME types against a whitelist.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * WordPress ships with a set of allowed upload filetypes,
</span></span></pre>
</div>
</div>

</body>
</html>