Refactors Newsletters as per @badshark's comments

This commit is contained in:
MrCasual
2015-09-21 13:18:01 -04:00
parent 692e69d567
commit 01b5416882
23 changed files with 462 additions and 413 deletions

View File

@@ -0,0 +1,49 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
use MailPoet\Newsletter\Renderer\StylesHelper;
class Button {
static function render($element) {
$stylesHelper = new StylesHelper();
$template = '
<tr>
<td class="mailpoet_col mailpoet_button mailpoet_padded" valign = "top" >
<div>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="' . $element['styles']['block']['textAlign'] . '">
<!--[if mso]>
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w="urn:schemas-microsoft-com:office:word"
href="' . $element['url'] . '"
style="height:' . $element['styles']['block']['lineHeight'] . ';
width:' . $element['styles']['block']['width'] . ';
v-text-anchor:middle;"
arcsize="' . round($element['styles']['block']['borderRadius'] / $element['styles']['block']['lineHeight'] * 100) . '%"
strokecolor="' . $element['styles']['block']['borderColor'] . '"
fillcolor="' . $element['styles']['block']['backgroundColor'] . '">
<w:anchorlock/>
<center style="color:' . $element['styles']['block']['fontColor'] . ';
font-family:' . $element['styles']['block']['fontFamily'] . ';
font-size:' . $element['styles']['block']['fontSize'] . ';
font-weight:bold;">' . $element['text'] . '
</center>
</v:roundrect>
<![endif]-->
<a class="mailpoet_button"
href="' . $element['url'] . '"
style="display:inline-block;text-align:center;text-decoration:none;-webkit-text-size-adjust:none;mso-hide:all;' . $stylesHelper->getBlockStyles($element, array('textAlign')) . '"> ' . $element['text'] . '
</a>
</td>
</tr>
</table>
</div>
</td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,25 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
class Divider {
static function render($element) {
$template = '
<tr>
<td class="mailpoet_col mailpoet_divider mailpoet_padded"
style="background-color: ' . $element['styles']['block']['backgroundColor'] . '; padding: ' . $element['styles']['block']['padding'] . ' 0;"
valign="top">
<table width="100%">
<tr>
<td style="border-top-width: ' . $element['styles']['block']['borderWidth'] . ';
border-top-style: ' . $element['styles']['block']['borderStyle'] . ';
border-top-color: ' . $element['styles']['block']['borderColor'] . ';">
</td>
</tr>
</table>
</td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,32 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
use MailPoet\Newsletter\Renderer\StylesHelper;
class Footer {
static function render($element) {
$stylesHelper = new StylesHelper();
// apply link styles
if(isset($element['styles']['link'])) {
$element['text'] = str_replace('<a', '<a style="' . $stylesHelper->getStyles($element['styles'], 'link') . '"', $element['text']);
}
// apply text styles
if(isset($element['styles']['link'])) {
$element['text'] = str_replace('<p', '<p style="' . $stylesHelper->getStyles($element['styles'], 'text') . '"', $element['text']);
}
$template = '
<tr>
<td class="mailpoet_col mailpoet_footer"
style="' . $stylesHelper->getStyles($element['styles'], 'block') . '"
valign="top">
<div>' . $element['text'] . '</div>
</td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,32 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
use MailPoet\Newsletter\Renderer\StylesHelper;
class Header {
static function render($element) {
$stylesHelper = new StylesHelper();
// apply link styles
if(isset($element['styles']['link'])) {
$element['text'] = str_replace('<a', '<a style="' . $stylesHelper->getStyles($element['styles'], 'link') . '"', $element['text']);
}
// apply text styles
if(isset($element['styles']['link'])) {
$element['text'] = str_replace('<p', '<p style="' . $stylesHelper->getStyles($element['styles'], 'text') . '"', $element['text']);
}
$template = '
<tr>
<td class="mailpoet_col mailpoet_header"
style="' . $stylesHelper->getBlockStyles($element) . '"
valign="top">
<div>' . $element['text'] . '</div>
</td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,26 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
use MailPoet\Newsletter\Renderer\StylesHelper;
class Image {
static function render($element) {
$stylesHelper = new StylesHelper();
$element['width'] = (int) $element['width'];
$template = '
<tr>
<td class="mailpoet_col mailpoet_image ' . (($element['padded'] === true) ? "mailpoet_padded" : "") . '"
style="' . $stylesHelper->getBlockStyles($element) . '"
valign="top">
<img style="top:0; left:0; height: auto; width:100%;"
src="' . $element['src'] . '"
width="' . (($element['padded'] === true) ? $element['width'] - (20 * 2) : $element['width']) . '">
</td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,25 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
class Renderer {
function render($data, $column = null) {
array_map(function ($block) use (&$blockContent, &$columns) {
$blockContent .= $this->createElementFromBlockType($block);
if(isset($block['blocks'])) {
$blockContent = $this->render($block);
}
// vertical orientation denotes column container
if($block['type'] === 'container' && $block['orientation'] === 'vertical') {
$columns[] = $blockContent;
}
}, $data['blocks']);
return (isset($columns)) ? $columns : $blockContent;
}
function createElementFromBlockType($block) {
$blockClass = __NAMESPACE__ . '\\' . ucfirst($block['type']);
return (class_exists($blockClass)) ? $blockClass::render($block) : '';
}
}

View File

@@ -0,0 +1,28 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
class Social {
static function render($element) {
$iconsBlock = '';
if(is_array($element['icons'])) {
foreach ($element['icons'] as $icon) {
$iconsBlock .= '
<a href="' . $icon['link'] . '">
<img src="' . $icon['image'] . '" width = "32" height = "32" style="width: 32px; height: 32px;" alt="' . $icon['iconType'] . '">
</a>
<img src="http://mp3.mailpoet.net/spacer.gif" width = "10" height = "1" style=" width: 10px; height: 1px;">';
}
}
$template = '
<tr>
<td class="mailpoet_col mailpoet_social" valign="top">
<div class="mailpoet_social-icon mailpoet_padded">' . $iconsBlock . ' </div>
</td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,25 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
use MailPoet\Newsletter\Renderer\StylesHelper;
class Spacer {
static function render($element) {
$stylesHelper = new StylesHelper();
// if the parent container (column) has background set and the divider element has a transparent background,
// it will assume the newsletter background, not that of the parent container
if($element['styles']['block']['backgroundColor'] === 'transparent') {
unset($element['styles']['block']['backgroundColor']);
}
$template = '
<tr>
<td class="mailpoet_col mailpoet_spacer" style="' . $stylesHelper->getBlockStyles($element) . '" valign="top"> </td>
</tr>';
return $template;
}
}

View File

@@ -0,0 +1,116 @@
<?php namespace MailPoet\Newsletter\Renderer\Blocks;
class Text {
static $typeFace = array(
'Arial' => "Arial, 'Helvetica Neue', Helvetica, sans-serif",
'Comic Sans MS' => "'Comic Sans MS', 'Marker Felt-Thin', Arial, sans-serif",
'Courier New' => "'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace",
'Georgia' => "Georgia, Times, 'Times New Roman', serif",
'Lucida' => "'Lucida Sans Unicode', 'Lucida Grande', sans-serif",
'Tahoma' => "Tahoma, Verdana, Segoe, sans-serif",
'Times New Roman' => "'Times New Roman', Times, Baskerville, Georgia, serif",
'Trebuchet MS' => "'Trebuchet MS', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Tahoma, sans-serif",
'Verdana' => "Verdana, Geneva, sans-serif"
);
static function render($element) {
$html = $element['text'];
$html = self::convertBlockquotesToTables($html);
$html = self::addLineBreakAfterTags($html);
$html = self::removeEmptyTags($html);
$html = self::convertEmptyParagraphsToLineBreaks($html);
$html = self::convertParagraphsToTables($html);
$html = self::removeLastBreakLine($html);
$template = '
<tr>
<td class="mailpoet_col mailpoet_text mailpoet_padded" valign="top">' . $html . ' </td>
</tr>';
return $template;
}
static function removeLastBreakLine($html) {
return preg_replace('/<br>([^<br>]*)$/s', '', $html);
}
static function convertParagraphsToTables($html) {
$html = preg_replace('/<p>(.*?)<\/p>/', '
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<span class="paragraph">
$1
</span>
</td>
</tr>
</table>', $html);
return preg_replace('/<p(.+style=\".*?\")?>(.*?)<\/p>/', '
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td $1>
<span class="paragraph">
$2
</span>
</td>
</tr>
</table>', $html);
}
static function convertEmptyParagraphsToLineBreaks($html) {
return preg_replace('/<p(?:.+style=\".*?\")?><\/p>/', '<br>', $html);
}
static function addLineBreakAfterTags($html) {
return preg_replace('/(<\/(ul|ol|h\d)>)/', '$1<br>', $html);
}
static function convertBlockquotesToTables($html) {
$template = '
<table cellpadding="0" cellspacing="0" border="0" class="mailpoet_blockquote">
<tbody>
<tr>
<td valign="top">$1</td>
</tr>
</tbody>
</table>
<br>';
return preg_replace('/<blockquote>(.*?)<\/blockquote>/s', $template, $html);
}
static function removeEmptyTags($html) {
$pattern = <<<'EOD'
~
<
(?:
!--[^-]*(?:-(?!->)[^-]*)*-->[^<]*(*SKIP)(*F) # skip comments
|
( # group 1
(span|em|strong) # tag name in group 2
[^"'>]* #'"# all that is not a quote or a closing angle bracket
(?: # quoted attributes
"[^\\"]*(?:\\.[^\\"]*)*+" [^"'>]* #'"# double quote
|
'[^\\']*(?:\\.[^\\']*)*+' [^"'>]* #'"# single quote
)*+
>
\s*
(?:
<!--[^-]*(?:-(?!->)[^-]*)*+--> \s* # html comments
|
<(?1) \s* # recursion with the group 1
)*+
</\2> # closing tag
) # end of the group 1
)
~sxi
EOD;
return preg_replace($pattern, '', $html);
}
}