Support » Theme: Spacious » PHP Deprecated message on call to hexdec in functions.php

  • The following message is being generated by lines 320 and 350 (calls to hexdec) in the Spacious theme functions file:

    [03-Mar-2023 20:03:31 UTC] PHP Deprecated:  Invalid characters passed for attempted conversion, these have been ignored in /.../wp-content/themes/spacious/inc/functions.php on line 320
    [03-Mar-2023 20:03:31 UTC] PHP Deprecated:  Invalid characters passed for attempted conversion, these have been ignored in /.../wp-content/themes/spacious/inc/functions.php on line 350

    Is there an easy solution?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @joantu234,

    Thanks for reaching us,

    Can you please share the screenshot of the area where it actually gets displays on the site or in the admin area. We have checked on your end but it is not replicated.

    Also, let us know the theme version you are using on your end so that we can check on the same version and provide an appropriate solutions.

    Regards

    Hello same issue for me with same theme

    [13-Mar-2023 20:03:31 UTC] PHP Deprecated: Invalid characters passed for attempted conversion, these have been ignored in /…/wp-content/themes/spacious/inc/functions.php on line 320

    Hi @ggomis,

    Can you please let me know the PHP version you are using on your end?

    Regards

    Hi, my PHP version was 4.4 I migrated the version to 8.0 and issue has been fixed

    Thread Starter joantu234

    (@joantu234)

    Sorry for slow reply. I’m running PHP 8.0 and the messages were appearing in the WP error log.

    Hi @joantu234,

    Thanks for getting back,

    I suspect one of the plugins you’re using might be causing the problem, so please try deactivating all the plugins one by one and check if the problem resolves.

    Let me know if this helps you or not and I will be back.

    Regards

    Thread Starter joantu234

    (@joantu234)

    It’s not a plugin problem – it’s a lack of data sanitizing in a couple of theme functions. The color string is being passed as an rgb string, not a hex string and both spacious_darkcolor and spacious_hex2rgb are assuming a hex string. Hence, both fall over when a rgb string is passed as a parameter hexdec.

    Below is a version of both functions that work. No doubt, additional error checking could be added but this solves the ongoing runtime error.

    Function spacious_hex2rgb can be overriden in a child theme functions file (since it’s within a function_exists wrapper), but not spacious_darkcolour where one must hack the theme function file.

    /**
     * Change hex code to RGB
     * Source: https://css-tricks.com/snippets/php/convert-hex-to-rgb/#comment-1052011
     * Extended to handle rgb codes
     */
    if ( ! function_exists( 'spacious_hex2rgb' ) ) {
    	function spacious_hex2rgb( $hexstr ) {
    		if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $hexstr)){
    		$int = hexdec( str_replace( '#', '', $hexstr ) );
    
    		$rgb = array( "red" => 0xFF & ( $int >> 0x10 ), "green" => 0xFF & ( $int >> 0x8 ), "blue" => 0xFF & $int );
    		$r   = $rgb['red'];
    		$g   = $rgb['green'];
    		$b   = $rgb['blue'];
    		return "rgba(".$r.",".$g.",".$b.", 0.85)";		
    		}elseif (preg_match("/^rgb\(/i", $hexstr)){
    		    return str_replace(')',',0.85)',$hexstr);		
    		}
    	}
    }
    
    /**
     * Generate darker color
     * Source: http://stackoverflow.com/questions/3512311/how-to-generate-lighter-darker-color-with-php
     * Extended to  handle rgb codes
     */
    function spacious_darkcolor( $hex, $steps ) {
    	// Steps should be between -255 and 255. Negative = darker, positive = lighter
    	$steps = max( -255, min( 255, $steps ) );
    	$return = $hex;
    	if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $hex)){
    		// Normalize into a six character long hex string
    		$hex = str_replace( '#', '', $hex );
    		if ( strlen( $hex ) == 3 ) {
    			$hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
    		}
    
    		// Split into three parts: R, G and B
    		$color_parts = str_split( $hex, 2 );
    		$return      = '#';
    		foreach ( $color_parts as $color ) {
    			$color  = hexdec( $color ); // Convert to decimal
    			$color  = max( 0, min( 255, $color + $steps ) ); // Adjust color
    			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
    		}
    	}elseif (preg_match("/^rgb\(/i", $hex)){
    		$hex = str_replace(array('rgb','(',')'),'',$hex);
    		//replace possible number separator
    		$hex = str_replace(array(' ',','),':',$hex);
    		//extract numbers
    		$color_parts = explode(":", $hex);
    		$return      = '#';
    		foreach ( $color_parts as $color ){	
    			//color already a decimal
    			$color  = max( 0, min( 255, $color + $steps ) ); // Adjust color
    			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
    		}			   		
    	}
    
    	return $return;
    }
    
    Thread Starter joantu234

    (@joantu234)

    One line amendment to spacious_hex2rgb to ensure correctly formatted rgba string:

    // overwrite faulty parent theme function
    function spacious_hex2rgb( $hexstr ) {
    		if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $hexstr)){
    		$int = hexdec( str_replace( '#', '', $hexstr ) );
    
    		$rgb = array( "red" => 0xFF & ( $int >> 0x10 ), "green" => 0xFF & ( $int >> 0x8 ), "blue" => 0xFF & $int );
    		$r   = $rgb['red'];
    		$g   = $rgb['green'];
    		$b   = $rgb['blue'];
    		return "rgba(".$r.",".$g.",".$b.", 0.85)";		
    		}elseif (preg_match("/^rgb\(/i", $hexstr)){
    		    $hexstr = str_replace('rcb(','rcba(',$hexstr);
    			return str_replace(')',',0.85)',$hexstr);		
    		}
    	}

    Hi @joantu234,

    Thanks for the suggestaion,

    I will pass your suggestion to the developer and make the changes on the theme accordingly.

    Let me know of any other issues or confusion in the future and I will be back.

    Regards

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.