Formatting: Add support for formatting sizes as PB, EB, ZB, and YB.

Props henry.wright, Presskopp

Fixes #40875


git-svn-id: https://develop.svn.wordpress.org/trunk@52955 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2022-03-18 20:46:17 +00:00
parent ab7d749e19
commit 95d26d682f
3 changed files with 45 additions and 2 deletions

View File

@ -22,11 +22,16 @@ function wp_initial_constants() {
* Constants for expressing human-readable data sizes in their respective number of bytes.
*
* @since 4.4.0
* @since 6.0.0 `PB_IN_BYTES`, `EB_IN_BYTES`, `ZB_IN_BYTES`, and `YB_IN_BYTES` were added.
*/
define( 'KB_IN_BYTES', 1024 );
define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
define( 'GB_IN_BYTES', 1024 * MB_IN_BYTES );
define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
define( 'PB_IN_BYTES', 1024 * TB_IN_BYTES );
define( 'EB_IN_BYTES', 1024 * PB_IN_BYTES );
define( 'ZB_IN_BYTES', 1024 * EB_IN_BYTES );
define( 'YB_IN_BYTES', 1024 * ZB_IN_BYTES );
/**#@-*/
// Start of run timestamp.

View File

@ -441,11 +441,11 @@ function number_format_i18n( $number, $decimals = 0 ) {
}
/**
* Convert number of bytes largest unit bytes will fit into.
* Converts a number of bytes to the largest unit the bytes will fit into.
*
* It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
* number of bytes to human readable number by taking the number of that unit
* that the bytes will go into it. Supports TB value.
* that the bytes will go into it. Supports YB value.
*
* Please note that integers in PHP are limited to 32 bits, unless they are on
* 64 bit architecture, then they have 64 bit size. If you need to place the
@ -455,6 +455,7 @@ function number_format_i18n( $number, $decimals = 0 ) {
* Technically the correct unit names for powers of 1024 are KiB, MiB etc.
*
* @since 2.3.0
* @since 6.0.0 Support for PB, EB, ZB, and YB was added.
*
* @param int|string $bytes Number of bytes. Note max integer size for integers.
* @param int $decimals Optional. Precision of number of decimal places. Default 0.
@ -462,6 +463,14 @@ function number_format_i18n( $number, $decimals = 0 ) {
*/
function size_format( $bytes, $decimals = 0 ) {
$quant = array(
/* translators: Unit symbol for yottabyte. */
_x( 'YB', 'unit symbol' ) => YB_IN_BYTES,
/* translators: Unit symbol for zettabyte. */
_x( 'ZB', 'unit symbol' ) => ZB_IN_BYTES,
/* translators: Unit symbol for exabyte. */
_x( 'EB', 'unit symbol' ) => EB_IN_BYTES,
/* translators: Unit symbol for petabyte. */
_x( 'PB', 'unit symbol' ) => PB_IN_BYTES,
/* translators: Unit symbol for terabyte. */
_x( 'TB', 'unit symbol' ) => TB_IN_BYTES,
/* translators: Unit symbol for gigabyte. */

View File

@ -3,7 +3,9 @@
/**
* Tests for size_format()
*
* @ticket 22405
* @ticket 36635
* @ticket 40875
*
* @group functions.php
* @covers ::size_format
@ -12,31 +14,58 @@ class Tests_Functions_SizeFormat extends WP_UnitTestCase {
public function _data_size_format() {
return array(
// Invalid values
array( array(), 0, false ),
array( 'baba', 0, false ),
array( '', 0, false ),
array( '-1', 0, false ),
array( -1, 0, false ),
// Bytes
array( 0, 0, '0 B' ),
array( 1, 0, '1 B' ),
array( 1023, 0, '1,023 B' ),
// Kilobytes
array( KB_IN_BYTES, 0, '1 KB' ),
array( KB_IN_BYTES, 2, '1.00 KB' ),
array( 2.5 * KB_IN_BYTES, 0, '3 KB' ),
array( 2.5 * KB_IN_BYTES, 2, '2.50 KB' ),
array( 10 * KB_IN_BYTES, 0, '10 KB' ),
// Megabytes
array( (string) 1024 * KB_IN_BYTES, 2, '1.00 MB' ),
array( MB_IN_BYTES, 0, '1 MB' ),
array( 2.5 * MB_IN_BYTES, 0, '3 MB' ),
array( 2.5 * MB_IN_BYTES, 2, '2.50 MB' ),
// Gigabytes
array( (string) 1024 * MB_IN_BYTES, 2, '1.00 GB' ),
array( GB_IN_BYTES, 0, '1 GB' ),
array( 2.5 * GB_IN_BYTES, 0, '3 GB' ),
array( 2.5 * GB_IN_BYTES, 2, '2.50 GB' ),
// Terabytes
array( (string) 1024 * GB_IN_BYTES, 2, '1.00 TB' ),
array( TB_IN_BYTES, 0, '1 TB' ),
array( 2.5 * TB_IN_BYTES, 0, '3 TB' ),
array( 2.5 * TB_IN_BYTES, 2, '2.50 TB' ),
// Petabytes
array( (string) 1024 * TB_IN_BYTES, 2, '1.00 PB' ),
array( PB_IN_BYTES, 0, '1 PB' ),
array( 2.5 * PB_IN_BYTES, 0, '3 PB' ),
array( 2.5 * PB_IN_BYTES, 2, '2.50 PB' ),
// Exabytes
array( (string) 1024 * PB_IN_BYTES, 2, '1.00 EB' ),
array( EB_IN_BYTES, 0, '1 EB' ),
array( 2.5 * EB_IN_BYTES, 0, '3 EB' ),
array( 2.5 * EB_IN_BYTES, 2, '2.50 EB' ),
// Zettabytes
array( (string) 1024 * EB_IN_BYTES, 2, '1.00 ZB' ),
array( ZB_IN_BYTES, 0, '1 ZB' ),
array( 2.5 * ZB_IN_BYTES, 0, '3 ZB' ),
array( 2.5 * ZB_IN_BYTES, 2, '2.50 ZB' ),
// Yottabytes
array( (string) 1024 * ZB_IN_BYTES, 2, '1.00 YB' ),
array( YB_IN_BYTES, 0, '1 YB' ),
array( 2.5 * YB_IN_BYTES, 0, '3 YB' ),
array( 2.5 * YB_IN_BYTES, 2, '2.50 YB' ),
// Edge values
array( TB_IN_BYTES + ( TB_IN_BYTES / 2 ) + MB_IN_BYTES, 1, '1.5 TB' ),
array( TB_IN_BYTES - MB_IN_BYTES - KB_IN_BYTES, 3, '1,023.999 GB' ),
);