BlockRendererHelper.php
3 weeks ago
Checkbox.php
1 year ago
Close.php
2 weeks ago
Column.php
2 months ago
Columns.php
2 months ago
Date.php
1 year ago
Divider.php
3 years ago
Heading.php
2 months ago
Html.php
1 year ago
Image.php
10 months ago
Paragraph.php
3 weeks ago
Radio.php
1 year ago
Segment.php
2 months ago
Select.php
2 months ago
Submit.php
1 year ago
Text.php
1 year ago
Textarea.php
1 year ago
index.php
3 years ago
Heading.php
122 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Form\Block; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class Heading { |
| 11 | /** @var WPFunctions */ |
| 12 | private $wp; |
| 13 | |
| 14 | public function __construct( |
| 15 | WPFunctions $wp |
| 16 | ) { |
| 17 | $this->wp = $wp; |
| 18 | } |
| 19 | |
| 20 | public function render(array $block): string { |
| 21 | $content = ($block['params']['content'] ?? ''); |
| 22 | return $this->wrapContent($content, $block); |
| 23 | } |
| 24 | |
| 25 | private function wrapContent(string $content, array $block): string { |
| 26 | $tag = $this->renderTag($block); |
| 27 | $attributes = $this->renderAttributes($block); |
| 28 | $openTag = $this->getOpenTag($tag, $attributes); |
| 29 | return $openTag |
| 30 | . $content |
| 31 | . "</$tag>"; |
| 32 | } |
| 33 | |
| 34 | private function renderTag(array $block): string { |
| 35 | $level = isset($block['params']['level']) ? (int)$block['params']['level'] : 2; |
| 36 | return ($level < 1 || $level > 6) ? 'h2' : 'h' . $level; |
| 37 | } |
| 38 | |
| 39 | private function renderAttributes(array $block): array { |
| 40 | $result = []; |
| 41 | $classes = $this->renderClass($block); |
| 42 | if ($classes) { |
| 43 | $result[] = $classes; |
| 44 | } |
| 45 | if (!empty($block['params']['anchor'])) { |
| 46 | $result[] = $this->renderAnchor($block); |
| 47 | } |
| 48 | $styles = $this->renderStyle($block); |
| 49 | if ($styles) { |
| 50 | $result[] = $styles; |
| 51 | } |
| 52 | return $result; |
| 53 | } |
| 54 | |
| 55 | private function getOpenTag(string $tag, array $attributes): string { |
| 56 | if (empty($attributes)) { |
| 57 | return "<$tag>"; |
| 58 | } |
| 59 | return "<$tag " . join(' ', $attributes) . ">"; |
| 60 | } |
| 61 | |
| 62 | private function renderClass(array $block): string { |
| 63 | $classes = ['mailpoet-heading']; |
| 64 | if (isset($block['params']['class_name'])) { |
| 65 | $classes[] = $block['params']['class_name']; |
| 66 | } |
| 67 | |
| 68 | if (!empty($block['params']['background_color']) || !empty($block['params']['gradient'])) { |
| 69 | $classes[] = 'mailpoet-has-background-color'; |
| 70 | } |
| 71 | |
| 72 | if (!empty($block['params']['font_size'])) { |
| 73 | $classes[] = 'mailpoet-has-font-size'; |
| 74 | } |
| 75 | |
| 76 | return 'class="' |
| 77 | . $this->wp->escAttr(join(' ', $classes)) |
| 78 | . '"'; |
| 79 | } |
| 80 | |
| 81 | private function renderAnchor(array $block): string { |
| 82 | return 'id="' |
| 83 | . $this->wp->escAttr($block['params']['anchor']) |
| 84 | . '"'; |
| 85 | } |
| 86 | |
| 87 | private function renderStyle(array $block): string { |
| 88 | $styles = []; |
| 89 | if (!empty($block['params']['align'])) { |
| 90 | $styles[] = 'text-align: ' . $block['params']['align']; |
| 91 | } |
| 92 | if (!empty($block['params']['text_color'])) { |
| 93 | $styles[] = 'color: ' . $block['params']['text_color']; |
| 94 | } |
| 95 | if (!empty($block['params']['font_size'])) { |
| 96 | $styles[] = 'font-size: ' . $block['params']['font_size'] . (is_numeric($block['params']['font_size']) ? 'px' : ''); |
| 97 | } |
| 98 | if (!empty($block['params']['line_height'])) { |
| 99 | $styles[] = 'line-height: ' . $block['params']['line_height']; |
| 100 | } |
| 101 | if (!empty($block['params']['background_color'])) { |
| 102 | $styles[] = 'background-color: ' . $block['params']['background_color']; |
| 103 | } |
| 104 | if (!empty($block['params']['gradient'])) { |
| 105 | $styles[] = "background: {$block['params']['gradient']};"; |
| 106 | } |
| 107 | if (!empty($block['params']['padding']) && is_array($block['params']['padding'])) { |
| 108 | $top = is_scalar($block['params']['padding']['top'] ?? null) ? (string)$block['params']['padding']['top'] : '0'; |
| 109 | $right = is_scalar($block['params']['padding']['right'] ?? null) ? (string)$block['params']['padding']['right'] : '0'; |
| 110 | $bottom = is_scalar($block['params']['padding']['bottom'] ?? null) ? (string)$block['params']['padding']['bottom'] : '0'; |
| 111 | $left = is_scalar($block['params']['padding']['left'] ?? null) ? (string)$block['params']['padding']['left'] : '0'; |
| 112 | $styles[] = "padding:{$top} {$right} {$bottom} {$left};"; |
| 113 | } |
| 114 | if (empty($styles)) { |
| 115 | return ''; |
| 116 | } |
| 117 | return 'style="' |
| 118 | . $this->wp->escAttr(join('; ', $styles)) |
| 119 | . '"'; |
| 120 | } |
| 121 | } |
| 122 |