Use a helper divider method to account for strings and zeros when dividing closes #29768
This commit is contained in:
parent
d1f5784fa4
commit
bb2664d56a
|
@ -978,13 +978,13 @@ function wc_get_image_size( $image_size ) {
|
|||
} elseif ( 'custom' === $cropping ) {
|
||||
$width = max( 1, get_option( 'woocommerce_thumbnail_cropping_custom_width', '4' ) );
|
||||
$height = max( 1, get_option( 'woocommerce_thumbnail_cropping_custom_height', '3' ) );
|
||||
$size['height'] = absint( NumberUtil::round( ( $size['width'] / $width ) * $height ) );
|
||||
$size['height'] = absint( NumberUtil::round( NumberUtil::divide( $size['width'], $width ) * (int) $height ) );
|
||||
$size['crop'] = 1;
|
||||
} else {
|
||||
$cropping_split = explode( ':', $cropping );
|
||||
$width = max( 1, current( $cropping_split ) );
|
||||
$height = max( 1, end( $cropping_split ) );
|
||||
$size['height'] = absint( NumberUtil::round( ( $size['width'] / $width ) * $height ) );
|
||||
$size['height'] = absint( NumberUtil::round( ( NumberUtil::divide( $size['width'], $width ) ) * (int) $height ) );
|
||||
$size['crop'] = 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,4 +31,22 @@ final class NumberUtil {
|
|||
}
|
||||
return round( $val, $precision, $mode );
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides two numbers making sure to check for zeros and casting as floats.
|
||||
*
|
||||
* This is needed because in PHP 7 dividing a number by a non number results in a PHP warning.
|
||||
* But in PHP 8, it throws a fatal error.
|
||||
*
|
||||
* @param float $first The first number to be divided from.
|
||||
* @param float $second The second number to be divided by.
|
||||
* @return float The divided value.
|
||||
*/
|
||||
public static function divide( float $first, float $second ) : float {
|
||||
if ( 0 === $second || 0 === $first ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $first / $second;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue