Skip to content

Commit daf2636

Browse files
committed
fix bug with shadow colors that uses rgba
1 parent 6605479 commit daf2636

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/global-settings.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,17 @@ public static function generate_global_block_layouts( $option_name, $settings_na
907907
}
908908

909909
if ( is_string( $value ) ) {
910-
$style = $value;
910+
if ( strpos( $value, 'rgb' ) ) {
911+
// Convert rgba colors to hex alpha colors because
912+
// the function wp_style_engine_get_stylesheet_from_css_rules() doesn't allow css values to have '('
913+
// See safecss_filter_attr() of wp-includes/kses.php
914+
$split_value = Stackable_Global_Settings::extract_rgba( $value );
915+
$color = Stackable_Global_Settings::rgba_to_hex_alpha( $split_value['color'] );
916+
917+
$style = $split_value[ 'options' ] . ' ' . $color;
918+
} else {
919+
$style = $value;
920+
}
911921
} else if ( is_array( $value ) ) {
912922
$default_value = Stackable_Global_Settings::get_block_layout_defaults( $defaults, $property, $device );
913923
$top = isset( $value[ 'top' ] ) ? $value[ 'top' ] : $default_value[ 'top' ];
@@ -958,6 +968,40 @@ public static function generate_global_block_layouts( $option_name, $settings_na
958968
return $generated_css;
959969
}
960970

971+
public static function extract_rgba($value) {
972+
$options = $value;
973+
$color = '';
974+
975+
// Use a regex to find and extract the rgba value
976+
if (preg_match('/rgba\(.*\)$/', $options, $matches)) {
977+
$color = $matches[0];
978+
$options = str_replace($color, '', $options);
979+
}
980+
981+
$options = trim($options);
982+
983+
return [
984+
'options' => $options,
985+
'color' => $color,
986+
];
987+
}
988+
989+
public static function rgba_to_hex_alpha($color) {
990+
// Remove 'rgba(' and ')' and split the values
991+
$rgba = explode(',', substr($color, 5, -1));
992+
993+
$hexAlpha = array_map(function($val, $i) {
994+
if ($i === 3) {
995+
$opacity = floatval($val);
996+
return str_pad(dechex(ceil($opacity * 255)), 2, '0', STR_PAD_LEFT);
997+
}
998+
$hex = dechex(intval($val));
999+
return str_pad($hex, 2, '0', STR_PAD_LEFT);
1000+
}, $rgba, array_keys($rgba));
1001+
1002+
return '#' . implode('', $hexAlpha);
1003+
}
1004+
9611005
public static function get_block_layout_unit( $block_layouts, $property, $state ) {
9621006
return $block_layouts[ $property ][ $state . 'Unit' ] ?? 'px';
9631007
}

0 commit comments

Comments
 (0)