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