Last active
January 13, 2023 13:39
-
-
Save Zodiac1978/3205315bc772b95ec7c80c3e0b37bc20 to your computer and use it in GitHub Desktop.
Sometimes the server config return bytes (instead of MB), this little helper fixes the display in the site health plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns always bytes for every config (M, K or G) | |
* | |
* @param [string] $size_str Input from config. | |
* @return [int] Return in bytes. | |
*/ | |
function return_bytes( $size_str ) { | |
switch ( substr( $size_str, -1 ) ) { | |
case 'M': | |
case 'm': | |
return (int) $size_str * 1048576; | |
case 'K': | |
case 'k': | |
return (int) $size_str * 1024; | |
case 'G': | |
case 'g': | |
return (int) $size_str * 1073741824; | |
default: | |
return $size_str; | |
} | |
} | |
/** | |
* Returns bytes in formatted way | |
* | |
* @param [int] $bytes Amount of bytes. | |
* @param integer $precision Number of digits after the decimal point. | |
* @return [string] Formatted string with unit. | |
*/ | |
function format_bytes( $bytes, $precision = 2 ) { | |
$units = array( 'B', 'KB', 'MB', 'GB', 'TB' ); | |
$bytes = max( $bytes, 0 ); | |
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) ); | |
$pow = min( $pow, count( $units ) - 1 ); | |
// Uncomment one of the following alternatives. | |
$bytes /= pow( 1024, $pow ); | |
// $bytes /= (1 << (10 * $pow)); | |
return round( $bytes, $precision ) . ' ' . $units[ $pow ]; | |
} | |
/** | |
* Repairs the display of the memory_limit debug info | |
* | |
* @param [string] $info Bytes with unit from config. | |
* @return [string] Formatted bytes with unit. | |
*/ | |
function repair_bytes( $info ) { | |
$memory_limit = $info['wp-server']['fields']['memory_limit']['value']; | |
// Renamed from upload_max_size to upload_max_filesize in https://core.trac.wordpress.org/changeset/48801 | |
$upload_max_size = $info['wp-server']['fields']['upload_max_filesize']['value']; | |
$php_post_max_size = $info['wp-server']['fields']['php_post_max_size']['value']; | |
if ( '-1' === $memory_limit ) { | |
$memory_limit .= ' (no limit)'; | |
} else { | |
$memory_limit = format_bytes( return_bytes( $memory_limit ) ); | |
} | |
$upload_max_size = format_bytes( return_bytes( $upload_max_size ) ); | |
$php_post_max_size = format_bytes( return_bytes( $php_post_max_size ) ); | |
$info['wp-server']['fields']['memory_limit']['value'] = $memory_limit; | |
// Renamed from upload_max_size to upload_max_filesize in https://core.trac.wordpress.org/changeset/48801 | |
$info['wp-server']['fields']['upload_max_filesize']['value'] = $upload_max_size; | |
$info['wp-server']['fields']['php_post_max_size']['value'] = $php_post_max_size; | |
return $info; | |
} | |
add_filter( 'debug_information', 'repair_bytes' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment