Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Utilitie...
File: NumberUtil.php
<?php //phpcs:ignore Generic.PHP.RequireStrictTypes.MissingDeclaration
[0] Fix | Delete
// many places seem to be calling round with a string, and that causes PHP > 8.1 to throw a TypeError.
[1] Fix | Delete
// It's not respecting the 'mixed' type hint in the docblock, and type unions aren't supported until PHP > 8.0.
[2] Fix | Delete
// so I'm not sure how to handle this since adding the type union will cause errors below PHP < 8.0.
[3] Fix | Delete
/**
[4] Fix | Delete
* A class of utilities for dealing with numbers.
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\WooCommerce\Utilities;
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* A class of utilities for dealing with numbers.
[12] Fix | Delete
*/
[13] Fix | Delete
final class NumberUtil {
[14] Fix | Delete
/**
[15] Fix | Delete
* Converts numbers (floats, strings, integers) to numeric values to be safely used in PHP functions like floor() which expect int or float.
[16] Fix | Delete
*
[17] Fix | Delete
* @param mixed $value The value to convert.
[18] Fix | Delete
* @param mixed $fallback The value to return if the conversion fails.
[19] Fix | Delete
* @return int|float|mixed Returns the numeric value or the fallback value if conversion fails.
[20] Fix | Delete
*/
[21] Fix | Delete
public static function normalize( $value, $fallback = 0 ) {
[22] Fix | Delete
// Trim string values to handle whitespace consistently across PHP versions.
[23] Fix | Delete
if ( is_string( $value ) ) {
[24] Fix | Delete
$value = trim( $value );
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
if ( is_numeric( $value ) ) {
[28] Fix | Delete
$numeric_value = is_string( $value ) ? floatval( $value ) : $value;
[29] Fix | Delete
[30] Fix | Delete
// Round to precision to avoid floating-point precision issues.
[31] Fix | Delete
return is_int( $numeric_value ) ? $numeric_value : round( $numeric_value, WC_ROUNDING_PRECISION );
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
return $fallback;
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Round a number using the built-in `round` function, but unless the value to round is numeric
[39] Fix | Delete
* (a number or a string that can be parsed as a number), apply 'floatval' first to it
[40] Fix | Delete
* (so it will convert it to 0 in most cases).
[41] Fix | Delete
*
[42] Fix | Delete
* This is needed because in PHP 7 applying `round` to a non-numeric value returns 0,
[43] Fix | Delete
* but in PHP 8 it throws an error. Specifically, in WooCommerce we have a few places where
[44] Fix | Delete
* round('') is often executed.
[45] Fix | Delete
*
[46] Fix | Delete
* @param mixed $val The value to round.
[47] Fix | Delete
* @param int $precision The optional number of decimal digits to round to.
[48] Fix | Delete
* @param int $mode A constant to specify the mode in which rounding occurs.
[49] Fix | Delete
*
[50] Fix | Delete
* @return float The value rounded to the given precision as a float.
[51] Fix | Delete
*/
[52] Fix | Delete
public static function round( $val, int $precision = 0, int $mode = PHP_ROUND_HALF_UP ): float {
[53] Fix | Delete
return round( self::normalize( $val ), $precision, $mode );
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* Floor a number using the built-in `floor` function.
[58] Fix | Delete
*
[59] Fix | Delete
* @param mixed $val The value to floor.
[60] Fix | Delete
* @return float
[61] Fix | Delete
*/
[62] Fix | Delete
public static function floor( $val ): float {
[63] Fix | Delete
return floor( self::normalize( $val ) );
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Ceil a number using the built-in `ceil` function.
[68] Fix | Delete
*
[69] Fix | Delete
* @param mixed $val The value to ceil.
[70] Fix | Delete
* @return float
[71] Fix | Delete
*/
[72] Fix | Delete
public static function ceil( $val ): float {
[73] Fix | Delete
return ceil( self::normalize( $val ) );
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Get the sum of an array of values using the built-in array_sum function, but sanitize the array values
[78] Fix | Delete
* first to ensure they are all floats.
[79] Fix | Delete
*
[80] Fix | Delete
* This is needed because in PHP 8.3 non-numeric values that cannot be cast as an int or a float will
[81] Fix | Delete
* cause an E_WARNING to be emitted. Prior to PHP 8.3 these values were just ignored.
[82] Fix | Delete
*
[83] Fix | Delete
* Note that, unlike the built-in array_sum, this one will always return a float, never an int.
[84] Fix | Delete
*
[85] Fix | Delete
* @param array $arr The array of values to sum.
[86] Fix | Delete
*
[87] Fix | Delete
* @return float
[88] Fix | Delete
*/
[89] Fix | Delete
public static function array_sum( array $arr ): float {
[90] Fix | Delete
$sanitized_array = array_map( 'floatval', $arr );
[91] Fix | Delete
[92] Fix | Delete
return array_sum( $sanitized_array );
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Sanitize a cost value based on the current locale decimal and thousand separators.
[97] Fix | Delete
*
[98] Fix | Delete
* @param string $value The value to sanitize.
[99] Fix | Delete
* @return string The sanitized value.
[100] Fix | Delete
* @throws \InvalidArgumentException If the value is not a valid numeric value.
[101] Fix | Delete
*/
[102] Fix | Delete
public static function sanitize_cost_in_current_locale( $value ): string {
[103] Fix | Delete
$value = is_null( $value ) ? '' : $value;
[104] Fix | Delete
$value = wp_kses_post( trim( wp_unslash( $value ) ) );
[105] Fix | Delete
$currency_symbol_encoded = get_woocommerce_currency_symbol();
[106] Fix | Delete
$currency_symbol_variations = array( $currency_symbol_encoded, wp_kses_normalize_entities( $currency_symbol_encoded ), html_entity_decode( $currency_symbol_encoded, ENT_COMPAT ) );
[107] Fix | Delete
[108] Fix | Delete
$value = str_replace( $currency_symbol_variations, '', $value );
[109] Fix | Delete
[110] Fix | Delete
// Count the number of decimal points.
[111] Fix | Delete
$decimal_point_count = substr_count( $value, '.' );
[112] Fix | Delete
[113] Fix | Delete
// If it's a standard decimal number (single decimal point and is_numeric), accept it directly. This could be in the case where the frontend has de-localised the value.
[114] Fix | Delete
// We check for the decimal point count in addition to using is_numeric.
[115] Fix | Delete
// This is because is_numeric is much looser and accepts non-base10 numbers as well as 'e' to demarcate exponents.
[116] Fix | Delete
if ( 1 === $decimal_point_count && is_numeric( $value ) ) {
[117] Fix | Delete
return $value;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
// Otherwise, attempt to delocalise according to localisation rules.
[121] Fix | Delete
$allowed_characters_regex = sprintf(
[122] Fix | Delete
'/^[0-9\%s\%s]*$/',
[123] Fix | Delete
wc_get_price_thousand_separator(),
[124] Fix | Delete
wc_get_price_decimal_separator()
[125] Fix | Delete
);
[126] Fix | Delete
[127] Fix | Delete
if ( 1 !== preg_match( $allowed_characters_regex, $value ) ) {
[128] Fix | Delete
throw new \InvalidArgumentException(
[129] Fix | Delete
esc_html(
[130] Fix | Delete
sprintf(
[131] Fix | Delete
/* translators: %1$s: Invalid value that was input by the user, %2$s: thousand separator, %3$s: decimal separator */
[132] Fix | Delete
__( '%1$s is not a valid numeric value. Allowed characters are numbers, the thousand (%2$s), and decimal (%3$s) separators.', 'woocommerce' ),
[133] Fix | Delete
$value,
[134] Fix | Delete
wc_get_price_thousand_separator(),
[135] Fix | Delete
wc_get_price_decimal_separator()
[136] Fix | Delete
)
[137] Fix | Delete
)
[138] Fix | Delete
);
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
// Validate decimal and thousand separator positions.
[142] Fix | Delete
$decimal_separator = wc_get_price_decimal_separator();
[143] Fix | Delete
$thousand_separator = wc_get_price_thousand_separator();
[144] Fix | Delete
[145] Fix | Delete
if (
[146] Fix | Delete
// Check that there is only 1 decimal separator.
[147] Fix | Delete
substr_count( $value, $decimal_separator ) > 1 ||
[148] Fix | Delete
(
[149] Fix | Delete
// Check that decimal separator appears after thousand separator if both exist.
[150] Fix | Delete
false !== strpos( $value, $thousand_separator ) &&
[151] Fix | Delete
false !== strpos( $value, $decimal_separator ) &&
[152] Fix | Delete
strpos( $value, $decimal_separator ) <= strpos( $value, $thousand_separator )
[153] Fix | Delete
)
[154] Fix | Delete
) {
[155] Fix | Delete
throw new \InvalidArgumentException(
[156] Fix | Delete
esc_html(
[157] Fix | Delete
sprintf(
[158] Fix | Delete
/* translators: %s: Invalid value that was input by the user */
[159] Fix | Delete
__( '%s is not a valid numeric value: there should be one decimal separator and it has to be after the thousands separator.', 'woocommerce' ),
[160] Fix | Delete
$value
[161] Fix | Delete
)
[162] Fix | Delete
)
[163] Fix | Delete
);
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* For context, as of 2025.
[168] Fix | Delete
* The full set of thousands separators is PERIOD, COMMA, SPACE, APOSTROPHE.
[169] Fix | Delete
* And the full set of decimal separators is PERIOD, COMMA.
[170] Fix | Delete
* There are no locales that use the same thousands and decimal separators.
[171] Fix | Delete
*/
[172] Fix | Delete
[173] Fix | Delete
$value = str_replace( wc_get_price_thousand_separator(), '', $value );
[174] Fix | Delete
$value = str_replace( wc_get_price_decimal_separator(), '.', $value );
[175] Fix | Delete
[176] Fix | Delete
if ( $value && ! is_numeric( $value ) ) {
[177] Fix | Delete
/* translators: %s: Invalid value that was input by the user */
[178] Fix | Delete
throw new \InvalidArgumentException(
[179] Fix | Delete
esc_html(
[180] Fix | Delete
sprintf(
[181] Fix | Delete
/* translators: %s: Invalid value that was input by the user */
[182] Fix | Delete
__( '%s is not a valid numeric value.', 'woocommerce' ),
[183] Fix | Delete
$value
[184] Fix | Delete
)
[185] Fix | Delete
)
[186] Fix | Delete
);
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
return $value;
[190] Fix | Delete
}
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function