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