| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
class ESHB_Block_Helper { |
| 7 |
|
| 8 |
public static function add_responsive_vars ($attributes, &$target_array, $attr_base, $prop_name, $properties = [], $is_object = false) { |
| 9 |
$devices = ['' => 'desktop', 'Tablet' => 'tablet', 'Mobile' => 'mobile']; |
| 10 |
|
| 11 |
foreach ($devices as $d_suffix => $device) { |
| 12 |
$attr_name = $attr_base . $d_suffix; |
| 13 |
$val = isset($attributes[$attr_name]) && $attributes[$attr_name] !== '' ? $attributes[$attr_name] : null; |
| 14 |
|
| 15 |
if ($is_object && is_array($val)) { |
| 16 |
foreach ($properties as $prop_key => $css_prop) { |
| 17 |
if ( isset( $val[$prop_key] ) && $val[$prop_key] !== '' ) { |
| 18 |
$v = $val[$prop_key]; |
| 19 |
if ( in_array( $prop_key, ['top', 'right', 'bottom', 'left', 'fontSize', 'letterSpacing', 'itemGap'] ) ) { |
| 20 |
$v = self::ensure_unit($v); |
| 21 |
} |
| 22 |
$target_array[$device][$css_prop] = $v; |
| 23 |
} |
| 24 |
} |
| 25 |
} elseif ( ! $is_object && ! empty( $val ) ) { |
| 26 |
$v = $val; |
| 27 |
if ( in_array($attr_base, ['itemGap', 'itemColGap', 'itemRowGap']) ) { |
| 28 |
// handled per block usually, but let's store it |
| 29 |
$v = self::ensure_unit($v); |
| 30 |
} |
| 31 |
$target_array[$device][$prop_name] = $v; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public static function ensure_unit ($value) { |
| 37 |
if ( $value === '' || $value === null ) return '0px'; |
| 38 |
if ( is_numeric( $value ) && $value != 0 ) return $value . 'px'; |
| 39 |
return $value; |
| 40 |
} |
| 41 |
|
| 42 |
public static function get_inline_styles ($style_map) { |
| 43 |
$styles = []; |
| 44 |
foreach ( $style_map as $prop => $value ) { |
| 45 |
//if ( $value !== '' && $value !== null && $value !== 'inherit' && $value !== '0px' ) { |
| 46 |
$styles[] = $prop . ':' . $value; |
| 47 |
//} |
| 48 |
} |
| 49 |
return implode( ';', $styles ); |
| 50 |
} |
| 51 |
|
| 52 |
public static function generate_responsive_css($selector, $responsive_data) { |
| 53 |
$css = ""; |
| 54 |
$breakpoints = [ |
| 55 |
'desktop' => '', |
| 56 |
'tablet' => '@media (max-width: 1024px)', |
| 57 |
'mobile' => '@media (max-width: 767px)' |
| 58 |
]; |
| 59 |
|
| 60 |
foreach ($breakpoints as $device => $media) { |
| 61 |
if (!empty($responsive_data[$device])) { |
| 62 |
$decls = ""; |
| 63 |
foreach ($responsive_data[$device] as $prop => $val) { |
| 64 |
$decls .= $prop . ":" . wp_strip_all_tags( $val ) . ";"; |
| 65 |
} |
| 66 |
if ($media) { |
| 67 |
$css .= $media . " { " . $selector . " { " . $decls . " } }\n"; |
| 68 |
} else { |
| 69 |
$css .= $selector . " { " . $decls . " }\n"; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return $css; |
| 74 |
} |
| 75 |
|
| 76 |
public static function add_custom_style( $handle, $selector, $responsive_css = "", $sub_styles = [] ) { |
| 77 |
$css = $responsive_css; |
| 78 |
|
| 79 |
foreach ( $sub_styles as $sub_sel => $style ) { |
| 80 |
if ( ! empty( $style ) ) { |
| 81 |
$css .= $selector . " " . $sub_sel . " { " . $style . "; }\n"; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! empty( $css ) ) { |
| 86 |
//if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 87 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS values are sanitized via wp_strip_all_tags() during generation |
| 88 |
echo '<style>' . $css . '</style>'; |
| 89 |
//} else { |
| 90 |
// wp_add_inline_style( $handle, $css ); |
| 91 |
//} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public static function box_shadow_to_css($shadow) { |
| 96 |
$x = self::ensure_unit($shadow['x'] ?? 0); |
| 97 |
$y = self::ensure_unit($shadow['y'] ?? 0); |
| 98 |
$b = self::ensure_unit($shadow['b'] ?? 0); |
| 99 |
$s = self::ensure_unit($shadow['s'] ?? 0); |
| 100 |
$c = $shadow['c'] ?? 'rgba(0,0,0,0)'; |
| 101 |
return "$x $y $b $s $c"; |
| 102 |
} |
| 103 |
|
| 104 |
public static function border_to_css($border) { |
| 105 |
$width = self::ensure_unit($border['width'] ?? 0); |
| 106 |
$style = $border['style'] ?? 'solid'; |
| 107 |
$color = $border['color'] ?? 'rgba(0,0,0,0)'; |
| 108 |
return "$width $style $color"; |
| 109 |
} |
| 110 |
} |