@@ -110,20 +110,181 @@ public function get_block_data( array $block, $content = '', $is_preview = false
110110
111111 // Create class attribute allowing for custom "className" and "align" values.
112112 $ class_name = $ this ->get_slug ();
113+
113114 if ( ! empty ( $ block ['className ' ] ) ) {
114115 $ class_name .= ' ' . $ block ['className ' ];
115116 }
117+
118+ // create align class
116119 if ( ! empty ( $ block ['align ' ] ) ) {
117120 $ class_name .= ' align ' . $ block ['align ' ];
118121 }
119122
123+ // create text color class
124+ if ( ! empty ( $ block ['textColor ' ] ) ) {
125+ $ class_name .= ' has-text-color has- ' . $ block ['textColor ' ] . '-color ' ;
126+ }
127+
128+ // create background color class
129+ if ( ! empty ( $ block ['backgroundColor ' ] ) ) {
130+ $ class_name .= ' has-background has- ' . $ block ['backgroundColor ' ] . '-background-color ' ;
131+ }
132+
133+ // create style attribute value
134+ $ style = '' ;
135+
136+ if ( ! empty ( $ block ['style ' ] ) ) {
137+ $ block_style = $ block ['style ' ];
138+
139+ if ( ! empty ( $ block_style ['spacing ' ] ) ) {
140+ $ style .= $ this ->get_spacing_style ( $ block_style ['spacing ' ] ) . '; ' ;
141+ }
142+
143+ if ( ! empty ( $ block_style ['border ' ] ) ) {
144+ $ style .= $ this ->get_border_style ( $ block_style ['border ' ] );
145+ }
146+ }
147+
120148 return [
121149 'id ' => $ block ['id ' ],
122150 'block_id ' => $ block_id ,
123151 'block_is_preview ' => $ is_preview ,
124152 'block_classname ' => implode ( ' ' , array_map ( 'sanitize_html_class ' , explode ( ' ' , $ class_name ) ) ),
125153 'block_content ' => $ content ,
126154 'block_post_id ' => $ post_id ,
155+ 'block_style ' => $ style ,
127156 ];
128157 }
158+
159+ /**
160+ * Get the CSS value for the block.
161+ *
162+ * @param string $value The value to get the CSS value for.
163+ * @return string The CSS value.
164+ */
165+ private function get_block_css_value ( string $ value ): string {
166+ if ( str_starts_with ( $ value , 'var: ' ) ) {
167+ $ value = str_replace ( 'var: ' , 'var(--wp-- ' , $ value );
168+ $ value = str_replace ( '| ' , '-- ' , $ value );
169+ return $ value . ') ' ;
170+ }
171+
172+ return $ value ;
173+ }
174+
175+ /**
176+ * Format the CSS property.
177+ *
178+ * @param string $prefix The prefix.
179+ * @param string $property The property.
180+ * @param string $suffix The suffix.
181+ * @return string The formatted CSS property.
182+ */
183+ private function format_css_property ( string $ prefix , string $ property , string $ suffix ): string {
184+ $ property = preg_replace ( '/([A-Z])/ ' , '-$1 ' , $ property );
185+ $ property = strtolower ( $ property );
186+
187+ if ( ! empty ( $ prefix ) ) {
188+ $ property = $ prefix . '- ' . $ property ;
189+ }
190+
191+ if ( ! empty ( $ suffix ) ) {
192+ $ property = $ property . '- ' . $ suffix ;
193+ }
194+
195+ return $ property ;
196+ }
197+
198+ /**
199+ * Get the spacing style.
200+ *
201+ * @param array $spacing The spacing.
202+ * @return string The spacing style.
203+ */
204+ private function get_spacing_style ( array $ spacing ): string {
205+ $ style_value = '' ;
206+
207+ if ( empty ( $ spacing ) ) {
208+ return $ style_value ;
209+ }
210+
211+ foreach ( $ spacing as $ type => $ directions ) {
212+ foreach ( $ directions as $ direction => $ value ) {
213+ $ style_value .= $ type . '- ' . $ direction . ': ' . $ this ->get_block_css_value ( $ value ) . '; ' ;
214+ }
215+ }
216+
217+ return $ style_value ;
218+ }
219+
220+ /**
221+ * Get the border style.
222+ *
223+ * @param array $border The border.
224+ * @return string The border style.
225+ */
226+ private function get_border_style ( array $ border ): string {
227+ $ style_value = '' ;
228+
229+ if ( empty ( $ border ) ) {
230+ return $ style_value ;
231+ }
232+
233+ // handle border radius
234+ if ( ! empty ( $ border ['radius ' ] ) ) {
235+ if ( is_array ( $ border ['radius ' ] ) ) {
236+ foreach ( $ border ['radius ' ] as $ direction => $ radius ) {
237+ $ style_value .= $ this ->format_css_property ( 'border ' , $ direction , 'radius ' ) . ': ' . $ this ->get_block_css_value ( $ radius ) . '; ' ;
238+ }
239+ } else {
240+ $ style_value .= 'border-radius: ' . $ this ->get_block_css_value ( $ border ['radius ' ] ) . '; ' ;
241+ }
242+ }
243+
244+ // handle global border value
245+ if ( ! empty ( $ border ['width ' ] ) ) {
246+ $ style_value .= 'border-width: ' . $ this ->get_block_css_value ( $ border ['width ' ] ) . '; ' ;
247+ }
248+
249+ if ( ! empty ( $ border ['color ' ] ) ) {
250+ $ style_value .= 'border-color: ' . $ this ->get_block_css_value ( $ border ['color ' ] ) . '; ' ;
251+ }
252+
253+ if ( empty ( $ border ['style ' ] ) && ! empty ( $ border ['width ' ] ) && ! empty ( $ border ['color ' ] ) ) {
254+ $ border ['style ' ] = 'solid ' ;
255+ }
256+
257+ if ( ! empty ( $ border ['style ' ] ) ) {
258+ $ style_value .= 'border-style: ' . $ this ->get_block_css_value ( $ border ['style ' ] ) . '; ' ;
259+ }
260+
261+ // handle border by direction
262+ foreach ( $ border as $ direction => $ params ) {
263+ if ( empty ( $ params ) || ! in_array ( $ direction , [ 'top ' , 'right ' , 'bottom ' , 'left ' ] ) ) {
264+ continue ;
265+ }
266+
267+ $ border_props_values = [];
268+
269+ if ( ! empty ( $ params ['color ' ] ) ) {
270+ $ border_props_values [] = $ this ->get_block_css_value ( $ params ['color ' ] );
271+ }
272+
273+ if ( ! empty ( $ params ['width ' ] ) ) {
274+ $ border_props_values [] = $ params ['width ' ];
275+ }
276+
277+ if ( empty ( $ params ['style ' ] ) && ! empty ( $ params ['width ' ] ) && ! empty ( $ params ['color ' ] ) ) {
278+ $ params ['style ' ] = 'solid ' ;
279+ }
280+
281+ if ( ! empty ( $ params ['style ' ] ) ) {
282+ $ border_props_values [] = $ params ['style ' ];
283+ }
284+
285+ $ style_value .= 'border- ' . $ direction . ': ' . implode ( ' ' , $ border_props_values ) . '; ' ;
286+ }
287+
288+ return $ style_value ;
289+ }
129290}
0 commit comments