WordPress.org

Make WordPress Core

Changeset 38013


Ignore:
Timestamp:
07/08/2016 12:53:08 PM (4 years ago)
Author:
ocean90
Message:

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.

Location:
trunk
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/load.php

    r38012 r38013  
    957957 *
    958958 * @since 2.6.0
    959  * @since 4.6.0 Moved from functions.php to load.php
     959 * @since 4.6.0 Moved from functions.php to load.php.
    960960 *
    961961 * @return bool True if SSL, otherwise false.
     
    980980 *
    981981 * @since 2.3.0
    982  * @since 4.6.0 Moved from media.php to load.php
    983  *
    984  * @param string $size A shorthand byte value.
     982 * @since 4.6.0 Moved from media.php to load.php.
     983 *
     984 * @link http://php.net/manual/en/function.ini-get.php
     985 * @link http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes
     986 *
     987 * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
    985988 * @return int An integer byte value.
    986989 */
    987 function wp_convert_hr_to_bytes( $size ) {
    988     $size  = strtolower( $size );
    989     $bytes = (int) $size;
    990     if ( strpos( $size, 'k' ) !== false )
    991         $bytes = intval( $size ) * KB_IN_BYTES;
    992     elseif ( strpos( $size, 'm' ) !== false )
    993         $bytes = intval($size) * MB_IN_BYTES;
    994     elseif ( strpos( $size, 'g' ) !== false )
    995         $bytes = intval( $size ) * GB_IN_BYTES;
    996     return $bytes;
    997 }
     990function wp_convert_hr_to_bytes( $value ) {
     991    $value = strtolower( trim( $value ) );
     992    $bytes = (int) $value;
     993
     994    if ( false !== strpos( $value, 'g' ) ) {
     995        $bytes *= GB_IN_BYTES;
     996    } elseif ( false !== strpos( $value, 'm' ) ) {
     997        $bytes *= MB_IN_BYTES;
     998    } elseif ( false !== strpos( $value, 'k' ) ) {
     999        $bytes *= KB_IN_BYTES;
     1000    }
     1001
     1002    // Deal with large (float) values which run into the maximum integer size.
     1003    return min( $bytes, PHP_INT_MAX );
     1004}
Note: See TracChangeset for help on using the changeset viewer.