Skip to content

Commit

Permalink
Version 1.3.2 (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clorith authored May 7, 2019
1 parent 39333bb commit 5c86c77
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 6 deletions.
6 changes: 5 additions & 1 deletion docs/plugin/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Tags: health check
Contributors: wordpressdotorg, westi, pento, Clorith
Requires at least: 4.0
Tested up to: 5.2
Stable tag: 1.3.1
Stable tag: 1.3.2
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -39,6 +39,10 @@ Are you unfamiliar with how to clear your cookies? No worries, you may also clos

== Changelog ==

= v1.3.2 =
* Add polyfill for directory size calculations for sites running WordPress versions older than 5.2.0
* Fix link for the extended PHP information

= v1.3.1 =
* Include missing dependency for JavaScript files, first introduced in WordPress 5.2

Expand Down
4 changes: 2 additions & 2 deletions src/health-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Plugin URI: https://wordpress.org/plugins/health-check/
* Description: Checks the health of your WordPress install.
* Author: The WordPress.org community
* Version: 1.3.1
* Version: 1.3.2
* Author URI: https://wordpress.org/plugins/health-check/
* Text Domain: health-check
*/
Expand All @@ -35,7 +35,7 @@
define( 'HEALTH_CHECK_MYSQL_REC_VERSION', '5.6' );

// Set the plugin version.
define( 'HEALTH_CHECK_PLUGIN_VERSION', '1.3.1' );
define( 'HEALTH_CHECK_PLUGIN_VERSION', '1.3.2' );

// Set the absolute path for the plugin.
define( 'HEALTH_CHECK_PLUGIN_DIRECTORY', plugin_dir_path( __FILE__ ) );
Expand Down
132 changes: 130 additions & 2 deletions src/includes/class-health-check-debug-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,50 @@ public static function get_database_size() {
return (int) $size;
}

public static function ajax_get_sizes() {
check_ajax_referer( 'health-check-site-status-result' );

if ( ! current_user_can( 'install_plugins' ) || is_multisite() ) {
wp_send_json_error();
}

$sizes_data = Health_Check_Debug_Data::get_sizes();
$all_sizes = array( 'raw' => 0 );

foreach ( $sizes_data as $name => $value ) {
$name = sanitize_text_field( $name );
$data = array();

if ( isset( $value['size'] ) ) {
if ( is_string( $value['size'] ) ) {
$data['size'] = sanitize_text_field( $value['size'] );
} else {
$data['size'] = (int) $value['size'];
}
}

if ( isset( $value['debug'] ) ) {
if ( is_string( $value['debug'] ) ) {
$data['debug'] = sanitize_text_field( $value['debug'] );
} else {
$data['debug'] = (int) $value['debug'];
}
}

if ( ! empty( $value['raw'] ) ) {
$data['raw'] = (int) $value['raw'];
}

$all_sizes[ $name ] = $data;
}

if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
wp_send_json_error( $all_sizes );
}

wp_send_json_success( $all_sizes );
}

/**
* Fetch the sizes of the WordPress directories: `wordpress` (ABSPATH), `plugins`, `themes`, and `uploads`.
* Intended to supplement the array returned by `WP_Debug_Data::debug_data()`.
Expand Down Expand Up @@ -1156,6 +1200,15 @@ public static function get_sizes() {
$max_execution_time -= 2;
}

if ( ! defined( 'WP_START_TIMESTAMP' ) ) {
global $timestart;
if ( version_compare( phpversion(), '5.4.0', '>=' ) && isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ) {
define( 'WP_START_TIMESTAMP', $_SERVER['REQUEST_TIME_FLOAT'] );
} else {
define( 'WP_START_TIMESTAMP', $timestart );
}
}

// Go through the various installation directories and calculate their sizes.
// No trailing slashes.
$paths = array(
Expand All @@ -1182,9 +1235,17 @@ public static function get_sizes() {

if ( microtime( true ) - WP_START_TIMESTAMP < $max_execution_time ) {
if ( 'wordpress_size' === $name ) {
$dir_size = recurse_dirsize( $path, $exclude, $max_execution_time );
if ( version_compare( get_bloginfo( 'version' ), '5.2.0', '<' ) ) {
$dir_size = Health_Check_Debug_Data::recurse_dirsize( $path, $exclude, $max_execution_time );
} else {
$dir_size = recurse_dirsize( $path, $exclude, $max_execution_time );
}
} else {
$dir_size = recurse_dirsize( $path, null, $max_execution_time );
if ( version_compare( get_bloginfo( 'version' ), '5.2.0', '<' ) ) {
$dir_size = Health_Check_Debug_Data::recurse_dirsize( $path, null, $max_execution_time );
} else {
$dir_size = recurse_dirsize( $path, null, $max_execution_time );
}
}
}

Expand Down Expand Up @@ -1248,4 +1309,71 @@ public static function get_sizes() {

return $all_sizes;
}

/**
* Fallback function for directory size calculation on sites running WordPress <5.2
*
* @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.
*/
static 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 ) ) {
return false;
}

if (
( is_string( $exclude ) && $directory === $exclude ) ||
( is_array( $exclude ) && in_array( $directory, $exclude, true ) )
) {
return false;
}

if ( null === $max_execution_time ) {
// Keep the previous behavior but attempt to prevent fatal errors from timeout if possible.
if ( function_exists( 'ini_get' ) ) {
$max_execution_time = ini_get( 'max_execution_time' );
} else {
// Disable...
$max_execution_time = 0;
}

// Leave 1 second "buffer" for other operations if $max_execution_time has reasonable value.
if ( $max_execution_time > 10 ) {
$max_execution_time -= 1;
}
}

$handle = opendir( $directory );
if ( $handle ) {
while ( ( $file = readdir( $handle ) ) !== false ) {
$path = $directory . '/' . $file;
if ( '.' != $file && '..' != $file ) {
if ( is_file( $path ) ) {
$size += filesize( $path );
} elseif ( is_dir( $path ) ) {
$handlesize = Health_Check_Debug_Data::recurse_dirsize( $path, $exclude, $max_execution_time );
if ( $handlesize > 0 ) {
$size += $handlesize;
}
}

if ( $max_execution_time > 0 && 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;
}
}
1 change: 1 addition & 0 deletions src/includes/class-health-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function init() {
add_action( 'wp_ajax_health-check-files-integrity-check', array( 'Health_Check_Files_Integrity', 'run_files_integrity_check' ) );
add_action( 'wp_ajax_health-check-view-file-diff', array( 'Health_Check_Files_Integrity', 'view_file_diff' ) );
add_action( 'wp_ajax_health-check-mail-check', array( 'Health_Check_Mail_Check', 'run_mail_check' ) );
add_action( 'wp_ajax_health-check-get-sizes', array( 'Health_Check_Debug_Data', 'ajax_get_sizes' ) );

add_filter( 'health_check_tools_tab', array( 'Health_Check_Files_Integrity', 'tools_tab' ) );
add_filter( 'health_check_tools_tab', array( 'Health_Check_Mail_Check', 'tools_tab' ) );
Expand Down
2 changes: 1 addition & 1 deletion src/pages/debug-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<?php
printf(
'<a href="%s" class="button button-primary">%s</a>',
esc_url( admin_url( '?page=health-check&tab=phpinfo' ) ),
esc_url( admin_url( 'tools.php?page=health-check&tab=phpinfo' ) ),
esc_html__( 'View extended PHP information', 'health-check' )
);
?>
Expand Down

0 comments on commit 5c86c77

Please sign in to comment.