PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.2.0
Tableberg – Simple Gutenberg Table Block v1.2.0
1.2.0 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 All 43 releases
tableberg / renderer / Button / ButtonRenderer.php

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

152 lines 5.2 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 // Outline draws the button's colour as its text and border with
23 // nothing behind it, the way the core Buttons block does; hover then
24 // fills it in, which is where the text colour comes back.
25 $isOutline = $attrs->styles->variant->equals('outline');
26
27 $wrapperStyles = [
28 'display:flex',
29 'justify-content:' . $this->alignment_to_justify_content($attrs->align->asText()),
30 '--tableberg-button-background-color:' . ($isOutline ? 'transparent' : $backgroundColor),
31 '--tableberg-button-text-color:' . ($isOutline ? $backgroundColor : $textColor),
32 '--tableberg-button-hover-background-color:' . $backgroundHoverColor,
33 '--tableberg-button-text-hover-color:' . $textHoverColor,
34 ];
35
36 $buttonStyles = [
37 'display:inline-block',
38 'border:' . ($isOutline ? '2px solid ' . $backgroundColor : 'none'),
39 'text-align:' . $attrs->styles->textAlign->asAttr(),
40 'padding-top:' . $attrs->styles->padding->top->asAttr(),
41 'padding-right:' . $attrs->styles->padding->right->asAttr(),
42 'padding-bottom:' . $attrs->styles->padding->bottom->asAttr(),
43 'padding-left:' . $attrs->styles->padding->left->asAttr(),
44 'border-top-left-radius:' . $attrs->styles->borderRadius->topLeft->asAttr(),
45 'border-top-right-radius:' . $attrs->styles->borderRadius->topRight->asAttr(),
46 'border-bottom-right-radius:' . $attrs->styles->borderRadius->bottomRight->asAttr(),
47 'border-bottom-left-radius:' . $attrs->styles->borderRadius->bottomLeft->asAttr(),
48 ];
49
50 // Without a size of its own the button takes the table's.
51 if ($attrs->styles->fontSize->isNotEmpty()) {
52 $buttonStyles[] = 'font-size:' . $attrs->styles->fontSize->asAttr();
53 }
54
55 $customWidth = $this->get_custom_width($attrs->styles->width->asText());
56
57 if ($customWidth !== null) {
58 $wrapperStyles[] = 'width:100%';
59 $buttonStyles[] = 'width:100%';
60 }
61
62 $target = $attrs->link->target->asAttr();
63
64 $buttonStyleString = implode(';', $buttonStyles);
65
66 $buttonHtml = '';
67 if ($attrs->link->url->isNotEmpty()) {
68 $relAttr = $target === '_blank' ? ' rel="noopener noreferrer"' : '';
69
70 $buttonHtml =
71 "<a
72 class='wp-block-button__link wp-element-button'
73 href='{$attrs->link->url->asUrl()}'
74 target='{$target}'
75 {$relAttr}
76 style='{$buttonStyleString}'
77 >
78 {$attrs->content->asHtml()}
79 </a>";
80 } else {
81 $buttonHtml =
82 "<div
83 class='wp-block-button__link wp-element-button'
84 style='{$buttonStyleString}'
85 >
86 {$attrs->content->asHtml()}
87 </div>";
88 }
89
90 if ($customWidth !== null) {
91 $customWidthWrapperStyles = implode(';', [
92 'width:' . $customWidth,
93 'max-width:none',
94 'min-width:0',
95 $attrs->styles->width->asText() === '100%' ? 'flex-basis:100%' : null,
96 ]);
97
98 $buttonHtml =
99 "<div style='{$customWidthWrapperStyles}'>
100 {$buttonHtml}
101 </div>";
102 }
103
104 $wrapperStyleString = implode(';', $wrapperStyles);
105
106 return
107 "<div id='{$attrs->id->asAttr()}' style='{$wrapperStyleString}'>
108 {$buttonHtml}
109 </div>";
110 }
111
112 /**
113 * @param string $align
114 * @return string
115 */
116 private function alignment_to_justify_content($align) {
117 if ($align === 'center') {
118 return 'center';
119 }
120
121 if ($align === 'right') {
122 return 'flex-end';
123 }
124
125 return 'flex-start';
126 }
127
128 /**
129 * @param string $width
130 * @return string|null
131 */
132 private function get_custom_width($width) {
133 if ($width === '25%') {
134 return 'calc(25% - (var(--wp--style--block-gap, 0.5em) * 0.75))';
135 }
136
137 if ($width === '50%') {
138 return 'calc(50% - (var(--wp--style--block-gap, 0.5em) * 0.5))';
139 }
140
141 if ($width === '75%') {
142 return 'calc(75% - (var(--wp--style--block-gap, 0.5em) * 0.25))';
143 }
144
145 if ($width === '100%') {
146 return '100%';
147 }
148
149 return null;
150 }
151 }
152