PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / renderer / Button / ButtonRenderer.php

ButtonRenderer.php in Tableberg – Simple Gutenberg Table Block 1.1.5, at renderer/Button/ButtonRenderer.php

142 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tableberg\Renderer\Button;
4
5 class ButtonRenderer {
6 /**
7 * @param array<string, mixed>|null $attributes
8 * @return string
9 */
10 public function render($attributes) {
11 $attrs = ButtonAttrs::from_array($attributes);
12
13 $backgroundColor = $attrs->styles->backgroundColor->asAttr();
14 $textColor = $attrs->styles->textColor->asAttr();
15 $backgroundHoverColor = $attrs->styles->backgroundHoverColor->isNotEmpty()
16 ? $attrs->styles->backgroundHoverColor->asAttr()
17 : $backgroundColor;
18 $textHoverColor = $attrs->styles->textHoverColor->isNotEmpty()
19 ? $attrs->styles->textHoverColor->asAttr()
20 : $textColor;
21
22 $wrapperStyles = [
23 'display:flex',
24 'justify-content:' . $this->alignment_to_justify_content($attrs->align->asText()),
25 '--tableberg-button-background-color:' . $backgroundColor,
26 '--tableberg-button-text-color:' . $textColor,
27 '--tableberg-button-hover-background-color:' . $backgroundHoverColor,
28 '--tableberg-button-text-hover-color:' . $textHoverColor,
29 ];
30
31 $buttonStyles = [
32 'display:inline-block',
33 'text-align:' . $attrs->styles->textAlign->asAttr(),
34 'font-size:' . $attrs->styles->fontSize->asAttr(),
35 'padding-top:' . $attrs->styles->padding->top->asAttr(),
36 'padding-right:' . $attrs->styles->padding->right->asAttr(),
37 'padding-bottom:' . $attrs->styles->padding->bottom->asAttr(),
38 'padding-left:' . $attrs->styles->padding->left->asAttr(),
39 'border-top-left-radius:' . $attrs->styles->borderRadius->topLeft->asAttr(),
40 'border-top-right-radius:' . $attrs->styles->borderRadius->topRight->asAttr(),
41 'border-bottom-right-radius:' . $attrs->styles->borderRadius->bottomRight->asAttr(),
42 'border-bottom-left-radius:' . $attrs->styles->borderRadius->bottomLeft->asAttr(),
43 ];
44
45 $customWidth = $this->get_custom_width($attrs->styles->width->asText());
46
47 if ($customWidth !== null) {
48 $wrapperStyles[] = 'width:100%';
49 $buttonStyles[] = 'width:100%';
50 }
51
52 $target = $attrs->link->target->asAttr();
53
54 $buttonStyleString = implode(';', $buttonStyles);
55
56 $buttonHtml = '';
57 if ($attrs->link->url->isNotEmpty()) {
58 $relAttr = $target === '_blank' ? ' rel="noopener noreferrer"' : '';
59
60 $buttonHtml =
61 "<a
62 class='wp-block-button__link wp-element-button'
63 href='{$attrs->link->url->asUrl()}'
64 target='{$target}'
65 {$relAttr}
66 style='{$buttonStyleString}'
67 >
68 {$attrs->content->asHtml()}
69 </a>";
70 } else {
71 $buttonHtml =
72 "<div
73 class='wp-block-button__link wp-element-button'
74 style='{$buttonStyleString}'
75 >
76 {$attrs->content->asHtml()}
77 </div>";
78 }
79
80 if ($customWidth !== null) {
81 $customWidthWrapperStyles = implode(';', [
82 'width:' . $customWidth,
83 'max-width:none',
84 'min-width:0',
85 $attrs->styles->width->asText() === '100%' ? 'flex-basis:100%' : null,
86 ]);
87
88 $buttonHtml =
89 "<div style='{$customWidthWrapperStyles}'>
90 {$buttonHtml}
91 </div>";
92 }
93
94 $wrapperStyleString = implode(';', $wrapperStyles);
95
96 return
97 "<div id='{$attrs->id->asAttr()}' style='{$wrapperStyleString}'>
98 {$buttonHtml}
99 </div>";
100 }
101
102 /**
103 * @param string $align
104 * @return string
105 */
106 private function alignment_to_justify_content($align) {
107 if ($align === 'center') {
108 return 'center';
109 }
110
111 if ($align === 'right') {
112 return 'flex-end';
113 }
114
115 return 'flex-start';
116 }
117
118 /**
119 * @param string $width
120 * @return string|null
121 */
122 private function get_custom_width($width) {
123 if ($width === '25%') {
124 return 'calc(25% - (var(--wp--style--block-gap, 0.5em) * 0.75))';
125 }
126
127 if ($width === '50%') {
128 return 'calc(50% - (var(--wp--style--block-gap, 0.5em) * 0.5))';
129 }
130
131 if ($width === '75%') {
132 return 'calc(75% - (var(--wp--style--block-gap, 0.5em) * 0.25))';
133 }
134
135 if ($width === '100%') {
136 return '100%';
137 }
138
139 return null;
140 }
141 }
142