Bootstrap: Clean up wp_convert_hr_to_bytes().

* Don't return a value higher than `PHP_INT_MAX`.
* Add unit tests.

Props jrf.
See #32075.

git-svn-id: https://develop.svn.wordpress.org/trunk@38013 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling
2016-07-08 12:53:08 +00:00
parent df78fb4ef3
commit 629c6e36b5
2 changed files with 86 additions and 13 deletions
+20 -13
View File
@@ -956,7 +956,7 @@ function wp_installing( $is_installing = null ) {
* Determines if SSL is used.
*
* @since 2.6.0
* @since 4.6.0 Moved from functions.php to load.php
* @since 4.6.0 Moved from functions.php to load.php.
*
* @return bool True if SSL, otherwise false.
*/
@@ -979,19 +979,26 @@ function is_ssl() {
* Converts a shorthand byte value to an integer byte value.
*
* @since 2.3.0
* @since 4.6.0 Moved from media.php to load.php
* @since 4.6.0 Moved from media.php to load.php.
*
* @param string $size A shorthand byte value.
* @link http://php.net/manual/en/function.ini-get.php
* @link http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes
*
* @param string $value A (PHP ini) byte value, either shorthand or ordinary.
* @return int An integer byte value.
*/
function wp_convert_hr_to_bytes( $size ) {
$size = strtolower( $size );
$bytes = (int) $size;
if ( strpos( $size, 'k' ) !== false )
$bytes = intval( $size ) * KB_IN_BYTES;
elseif ( strpos( $size, 'm' ) !== false )
$bytes = intval($size) * MB_IN_BYTES;
elseif ( strpos( $size, 'g' ) !== false )
$bytes = intval( $size ) * GB_IN_BYTES;
return $bytes;
function wp_convert_hr_to_bytes( $value ) {
$value = strtolower( trim( $value ) );
$bytes = (int) $value;
if ( false !== strpos( $value, 'g' ) ) {
$bytes *= GB_IN_BYTES;
} elseif ( false !== strpos( $value, 'm' ) ) {
$bytes *= MB_IN_BYTES;
} elseif ( false !== strpos( $value, 'k' ) ) {
$bytes *= KB_IN_BYTES;
}
// Deal with large (float) values which run into the maximum integer size.
return min( $bytes, PHP_INT_MAX );
}