BlockHooksTrait.php
4 weeks ago
BlockTemplateUtils.php
2 months ago
BlocksSharedState.php
3 months ago
BlocksWpQuery.php
2 years ago
CartCheckoutUtils.php
4 weeks ago
MiniCartUtils.php
1 year ago
ProductAvailabilityUtils.php
11 months ago
ProductDataUtils.php
1 year ago
ProductGalleryUtils.php
4 weeks ago
StyleAttributesUtils.php
1 year ago
Utils.php
2 years ago
StyleAttributesUtils.php
825 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Utils; |
| 3 | |
| 4 | /** |
| 5 | * StyleAttributesUtils class used for getting class and style from attributes. |
| 6 | */ |
| 7 | class StyleAttributesUtils { |
| 8 | |
| 9 | // Empty style array. |
| 10 | const EMPTY_STYLE = [ |
| 11 | 'class' => '', |
| 12 | 'style' => '', |
| 13 | 'value' => '', |
| 14 | ]; |
| 15 | |
| 16 | /** |
| 17 | * If color value is in preset format, convert it to a CSS var. Else return same value |
| 18 | * For example: |
| 19 | * "var:preset|color|pale-pink" -> "var(--wp--preset--color--pale-pink)" |
| 20 | * "#98b66e" -> "#98b66e" |
| 21 | * |
| 22 | * @param string $color_value value to be processed. |
| 23 | * |
| 24 | * @return (string) |
| 25 | */ |
| 26 | public static function get_color_value( $color_value ) { |
| 27 | if ( is_string( $color_value ) && strpos( $color_value, 'var:preset|color|' ) !== false ) { |
| 28 | $color_value = str_replace( 'var:preset|color|', '', $color_value ); |
| 29 | return sprintf( 'var(--wp--preset--color--%s)', $color_value ); |
| 30 | } |
| 31 | |
| 32 | return $color_value; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get CSS value for color preset. |
| 37 | * |
| 38 | * @param string $preset_name Preset name. |
| 39 | * |
| 40 | * @return string CSS value for color preset. |
| 41 | */ |
| 42 | public static function get_preset_value( $preset_name ) { |
| 43 | return "var(--wp--preset--color--$preset_name)"; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get CSS value for shadow preset. Returns the same value if it's not a preset. |
| 48 | * |
| 49 | * @param string $shadow_name Shadow name. |
| 50 | * |
| 51 | * @return string CSS value for shadow preset. |
| 52 | */ |
| 53 | public static function get_shadow_value( $shadow_name ) { |
| 54 | if ( is_string( $shadow_name ) && strpos( $shadow_name, 'var:preset|shadow|' ) !== false ) { |
| 55 | $shadow_name = str_replace( 'var:preset|shadow|', '', $shadow_name ); |
| 56 | return "var(--wp--preset--shadow--{$shadow_name})"; |
| 57 | } |
| 58 | |
| 59 | return $shadow_name; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * If spacing value is in preset format, convert it to a CSS var. Else return same value |
| 64 | * For example: |
| 65 | * "var:preset|spacing|50" -> "var(--wp--preset--spacing--50)" |
| 66 | * "50px" -> "50px" |
| 67 | * |
| 68 | * @param string $spacing_value value to be processed. |
| 69 | * |
| 70 | * @return (string) |
| 71 | */ |
| 72 | public static function get_spacing_value( $spacing_value ) { |
| 73 | // Used following code as reference: https://github.com/WordPress/gutenberg/blob/cff6d70d6ff5a26e212958623dc3130569f95685/lib/block-supports/layout.php/#L219-L225. |
| 74 | if ( is_string( $spacing_value ) && strpos( $spacing_value, 'var:preset|spacing|' ) !== false ) { |
| 75 | $spacing_value = str_replace( 'var:preset|spacing|', '', $spacing_value ); |
| 76 | return sprintf( 'var(--wp--preset--spacing--%s)', $spacing_value ); |
| 77 | } |
| 78 | |
| 79 | return $spacing_value; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get class and style for align from attributes. |
| 84 | * |
| 85 | * @param array $attributes Block attributes. |
| 86 | * @return array |
| 87 | */ |
| 88 | public static function get_align_class_and_style( $attributes ) { |
| 89 | $align_attribute = $attributes['align'] ?? null; |
| 90 | |
| 91 | if ( 'wide' === $align_attribute ) { |
| 92 | return array( |
| 93 | 'class' => 'alignwide', |
| 94 | 'style' => null, |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | if ( 'full' === $align_attribute ) { |
| 99 | return array( |
| 100 | 'class' => 'alignfull', |
| 101 | 'style' => null, |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | if ( 'left' === $align_attribute ) { |
| 106 | return array( |
| 107 | 'class' => 'alignleft', |
| 108 | 'style' => null, |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | if ( 'right' === $align_attribute ) { |
| 113 | return array( |
| 114 | 'class' => 'alignright', |
| 115 | 'style' => null, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | if ( 'center' === $align_attribute ) { |
| 120 | return array( |
| 121 | 'class' => 'aligncenter', |
| 122 | 'style' => null, |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | return self::EMPTY_STYLE; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get class and style for background-color from attributes. |
| 131 | * |
| 132 | * @param array $attributes Block attributes. |
| 133 | * @return array |
| 134 | */ |
| 135 | public static function get_background_color_class_and_style( $attributes ) { |
| 136 | $gradient = $attributes['gradient'] ?? null; |
| 137 | $background_color = $attributes['backgroundColor'] ?? ''; |
| 138 | $custom_background_color = $attributes['style']['color']['background'] ?? ''; |
| 139 | $classes = [ $gradient ]; |
| 140 | $styles = []; |
| 141 | $value = null; |
| 142 | |
| 143 | if ( $background_color || $custom_background_color || $gradient ) { |
| 144 | $classes[] = 'has-background'; |
| 145 | } |
| 146 | |
| 147 | if ( $background_color ) { |
| 148 | $classes[] = sprintf( 'has-%s-background-color', $background_color ); |
| 149 | $value = self::get_preset_value( $background_color ); |
| 150 | } |
| 151 | |
| 152 | if ( $custom_background_color ) { |
| 153 | $styles[] = sprintf( 'background-color: %s;', $custom_background_color ); |
| 154 | $value = $custom_background_color; |
| 155 | } |
| 156 | |
| 157 | if ( $gradient ) { |
| 158 | $classes[] = sprintf( 'has-%s-gradient-background', $gradient ); |
| 159 | } |
| 160 | |
| 161 | return array( |
| 162 | 'class' => self::join_styles( $classes ), |
| 163 | 'style' => self::join_styles( $styles ), |
| 164 | 'value' => $value, |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Join classes and styles while removing duplicates and null values. |
| 170 | * |
| 171 | * @param array $rules Array of classes or styles. |
| 172 | * @return array |
| 173 | */ |
| 174 | protected static function join_styles( $rules ) { |
| 175 | return implode( ' ', array_unique( array_filter( $rules ) ) ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get class and style for border-color from attributes. |
| 180 | * |
| 181 | * Data passed to this function is not always consistent. It can be: |
| 182 | * Linked - preset color: $attributes['borderColor'] => 'luminous-vivid-orange'. |
| 183 | * Linked - custom color: $attributes['style']['border']['color'] => '#681228'. |
| 184 | * Unlinked - preset color: $attributes['style']['border']['top']['color'] => 'var:preset|color|luminous-vivid-orange' |
| 185 | * Unlinked - custom color: $attributes['style']['border']['top']['color'] => '#681228'. |
| 186 | * |
| 187 | * @param array $attributes Block attributes. |
| 188 | * @return array |
| 189 | */ |
| 190 | public static function get_border_color_class_and_style( $attributes ) { |
| 191 | $border_color_linked_preset = $attributes['borderColor'] ?? ''; |
| 192 | $border_color_linked_custom = $attributes['style']['border']['color'] ?? ''; |
| 193 | $custom_border = $attributes['style']['border'] ?? ''; |
| 194 | |
| 195 | $class = ''; |
| 196 | $style = ''; |
| 197 | $value = ''; |
| 198 | |
| 199 | if ( $border_color_linked_preset ) { |
| 200 | // Linked preset color. |
| 201 | $class = sprintf( 'has-border-color has-%s-border-color', $border_color_linked_preset ); |
| 202 | $value = self::get_preset_value( $border_color_linked_preset ); |
| 203 | $style = 'border-color:' . $value . ';'; |
| 204 | } elseif ( $border_color_linked_custom ) { |
| 205 | // Linked custom color. |
| 206 | $style .= 'border-color:' . $border_color_linked_custom . ';'; |
| 207 | $value = $border_color_linked_custom; |
| 208 | } elseif ( is_array( $custom_border ) ) { |
| 209 | // Unlinked. |
| 210 | foreach ( $custom_border as $border_color_key => $border_color_value ) { |
| 211 | if ( is_array( $border_color_value ) && array_key_exists( 'color', ( $border_color_value ) ) ) { |
| 212 | $style .= 'border-' . $border_color_key . '-color:' . self::get_color_value( $border_color_value['color'] ) . ';'; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if ( ! $class && ! $style ) { |
| 218 | return self::EMPTY_STYLE; |
| 219 | } |
| 220 | |
| 221 | return array( |
| 222 | 'class' => $class, |
| 223 | 'style' => $style, |
| 224 | 'value' => $value, |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Get class and style for border-radius from attributes. |
| 230 | * |
| 231 | * @param array $attributes Block attributes. |
| 232 | * @return array |
| 233 | */ |
| 234 | public static function get_border_radius_class_and_style( $attributes ) { |
| 235 | $custom_border_radius = $attributes['style']['border']['radius'] ?? ''; |
| 236 | |
| 237 | if ( '' === $custom_border_radius ) { |
| 238 | return self::EMPTY_STYLE; |
| 239 | } |
| 240 | |
| 241 | $style = ''; |
| 242 | |
| 243 | if ( is_string( $custom_border_radius ) ) { |
| 244 | // Linked sides. |
| 245 | $style = 'border-radius:' . $custom_border_radius . ';'; |
| 246 | } else { |
| 247 | // Unlinked sides. |
| 248 | $border_radius = array(); |
| 249 | |
| 250 | $border_radius['border-top-left-radius'] = $custom_border_radius['topLeft'] ?? ''; |
| 251 | $border_radius['border-top-right-radius'] = $custom_border_radius['topRight'] ?? ''; |
| 252 | $border_radius['border-bottom-right-radius'] = $custom_border_radius['bottomRight'] ?? ''; |
| 253 | $border_radius['border-bottom-left-radius'] = $custom_border_radius['bottomLeft'] ?? ''; |
| 254 | |
| 255 | foreach ( $border_radius as $border_radius_side => $border_radius_value ) { |
| 256 | if ( '' !== $border_radius_value ) { |
| 257 | $style .= $border_radius_side . ':' . $border_radius_value . ';'; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return array( |
| 263 | 'class' => null, |
| 264 | 'style' => $style, |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get class and style for border width from attributes. |
| 270 | * |
| 271 | * @param array $attributes Block attributes. |
| 272 | * @return array |
| 273 | */ |
| 274 | public static function get_border_width_class_and_style( $attributes ) { |
| 275 | $custom_border = $attributes['style']['border'] ?? ''; |
| 276 | |
| 277 | if ( '' === $custom_border ) { |
| 278 | return self::EMPTY_STYLE; |
| 279 | } |
| 280 | |
| 281 | $style = ''; |
| 282 | |
| 283 | if ( array_key_exists( 'width', ( $custom_border ) ) && ! empty( $custom_border['width'] ) ) { |
| 284 | // Linked sides. |
| 285 | $style = 'border-width:' . $custom_border['width'] . ';'; |
| 286 | } else { |
| 287 | // Unlinked sides. |
| 288 | foreach ( $custom_border as $border_width_side => $border_width_value ) { |
| 289 | if ( isset( $border_width_value['width'] ) ) { |
| 290 | $style .= 'border-' . $border_width_side . '-width:' . $border_width_value['width'] . ';'; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return array( |
| 296 | 'class' => null, |
| 297 | 'style' => $style, |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Get class and style for border width from attributes. |
| 303 | * |
| 304 | * @param array $attributes Block attributes. |
| 305 | * @return array |
| 306 | */ |
| 307 | public static function get_border_style_class_and_style( $attributes ) { |
| 308 | $custom_border = $attributes['style']['border'] ?? ''; |
| 309 | |
| 310 | if ( '' === $custom_border ) { |
| 311 | return self::EMPTY_STYLE; |
| 312 | } |
| 313 | |
| 314 | $style = ''; |
| 315 | |
| 316 | if ( array_key_exists( 'style', ( $custom_border ) ) && ! empty( $custom_border['style'] ) ) { |
| 317 | $style = 'border-style:' . $custom_border['style'] . ';'; |
| 318 | } else { |
| 319 | foreach ( $custom_border as $side => $value ) { |
| 320 | if ( isset( $value['style'] ) ) { |
| 321 | $style .= 'border-' . $side . '-style:' . $value['style'] . ';'; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return array( |
| 327 | 'class' => null, |
| 328 | 'style' => $style, |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get space-separated classes from block attributes. |
| 334 | * |
| 335 | * @param array $attributes Block attributes. |
| 336 | * @param array $properties Properties to get classes from. |
| 337 | * |
| 338 | * @return string Space-separated classes. |
| 339 | */ |
| 340 | public static function get_classes_by_attributes( $attributes, $properties = array() ) { |
| 341 | $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
| 342 | |
| 343 | return $classes_and_styles['classes']; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Get class and style for font-family from attributes. |
| 348 | * |
| 349 | * @param array $attributes Block attributes. |
| 350 | * @return array |
| 351 | */ |
| 352 | public static function get_font_family_class_and_style( $attributes ) { |
| 353 | |
| 354 | $font_family = $attributes['fontFamily'] ?? ''; |
| 355 | |
| 356 | if ( $font_family ) { |
| 357 | return array( |
| 358 | 'class' => sprintf( 'has-%s-font-family', $font_family ), |
| 359 | 'style' => null, |
| 360 | ); |
| 361 | } |
| 362 | return self::EMPTY_STYLE; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Get class and style for font-size from attributes. |
| 367 | * |
| 368 | * @param array $attributes Block attributes. |
| 369 | * @return array |
| 370 | */ |
| 371 | public static function get_font_size_class_and_style( $attributes ) { |
| 372 | |
| 373 | $font_size = $attributes['fontSize'] ?? ''; |
| 374 | |
| 375 | $custom_font_size = $attributes['style']['typography']['fontSize'] ?? ''; |
| 376 | |
| 377 | if ( ! $font_size && '' === $custom_font_size ) { |
| 378 | return self::EMPTY_STYLE; |
| 379 | } |
| 380 | |
| 381 | if ( $font_size ) { |
| 382 | return array( |
| 383 | 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ), |
| 384 | 'style' => null, |
| 385 | ); |
| 386 | } elseif ( '' !== $custom_font_size ) { |
| 387 | return array( |
| 388 | 'class' => null, |
| 389 | 'style' => sprintf( 'font-size: %s;', $custom_font_size ), |
| 390 | ); |
| 391 | } |
| 392 | |
| 393 | return self::EMPTY_STYLE; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Get class and style for font-style from attributes. |
| 398 | * |
| 399 | * @param array $attributes Block attributes. |
| 400 | * @return array |
| 401 | */ |
| 402 | public static function get_font_style_class_and_style( $attributes ) { |
| 403 | |
| 404 | $custom_font_style = $attributes['style']['typography']['fontStyle'] ?? ''; |
| 405 | |
| 406 | if ( '' !== $custom_font_style ) { |
| 407 | return array( |
| 408 | 'class' => null, |
| 409 | 'style' => sprintf( 'font-style: %s;', $custom_font_style ), |
| 410 | ); |
| 411 | } |
| 412 | return self::EMPTY_STYLE; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Get class and style for font-weight from attributes. |
| 417 | * |
| 418 | * @param array $attributes Block attributes. |
| 419 | * @return array |
| 420 | */ |
| 421 | public static function get_font_weight_class_and_style( $attributes ) { |
| 422 | |
| 423 | $custom_font_weight = $attributes['style']['typography']['fontWeight'] ?? ''; |
| 424 | |
| 425 | if ( '' !== $custom_font_weight ) { |
| 426 | return array( |
| 427 | 'class' => null, |
| 428 | 'style' => sprintf( 'font-weight: %s;', $custom_font_weight ), |
| 429 | ); |
| 430 | } |
| 431 | return self::EMPTY_STYLE; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Get class and style for letter-spacing from attributes. |
| 436 | * |
| 437 | * @param array $attributes Block attributes. |
| 438 | * @return array |
| 439 | */ |
| 440 | public static function get_letter_spacing_class_and_style( $attributes ) { |
| 441 | |
| 442 | $custom_letter_spacing = $attributes['style']['typography']['letterSpacing'] ?? ''; |
| 443 | |
| 444 | if ( '' !== $custom_letter_spacing ) { |
| 445 | return array( |
| 446 | 'class' => null, |
| 447 | 'style' => sprintf( 'letter-spacing: %s;', $custom_letter_spacing ), |
| 448 | ); |
| 449 | } |
| 450 | return self::EMPTY_STYLE; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Get class and style for line height from attributes. |
| 455 | * |
| 456 | * @param array $attributes Block attributes. |
| 457 | * @return array |
| 458 | */ |
| 459 | public static function get_line_height_class_and_style( $attributes ) { |
| 460 | |
| 461 | $line_height = $attributes['style']['typography']['lineHeight'] ?? ''; |
| 462 | |
| 463 | if ( ! $line_height ) { |
| 464 | return self::EMPTY_STYLE; |
| 465 | } |
| 466 | |
| 467 | return array( |
| 468 | 'class' => null, |
| 469 | 'style' => sprintf( 'line-height: %s;', $line_height ), |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Get a value from an array based on a path e.g style.elements.link |
| 475 | * |
| 476 | * @param array $array Target array. |
| 477 | * @param string $path Path joined by delimiter. |
| 478 | * @param string $delimiter Chosen delimiter defaults to ".". |
| 479 | * @return mixed |
| 480 | */ |
| 481 | protected static function array_get_value_by_path( array &$array, $path, $delimiter = '.' ) { |
| 482 | $array_path = explode( $delimiter, $path ); |
| 483 | $ref = &$array; |
| 484 | |
| 485 | foreach ( $array_path as $key ) { |
| 486 | if ( is_array( $ref ) && array_key_exists( $key, $ref ) ) { |
| 487 | $ref = &$ref[ $key ]; |
| 488 | } else { |
| 489 | return null; |
| 490 | } |
| 491 | } |
| 492 | return $ref; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Get class and style for link-color from attributes. |
| 497 | * |
| 498 | * @param array $attributes Block attributes. |
| 499 | * @return array |
| 500 | */ |
| 501 | public static function get_link_color_class_and_style( $attributes ) { |
| 502 | $link_color = self::array_get_value_by_path( $attributes, 'style.elements.link.color.text' ); |
| 503 | |
| 504 | if ( empty( $link_color ) ) { |
| 505 | return self::EMPTY_STYLE; |
| 506 | } |
| 507 | |
| 508 | // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. |
| 509 | // If the link color is selected from the core color picker, the value of $link_color is an hex value. |
| 510 | // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. |
| 511 | if ( strstr( $link_color, '|' ) ) { |
| 512 | $link_color_parts = explode( '|', $link_color ); |
| 513 | $link_color = self::get_preset_value( end( $link_color_parts ) ); |
| 514 | } |
| 515 | |
| 516 | return array( |
| 517 | 'class' => 'has-link-color', |
| 518 | 'style' => sprintf( 'color: %s;', $link_color ), |
| 519 | 'value' => $link_color, |
| 520 | ); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Get class and style for link-hover-color from attributes. |
| 525 | * |
| 526 | * @param array $attributes Block attributes. |
| 527 | * @return array |
| 528 | */ |
| 529 | public static function get_link_hover_color_class_and_style( $attributes ) { |
| 530 | $link_color = self::array_get_value_by_path( $attributes, 'style.elements.link.:hover.color.text' ); |
| 531 | |
| 532 | if ( empty( $link_color ) ) { |
| 533 | return self::EMPTY_STYLE; |
| 534 | } |
| 535 | |
| 536 | // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug. |
| 537 | // If the link color is selected from the core color picker, the value of $link_color is an hex value. |
| 538 | // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value. |
| 539 | if ( strstr( $link_color, '|' ) ) { |
| 540 | $link_color_parts = explode( '|', $link_color ); |
| 541 | $link_color = self::get_preset_value( end( $link_color_parts ) ); |
| 542 | } |
| 543 | |
| 544 | return array( |
| 545 | 'class' => 'has-link-color', |
| 546 | 'style' => sprintf( 'color: %s;', $link_color ), |
| 547 | 'value' => $link_color, |
| 548 | ); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Get class and style for margin from attributes. |
| 553 | * |
| 554 | * @param array $attributes Block attributes. |
| 555 | * @return array |
| 556 | */ |
| 557 | public static function get_margin_class_and_style( $attributes ) { |
| 558 | $margin = $attributes['style']['spacing']['margin'] ?? null; |
| 559 | |
| 560 | if ( ! $margin ) { |
| 561 | return self::EMPTY_STYLE; |
| 562 | } |
| 563 | |
| 564 | $spacing_values_css = ''; |
| 565 | |
| 566 | foreach ( $margin as $margin_side => $margin_value ) { |
| 567 | $spacing_values_css .= 'margin-' . $margin_side . ':' . self::get_spacing_value( $margin_value ) . ';'; |
| 568 | } |
| 569 | |
| 570 | return array( |
| 571 | 'class' => null, |
| 572 | 'style' => $spacing_values_css, |
| 573 | ); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Get class and style for padding from attributes. |
| 578 | * |
| 579 | * @param array $attributes Block attributes. |
| 580 | * |
| 581 | * @return array |
| 582 | */ |
| 583 | public static function get_padding_class_and_style( $attributes ) { |
| 584 | $padding = $attributes['style']['spacing']['padding'] ?? null; |
| 585 | |
| 586 | if ( ! $padding ) { |
| 587 | return self::EMPTY_STYLE; |
| 588 | } |
| 589 | |
| 590 | $spacing_values_css = ''; |
| 591 | |
| 592 | foreach ( $padding as $padding_side => $padding_value ) { |
| 593 | $spacing_values_css .= 'padding-' . $padding_side . ':' . self::get_spacing_value( $padding_value ) . ';'; |
| 594 | } |
| 595 | |
| 596 | return array( |
| 597 | 'class' => null, |
| 598 | 'style' => $spacing_values_css, |
| 599 | ); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Get class and style for shadow from attributes. |
| 604 | * |
| 605 | * @param array $attributes Block attributes. |
| 606 | * @return array |
| 607 | */ |
| 608 | public static function get_shadow_class_and_style( $attributes ) { |
| 609 | $shadow = $attributes['style']['shadow'] ?? null; |
| 610 | |
| 611 | if ( ! $shadow ) { |
| 612 | return self::EMPTY_STYLE; |
| 613 | } |
| 614 | |
| 615 | return array( |
| 616 | 'class' => null, |
| 617 | 'style' => sprintf( 'box-shadow: %s;', self::get_shadow_value( $shadow ) ), |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Get space-separated style rules from block attributes. |
| 623 | * |
| 624 | * @param array $attributes Block attributes. |
| 625 | * @param array $properties Properties to get styles from. |
| 626 | * |
| 627 | * @return string Space-separated style rules. |
| 628 | */ |
| 629 | public static function get_styles_by_attributes( $attributes, $properties = array() ) { |
| 630 | $classes_and_styles = self::get_classes_and_styles_by_attributes( $attributes, $properties ); |
| 631 | |
| 632 | return $classes_and_styles['styles']; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Get class and style for text align from attributes. |
| 637 | * |
| 638 | * @param array $attributes Block attributes. |
| 639 | * @return array |
| 640 | */ |
| 641 | public static function get_text_align_class_and_style( $attributes ) { |
| 642 | // Check if the text align is set in the attributes manually (legacy) or in the global styles. |
| 643 | $text_align = $attributes['textAlign'] ?? $attributes['style']['typography']['textAlign'] ?? null; |
| 644 | |
| 645 | if ( $text_align ) { |
| 646 | return array( |
| 647 | 'class' => 'has-text-align-' . $text_align, |
| 648 | 'style' => null, |
| 649 | ); |
| 650 | } |
| 651 | return self::EMPTY_STYLE; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Get class and style for text-color from attributes. |
| 656 | * |
| 657 | * @param array $attributes Block attributes. |
| 658 | * @return array |
| 659 | */ |
| 660 | public static function get_text_color_class_and_style( $attributes ) { |
| 661 | |
| 662 | $text_color = $attributes['textColor'] ?? ''; |
| 663 | |
| 664 | $custom_text_color = $attributes['style']['color']['text'] ?? ''; |
| 665 | |
| 666 | if ( ! $text_color && ! $custom_text_color ) { |
| 667 | return self::EMPTY_STYLE; |
| 668 | } |
| 669 | |
| 670 | if ( $text_color ) { |
| 671 | return array( |
| 672 | 'class' => sprintf( 'has-text-color has-%s-color', $text_color ), |
| 673 | 'style' => null, |
| 674 | 'value' => self::get_preset_value( $text_color ), |
| 675 | ); |
| 676 | } elseif ( $custom_text_color ) { |
| 677 | return array( |
| 678 | 'class' => null, |
| 679 | 'style' => sprintf( 'color: %s;', $custom_text_color ), |
| 680 | 'value' => $custom_text_color, |
| 681 | ); |
| 682 | } |
| 683 | |
| 684 | return self::EMPTY_STYLE; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Get class and style for text-decoration from attributes. |
| 689 | * |
| 690 | * @param array $attributes Block attributes. |
| 691 | * |
| 692 | * @return array |
| 693 | */ |
| 694 | public static function get_text_decoration_class_and_style( $attributes ) { |
| 695 | |
| 696 | $custom_text_decoration = $attributes['style']['typography']['textDecoration'] ?? ''; |
| 697 | |
| 698 | if ( '' !== $custom_text_decoration ) { |
| 699 | return array( |
| 700 | 'class' => null, |
| 701 | 'style' => sprintf( 'text-decoration: %s;', $custom_text_decoration ), |
| 702 | ); |
| 703 | } |
| 704 | |
| 705 | return self::EMPTY_STYLE; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Get class and style for text-transform from attributes. |
| 710 | * |
| 711 | * @param array $attributes Block attributes. |
| 712 | * @return array |
| 713 | */ |
| 714 | public static function get_text_transform_class_and_style( $attributes ) { |
| 715 | |
| 716 | $custom_text_transform = $attributes['style']['typography']['textTransform'] ?? ''; |
| 717 | |
| 718 | if ( '' !== $custom_text_transform ) { |
| 719 | return array( |
| 720 | 'class' => null, |
| 721 | 'style' => sprintf( 'text-transform: %s;', $custom_text_transform ), |
| 722 | ); |
| 723 | } |
| 724 | return self::EMPTY_STYLE; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Get extra CSS classes from attributes. |
| 729 | * |
| 730 | * @param array $attributes Block attributes. |
| 731 | * @return array |
| 732 | */ |
| 733 | public static function get_classes_from_attributes( $attributes ) { |
| 734 | |
| 735 | $extra_css_classes = $attributes['className'] ?? ''; |
| 736 | |
| 737 | if ( '' !== $extra_css_classes ) { |
| 738 | return array( |
| 739 | 'class' => esc_attr( $extra_css_classes ), |
| 740 | 'style' => null, |
| 741 | ); |
| 742 | } |
| 743 | return self::EMPTY_STYLE; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * Get classes and styles from attributes. |
| 748 | * |
| 749 | * Excludes link_color and link_hover_color since those should not apply to the container. |
| 750 | * |
| 751 | * @param array $attributes Block attributes. |
| 752 | * @param array $properties Properties to get classes/styles from. |
| 753 | * @param array $exclude Properties to exclude. |
| 754 | * @return array |
| 755 | */ |
| 756 | public static function get_classes_and_styles_by_attributes( $attributes, $properties = array(), $exclude = array() ) { |
| 757 | $classes_and_styles = array( |
| 758 | 'align' => self::get_align_class_and_style( $attributes ), |
| 759 | 'background_color' => self::get_background_color_class_and_style( $attributes ), |
| 760 | 'border_color' => self::get_border_color_class_and_style( $attributes ), |
| 761 | 'border_radius' => self::get_border_radius_class_and_style( $attributes ), |
| 762 | 'border_width' => self::get_border_width_class_and_style( $attributes ), |
| 763 | 'border_style' => self::get_border_style_class_and_style( $attributes ), |
| 764 | 'font_family' => self::get_font_family_class_and_style( $attributes ), |
| 765 | 'font_size' => self::get_font_size_class_and_style( $attributes ), |
| 766 | 'font_style' => self::get_font_style_class_and_style( $attributes ), |
| 767 | 'font_weight' => self::get_font_weight_class_and_style( $attributes ), |
| 768 | 'letter_spacing' => self::get_letter_spacing_class_and_style( $attributes ), |
| 769 | 'line_height' => self::get_line_height_class_and_style( $attributes ), |
| 770 | 'margin' => self::get_margin_class_and_style( $attributes ), |
| 771 | 'padding' => self::get_padding_class_and_style( $attributes ), |
| 772 | 'shadow' => self::get_shadow_class_and_style( $attributes ), |
| 773 | 'text_align' => self::get_text_align_class_and_style( $attributes ), |
| 774 | 'text_color' => self::get_text_color_class_and_style( $attributes ), |
| 775 | 'text_decoration' => self::get_text_decoration_class_and_style( $attributes ), |
| 776 | 'text_transform' => self::get_text_transform_class_and_style( $attributes ), |
| 777 | 'extra_classes' => self::get_classes_from_attributes( $attributes ), |
| 778 | ); |
| 779 | |
| 780 | if ( ! empty( $properties ) ) { |
| 781 | foreach ( $classes_and_styles as $key => $value ) { |
| 782 | if ( ! in_array( $key, $properties, true ) ) { |
| 783 | unset( $classes_and_styles[ $key ] ); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | if ( ! empty( $exclude ) ) { |
| 789 | foreach ( $classes_and_styles as $key => $value ) { |
| 790 | if ( in_array( $key, $exclude, true ) ) { |
| 791 | unset( $classes_and_styles[ $key ] ); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | $classes_and_styles = array_filter( $classes_and_styles ); |
| 797 | |
| 798 | $classes = array_map( |
| 799 | function ( $item ) { |
| 800 | return $item['class']; |
| 801 | }, |
| 802 | $classes_and_styles |
| 803 | ); |
| 804 | |
| 805 | $styles = array_map( |
| 806 | function ( $item ) { |
| 807 | return $item['style']; |
| 808 | }, |
| 809 | // Exclude link color styles from parent to avoid conflict with text color. |
| 810 | array_diff_key( |
| 811 | $classes_and_styles, |
| 812 | array_flip( array( 'link_color' ) ) |
| 813 | ) |
| 814 | ); |
| 815 | |
| 816 | $classes = array_filter( $classes ); |
| 817 | $styles = array_filter( $styles ); |
| 818 | |
| 819 | return array( |
| 820 | 'classes' => implode( ' ', $classes ), |
| 821 | 'styles' => implode( ' ', $styles ), |
| 822 | ); |
| 823 | } |
| 824 | } |
| 825 |