From 95d26d682fb0fcb77fed308436e87914f7ce2e82 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 18 Mar 2022 20:46:17 +0000 Subject: [PATCH] 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 --- src/wp-includes/default-constants.php | 5 ++++ src/wp-includes/functions.php | 13 +++++++-- tests/phpunit/tests/functions/sizeFormat.php | 29 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/default-constants.php b/src/wp-includes/default-constants.php index 64baf0d20c..56754033a0 100644 --- a/src/wp-includes/default-constants.php +++ b/src/wp-includes/default-constants.php @@ -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. diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 1a9ecfe859..0e0f626e56 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -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. */ diff --git a/tests/phpunit/tests/functions/sizeFormat.php b/tests/phpunit/tests/functions/sizeFormat.php index 281db07f5d..1337bb39c8 100644 --- a/tests/phpunit/tests/functions/sizeFormat.php +++ b/tests/phpunit/tests/functions/sizeFormat.php @@ -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' ), );