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 / Blocks / DataHelper.php
kubio / lib / src / Core / Blocks Last commit date
Query 1 year ago BlockBase.php 1 year ago BlockContainerBase.php 1 year ago BlockElement.php 3 years ago BlockStyle.php 4 years ago DataHelper.php 1 year ago TemplatePartBlockBase.php 1 year ago
DataHelper.php
245 lines
1 <?php
2
3 namespace Kubio\Core\Blocks;
4
5 use Kubio\Config;
6 use Kubio\Core\LodashBasic;
7 use Kubio\Core\StyleManager\Utils;
8
9 class DataHelper {
10
11 protected $_merged_data = null;
12 protected $default = null;
13 protected $attributes = null;
14
15 public function __construct( $default, $attributes ) {
16 $this->attributes = $attributes;
17 $this->default = $default;
18 }
19
20 public function getMainAttributeProp( $path, $default = null ) {
21 $main_attr = $this->getMergedMainAttribute();
22 return LodashBasic::get( $main_attr, $path, $default );
23 }
24
25 public function getMergedMainAttribute() {
26 if ( ! $this->_merged_data ) {
27 $this->_merged_data = $this->getMergedData();
28 }
29 return $this->_merged_data;
30 }
31
32 public function getMainAttributeData() {
33 return LodashBasic::get( $this->getAttributes(), Config::$mainAttributeKey, array() );
34 }
35
36 public function getMergedData() {
37
38 $data = $this->getMainAttributeData();
39 $defaultValue = $this->getDefaultValue();
40
41 $style = $this->mergeStyle( LodashBasic::get( $data, 'style' ), LodashBasic::get( $defaultValue, 'style' ), array() );
42 $props = $this->mergeProps( LodashBasic::get( $data, 'props' ), LodashBasic::get( $defaultValue, 'props' ) );
43 $_style = $this->mergeStyle( LodashBasic::get( $data, '_style' ), LodashBasic::get( $defaultValue, '_style' ), array() );
44
45 $mergedData = LodashBasic::merge(
46 $data,
47 array(
48 'style' => $style,
49 'props' => $props,
50 '_style' => $_style,
51 )
52 );
53 return $mergedData;
54 }
55
56 public function mergeProps( $props, $defaultProps ) {
57
58 $mergedData = LodashBasic::merge( array(), $defaultProps, $props );
59 $dataByMedia = LodashBasic::get( $mergedData, 'media', array() );
60
61 LodashBasic::unsetValue( $mergedData, 'media' );
62 $desktopData = LodashBasic::merge( array(), $mergedData );
63 $mediasById = Config::mediasById();
64 foreach ( $mediasById as $mediaId => $media ) {
65 if ( $mediaId !== 'desktop' ) {
66 LodashBasic::set( $mergedData, array( 'media', $mediaId ), LodashBasic::merge( array(), $desktopData, LodashBasic::get( $dataByMedia, $mediaId ) ) );
67 }
68 }
69 return $mergedData;
70 }
71
72 public function mergeStyle( $style, $defaultStyle, $options ) {
73 $mergedOptions = LodashBasic::merge(
74 array(
75 'states' => array(),
76 'statesByComponent' => array(),
77 ),
78 $options
79 );
80
81 $mergedStyle = LodashBasic::merge( array(), $defaultStyle, $style );
82 $styledComponents = Utils::normalizeStyle( $mergedStyle, array( 'skipClone' => true ) );
83 $mergedStyledComponents = $this->mergeNormalizedComponents( $styledComponents, $mergedOptions );
84
85 $denormalizedMergedStyle = Utils::denormalizeComponents(
86 $mergedStyledComponents
87 );
88 return $denormalizedMergedStyle;
89 // return LodashBasic::merge($defaultStyle, $style);
90 }
91
92
93 public function mergeNormalizedComponents( $style, $data ) {
94 $mergedData = LodashBasic::merge(
95 array(
96 'states' => array(),
97 'statesByComponent' => array(),
98 ),
99 $data
100 );
101 $merged = array();
102 foreach ( array_keys( $style ) as $componentName ) {
103 $componentStates = LodashBasic::get( $mergedData['statesByComponent'], $componentName, $mergedData['states'] );
104 if ( $componentName === 'default' ) {
105 $componentStates = $mergedData['states'];
106 }
107 $merged[ $componentName ] = $this->mergeNormalizedStyle( $style[ $componentName ], array( 'states' => $componentStates ) );
108 }
109
110 return $merged;
111 }
112
113 public function mergeNormalizedStyle( $style, $data ) {
114 $mergedData = LodashBasic::merge(
115 array(
116 'states' => array(),
117 ),
118 $data
119 );
120 $merged = array();
121 $this->addState( $style, 'normal', $merged );
122 foreach ( $mergedData['states'] as $stateId ) {
123 if ( $stateId !== 'normal' ) {
124 $this->addState( $style, $stateId, $merged );
125 }
126 }
127
128 return $merged;
129 }
130
131 public function addState( $normalizedStyle, $state, &$merged = array() ) {
132 $isNormal = $state === 'normal';
133 $desktopNormalStyle = $isNormal ? array() : LodashBasic::get( $normalizedStyle, 'desktop.normal' );
134 $desktopStateStyle = LodashBasic::get( $normalizedStyle, 'desktop.' . $state );
135 LodashBasic::set( $merged, 'desktop.' . $state, LodashBasic::merge( array(), $desktopNormalStyle, $desktopStateStyle ) );
136 foreach ( array_keys( $normalizedStyle ) as $mediaName ) {
137 if ( $mediaName === 'desktop' ) {
138 continue;
139 }
140 $deviceNormalMergedStyle = $isNormal ? array() : LodashBasic::get( $merged, $mediaName . '.normal' );
141 $statePath = $mediaName . '.' . $state;
142 $deviceStateStyle = LodashBasic::get( $normalizedStyle, $statePath, array() );
143 $tempValue = LodashBasic::merge( array(), $desktopNormalStyle, $deviceNormalMergedStyle, $desktopStateStyle, $deviceStateStyle );
144 LodashBasic::set( $merged, $statePath, $tempValue );
145 }
146 }
147 public function getDefaultValue() {
148 return $this->default;
149 }
150
151 public function getAttributes() {
152 return $this->attributes;
153 }
154
155 public function getStyle( $relPath, $default_value = null, $options = array() ) {
156 $absPath = $this->getPathRoot( 'style', $options, $relPath );
157 return $this->getPathValue( $absPath, $default_value, $options );
158 }
159
160 public function getPathRoot( $type, $options, $relPath ) {
161 $local = LodashBasic::get( $options, 'local', false );
162 $type_root = LodashBasic::get( $this->rootPaths( $local ), $type, '' );
163 return $this->getRootPropertyPath( $type_root, $options ) . '.' . $relPath;
164 }
165
166 public function rootPaths( $local ) {
167 $prefix = $local ? '_' : '';
168 return array(
169 'props' => $prefix . 'props',
170 'style' => $prefix . 'style',
171 );
172 }
173
174 public function getRootPropertyPath( $root, $options ) {
175 $paths = LodashBasic::concat( array(), $root );
176 $paths_ordered = array(
177 'ancestor' => 'ancestor',
178 'styledComponent' => 'descendants',
179 'state' => 'states',
180 );
181
182 foreach ( $paths_ordered as $key => $name ) {
183 if ( isset( $options[ $key ] ) && $options[ $key ] ) {
184 $paths = LodashBasic::concat( $paths, array( $name, $options[ $key ] ) );
185 }
186 }
187
188 if ( isset( $options['media'] ) && $options['media'] !== 'desktop' ) {
189 $paths = LodashBasic::concat( $paths, array( 'media', $options['media'] ) );
190 }
191
192 return implode( '.', $paths );
193 }
194
195 public function getPathValue( $abs_path, $default_value = null, $options = array() ) {
196 $fromRoot = LodashBasic::get( $options, 'fromRoot', false );
197 $attr = LodashBasic::get( $options, 'attr', false );
198 $source = $attr ? $this->getAttributes() : ( $fromRoot ? $this->getMainAttributeData() : $this->getMergedMainAttribute() );
199 $value = LodashBasic::get( $source, $abs_path, $default_value );
200 return $value;
201 }
202
203 public function getProp( $relPath, $default_value = null, $options = array() ) {
204 $absPath = $this->getPathRoot( 'props', $options, $relPath );
205 return $this->getPathValue( $absPath, $default_value, $options );
206 }
207
208 public function getPropByMedia( $path, $default_value = null, $options = array() ) {
209 return $this->getPathByMedia( $path, $default_value, $options, 'props' );
210 }
211
212 function getPathByMedia( $relPath, $defaultValue, $options, $type = 'style' ) {
213 $byMedia = array();
214 $mediasById = Config::mediasById();
215 foreach ( $mediasById as $id => $media ) {
216 $mediaOptions = LodashBasic::merge(
217 $options,
218 array(
219 'media' => $id,
220 )
221 );
222 $absPath = $this->getPathRoot( $type, $mediaOptions, $relPath );
223 $byMedia[ $id ] = $this->getPathValue( $absPath, $defaultValue, $mediaOptions );
224 }
225
226 // temporary
227 foreach ( $mediasById as $id => $media ) {
228 if ( $byMedia[ $id ] === null ) {
229 $byMedia[ $id ] = $byMedia['desktop'];
230 }
231 }
232 return $byMedia;
233 }
234
235 public function getStyleByMedia( $path, $default_value = null, $options = array() ) {
236 return $this->getPathByMedia( $path, $default_value, $options, 'style' );
237 }
238
239 public function getAttribute( $relPath, $defaultValue = null, $options = array() ) {
240 $defaultOptions = array( 'attr' => true );
241 $mergedOptions = LodashBasic::merge( $defaultOptions, $options );
242 return $this->getPathValue( $relPath, $defaultValue, $mergedOptions );
243 }
244 }
245