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
Divider.php
64 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 Divider { |
| 11 | /** @var WPFunctions */ |
| 12 | private $wp; |
| 13 | |
| 14 | public function __construct( |
| 15 | WPFunctions $wp |
| 16 | ) { |
| 17 | $this->wp = $wp; |
| 18 | } |
| 19 | |
| 20 | const DEFAULT_ATTRIBUTES = [ |
| 21 | 'height' => 1, |
| 22 | 'type' => 'divider', |
| 23 | 'style' => 'solid', |
| 24 | 'dividerHeight' => 1, |
| 25 | 'dividerWidth' => 100, |
| 26 | 'color' => 'black', |
| 27 | ]; |
| 28 | |
| 29 | public function render($block): string { |
| 30 | $classes = ['mailpoet_spacer']; |
| 31 | if (isset($block['params']['type']) && $block['params']['type'] === 'divider') { |
| 32 | $classes[] = 'mailpoet_has_divider'; |
| 33 | } |
| 34 | if (!empty($block['params']['class_name'])) { |
| 35 | $classes[] = $block['params']['class_name']; |
| 36 | } |
| 37 | $classAttr = $this->wp->escAttr(join(' ', $classes)); |
| 38 | $height = $this->wp->escAttr($block['params']['height'] ?? self::DEFAULT_ATTRIBUTES['height']); |
| 39 | return "<div class='{$classAttr}' style='height: {$height}px;'>" |
| 40 | . $this->renderDivider($block) |
| 41 | . '</div>'; |
| 42 | } |
| 43 | |
| 44 | private function renderDivider(array $block): string { |
| 45 | if (isset($block['params']['type']) && $block['params']['type'] === 'spacer') { |
| 46 | return ''; |
| 47 | } |
| 48 | $width = $block['params']['divider_width'] ?? self::DEFAULT_ATTRIBUTES['dividerWidth']; |
| 49 | $style = $block['params']['style'] ?? self::DEFAULT_ATTRIBUTES['style']; |
| 50 | $dividerHeight = $block['params']['divider_height'] ?? self::DEFAULT_ATTRIBUTES['dividerHeight']; |
| 51 | $color = $block['params']['color'] ?? self::DEFAULT_ATTRIBUTES['color']; |
| 52 | |
| 53 | $dividerStyles = [ |
| 54 | "border-top-style: $style", |
| 55 | "border-top-width: {$dividerHeight}px", |
| 56 | "border-top-color: $color", |
| 57 | "height: {$dividerHeight}px", |
| 58 | "width: $width%", |
| 59 | ]; |
| 60 | $style = $this->wp->escAttr(implode(";", $dividerStyles)); |
| 61 | return "<div class='mailpoet_divider' data-automation-id='form_divider' style='$style'></div>"; |
| 62 | } |
| 63 | } |
| 64 |