| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\BlockLibrary; |
| 4 |
|
| 5 |
/** |
| 6 |
* Provide general block-related functionality. |
| 7 |
*/ |
| 8 |
class BlockStylesService { |
| 9 |
/** |
| 10 |
* Block. |
| 11 |
* |
| 12 |
* @var object |
| 13 |
*/ |
| 14 |
public $block = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Get the spacing values from the block editor |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
public function getSpacingValues() { |
| 22 |
$block = $this->block ? $this->block : \WP_Block_Supports::$block_to_render; |
| 23 |
$block_type = \WP_Block_Type_Registry::get_instance()->get_registered( |
| 24 |
$block['blockName'] |
| 25 |
); |
| 26 |
$block_attributes = $block['attrs'] ?? []; |
| 27 |
|
| 28 |
$attributes = array(); |
| 29 |
$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); |
| 30 |
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); |
| 31 |
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; |
| 32 |
|
| 33 |
if ( ! $block_styles ) { |
| 34 |
return $attributes; |
| 35 |
} |
| 36 |
|
| 37 |
$spacing_block_styles = array( |
| 38 |
'padding' => null, |
| 39 |
'margin' => null, |
| 40 |
); |
| 41 |
if ( $has_padding_support ) { |
| 42 |
$spacing_block_styles['padding'] = isset( $block_styles['spacing']['padding'] ) ? $block_styles['spacing']['padding'] : null; |
| 43 |
} |
| 44 |
if ( $has_margin_support ) { |
| 45 |
$spacing_block_styles['margin'] = isset( $block_styles['spacing']['margin'] ) ? $block_styles['spacing']['margin'] : null; |
| 46 |
} |
| 47 |
|
| 48 |
return $spacing_block_styles; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the border values from the block editor |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function getBorderValues() { |
| 57 |
$block = $this->block ? $this->block : \WP_Block_Supports::$block_to_render; |
| 58 |
$block_type = \WP_Block_Type_Registry::get_instance()->get_registered( |
| 59 |
$block['blockName'] |
| 60 |
); |
| 61 |
$block_attributes = $block['attrs'] ?? []; |
| 62 |
|
| 63 |
$border_block_styles = array(); |
| 64 |
$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' ); |
| 65 |
$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' ); |
| 66 |
|
| 67 |
// Border radius. |
| 68 |
if ( |
| 69 |
wp_has_border_feature_support( $block_type, 'radius' ) && |
| 70 |
isset( $block_attributes['style']['border']['radius'] ) |
| 71 |
) { |
| 72 |
$border_radius = $block_attributes['style']['border']['radius']; |
| 73 |
|
| 74 |
if ( is_numeric( $border_radius ) ) { |
| 75 |
$border_radius .= 'px'; |
| 76 |
} |
| 77 |
|
| 78 |
$border_block_styles['radius'] = $border_radius; |
| 79 |
} |
| 80 |
|
| 81 |
// Border style. |
| 82 |
if ( |
| 83 |
wp_has_border_feature_support( $block_type, 'style' ) && |
| 84 |
isset( $block_attributes['style']['border']['style'] ) |
| 85 |
) { |
| 86 |
$border_block_styles['style'] = $block_attributes['style']['border']['style']; |
| 87 |
} |
| 88 |
|
| 89 |
// Border width. |
| 90 |
if ( |
| 91 |
$has_border_width_support && |
| 92 |
isset( $block_attributes['style']['border']['width'] ) |
| 93 |
) { |
| 94 |
$border_width = $block_attributes['style']['border']['width']; |
| 95 |
|
| 96 |
// This check handles original unitless implementation. |
| 97 |
if ( is_numeric( $border_width ) ) { |
| 98 |
$border_width .= 'px'; |
| 99 |
} |
| 100 |
|
| 101 |
$border_block_styles['width'] = $border_width; |
| 102 |
} |
| 103 |
|
| 104 |
// Border color. |
| 105 |
if ( |
| 106 |
$has_border_color_support |
| 107 |
) { |
| 108 |
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; |
| 109 |
$custom_border_color = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null; |
| 110 |
$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; |
| 111 |
} |
| 112 |
|
| 113 |
// Generates styles for individual border sides. |
| 114 |
if ( $has_border_color_support || $has_border_width_support ) { |
| 115 |
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { |
| 116 |
$border = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null; |
| 117 |
$border_side_values = array( |
| 118 |
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, |
| 119 |
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, |
| 120 |
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, |
| 121 |
); |
| 122 |
$border_block_styles[ $side ] = $border_side_values; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $border_block_styles; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get the dimensions values from the block editor |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public function getDimensionsValues() { |
| 135 |
$block = $this->block ? $this->block : \WP_Block_Supports::$block_to_render; |
| 136 |
$block_type = \WP_Block_Type_Registry::get_instance()->get_registered( |
| 137 |
$block['blockName'] |
| 138 |
); |
| 139 |
$block_attributes = $block['attrs']; |
| 140 |
|
| 141 |
$attributes = array(); |
| 142 |
|
| 143 |
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false ); |
| 144 |
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; |
| 145 |
|
| 146 |
if ( ! $block_styles ) { |
| 147 |
return $attributes; |
| 148 |
} |
| 149 |
|
| 150 |
$dimensions_block_styles = array(); |
| 151 |
$dimensions_block_styles['minHeight'] = null; |
| 152 |
if ( $has_min_height_support ) { |
| 153 |
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] ) |
| 154 |
? $block_styles['dimensions']['minHeight'] |
| 155 |
: null; |
| 156 |
} |
| 157 |
|
| 158 |
return $dimensions_block_styles; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the color values from the block editor |
| 163 |
* |
| 164 |
* @return array |
| 165 |
*/ |
| 166 |
public function getColorValues() { |
| 167 |
$block = $this->block ? $this->block : \WP_Block_Supports::$block_to_render; |
| 168 |
$block_type = \WP_Block_Type_Registry::get_instance()->get_registered( |
| 169 |
$block['blockName'] |
| 170 |
); |
| 171 |
$block_attributes = $block['attrs'] ?? []; |
| 172 |
|
| 173 |
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false; |
| 174 |
|
| 175 |
$has_text_colors_support = true === $color_support || |
| 176 |
( isset( $color_support['text'] ) && $color_support['text'] ) || |
| 177 |
( is_array( $color_support ) && ! isset( $color_support['text'] ) ); |
| 178 |
$has_background_colors_support = true === $color_support || |
| 179 |
( isset( $color_support['background'] ) && $color_support['background'] ) || |
| 180 |
( is_array( $color_support ) && ! isset( $color_support['background'] ) ); |
| 181 |
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false; |
| 182 |
$color_block_styles = array(); |
| 183 |
|
| 184 |
// Text colors. |
| 185 |
if ( $has_text_colors_support ) { |
| 186 |
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; |
| 187 |
$custom_text_color = isset( $block_attributes['style']['color']['text'] ) ? $block_attributes['style']['color']['text'] : null; |
| 188 |
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; |
| 189 |
} |
| 190 |
|
| 191 |
// Background colors. |
| 192 |
if ( $has_background_colors_support ) { |
| 193 |
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; |
| 194 |
$custom_background_color = isset( $block_attributes['style']['color']['background'] ) ? $block_attributes['style']['color']['background'] : null; |
| 195 |
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; |
| 196 |
} |
| 197 |
|
| 198 |
// Gradients. |
| 199 |
if ( $has_gradients_support ) { |
| 200 |
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; |
| 201 |
$custom_gradient_color = isset( $block_attributes['style']['color']['gradient'] ) ? $block_attributes['style']['color']['gradient'] : null; |
| 202 |
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; |
| 203 |
} |
| 204 |
|
| 205 |
return $color_block_styles; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get the style declarations |
| 210 |
* |
| 211 |
* @param bool $combine Whether to combine the styles or not. |
| 212 |
* @param object $block The block object. |
| 213 |
* |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
public function get( $combine = true, $block = null ) { |
| 217 |
if ( $block ) { |
| 218 |
$this->block = $block; |
| 219 |
} |
| 220 |
|
| 221 |
if ( $combine ) { |
| 222 |
return wp_style_engine_get_styles( |
| 223 |
array( |
| 224 |
'spacing' => $this->getSpacingValues(), |
| 225 |
'border' => $this->getBorderValues(), |
| 226 |
'color' => $this->getColorValues(), |
| 227 |
'dimensions' => $this->getDimensionsValues(), |
| 228 |
), |
| 229 |
array( |
| 230 |
'context' => 'block-supports', |
| 231 |
) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
return array( |
| 236 |
'spacing' => wp_style_engine_get_styles( array( 'spacing' => $this->getSpacingValues() ) ), |
| 237 |
'border' => wp_style_engine_get_styles( array( 'border' => $this->getBorderValues() ) ), |
| 238 |
'color' => wp_style_engine_get_styles( array( 'color' => $this->getColorValues() ) ), |
| 239 |
'dimensions' => wp_style_engine_get_styles( array( 'dimensions' => $this->getDimensionsValues() ) ), |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get the spacing preset css variable. |
| 245 |
* |
| 246 |
* @param string $value The value. |
| 247 |
* |
| 248 |
* @return string|void |
| 249 |
*/ |
| 250 |
public function getBlockGapPresetCssVar( $value ) { |
| 251 |
if ( ! $value ) { |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
preg_match( '/var:preset\|spacing\|(.+)/', $value, $matches ); |
| 256 |
|
| 257 |
if ( ! $matches ) { |
| 258 |
return $value; |
| 259 |
} |
| 260 |
|
| 261 |
return "var(--wp--preset--spacing--$matches[1])"; |
| 262 |
} |
| 263 |
} |
| 264 |
|