Нет описания

Divider.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace MailPoet\Form\Block;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class Divider {
  6. /** @var WPFunctions */
  7. private $wp;
  8. public function __construct(
  9. WPFunctions $wp
  10. ) {
  11. $this->wp = $wp;
  12. }
  13. const DEFAULT_ATTRIBUTES = [
  14. 'height' => 1,
  15. 'type' => 'divider',
  16. 'style' => 'solid',
  17. 'dividerHeight' => 1,
  18. 'dividerWidth' => 100,
  19. 'color' => 'black',
  20. ];
  21. public function render($block): string {
  22. $classes = ['mailpoet_spacer'];
  23. if (isset($block['params']['type']) && $block['params']['type'] === 'divider') {
  24. $classes[] = 'mailpoet_has_divider';
  25. }
  26. if (!empty($block['params']['class_name'])) {
  27. $classes[] = $block['params']['class_name'];
  28. }
  29. $classAttr = $this->wp->escAttr(join(' ', $classes));
  30. $height = $this->wp->escAttr($block['params']['height'] ?? self::DEFAULT_ATTRIBUTES['height']);
  31. return "<div class='{$classAttr}' style='height: {$height}px;'>"
  32. . $this->renderDivider($block)
  33. . '</div>';
  34. }
  35. private function renderDivider(array $block): string {
  36. if (isset($block['params']['type']) && $block['params']['type'] === 'spacer') {
  37. return '';
  38. }
  39. $width = $block['params']['divider_width'] ?? self::DEFAULT_ATTRIBUTES['dividerWidth'];
  40. $style = $block['params']['style'] ?? self::DEFAULT_ATTRIBUTES['style'];
  41. $dividerHeight = $block['params']['divider_height'] ?? self::DEFAULT_ATTRIBUTES['dividerHeight'];
  42. $color = $block['params']['color'] ?? self::DEFAULT_ATTRIBUTES['color'];
  43. $dividerStyles = [
  44. "border-top-style: $style",
  45. "border-top-width: {$dividerHeight}px",
  46. "border-top-color: $color",
  47. "height: {$dividerHeight}px",
  48. "width: $width%",
  49. ];
  50. $style = $this->wp->escAttr(implode(";", $dividerStyles));
  51. return "<div class='mailpoet_divider' data-automation-id='form_divider' style='$style'></div>";
  52. }
  53. }