PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.13.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.13.0
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 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.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / packages / blocks / Util / BlockStyleAttributes.php
surecart / packages / blocks / Util Last commit date
BlockStyleAttributes.php 3 years ago
BlockStyleAttributes.php
681 lines
1 <?php
2 namespace SureCartBlocks\Util;
3
4 /**
5 * BlockStyleAttributes class used for getting class and style from attributes.
6 */
7 class BlockStyleAttributes {
8 /**
9 * Get class and style for font-size from attributes.
10 *
11 * @param array $attributes Block attributes.
12 *
13 * @return (array | null)
14 */
15 public static function getFontSizeClassAndStyle( $attributes ) {
16
17 $font_size = $attributes['fontSize'] ?? '';
18
19 $custom_font_size = $attributes['style']['typography']['fontSize'] ?? '';
20
21 if ( ! $font_size && '' === $custom_font_size ) {
22 return null;
23 }
24
25 if ( $font_size ) {
26 return array(
27 'class' => sprintf( 'has-font-size has-%s-font-size', $font_size ),
28 'style' => null,
29 );
30 } elseif ( '' !== $custom_font_size ) {
31 return array(
32 'class' => null,
33 'style' => sprintf( 'font-size: %s;', $custom_font_size ),
34 );
35 }
36 return null;
37 }
38
39 /**
40 * Get class and style for font-weight from attributes.
41 *
42 * @param array $attributes Block attributes.
43 *
44 * @return (array | null)
45 */
46 public static function getFontWeightClassAndStyle( $attributes ) {
47
48 $custom_font_weight = $attributes['style']['typography']['fontWeight'] ?? '';
49
50 if ( '' !== $custom_font_weight ) {
51 return array(
52 'class' => null,
53 'style' => sprintf( 'font-weight: %s;', $custom_font_weight ),
54 );
55 }
56 return null;
57 }
58
59 /**
60 * Get class and style for font-weight from attributes.
61 *
62 * @param array $attributes Block attributes.
63 *
64 * @return (array | null)
65 */
66 public static function getTextTransformClassAndStyle( $attributes ) {
67
68 $custom_font_weight = $attributes['style']['typography']['textTransform'] ?? '';
69
70 if ( '' !== $custom_font_weight ) {
71 return array(
72 'class' => null,
73 'style' => sprintf( 'text-transform: %s;', $custom_font_weight ),
74 );
75 }
76 return null;
77 }
78
79 /**
80 * Get class and style for font-style from attributes.
81 *
82 * @param array $attributes Block attributes.
83 *
84 * @return (array | null)
85 */
86 public static function getFontStyleClassAndStyle( $attributes ) {
87
88 $custom_font_style = $attributes['style']['typography']['fontStyle'] ?? '';
89
90 if ( '' !== $custom_font_style ) {
91 return array(
92 'class' => null,
93 'style' => sprintf( 'font-style: %s;', $custom_font_style ),
94 );
95 }
96 return null;
97 }
98
99 /**
100 * Get class and style for font-family from attributes.
101 *
102 * @param array $attributes Block attributes.
103 *
104 * @return (array | null)
105 */
106 public static function getFontFamilyClassAndStyle( $attributes ) {
107
108 $font_family = $attributes['fontFamily'] ?? '';
109
110 if ( $font_family ) {
111 return array(
112 'class' => sprintf( 'has-%s-font-family', $font_family ),
113 'style' => null,
114 );
115 }
116 return null;
117 }
118
119 /**
120 * Get class and style for text-color from attributes.
121 *
122 * @param array $attributes Block attributes.
123 *
124 * @return (array | null)
125 */
126 public static function getTextColorClassAndStyle( $attributes ) {
127
128 $text_color = $attributes['textColor'] ?? '';
129
130 $custom_text_color = $attributes['style']['color']['text'] ?? '';
131
132 if ( ! $text_color && ! $custom_text_color ) {
133 return null;
134 }
135
136 if ( $text_color ) {
137 return array(
138 'class' => sprintf( 'has-text-color has-%s-color', $text_color ),
139 'style' => null,
140 'value' => self::getPresetValue( $text_color ),
141 );
142 } elseif ( $custom_text_color ) {
143 return array(
144 'class' => null,
145 'style' => sprintf( 'color: %s;', $custom_text_color ),
146 'value' => $custom_text_color,
147 );
148 }
149 return null;
150 }
151
152 /**
153 * Get class and style for link-color from attributes.
154 *
155 * @param array $attributes Block attributes.
156 *
157 * @return (array | null)
158 */
159 public static function getLinkColorClassAndStyle( $attributes ) {
160
161 if ( ! isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
162 return null;
163 }
164
165 $link_color = $attributes['style']['elements']['link']['color']['text'];
166
167 // If the link color is selected from the theme color picker, the value of $link_color is var:preset|color|slug.
168 // If the link color is selected from the core color picker, the value of $link_color is an hex value.
169 // When the link color is a string var:preset|color|slug we parsed it for get the slug, otherwise we use the hex value.
170 $index_named_link_color = strrpos( $link_color, '|' );
171
172 if ( ! empty( $index_named_link_color ) ) {
173 $parsed_named_link_color = substr( $link_color, $index_named_link_color + 1 );
174 return array(
175 'class' => null,
176 'style' => sprintf( 'color: %s;', self::getPresetValue( $parsed_named_link_color ) ),
177 'value' => self::getPresetValue( $parsed_named_link_color ),
178 );
179 } else {
180 return array(
181 'class' => null,
182 'style' => sprintf( 'color: %s;', $link_color ),
183 'value' => $link_color,
184 );
185 }
186 }
187
188 /**
189 * Get class and style for line height from attributes.
190 *
191 * @param array $attributes Block attributes.
192 *
193 * @return (array | null)
194 */
195 public static function getLineHeightClassAndStyle( $attributes ) {
196
197 $line_height = $attributes['style']['typography']['lineHeight'] ?? '';
198
199 if ( ! $line_height ) {
200 return null;
201 }
202
203 return array(
204 'class' => null,
205 'style' => sprintf( 'line-height: %s;', $line_height ),
206 );
207 }
208
209 /**
210 * Get class and style for background-color from attributes.
211 *
212 * @param array $attributes Block attributes.
213 *
214 * @return (array | null)
215 */
216 public static function getBackgroundColorClassAndStyle( $attributes ) {
217 $background_color = $attributes['backgroundColor'] ?? '';
218
219 $custom_background_color = $attributes['style']['color']['background'] ?? '';
220
221 if ( ! $background_color && '' === $custom_background_color ) {
222 return null;
223 }
224
225 if ( $background_color ) {
226 return array(
227 'class' => sprintf( 'has-background has-%s-background-color', $background_color ),
228 'style' => null,
229 'value' => self::getPresetValue( $background_color ),
230 );
231 } elseif ( '' !== $custom_background_color ) {
232 return array(
233 'class' => null,
234 'style' => sprintf( 'background-color: %s;', $custom_background_color ),
235 'value' => $custom_background_color,
236 );
237 }
238 return null;
239 }
240
241 /**
242 * Get class and style for background-color from attributes.
243 *
244 * @param array $attributes Block attributes.
245 *
246 * @return (array | null)
247 */
248 public static function getGradientClassAndStyle( $attributes ) {
249 $gradient = $attributes['gradient'] ?? '';
250
251 $custom_gradient = $attributes['style']['color']['gradient'] ?? '';
252
253 if ( ! $gradient && '' === $custom_gradient ) {
254 return null;
255 }
256
257 if ( $gradient ) {
258 return array(
259 'class' => sprintf( 'has-background has-%s-gradient-background', $gradient ),
260 'style' => null,
261 'value' => self::getPresetValue( $gradient ),
262 );
263 } elseif ( '' !== $custom_gradient ) {
264 return array(
265 'class' => null,
266 'style' => sprintf( 'background: %s;', $custom_gradient ),
267 'value' => $custom_gradient,
268 );
269 }
270 return null;
271 }
272
273 /**
274 * Get class and style for border-color from attributes.
275 *
276 * Data passed to this function is not always consistent. It can be:
277 * Linked - preset color: $attributes['borderColor'] => 'luminous-vivid-orange'.
278 * Linked - custom color: $attributes['style']['border']['color'] => '#681228'.
279 * Unlinked - preset color: $attributes['style']['border']['top']['color'] => 'var:preset|color|luminous-vivid-orange'
280 * Unlinked - custom color: $attributes['style']['border']['top']['color'] => '#681228'.
281 *
282 * @param array $attributes Block attributes.
283 *
284 * @return (array | null)
285 */
286 public static function getBorderColorClassAndStyle( $attributes ) {
287
288 $border_color_linked_preset = $attributes['borderColor'] ?? '';
289 $border_color_linked_custom = $attributes['style']['border']['color'] ?? '';
290 $custom_border = $attributes['style']['border'] ?? '';
291
292 $border_color_class = '';
293 $border_color_css = '';
294
295 if ( $border_color_linked_preset ) {
296 // Linked preset color.
297 $border_color_class = sprintf( 'has-border-color has-%s-border-color', $border_color_linked_preset );
298 } elseif ( $border_color_linked_custom ) {
299 // Linked custom color.
300 $border_color_css .= 'border-color:' . $border_color_linked_custom . ';';
301 } else {
302 // Unlinked.
303 if ( is_array( $custom_border ) ) {
304 foreach ( $custom_border as $border_color_key => $border_color_value ) {
305 if ( is_array( $border_color_value ) && array_key_exists( 'color', ( $border_color_value ) ) ) {
306 $border_color_css .= 'border-' . $border_color_key . '-color:' . self::getColorValue( $border_color_value['color'] ) . ';';
307 }
308 }
309 }
310 }
311
312 if ( ! $border_color_class && ! $border_color_css ) {
313 return null;
314 }
315
316 return array(
317 'class' => $border_color_class,
318 'style' => $border_color_css,
319 );
320 }
321
322 /**
323 * Get class and style for border-radius from attributes.
324 *
325 * @param array $attributes Block attributes.
326 *
327 * @return (array | null)
328 */
329 public static function getBorderRadiusClassAndStyle( $attributes ) {
330
331 $custom_border_radius = $attributes['style']['border']['radius'] ?? '';
332
333 if ( '' === $custom_border_radius ) {
334 return null;
335 }
336
337 $border_radius_css = '';
338
339 if ( is_string( $custom_border_radius ) ) {
340 // Linked sides.
341 $border_radius_css = 'border-radius:' . $custom_border_radius . ';';
342 } else {
343 // Unlinked sides.
344 $border_radius = array();
345
346 $border_radius['border-top-left-radius'] = $custom_border_radius['topLeft'] ?? '';
347 $border_radius['border-top-right-radius'] = $custom_border_radius['topRight'] ?? '';
348 $border_radius['border-bottom-right-radius'] = $custom_border_radius['bottomRight'] ?? '';
349 $border_radius['border-bottom-left-radius'] = $custom_border_radius['bottomLeft'] ?? '';
350
351 foreach ( $border_radius as $border_radius_side => $border_radius_value ) {
352 if ( '' !== $border_radius_value ) {
353 $border_radius_css .= $border_radius_side . ':' . $border_radius_value . ';';
354 }
355 }
356 }
357
358 return array(
359 'class' => null,
360 'style' => $border_radius_css,
361 );
362 }
363
364 /**
365 * Get class and style for border width from attributes.
366 *
367 * @param array $attributes Block attributes.
368 *
369 * @return (array | null)
370 */
371 public static function getBorderWidthClassAndStyle( $attributes ) {
372
373 $custom_border = $attributes['style']['border'] ?? '';
374
375 if ( '' === $custom_border ) {
376 return null;
377 }
378
379 $border_width_css = '';
380
381 if ( array_key_exists( 'width', ( $custom_border ) ) ) {
382 // Linked sides.
383 $border_width_css = 'border-width:' . $custom_border['width'] . ';';
384 } else {
385 // Unlinked sides.
386 foreach ( $custom_border as $border_width_side => $border_width_value ) {
387 if ( isset( $border_width_value['width'] ) ) {
388 $border_width_css .= 'border-' . $border_width_side . '-width:' . $border_width_value['width'] . ';';
389 }
390 }
391 }
392
393 return array(
394 'class' => null,
395 'style' => $border_width_css,
396 );
397 }
398
399 /**
400 * Get class and style for align from attributes.
401 *
402 * @param array $attributes Block attributes.
403 *
404 * @return (array | null)
405 */
406 public static function getAlignClassAndStyle( $attributes ) {
407
408 $align_attribute = $attributes['align'] ?? null;
409
410 if ( ! $align_attribute ) {
411 return null;
412 }
413
414 if ( 'wide' === $align_attribute ) {
415 return array(
416 'class' => 'alignwide',
417 'style' => null,
418 );
419 }
420
421 if ( 'full' === $align_attribute ) {
422 return array(
423 'class' => 'alignfull',
424 'style' => null,
425 );
426 }
427
428 if ( 'left' === $align_attribute ) {
429 return array(
430 'class' => 'alignleft',
431 'style' => null,
432 );
433 }
434
435 if ( 'right' === $align_attribute ) {
436 return array(
437 'class' => 'alignright',
438 'style' => null,
439 );
440 }
441
442 if ( 'center' === $align_attribute ) {
443 return array(
444 'class' => 'aligncenter',
445 'style' => null,
446 );
447 }
448
449 return null;
450 }
451
452 /**
453 * Get class and style for text align from attributes.
454 *
455 * @param array $attributes Block attributes.
456 *
457 * @return (array | null)
458 */
459 public static function getTextAlignClassAndStyle( $attributes ) {
460
461 $text_align_attribute = $attributes['textAlign'] ?? null;
462
463 if ( ! $text_align_attribute ) {
464 return null;
465 }
466
467 if ( 'left' === $text_align_attribute ) {
468 return array(
469 'class' => 'has-text-align-left',
470 'style' => null,
471 );
472 }
473
474 if ( 'center' === $text_align_attribute ) {
475 return array(
476 'class' => 'has-text-align-center',
477 'style' => null,
478 );
479 }
480
481 if ( 'right' === $text_align_attribute ) {
482 return array(
483 'class' => 'has-text-align-right',
484 'style' => null,
485 );
486 }
487
488 return null;
489 }
490
491 /**
492 * If spacing value is in preset format, convert it to a CSS var. Else return same value
493 * For example:
494 * "var:preset|spacing|50" -> "var(--wp--preset--spacing--50)"
495 * "50px" -> "50px"
496 *
497 * @param string $spacing_value value to be processed.
498 *
499 * @return (string)
500 */
501 public static function getSpacingValue( $spacing_value ) {
502 // Used following code as reference: https://github.com/WordPress/gutenberg/blob/cff6d70d6ff5a26e212958623dc3130569f95685/lib/block-supports/layout.php/#L219-L225.
503 if ( is_string( $spacing_value ) && str_contains( $spacing_value, 'var:preset|spacing|' ) ) {
504 $spacing_value = str_replace( 'var:preset|spacing|', '', $spacing_value );
505 return sprintf( 'var(--wp--preset--spacing--%s)', $spacing_value );
506 }
507
508 return $spacing_value;
509 }
510
511 /**
512 * Get class and style for padding from attributes.
513 *
514 * @param array $attributes Block attributes.
515 *
516 * @return (array | null)
517 */
518 public static function getPaddingClassAndStyle( $attributes ) {
519 $padding = $attributes['style']['spacing']['padding'] ?? null;
520
521 if ( ! $padding ) {
522 return null;
523 }
524
525 $spacing_values_css = '';
526
527 foreach ( $padding as $padding_side => $padding_value ) {
528 $spacing_values_css .= 'padding-' . $padding_side . ':' . self::getSpacingValue( $padding_value ) . ';';
529 }
530
531 return array(
532 'class' => null,
533 'style' => $spacing_values_css,
534 );
535 }
536
537 /**
538 * Get class and style for margin from attributes.
539 *
540 * @param array $attributes Block attributes.
541 *
542 * @return (array | null)
543 */
544 public static function getMarginClassAndStyle( $attributes ) {
545 $margin = $attributes['style']['spacing']['margin'] ?? null;
546
547 if ( ! $margin ) {
548 return null;
549 }
550
551 $spacing_values_css = '';
552
553 foreach ( $margin as $margin_side => $margin_value ) {
554 $spacing_values_css .= 'margin-' . $margin_side . ':' . self::getSpacingValue( $margin_value ) . ';';
555 }
556
557 return array(
558 'class' => null,
559 'style' => $spacing_values_css,
560 );
561 }
562
563 /**
564 * Get classes and styles from attributes.
565 *
566 * @param array $attributes Block attributes.
567 * @param array $properties Properties to get classes/styles from.
568 *
569 * @return array
570 */
571 public static function getClassesAndStylesFromAttributes( $attributes, $properties = array() ) {
572 $classes_and_styles = array(
573 'line_height' => self::getLineHeightClassAndStyle( $attributes ),
574 'text_color' => self::getTextColorClassAndStyle( $attributes ),
575 'font_size' => self::getFontSizeClassAndStyle( $attributes ),
576 'font_family' => self::getFontFamilyClassAndStyle( $attributes ),
577 'font_weight' => self::getFontWeightClassAndStyle( $attributes ),
578 'font_style' => self::getFontStyleClassAndStyle( $attributes ),
579 'link_color' => self::getLinkColorClassAndStyle( $attributes ),
580 'text_transform' => self::getTextTransformClassAndStyle( $attributes ),
581 'background_color' => self::getBackgroundColorClassAndStyle( $attributes ),
582 'gradient' => self::getGradientClassAndStyle( $attributes ),
583 'border_color' => self::getBorderColorClassAndStyle( $attributes ),
584 'border_radius' => self::getBorderRadiusClassAndStyle( $attributes ),
585 'border_width' => self::getBorderWidthClassAndStyle( $attributes ),
586 'padding' => self::getPaddingClassAndStyle( $attributes ),
587 'margin' => self::getMarginClassAndStyle( $attributes ),
588 );
589
590 if ( ! empty( $properties ) ) {
591 foreach ( $classes_and_styles as $key => $value ) {
592 if ( ! in_array( $key, $properties, true ) ) {
593 unset( $classes_and_styles[ $key ] );
594 }
595 }
596 }
597
598 $classes_and_styles = array_filter( $classes_and_styles );
599
600 $classes = array_map(
601 function( $item ) {
602 return $item['class'];
603 },
604 $classes_and_styles
605 );
606
607 $styles = array_map(
608 function( $item ) {
609 return $item['style'];
610 },
611 $classes_and_styles
612 );
613
614 $classes = array_filter( $classes );
615 $styles = array_filter( $styles );
616
617 return array(
618 'classes' => implode( ' ', $classes ),
619 'styles' => implode( ' ', $styles ),
620 );
621 }
622
623 /**
624 * Get space-separated classes from block attributes.
625 *
626 * @param array $attributes Block attributes.
627 * @param array $properties Properties to get classes from.
628 *
629 * @return string Space-separated classes.
630 */
631 public static function getClassesByAttributes( $attributes, $properties = array() ) {
632 $classes_and_styles = self::getClassesAndStyleAttributes( $attributes, $properties );
633
634 return $classes_and_styles['classes'];
635 }
636
637 /**
638 * Get space-separated style rules from block attributes.
639 *
640 * @param array $attributes Block attributes.
641 * @param array $properties Properties to get styles from.
642 *
643 * @return string Space-separated style rules.
644 */
645 public static function getStylesByAttributes( $attributes, $properties = array() ) {
646 $classes_and_styles = self::getClassesAndStyleAttributes( $attributes, $properties );
647
648 return $classes_and_styles['styles'];
649 }
650
651 /**
652 * Get CSS value for color preset.
653 *
654 * @param string $preset_name Preset name.
655 *
656 * @return string CSS value for color preset.
657 */
658 public static function getPresetValue( $preset_name ) {
659 return "var(--wp--preset--color--$preset_name)";
660 }
661
662 /**
663 * If color value is in preset format, convert it to a CSS var. Else return same value
664 * For example:
665 * "var:preset|color|pale-pink" -> "var(--wp--preset--color--pale-pink)"
666 * "#98b66e" -> "#98b66e"
667 *
668 * @param string $color_value value to be processed.
669 *
670 * @return (string)
671 */
672 public static function getColorValue( $color_value ) {
673 if ( is_string( $color_value ) && str_contains( $color_value, 'var:preset|color|' ) ) {
674 $color_value = str_replace( 'var:preset|color|', '', $color_value );
675 return sprintf( 'var(--wp--preset--color--%s)', $color_value );
676 }
677
678 return $color_value;
679 }
680 }
681