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 / Text / TextRenderer.php

TextRenderer.php in Tableberg – Simple Gutenberg Table Block 1.2.0, at renderer/Text/TextRenderer.php

75 lines 2.5 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\Text;
4
5 class TextRenderer {
6 /**
7 * @param array<string, mixed>|null $attributes
8 * @return string
9 */
10 public function render($attributes) {
11 $attrs = TextAttrs::from_array($attributes);
12
13 $marginShorthand = $this->sides_shorthand($attrs->styles->margin);
14 $paddingShorthand = $this->sides_shorthand($attrs->styles->padding);
15
16 $styleValues = [];
17 $styleValues[] = 'margin:' . ($marginShorthand !== '' ? $marginShorthand : '0');
18 if ($paddingShorthand !== '') {
19 $styleValues[] = 'padding:' . $paddingShorthand;
20 }
21
22 $styleValues[] = 'text-align:' . $attrs->align->asAttr();
23
24 if ($attrs->styles->textColor->isNotEmpty()) {
25 $styleValues[] = 'color:' . $attrs->styles->textColor->asAttr();
26 }
27
28 if ($attrs->styles->linkColor->isNotEmpty()) {
29 $styleValues[] = '--tableberg-text-link-color:' . $attrs->styles->linkColor->asAttr();
30 }
31
32 if ($attrs->styles->backgroundColor->isNotEmpty()) {
33 $styleValues[] = 'background-color:' . $attrs->styles->backgroundColor->asAttr();
34 }
35
36 if ($attrs->styles->fontSize->isNotEmpty()) {
37 $styleValues[] = 'font-size:' . $attrs->styles->fontSize->asAttr();
38 }
39
40 $styleAttr = implode(';', $styleValues);
41
42 // Only paint the links when a colour was picked; otherwise the
43 // theme's own link colour applies.
44 $className = 'tableberg-text-element';
45 if ($attrs->styles->linkColor->isNotEmpty()) {
46 $className .= ' tableberg-has-link-color';
47 }
48
49 return
50 "<p class='{$className}' style='{$styleAttr}'>
51 {$attrs->content->asHtml()}
52 </p>";
53 }
54
55 /**
56 * Build a CSS shorthand from a 4-side value; returns '' when all sides
57 * are empty so the property can be skipped.
58 *
59 * @param Sides $sides
60 * @return string
61 */
62 private function sides_shorthand($sides) {
63 $top = $sides->top->isNotEmpty() ? $sides->top->asAttr() : '0';
64 $right = $sides->right->isNotEmpty() ? $sides->right->asAttr() : '0';
65 $bottom = $sides->bottom->isNotEmpty() ? $sides->bottom->asAttr() : '0';
66 $left = $sides->left->isNotEmpty() ? $sides->left->asAttr() : '0';
67
68 if ($top === '0' && $right === '0' && $bottom === '0' && $left === '0') {
69 return '';
70 }
71
72 return "{$top} {$right} {$bottom} {$left}";
73 }
74 }
75