diff --git a/docs/plugin/readme.txt b/docs/plugin/readme.txt index 5a580c48..a3c03684 100644 --- a/docs/plugin/readme.txt +++ b/docs/plugin/readme.txt @@ -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 @@ -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 diff --git a/src/health-check.php b/src/health-check.php index 8e5685ef..105f1b0a 100644 --- a/src/health-check.php +++ b/src/health-check.php @@ -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 */ @@ -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__ ) ); diff --git a/src/includes/class-health-check-debug-data.php b/src/includes/class-health-check-debug-data.php index 91e9f766..83c20a42 100644 --- a/src/includes/class-health-check-debug-data.php +++ b/src/includes/class-health-check-debug-data.php @@ -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()`. @@ -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( @@ -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 ); + } } } @@ -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; + } } diff --git a/src/includes/class-health-check.php b/src/includes/class-health-check.php index c3c88372..623cb94a 100644 --- a/src/includes/class-health-check.php +++ b/src/includes/class-health-check.php @@ -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' ) ); diff --git a/src/pages/debug-data.php b/src/pages/debug-data.php index 9c722325..d9225757 100644 --- a/src/pages/debug-data.php +++ b/src/pages/debug-data.php @@ -113,7 +113,7 @@ %s', - 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' ) ); ?>