Site health, info tab:

- Change `recurse_dirsize()` to accept an array of excluded paths.
- Change so we don't calculate the sizes of dirs in wp-content twice.
- Add the size in bytes to the "debug" into.
- Add a custom DOM event after the dir sizes request is done. Can be used by plugins to "daisy chain" more requests.
- Move "WordPress directory location" and "WordPress directory size" to the top in the "Directories and Sizes" section.
- Move "Theme directory location" to the "Active Theme" section.
- Fix labels capitalization.

Props xkon, afercia, Clorith, azaozz.
Fixes #46707.
Built from https://develop.svn.wordpress.org/trunk@45220


git-svn-id: http://core.svn.wordpress.org/trunk@45029 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2019-04-16 23:02:51 +00:00
parent 8e9315f51e
commit 9a316c6ea4
6 changed files with 82 additions and 63 deletions

View File

@ -4976,7 +4976,7 @@ function wp_ajax_health_check_get_sizes() {
}
$sizes_data = WP_Debug_Data::get_sizes();
$all_sizes = array();
$all_sizes = array( 'raw' => 0 );
foreach ( $sizes_data as $name => $value ) {
$name = sanitize_text_field( $name );
@ -4998,6 +4998,10 @@ function wp_ajax_health_check_get_sizes() {
}
}
if ( ! empty( $value['raw'] ) ) {
$data['raw'] = (int) $value['raw'];
}
$all_sizes[ $name ] = $data;
}

View File

@ -392,43 +392,39 @@ class WP_Debug_Data {
$not_calculated = __( 'Not calculated' );
$info['wp-paths-sizes']['fields'] = array(
'wordpress_path' => array(
'label' => __( 'WordPress directory location' ),
'value' => untrailingslashit( ABSPATH ),
),
'wordpress_size' => array(
'label' => __( 'WordPress directory size' ),
'value' => $not_calculated,
'debug' => 'not calculated',
),
'uploads_path' => array(
'label' => __( 'Uploads Directory Location' ),
'label' => __( 'Uploads directory location' ),
'value' => $upload_dir['basedir'],
),
'uploads_size' => array(
'label' => __( 'Uploads Directory Size' ),
'label' => __( 'Uploads directory size' ),
'value' => $not_calculated,
'debug' => 'not calculated',
),
'themes_path' => array(
'label' => __( 'Themes Directory Location' ),
'value' => trailingslashit( get_theme_root() ),
),
'current_theme_path' => array(
'label' => __( 'Current Theme Directory' ),
'value' => get_template_directory(),
'label' => __( 'Themes directory location' ),
'value' => get_theme_root(),
),
'themes_size' => array(
'label' => __( 'Themes Directory Size' ),
'label' => __( 'Themes directory size' ),
'value' => $not_calculated,
'debug' => 'not calculated',
),
'plugins_path' => array(
'label' => __( 'Plugins Directory Location' ),
'value' => trailingslashit( WP_PLUGIN_DIR ),
'label' => __( 'Plugins directory location' ),
'value' => WP_PLUGIN_DIR,
),
'plugins_size' => array(
'label' => __( 'Plugins Directory Size' ),
'value' => $not_calculated,
'debug' => 'not calculated',
),
'wordpress_path' => array(
'label' => __( 'WordPress Directory Location' ),
'value' => ABSPATH,
),
'wordpress_size' => array(
'label' => __( 'WordPress Directory Size' ),
'label' => __( 'Plugins directory size' ),
'value' => $not_calculated,
'debug' => 'not calculated',
),
@ -898,6 +894,10 @@ class WP_Debug_Data {
'label' => __( 'Theme features' ),
'value' => implode( ', ', $theme_features ),
),
'theme_path' => array(
'label' => __( 'Theme directory location' ),
'value' => get_template_directory(),
),
);
// Populate a list of all themes available in the install.
@ -1137,68 +1137,71 @@ class WP_Debug_Data {
}
// Go through the various installation directories and calculate their sizes.
$all_sizes = array(
'wordpress_size' => array(
'path' => ABSPATH,
'size' => 0,
),
'themes_size' => array(
'path' => trailingslashit( get_theme_root() ),
'size' => 0,
),
'plugins_size' => array(
'path' => trailingslashit( WP_PLUGIN_DIR ),
'size' => 0,
),
'uploads_size' => array(
'path' => $upload_dir['basedir'],
'size' => 0,
),
// No trailing slashes.
$paths = array(
'wordpress_size' => untrailingslashit( ABSPATH ),
'themes_size' => get_theme_root(),
'plugins_size' => WP_PLUGIN_DIR,
'uploads_size' => $upload_dir['basedir'],
);
$exclude = $paths;
unset( $exclude['wordpress_size'] );
$exclude = array_values( $exclude );
$size_total = 0;
$all_sizes = array();
// Loop over all the directories we want to gather the sizes for.
foreach ( $all_sizes as $name => $attributes ) {
foreach ( $paths as $name => $path ) {
$dir_size = null; // Default to timeout.
$results = array(
'path' => $path,
'raw' => 0,
);
if ( microtime( true ) - WP_START_TIMESTAMP < $max_execution_time ) {
$dir_size = recurse_dirsize( $attributes['path'], null, $max_execution_time );
if ( 'wordpress_size' === $name ) {
$dir_size = recurse_dirsize( $path, $exclude, $max_execution_time );
} else {
$dir_size = recurse_dirsize( $path, null, $max_execution_time );
}
}
if ( false === $dir_size ) {
// Error reading.
$all_sizes[ $name ]['size'] = __( 'The size cannot be calculated. The directory is not accessible. Usually caused by invalid permissions.' );
$all_sizes[ $name ]['debug'] = 'not accessible';
$results['size'] = __( 'The size cannot be calculated. The directory is not accessible. Usually caused by invalid permissions.' );
$results['debug'] = 'not accessible';
// Stop total size calculation.
$size_total = null;
} elseif ( null === $dir_size ) {
// Timeout.
$all_sizes[ $name ]['size'] = __( 'The directory size calculation has timed out. Usually caused by a very large number of sub-directories and files.' );
$all_sizes[ $name ]['debug'] = 'timeout while calculating size';
$results['size'] = __( 'The directory size calculation has timed out. Usually caused by a very large number of sub-directories and files.' );
$results['debug'] = 'timeout while calculating size';
// Stop total size calculation.
$size_total = null;
} else {
$is_subdir = ( strpos( $attributes['path'], ABSPATH ) === 0 );
// phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
if ( null !== $size_total && ( 'wordpress_size' === $name || ! $is_subdir ) ) {
if ( null !== $size_total ) {
$size_total += $dir_size;
}
$all_sizes[ $name ]['size'] = size_format( $dir_size, 2 );
$all_sizes[ $name ]['debug'] = $all_sizes[ $name ]['size'];
$results['raw'] = $dir_size;
$results['size'] = size_format( $dir_size, 2 );
$results['debug'] = $results['size'] . " ({$dir_size} bytes)";
}
$all_sizes[ $name ] = $results;
}
if ( $size_db > 0 ) {
$database_size = size_format( $size_db, 2 );
$all_sizes['database_size'] = array(
'raw' => $size_db,
'size' => $database_size,
'debug' => $database_size,
'debug' => $database_size . " ({$size_db} bytes)",
);
} else {
$all_sizes['database_size'] = array(
@ -1208,11 +1211,13 @@ class WP_Debug_Data {
}
if ( null !== $size_total && $size_db > 0 ) {
$total_size = size_format( $size_total + $size_db, 2 );
$total_size = $size_total + $size_db;
$total_size_mb = size_format( $total_size, 2 );
$all_sizes['total_size'] = array(
'size' => $total_size,
'debug' => $total_size,
'raw' => $total_size,
'size' => $total_size_mb,
'debug' => $total_size_mb . " ({$total_size} bytes)",
);
} else {
$all_sizes['total_size'] = array(

View File

@ -276,6 +276,8 @@ jQuery( document ).ready( function( $ ) {
// Cancel the announcement.
window.clearTimeout( timeout );
}
$( document ).trigger( 'site-health-info-dirsizes-done' );
} );
}

View File

@ -1 +1 @@
jQuery(document).ready(function(a){function b(b){var c,d,e=wp.template("health-check-issue"),f=a("#health-check-issues-"+b.status);SiteHealth.site_status.issues[b.status]++,d=SiteHealth.site_status.issues[b.status],"critical"===b.status?c=j(i("%s Critical issue","%s Critical issues",d),'<span class="issue-count">'+d+"</span>"):"recommended"===b.status?c=j(i("%s Recommended improvement","%s Recommended improvements",d),'<span class="issue-count">'+d+"</span>"):"good"===b.status&&(c=j(i("%s Item with no issues detected","%s Items with no issues detected",d),'<span class="issue-count">'+d+"</span>")),c&&a(".site-health-issue-count-title",f).html(c),a(".issues","#health-check-issues-"+b.status).append(e(b))}function c(){var b,c,d,e=a(".site-health-progress"),f=e.find(".site-health-progress-count"),g=a(".site-health-progress svg #bar"),i=parseInt(SiteHealth.site_status.issues.good,0)+parseInt(SiteHealth.site_status.issues.recommended,0)+1.5*parseInt(SiteHealth.site_status.issues.critical,0),k=parseInt(SiteHealth.site_status.issues.recommended,0)+1.5*parseInt(SiteHealth.site_status.issues.critical,0),m=100-Math.ceil(k/i*100);return 0===i?void e.addClass("hidden"):(e.removeClass("loading"),b=g.attr("r"),c=Math.PI*(2*b),0>m&&(m=0),100<m&&(m=100),d=(100-m)/100*c,g.css({strokeDashoffset:d}),1>parseInt(SiteHealth.site_status.issues.critical,0)&&a("#health-check-issues-critical").addClass("hidden"),1>parseInt(SiteHealth.site_status.issues.recommended,0)&&a("#health-check-issues-recommended").addClass("hidden"),50<=m&&g.addClass("orange").removeClass("red"),90<=m&&g.addClass("green").removeClass("orange"),100===m&&(a(".site-status-all-clear").removeClass("hide"),a(".site-status-has-issues").addClass("hide")),f.text(m+"%"),void(l||(a.post(ajaxurl,{action:"health-check-site-status-result",_wpnonce:SiteHealth.nonce.site_status_result,counts:SiteHealth.site_status.issues}),wp.a11y.speak(j(h("All site health tests have finished running. Your site scored %s, and the results are now available on the page."),m+"%")))))}function d(){var e=!0;1<=SiteHealth.site_status.async.length&&a.each(SiteHealth.site_status.async,function(){var c={action:"health-check-"+this.test.replace("_","-"),_wpnonce:SiteHealth.nonce.site_status};return!!this.completed||(e=!1,this.completed=!0,a.post(ajaxurl,c,function(a){b(a.data),d()}),!1)}),e&&c()}function e(){var b={action:"health-check-get-sizes",_wpnonce:SiteHealth.nonce.site_status_result},d=(new Date).getTime(),e=window.setTimeout(function(){wp.a11y.speak(h("Please wait..."))},3e3);a.post({type:"POST",url:ajaxurl,data:b,dataType:"json"}).done(function(a){f(a.data||{})}).always(function(){var b=(new Date).getTime()-d;a(".health-check-wp-paths-sizes.spinner").css("visibility","hidden"),c(),b>3e3?(b=b>6e3?0:6500-b,window.setTimeout(function(){wp.a11y.speak(h("All site health tests have finished running."))},b)):window.clearTimeout(e)})}function f(b){var c=a("button.button.copy-button"),d=c.attr("data-clipboard-text");a.each(b,function(a,b){var c=b.debug||b.size;"undefined"!=typeof c&&(d=d.replace(a+": not calculated",a+": "+c))}),c.attr("data-clipboard-text",d),a("#health-check-accordion-block-wp-paths-sizes").find("td[class]").each(function(c,d){var e=a(d),f=e.attr("class");b.hasOwnProperty(f)&&b[f].size&&e.text(b[f].size)})}var g,h=wp.i18n.__,i=wp.i18n._n,j=wp.i18n.sprintf,k=new ClipboardJS(".site-health-copy-buttons .copy-button"),l=a(".health-check-body.health-check-debug-tab").length;k.on("success",function(b){var c=a(b.trigger).closest("div");a(".success",c).addClass("visible"),wp.a11y.speak(h("Site information has been added to your clipboard."))}),a(".health-check-accordion").on("click",".health-check-accordion-trigger",function(){var b="true"===a(this).attr("aria-expanded");b?(a(this).attr("aria-expanded","false"),a("#"+a(this).attr("aria-controls")).attr("hidden",!0)):(a(this).attr("aria-expanded","true"),a("#"+a(this).attr("aria-controls")).attr("hidden",!1))}),a(".site-health-view-passed").on("click",function(){var b=a("#health-check-issues-good");b.toggleClass("hidden"),a(this).attr("aria-expanded",!b.hasClass("hidden"))}),"undefined"==typeof SiteHealth||l||(0===SiteHealth.site_status.direct.length&&0===SiteHealth.site_status.async.length?c():SiteHealth.site_status.issues={good:0,recommended:0,critical:0},0<SiteHealth.site_status.direct.length&&a.each(SiteHealth.site_status.direct,function(){b(this)}),0<SiteHealth.site_status.async.length?(g={action:"health-check-"+SiteHealth.site_status.async[0].test.replace("_","-"),_wpnonce:SiteHealth.nonce.site_status},SiteHealth.site_status.async[0].completed=!0,a.post(ajaxurl,g,function(a){b(a.data),d()})):c()),l&&e()});
jQuery(document).ready(function(a){function b(b){var c,d,e=wp.template("health-check-issue"),f=a("#health-check-issues-"+b.status);SiteHealth.site_status.issues[b.status]++,d=SiteHealth.site_status.issues[b.status],"critical"===b.status?c=j(i("%s Critical issue","%s Critical issues",d),'<span class="issue-count">'+d+"</span>"):"recommended"===b.status?c=j(i("%s Recommended improvement","%s Recommended improvements",d),'<span class="issue-count">'+d+"</span>"):"good"===b.status&&(c=j(i("%s Item with no issues detected","%s Items with no issues detected",d),'<span class="issue-count">'+d+"</span>")),c&&a(".site-health-issue-count-title",f).html(c),a(".issues","#health-check-issues-"+b.status).append(e(b))}function c(){var b,c,d,e=a(".site-health-progress"),f=e.find(".site-health-progress-count"),g=a(".site-health-progress svg #bar"),i=parseInt(SiteHealth.site_status.issues.good,0)+parseInt(SiteHealth.site_status.issues.recommended,0)+1.5*parseInt(SiteHealth.site_status.issues.critical,0),k=parseInt(SiteHealth.site_status.issues.recommended,0)+1.5*parseInt(SiteHealth.site_status.issues.critical,0),m=100-Math.ceil(k/i*100);return 0===i?void e.addClass("hidden"):(e.removeClass("loading"),b=g.attr("r"),c=Math.PI*(2*b),0>m&&(m=0),100<m&&(m=100),d=(100-m)/100*c,g.css({strokeDashoffset:d}),1>parseInt(SiteHealth.site_status.issues.critical,0)&&a("#health-check-issues-critical").addClass("hidden"),1>parseInt(SiteHealth.site_status.issues.recommended,0)&&a("#health-check-issues-recommended").addClass("hidden"),50<=m&&g.addClass("orange").removeClass("red"),90<=m&&g.addClass("green").removeClass("orange"),100===m&&(a(".site-status-all-clear").removeClass("hide"),a(".site-status-has-issues").addClass("hide")),f.text(m+"%"),void(l||(a.post(ajaxurl,{action:"health-check-site-status-result",_wpnonce:SiteHealth.nonce.site_status_result,counts:SiteHealth.site_status.issues}),wp.a11y.speak(j(h("All site health tests have finished running. Your site scored %s, and the results are now available on the page."),m+"%")))))}function d(){var e=!0;1<=SiteHealth.site_status.async.length&&a.each(SiteHealth.site_status.async,function(){var c={action:"health-check-"+this.test.replace("_","-"),_wpnonce:SiteHealth.nonce.site_status};return!!this.completed||(e=!1,this.completed=!0,a.post(ajaxurl,c,function(a){b(a.data),d()}),!1)}),e&&c()}function e(){var b={action:"health-check-get-sizes",_wpnonce:SiteHealth.nonce.site_status_result},d=(new Date).getTime(),e=window.setTimeout(function(){wp.a11y.speak(h("Please wait..."))},3e3);a.post({type:"POST",url:ajaxurl,data:b,dataType:"json"}).done(function(a){f(a.data||{})}).always(function(){var b=(new Date).getTime()-d;a(".health-check-wp-paths-sizes.spinner").css("visibility","hidden"),c(),b>3e3?(b=b>6e3?0:6500-b,window.setTimeout(function(){wp.a11y.speak(h("All site health tests have finished running."))},b)):window.clearTimeout(e),a(document).trigger("site-health-info-dirsizes-done")})}function f(b){var c=a("button.button.copy-button"),d=c.attr("data-clipboard-text");a.each(b,function(a,b){var c=b.debug||b.size;"undefined"!=typeof c&&(d=d.replace(a+": not calculated",a+": "+c))}),c.attr("data-clipboard-text",d),a("#health-check-accordion-block-wp-paths-sizes").find("td[class]").each(function(c,d){var e=a(d),f=e.attr("class");b.hasOwnProperty(f)&&b[f].size&&e.text(b[f].size)})}var g,h=wp.i18n.__,i=wp.i18n._n,j=wp.i18n.sprintf,k=new ClipboardJS(".site-health-copy-buttons .copy-button"),l=a(".health-check-body.health-check-debug-tab").length;k.on("success",function(b){var c=a(b.trigger).closest("div");a(".success",c).addClass("visible"),wp.a11y.speak(h("Site information has been added to your clipboard."))}),a(".health-check-accordion").on("click",".health-check-accordion-trigger",function(){var b="true"===a(this).attr("aria-expanded");b?(a(this).attr("aria-expanded","false"),a("#"+a(this).attr("aria-controls")).attr("hidden",!0)):(a(this).attr("aria-expanded","true"),a("#"+a(this).attr("aria-controls")).attr("hidden",!1))}),a(".site-health-view-passed").on("click",function(){var b=a("#health-check-issues-good");b.toggleClass("hidden"),a(this).attr("aria-expanded",!b.hasClass("hidden"))}),"undefined"==typeof SiteHealth||l||(0===SiteHealth.site_status.direct.length&&0===SiteHealth.site_status.async.length?c():SiteHealth.site_status.issues={good:0,recommended:0,critical:0},0<SiteHealth.site_status.direct.length&&a.each(SiteHealth.site_status.direct,function(){b(this)}),0<SiteHealth.site_status.async.length?(g={action:"health-check-"+SiteHealth.site_status.async[0].test.replace("_","-"),_wpnonce:SiteHealth.nonce.site_status},SiteHealth.site_status.async[0].completed=!0,a.post(ajaxurl,g,function(a){b(a.data),d()})):c()),l&&e()});

View File

@ -7071,18 +7071,26 @@ function get_dirsize( $directory, $max_execution_time = null ) {
* @since 4.3.0 $exclude parameter added.
* @since 5.2.0 $max_execution_time 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.
* @param string $directory Full path of a directory.
* @param string|array $exclude Optional. Full path of a subdirectory to exclude from the total, or array of paths.
* Expected without trailing slash(es).
* @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 bytes 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 ) {
if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) {
return false;
}
if (
( is_string( $exclude ) && $directory === $exclude ) ||
( is_array( $exclude ) && in_array( $directory, $exclude, true ) )
) {
return false;
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.2-beta3-45219';
$wp_version = '5.2-beta3-45220';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.