Açıklama Yok

Heading.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace MailPoet\Form\Block;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class Heading {
  6. /** @var WPFunctions */
  7. private $wp;
  8. public function __construct(
  9. WPFunctions $wp
  10. ) {
  11. $this->wp = $wp;
  12. }
  13. public function render(array $block): string {
  14. $content = ($block['params']['content'] ?? '');
  15. return $this->wrapContent($content, $block);
  16. }
  17. private function wrapContent(string $content, array $block): string {
  18. $tag = $this->renderTag($block);
  19. $attributes = $this->renderAttributes($block);
  20. $openTag = $this->getOpenTag($tag, $attributes);
  21. return $openTag
  22. . $content
  23. . "</$tag>";
  24. }
  25. private function renderTag(array $block): string {
  26. $tag = 'h2';
  27. if (isset($block['params']['level'])) {
  28. $tag = 'h' . $block['params']['level'];
  29. }
  30. return $tag;
  31. }
  32. private function renderAttributes(array $block): array {
  33. $result = [];
  34. $classes = $this->renderClass($block);
  35. if ($classes) {
  36. $result[] = $classes;
  37. }
  38. if (isset($block['params']['anchor'])) {
  39. $result[] = $this->renderAnchor($block);
  40. }
  41. $styles = $this->renderStyle($block);
  42. if ($styles) {
  43. $result[] = $styles;
  44. }
  45. return $result;
  46. }
  47. private function getOpenTag(string $tag, array $attributes): string {
  48. if (empty($attributes)) {
  49. return "<$tag>";
  50. }
  51. return "<$tag " . join(' ', $attributes) . ">";
  52. }
  53. private function renderClass(array $block): string {
  54. $classes = ['mailpoet-heading'];
  55. if (isset($block['params']['class_name'])) {
  56. $classes[] = $block['params']['class_name'];
  57. }
  58. if (!empty($block['params']['background_color'])) {
  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. private function renderAnchor(array $block): string {
  69. return 'id="'
  70. . $block['params']['anchor']
  71. . '"';
  72. }
  73. private function renderStyle(array $block): string {
  74. $styles = [];
  75. if (isset($block['params']['align'])) {
  76. $styles[] = 'text-align: ' . $block['params']['align'];
  77. }
  78. if (isset($block['params']['text_color'])) {
  79. $styles[] = 'color: ' . $block['params']['text_color'];
  80. }
  81. if (!empty($block['params']['font_size'])) {
  82. $styles[] = 'font-size: ' . $block['params']['font_size'] . 'px';
  83. }
  84. if (!empty($block['params']['line_height'])) {
  85. $styles[] = 'line-height: ' . $block['params']['line_height'];
  86. }
  87. if (!empty($block['params']['background_color'])) {
  88. $styles[] = 'background-color: ' . $block['params']['background_color'];
  89. }
  90. if (empty($styles)) {
  91. return '';
  92. }
  93. return 'style="'
  94. . $this->wp->escAttr(join('; ', $styles))
  95. . '"';
  96. }
  97. }