Nav apraksta

Header.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace MailPoet\Newsletter\Renderer\Blocks;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper;
  5. use MailPoet\Newsletter\Renderer\StylesHelper;
  6. use MailPoet\Util\pQuery\pQuery;
  7. use MailPoetVendor\CSS;
  8. class Header {
  9. public function render($element) {
  10. $element['text'] = preg_replace('/\n/', '<br />', $element['text']);
  11. $element['text'] = preg_replace('/(<\/?p.*?>)/i', '', $element['text']);
  12. $lineHeight = sprintf(
  13. '%spx', StylesHelper::$defaultLineHeight * (int)$element['styles']['text']['fontSize']
  14. );
  15. if (!is_string($element['text'])) {
  16. throw new \RuntimeException('$element[\'text\'] should be a string.');
  17. }
  18. $dOMParser = new pQuery();
  19. $DOM = $dOMParser->parseStr($element['text']);
  20. if (isset($element['styles']['link'])) {
  21. $links = $DOM->query('a');
  22. if ($links->count()) {
  23. $css = new CSS();
  24. foreach ($links as $link) {
  25. $elementLinkStyles = StylesHelper::getStyles($element['styles'], 'link');
  26. $link->style = $css->mergeInlineStyles($elementLinkStyles, $link->style);
  27. }
  28. }
  29. }
  30. $backgroundColor = $element['styles']['block']['backgroundColor'];
  31. $backgroundColor = ($backgroundColor !== 'transparent') ?
  32. 'bgcolor="' . $backgroundColor . '"' :
  33. false;
  34. if (!$backgroundColor) unset($element['styles']['block']['backgroundColor']);
  35. $style = 'line-height: ' . $lineHeight . ';' . StylesHelper::getBlockStyles($element) . StylesHelper::getStyles($element['styles'], 'text');
  36. $style = EHelper::escapeHtmlStyleAttr($style);
  37. $template = '
  38. <tr>
  39. <td class="mailpoet_header_footer_padded mailpoet_header" ' . $backgroundColor . ' style="' . $style . '">
  40. ' . str_replace('&', '&amp;', $DOM->html()) . '
  41. </td>
  42. </tr>';
  43. return $template;
  44. }
  45. }