This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
AttributesUtils.php
145 lines (116 loc) · 3.34 KB
/
AttributesUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Automattic\WooCommerce\Blocks\Utils;
/**
* AttributesUtils class used for getting class and style from attributes.
*/
class AttributesUtils {
/**
* Get class and style for font-size from attributes.
*
* @param array $attributes Block attributes.
*
* @return string
*/
public static function get_font_size_class_and_style( $attributes ) {
$font_size = $attributes['fontSize'];
$custom_font_size = $attributes['style']['typography']['fontSize'];
if ( ! isset( $font_size ) && ! isset( $custom_font_size ) ) {
return null;
};
$has_named_font_size = ! empty( $font_size );
$has_custom_font_size = isset( $custom_font_size );
if ( $has_named_font_size ) {
return array(
'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ),
'style' => null,
);
} elseif ( $has_custom_font_size ) {
return array(
'class' => null,
'style' => sprintf( 'font-size: %s;', $custom_font_size ),
);
}
return array(
'class' => null,
'style' => null,
);
}
/**
* Get class and style for text-color from attributes.
*
* @param array $attributes Block attributes.
*
* @return (array | null)
*/
public static function get_text_color_class_and_style( $attributes ) {
$text_color = $attributes['textColor'];
$custom_text_color = $attributes['style']['color']['text'];
if ( ! isset( $text_color ) && ! isset( $custom_text_color ) ) {
return null;
};
$text_color_class = sprintf( 'has-text-color' );
$text_color_style = sprintf( 'color: %s;', ( ! empty( $text_color ) ? $text_color : $custom_text_color ) );
return array(
'class' => $text_color_class,
'style' => $text_color_style,
);
}
/**
* Get class and style for link-color from attributes.
*
* @param array $attributes Block attributes.
*
* @return (array | null)
*/
public static function get_link_color_class_and_style( $attributes ) {
$link_color = $attributes['style']['elements']['link']['color']['text'];
if ( ! isset( $link_color ) ) {
return null;
};
$link_color_class = sprintf( 'has-link-color', $link_color );
$link_color_style = sprintf( 'color: %s;', $link_color );
return array(
'class' => $link_color_class,
'style' => $link_color_style,
);
}
/**
* Get class and style for line height from attributes.
*
* @param array $attributes Block attributes.
*
* @return (array | null)
*/
public static function get_line_height_class_and_style( $attributes ) {
$line_height = $attributes['style']['typography']['lineHeight'];
if ( ! isset( $line_height ) ) {
return null;
};
$line_height_style = 'line-height: ' . $line_height . ';';
return array(
'class' => null,
'style' => $line_height_style,
);
}
/**
* Get classes and styles from attributes.
*
* @param array $attributes Block attributes.
*
* @return array
*/
public static function get_classes_and_styles_by_attributes( $attributes ) {
$classes_and_styles = array(
line_height => self::get_line_height_class_and_style( $attributes ),
text_color => self::get_text_color_class_and_style( $attributes ),
font_size => self::get_font_size_class_and_style( $attributes ),
link_color => self::get_link_color_class_and_style( $attributes ),
);
return array_filter(
$classes_and_styles,
function ( $var ) {
return ! is_null( $var );
}
);
}
}