In a wordpress site made with divi, in the functions.php
file in the divi theme includes builder folder, I'm getting an intermittent error on the line:
$value = substr( $value, 1, $value_length ) . trim();
The error says Warning: trim() expects at least 1 parameter, 0 given
Is this because there's nothing inside trim? But chaining it like this on the end it should refer to what it's chained onto, is that correct? So the substr
of $value
. Does this mean that $value
is just not given? Or that when you get the substr
there's nothing else there?
Would love to use javascript's console.log to see what's actually there round about now :D . Is there a php equivalent? Or some kind of plugin for php for dev tools where I can see the variables at any given moment?
Anyone know what the issue is here? I'm not even sure what this piece of code does at the moment, something to do with setting a css px value on some text I'm guessing..., and removing the !important
property?
if ( ! function_exists( 'et_pb_get_value_unit' ) ) :
/**
* Get unit of given value
*
* @param string $value string with unit.
* @param string $default_unit default unit.
*
* @return string unit name
*/
function et_pb_get_value_unit( $value, $default_unit = 'px' ) {
$value = isset( $value ) ? $value : '';
$valid_one_char_units = array( '%', 'x' );
$valid_two_chars_units = array( 'em', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ex', 'vh', 'vw', 'ms' );
$valid_three_chars_units = array( 'deg', 'rem' );
$important = '!important';
$important_length = strlen( $important );
$value_length = strlen( $value );
if ( '' === $value || is_numeric( $value ) ) {
return $default_unit;
}
if ( substr( $value, ( 0 - $important_length ), $important_length ) === $important ) {
$value_length = $value_length - $important_length;
$value = substr( $value, 1, $value_length ) . trim();
}
if ( in_array( substr( $value, -3, 3 ), $valid_three_chars_units, true ) ) {
return substr( $value, -3, 3 );
}
if ( in_array( substr( $value, -2, 2 ), $valid_two_chars_units, true ) ) {
return substr( $value, -2, 2 );
}
if ( in_array( substr( $value, -1, 1 ), $valid_one_char_units, true ) ) {
return substr( $value, -1, 1 );
}
return $default_unit;
}
endif;
CodePudding user response:
Chaining like that is a feature of other languages (like JavaScript), however it is not a feature of PHP. As it's written, you're "concatenating a 'trimmed null
'" onto the end of the value, which, doesn't really make sense.
Take a look at the trim()
function documentation - the string
argument is required so it knows what to trim.
If you wanted to "chain" like that, you'd actually need to encompass the value with the trim
like so:
$value = trim( substr( $value, 1, $value_length ) );
You may see "chained" functions like that referred to as "nested" as far as PHP is concerned.
As far as what the code is doing, it's a relatively convoluted way of returning the unit type from a value - probably to see if it's rem
, px
, %
- etc for use in other calculations elsewhere. It could be simplified quite a bit ( perhaps a single line with regex), but that's beyond the scope of this question