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
Paragraph.php
119 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 Paragraph { |
| 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 | $attributes = $this->renderAttributes($block); |
| 27 | $openTag = $this->getOpenTag($attributes); |
| 28 | return $openTag |
| 29 | . $content |
| 30 | . "</p>"; |
| 31 | } |
| 32 | |
| 33 | private function getOpenTag(array $attributes): string { |
| 34 | if (empty($attributes)) { |
| 35 | return "<p>"; |
| 36 | } |
| 37 | return "<p " . join(' ', $attributes) . ">"; |
| 38 | } |
| 39 | |
| 40 | private function renderAttributes(array $block): array { |
| 41 | $result = []; |
| 42 | $result[] = $this->renderClass($block); |
| 43 | $result[] = $this->renderStyle($block); |
| 44 | $result = array_filter($result, function ($attribute) { |
| 45 | return $attribute !== null; |
| 46 | }); |
| 47 | return $result; |
| 48 | } |
| 49 | |
| 50 | private function renderClass(array $block) { |
| 51 | $classes = ['mailpoet_form_paragraph']; |
| 52 | if (isset($block['params']['class_name'])) { |
| 53 | $classes[] = $block['params']['class_name']; |
| 54 | } |
| 55 | if (isset($block['params']['drop_cap']) && $block['params']['drop_cap'] === '1') { |
| 56 | $classes[] = 'has-drop-cap'; |
| 57 | } |
| 58 | if (!empty($block['params']['background_color']) || !empty($block['params']['gradient'])) { |
| 59 | $classes[] = 'mailpoet-has-background-color'; |
| 60 | } |
| 61 | if (!empty($block['params']['font_size'])) { |
| 62 | $classes[] = 'mailpoet-has-font-size'; |
| 63 | } |
| 64 | return 'class="' |
| 65 | . $this->wp->escAttr(join(' ', $classes)) |
| 66 | . '"'; |
| 67 | } |
| 68 | |
| 69 | private function renderStyle(array $block) { |
| 70 | $styles = []; |
| 71 | if (!empty($block['params']['background_color'])) { |
| 72 | $styles[] = 'background-color: ' . $block['params']['background_color']; |
| 73 | } |
| 74 | if (!empty($block['params']['gradient'])) { |
| 75 | $styles[] = "background: {$block['params']['gradient']};"; |
| 76 | } |
| 77 | if (!empty($block['params']['align'])) { |
| 78 | $styles[] = 'text-align: ' . $block['params']['align']; |
| 79 | } |
| 80 | if (!empty($block['params']['text_color'])) { |
| 81 | $styles[] = 'color: ' . $block['params']['text_color']; |
| 82 | } |
| 83 | if (!empty($block['params']['font_size'])) { |
| 84 | $styles[] = 'font-size: ' . $block['params']['font_size'] . (is_numeric($block['params']['font_size']) ? 'px' : ''); |
| 85 | } |
| 86 | if (!empty($block['params']['line_height'])) { |
| 87 | $styles[] = 'line-height: ' . $block['params']['line_height']; |
| 88 | } |
| 89 | $padding = $block['params']['padding'] ?? null; |
| 90 | if (is_array($padding) && !empty($padding)) { |
| 91 | $top = $this->normalizePaddingValue($padding['top'] ?? '0'); |
| 92 | $right = $this->normalizePaddingValue($padding['right'] ?? '0'); |
| 93 | $bottom = $this->normalizePaddingValue($padding['bottom'] ?? '0'); |
| 94 | $left = $this->normalizePaddingValue($padding['left'] ?? '0'); |
| 95 | $styles[] = "padding:{$top} {$right} {$bottom} {$left};"; |
| 96 | } elseif (is_scalar($padding) && trim((string)$padding) !== '') { |
| 97 | $styles[] = 'padding:' . $this->normalizePaddingValue($padding) . ';'; |
| 98 | } |
| 99 | if (empty($styles)) { |
| 100 | return null; |
| 101 | } |
| 102 | return 'style="' |
| 103 | . $this->wp->escAttr(join('; ', $styles)) |
| 104 | . '"'; |
| 105 | } |
| 106 | |
| 107 | private function normalizePaddingValue($value): string { |
| 108 | if (!is_scalar($value)) { |
| 109 | return '0'; |
| 110 | } |
| 111 | |
| 112 | $cssValue = trim((string)$value); |
| 113 | if ($cssValue !== '' && is_numeric($cssValue) && (float)$cssValue !== 0.0) { |
| 114 | return $cssValue . 'px'; |
| 115 | } |
| 116 | return $cssValue; |
| 117 | } |
| 118 | } |
| 119 |