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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Utils / StyleAttributesUtils.php
woocommerce / src / Blocks / Utils Last commit date
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