| 1 |
<?php |
| 2 |
/** |
| 3 |
* Table Block |
| 4 |
* |
| 5 |
* @package Tableberg |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Tableberg; |
| 9 |
|
| 10 |
/** |
| 11 |
* Styling Help full functions utility class |
| 12 |
*/ |
| 13 |
class Utils { |
| 14 |
/** |
| 15 |
* Check if border has split borders. |
| 16 |
* |
| 17 |
* @param array $border - block border. |
| 18 |
* @return bool Whether the border has split sides. |
| 19 |
*/ |
| 20 |
public static function has_split_borders( $border = array() ) { |
| 21 |
$sides = array( 'top', 'right', 'bottom', 'left' ); |
| 22 |
foreach ( $border as $side => $value ) { |
| 23 |
if ( in_array( $side, $sides, true ) ) { |
| 24 |
return true; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the border CSS from attributes. |
| 33 |
* |
| 34 |
* @param array $object - block border. |
| 35 |
* @return array CSS styles for the border. |
| 36 |
*/ |
| 37 |
public static function get_border_css( $object ) { |
| 38 |
$css = array(); |
| 39 |
|
| 40 |
if ( ! self::has_split_borders( $object ) ) { |
| 41 |
$css['top'] = $object; |
| 42 |
$css['right'] = $object; |
| 43 |
$css['bottom'] = $object; |
| 44 |
$css['left'] = $object; |
| 45 |
return $css; |
| 46 |
} |
| 47 |
|
| 48 |
return $object; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the CSS value for a single side of the border. |
| 53 |
* |
| 54 |
* @param array $border - border. |
| 55 |
* @param string $side - border side. |
| 56 |
* @return string CSS value for the specified side. |
| 57 |
*/ |
| 58 |
public static function get_single_side_border_value( $border, $side ) { |
| 59 |
$width = $border[ $side ]['width'] ?? ''; |
| 60 |
$style = $border[ $side ]['style'] ?? ''; |
| 61 |
$color = $border[ $side ]['color'] ?? ''; |
| 62 |
|
| 63 |
return "{$width} " . ( $width && empty( $border[ $side ]['style'] ) ? 'solid' : $style ) . " {$color}"; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get border variables for CSS. |
| 68 |
* |
| 69 |
* @param array $border - border. |
| 70 |
* @param string $slug - slug to use in variable. |
| 71 |
* @return array CSS styles for the border variables. |
| 72 |
*/ |
| 73 |
public static function get_border_variables_css( $border, $slug ) { |
| 74 |
$border_in_dimensions = self::get_border_css( $border ); |
| 75 |
$border_sides = array( 'top', 'right', 'bottom', 'left' ); |
| 76 |
$borders = array(); |
| 77 |
|
| 78 |
foreach ( $border_sides as $side ) { |
| 79 |
$side_property = "--tableberg-{$slug}-border-{$side}"; |
| 80 |
$side_value = self::get_single_side_border_value( $border_in_dimensions, $side ); |
| 81 |
$borders[ $side_property ] = $side_value; |
| 82 |
} |
| 83 |
|
| 84 |
return $borders; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if spacing value is preset or custom. |
| 89 |
* |
| 90 |
* @param string $value - spacing value. |
| 91 |
* @return bool Whether the value is a preset or custom spacing. |
| 92 |
*/ |
| 93 |
public static function is_value_spacing_preset( $value ) { |
| 94 |
if ( ! $value || ! is_string( $value ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
return '0' === $value || strpos( $value, 'var:preset|spacing|' ) === 0; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Return the spacing variable for CSS. |
| 102 |
* |
| 103 |
* @param string $value - spacing value. |
| 104 |
* @return string CSS variable or the original value. |
| 105 |
*/ |
| 106 |
public static function get_spacing_preset_css_var( $value ) { |
| 107 |
if ( ! $value ) { |
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
$matches = array(); |
| 112 |
preg_match( '/var:preset\|spacing\|(.+)/', $value, $matches ); |
| 113 |
|
| 114 |
if ( empty( $matches ) ) { |
| 115 |
return $value; |
| 116 |
} |
| 117 |
return "var(--wp--preset--spacing--{$matches[1]})"; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the CSS for spacing. |
| 122 |
* |
| 123 |
* @param array $object - spacing object. |
| 124 |
* @return array CSS styles for spacing. |
| 125 |
*/ |
| 126 |
public static function get_spacing_css( $object ) { |
| 127 |
$css = array(); |
| 128 |
|
| 129 |
foreach ( $object as $key => $value ) { |
| 130 |
if ( self::is_value_spacing_preset( $value ) ) { |
| 131 |
$css[ $key ] = self::get_spacing_preset_css_var( $value ); |
| 132 |
} else { |
| 133 |
$css[ $key ] = $value; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $css; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Check if a value is undefined. |
| 142 |
* |
| 143 |
* @param mixed $value - value. |
| 144 |
* @return bool Whether the value is undefined. |
| 145 |
*/ |
| 146 |
public static function is_undefined( $value ) { |
| 147 |
return null === $value || ! isset( $value ) || empty( $value ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Generate CSS string if value is not empty. |
| 152 |
* |
| 153 |
* @param array $styles - CSS styles. |
| 154 |
* @return string Generated CSS string. |
| 155 |
*/ |
| 156 |
public static function generate_css_string( $styles ) { |
| 157 |
$css_string = ''; |
| 158 |
|
| 159 |
foreach ( $styles as $key => $value ) { |
| 160 |
if ( ! self::is_undefined( $value ) && false !== $value && trim( $value ) !== '' && trim( $value ) !== 'undefined undefined undefined' && ! empty( $value ) ) { |
| 161 |
$css_string .= $key . ': ' . $value . '; '; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return $css_string; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get background color or gradient. |
| 170 |
* |
| 171 |
* @param array $attributes - block attributes. |
| 172 |
* @param string $color_attr_key - block background color attribute key. |
| 173 |
* @param string $gradient_attr_key - block background gradient attribute key. |
| 174 |
* @return string Background color or gradient. |
| 175 |
*/ |
| 176 |
public static function get_background_color( $attributes, $color_attr_key, $gradient_attr_key ) { |
| 177 |
$bg_color = ''; |
| 178 |
if ( ! empty( $attributes[ $color_attr_key ] ) ) { |
| 179 |
$bg_color = $attributes[ $color_attr_key ]; |
| 180 |
} elseif ( ! empty( $attributes[ $gradient_attr_key ] ) ) { |
| 181 |
$bg_color = $attributes[ $gradient_attr_key ]; |
| 182 |
} |
| 183 |
return $bg_color; |
| 184 |
} |
| 185 |
} |
| 186 |
|