PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / trunk
Kubio AI Page Builder vtrunk
2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / StyleManager / Utils.php
kubio / lib / src / Core / StyleManager Last commit date
Generics 1 year ago Props 1 month ago BlockStyleRender.php 1 year ago DynamicStyles.php 1 year ago GlobalStyleRender.php 1 year ago MainAttribute.php 4 years ago ParserUtils.php 1 year ago StyleManager.php 2 years ago StyleParser.php 1 year ago StyleRender.php 1 year ago Utils.php 1 year ago
Utils.php
222 lines
1 <?php
2
3 namespace Kubio\Core\StyleManager;
4
5 use Kubio\Config;
6 use Kubio\Core\LodashBasic;
7 use function array_keys;
8
9 class Utils {
10
11 public static function normalizeProps( $props ) {
12 return Utils::getMedias( $props );
13 }
14
15 public static function getMedias( $style ) {
16 return Utils::extractProperty( $style, 'media', 'desktop' );
17 }
18
19 public static function extractProperty( $style, $property, $newName, $defaultValue = array() ) {
20 $descendents = LodashBasic::get( $style, $property, $defaultValue );
21 $defaultDesc = $style;
22 LodashBasic::unsetValue( $defaultDesc, $property );
23 LodashBasic::set( $descendents, $newName, $defaultDesc );
24 return $descendents;
25 }
26
27 public static function denormalizeComponents( $style ) {
28 $componentStates = array();
29 self::walkStyle(
30 $style,
31 function ( $data ) use ( &$componentStates ) {
32 $component = LodashBasic::get( $data, 'element' );
33 $media = LodashBasic::get( $data, 'media' );
34 $state = LodashBasic::get( $data, 'state' );
35 $style = LodashBasic::get( $data, 'style' );
36 $path = array( $component, 'states', $state, 'media', $media );
37 if ( $media === 'desktop' ) {
38 if ( $state === 'normal' ) {
39 $path = array( $component );
40 } else {
41 $path = array( $component, 'states', $state );
42 }
43 } elseif ( $state == 'normal' ) {
44 $path = array( $component, 'media', $media );
45 }
46
47 $oldVal = LodashBasic::get( $componentStates, $path );
48 LodashBasic::set( $componentStates, $path, LodashBasic::merge( array(), $oldVal, $style ) );
49 }
50 );
51 return LodashBasic::merge( LodashBasic::get( $componentStates, 'default' ), array( 'descendants' => LodashBasic::omit( $componentStates, 'default' ) ) );
52 }
53 public static function normalizeStyle( $style, $settings_ ) {
54 $settings = (object) LodashBasic::merge(
55 array(
56 'allowedElements' => false,
57 'skipEmpty' => false,
58 'skipClone' => false,
59 ),
60 $settings_
61 );
62
63 $components = Utils::getElements( $style );
64 if ( $settings->allowedElements ) {
65 $components = LodashBasic::pick( $components, $settings->allowedElements );
66 }
67
68 $normalizedComponents = array();
69
70 foreach ( $components as $elementName => $component ) {
71 $component = is_array( $component ) ? $component : (array) $component;
72 $normalized = ( $settings->skipEmpty ) ? array() : Utils::normalizedDefault();
73 $states = Utils::getStates( $component );
74 foreach ( $states as $stateName => $state ) {
75 $medias = Utils::getMedias( $state );
76 foreach ( $medias as $mediaName => $media ) {
77 LodashBasic::set( $normalized, array( $mediaName, $stateName ), $media );
78 }
79 }
80 $normalizedComponents[ $elementName ] = $normalized;
81 }
82
83 return $normalizedComponents;
84 }
85
86 public static function getElements( $style ) {
87 return Utils::extractProperty( $style, 'descendants', 'default' );
88 }
89
90 public static function normalizedDefault() {
91 $normalized = array();
92 $mediasById = Config::mediasById();
93 foreach ( $mediasById as $mediaId => $media ) {
94 $normalized[ $mediaId ] = Utils::normalizedStates();
95 }
96 return $normalized;
97 }
98
99 public static function normalizedStates() {
100 $normalized = array();
101 $statesById = Config::statesById();
102 foreach ( $statesById as $stateId => $state ) {
103 $normalized[ $stateId ] = array();
104 }
105 return $normalized;
106 }
107
108 public static function getStates( $style ) {
109 return Utils::extractProperty( $style, 'states', 'normal' );
110 }
111
112 public static function walkStyle( $style, $callback ) {
113 $styleKeys = array_keys( $style );
114 usort(
115 $styleKeys,
116 function ( $name ) {
117 return ( $name === 'default' ) ? - 1 : 1;
118 }
119 );
120
121 foreach ( $styleKeys as $elementName ) {
122 $mediaKeys = array_keys( $style[ $elementName ] );
123 usort(
124 $mediaKeys,
125 function ( $name ) {
126 return ( $name === 'desktop' ) ? - 1 : 1;
127 }
128 );
129 foreach ( $mediaKeys as $mediaName ) {
130 $states = $style[ $elementName ][ $mediaName ];
131 $stateKeys = array();
132 $allStates = Config::value( 'states' );
133 foreach ( $allStates as $stateValue ) {
134 $stateId = $stateValue['id'];
135 if ( isset( $states[ $stateId ] ) ) {
136 array_push( $stateKeys, $stateId );
137 }
138 }
139 foreach ( $stateKeys as $stateName ) {
140 $callback(
141 array(
142 'element' => $elementName,
143 'media' => $mediaName,
144 'state' => $stateName,
145 'style' => $states[ $stateName ],
146 )
147 );
148 }
149 }
150 }
151 }
152
153
154 public static function hex2rgba( $color, $opacity = false, $values_only_string = false ) {
155 $default = 'rgb(0,0,0)';
156
157 if ( empty( $color ) ) {
158 return $default;
159 }
160
161 if ( $color[0] == '#' ) {
162 $color = substr( $color, 1 );
163 }
164
165 if ( strlen( $color ) == 6 ) {
166 $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
167 } elseif ( strlen( $color ) == 3 ) {
168 $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
169 } else {
170 return $default;
171 }
172
173 $rgb = array_map( 'hexdec', $hex );
174
175 if ( $opacity ) {
176 if ( abs( $opacity ) > 1 ) {
177 $opacity = 1.0;
178 }
179
180 $output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')';
181 } else {
182 $output = 'rgb(' . implode( ',', $rgb ) . ')';
183 }
184
185 if ( $values_only_string ) {
186 if ( $opacity ) {
187 if ( abs( $opacity ) > 1 ) {
188 $opacity = 1.0;
189 }
190 $output = implode( ',', $rgb ) . ',' . $opacity;
191 } else {
192 $output = implode( ',', $rgb );
193 }
194 }
195
196 return $output;
197 }
198
199
200 public static function normalizeFontWeights( $weights ) {
201 if ( empty( $weights ) ) {
202 return array( '400' );
203 }
204
205 foreach ( $weights as $index => $weight ) {
206 if ( $weight === 'italic' ) {
207 $weights[ $index ] = '400italic';
208 }
209
210 if ( $weight === 'regular' ) {
211 $weights[ $index ] = '400';
212 }
213 }
214
215 $weights = array_unique( $weights );
216 array_map( 'strval', $weights );
217 asort( $weights );
218
219 return $weights;
220 }
221 }
222