- Made all rendering classes dynamic except for single blocks

This commit is contained in:
MrCasual
2015-09-11 14:31:12 -04:00
parent 7e4bd0d044
commit 42d659472e
11 changed files with 72 additions and 52 deletions

View File

@@ -5,6 +5,8 @@ use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Button { class Button {
static function render($element) { static function render($element) {
$blocksRenderer = new Renderer();
$template = ' $template = '
<tr> <tr>
<td class="mailpoet_col mailpoet_button mailpoet_padded" valign = "top" > <td class="mailpoet_col mailpoet_button mailpoet_padded" valign = "top" >
@@ -32,7 +34,7 @@ class Button {
<![endif]--> <![endif]-->
<a class="mailpoet_button" <a class="mailpoet_button"
href="' . $element['url'] . '" href="' . $element['url'] . '"
style="display:inline-block;text-align:center;text-decoration:none;-webkit-text-size-adjust:none;mso-hide:all;' . BlocksRenderer::getBlockStyles($element, array('textAlign')) . '"> ' . $element['text'] . ' style="display:inline-block;text-align:center;text-decoration:none;-webkit-text-size-adjust:none;mso-hide:all;' . $blocksRenderer->getBlockStyles($element, array('textAlign')) . '"> ' . $element['text'] . '
</a> </a>
</td> </td>
</tr> </tr>

View File

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

View File

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

View File

@@ -5,11 +5,13 @@ use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Image { class Image {
static function render($element) { static function render($element) {
$blocksRenderer = new Renderer();
$element['width'] = (int) $element['width']; $element['width'] = (int) $element['width'];
$template = ' $template = '
<tr> <tr>
<td class="mailpoet_col mailpoet_image ' . (($element['padded'] === true) ? "mailpoet_padded" : "") . '" style="' . BlocksRenderer::getBlockStyles($element) . '" valign="top"> <td class="mailpoet_col mailpoet_image ' . (($element['padded'] === true) ? "mailpoet_padded" : "") . '" style="' . $blocksRenderer->getBlockStyles($element) . '" valign="top">
<img style="top:0; left:0; height: auto; width:100%;" <img style="top:0; left:0; height: auto; width:100%;"
src="' . $element['src'] . '" src="' . $element['src'] . '"
width="' . (($element['padded'] === true) ? $element['width'] - (20 * 2) : $element['width']) . '"> width="' . (($element['padded'] === true) ? $element['width'] - (20 * 2) : $element['width']) . '">

View File

@@ -2,7 +2,7 @@
class Renderer { class Renderer {
static $typeFace = array( public $typeFace = array(
'Arial' => "Arial, 'Helvetica Neue', Helvetica, sans-serif", 'Arial' => "Arial, 'Helvetica Neue', Helvetica, sans-serif",
'Comic Sans MS' => "'Comic Sans MS', 'Marker Felt-Thin', Arial, 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", 'Courier New' => "'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace",
@@ -14,7 +14,7 @@ class Renderer {
'Verdana' => "Verdana, Geneva, sans-serif" 'Verdana' => "Verdana, Geneva, sans-serif"
); );
static $cssAtributesTable = array( public $cssAtributesTable = array(
'backgroundColor' => 'background-color', 'backgroundColor' => 'background-color',
'fontColor' => 'color', 'fontColor' => 'color',
'fontFamily' => 'font-family', 'fontFamily' => 'font-family',
@@ -28,14 +28,14 @@ class Renderer {
'lineHeight' => 'line-height' 'lineHeight' => 'line-height'
); );
static function render($data, $column = null) { function render($data, $column = null) {
$blockContent = ''; $blockContent = '';
$blockCount = count($data['blocks']); $blockCount = count($data['blocks']);
foreach ($data['blocks'] as $i => $block) { foreach ($data['blocks'] as $i => $block) {
$blockContent .= self::createElementFromBlockType($block); $blockContent .= $this->createElementFromBlockType($block);
if(isset($block['blocks']) && is_array($block['blocks'])) { if(isset($block['blocks']) && is_array($block['blocks'])) {
$blockContent = self::render($block); $blockContent = $this->render($block);
} }
// vertical orientation denotes column container // vertical orientation denotes column container
@@ -47,7 +47,7 @@ class Renderer {
return (isset($columns)) ? $columns : $blockContent; return (isset($columns)) ? $columns : $blockContent;
} }
static function createElementFromBlockType($block) { function createElementFromBlockType($block) {
switch ($block['type']) { switch ($block['type']) {
case 'header': case 'header':
$element = Header::render($block); $element = Header::render($block);
@@ -81,27 +81,27 @@ class Renderer {
return $element; return $element;
} }
static function getBlockStyles($element, $ignore = false) { function getBlockStyles($element, $ignore = false) {
if(!isset($element['styles']['block'])) { if(!isset($element['styles']['block'])) {
return; return;
} }
return self::getStyles($element['styles'], 'block', $ignore); return $this->getStyles($element['styles'], 'block', $ignore);
} }
static function getStyles($styles, $type, $ignore = false) { function getStyles($styles, $type, $ignore = false) {
$css = ''; $css = '';
foreach ($styles[$type] as $attribute => $style) { foreach ($styles[$type] as $attribute => $style) {
if($ignore && in_array($attribute, $ignore)) { if($ignore && in_array($attribute, $ignore)) {
continue; continue;
} }
$css .= self::translateCSSAttribute($attribute) . ': ' . $style . ' !important;'; $css .= $this->translateCSSAttribute($attribute) . ': ' . $style . ' !important;';
} }
return $css; return $css;
} }
static function translateCSSAttribute($attribute) { function translateCSSAttribute($attribute) {
return (isset(self::$cssAtributesTable[$attribute])) ? self::$cssAtributesTable[$attribute] : $attribute; return (isset($this->cssAtributesTable[$attribute])) ? $this->cssAtributesTable[$attribute] : $attribute;
} }
} }

View File

@@ -4,11 +4,17 @@ class Social {
static function render($element) { static function render($element) {
$iconsBlock = ''; $iconsBlock = '';
if(is_array($element['icons'])) { if(is_array($element['icons'])) {
foreach ($element['icons'] as $icon) { 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;">'; $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 = ' $template = '
<tr> <tr>
<td class="mailpoet_col mailpoet_social" valign="top"> <td class="mailpoet_col mailpoet_social" valign="top">

View File

@@ -5,6 +5,9 @@ use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Spacer { class Spacer {
static function render($element) { static function render($element) {
$blocksRenderer = new Renderer();
// if the parent container (column) has background set and the divider element has a transparent background, // 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 // it will assume the newsletter background, not that of the parent container
if($element['styles']['block']['backgroundColor'] === 'transparent') { if($element['styles']['block']['backgroundColor'] === 'transparent') {
@@ -13,7 +16,7 @@ class Spacer {
$template = ' $template = '
<tr> <tr>
<td class="mailpoet_col mailpoet_spacer" style="' . BlocksRenderer::getBlockStyles($element) . '" valign="top"> </td> <td class="mailpoet_col mailpoet_spacer" style="' . $blocksRenderer->getBlockStyles($element) . '" valign="top"> </td>
</tr>'; </tr>';
return $template; return $template;

View File

@@ -2,19 +2,22 @@
class Renderer { class Renderer {
static function render($columnsCount, $columnsData) { public $columnWidths = array(
$columnWidths = array( 1 => 600,
1 => 600, 2 => 300,
2 => 300, 3 => 200
3 => 200 );
);
$columnClasses = array( public $columnClasses = array(
1 => 'mailpoet_col-one', 1 => 'mailpoet_col-one',
2 => 'mailpoet_col-two', 2 => 'mailpoet_col-two',
3 => 'mailpoet_col-three' 3 => 'mailpoet_col-three'
); );
$columnWidth = $columnWidths[$columnsCount];
$columnClass = $columnClasses[$columnsCount]; function render($columnsCount, $columnsData) {
$columnWidth = $this->columnWidths[$columnsCount];
$columnClass = $this->columnClasses[$columnsCount];
// open column container // open column container
$columnContainerTemplate = ' $columnContainerTemplate = '
@@ -30,12 +33,14 @@ class Renderer {
<tr> <tr>
<td width="' . $columnWidth . '" style="width: ' . $columnWidth . 'px;" valign="top"> <td width="' . $columnWidth . '" style="width: ' . $columnWidth . 'px;" valign="top">
<![endif]-->'; <![endif]-->';
$columnOpenTemplate = ' $columnOpenTemplate = '
<table width="' . $columnWidth . '" <table width="' . $columnWidth . '"
border="0" cellpadding="0" cellspacing="0" align="left" class="mailpoet_force-row ' . $columnClass . ' mailpoet_col" border="0" cellpadding="0" cellspacing="0" align="left" class="mailpoet_force-row ' . $columnClass . ' mailpoet_col"
style="width: ' . $columnWidth . 'px; border-spacing: 0; mso-table-lspace: 0pt; mso-table-rspace: 0pt; style="width: ' . $columnWidth . 'px; border-spacing: 0; mso-table-lspace: 0pt; mso-table-rspace: 0pt;
table-layout: fixed; margin-left: auto; margin-right: auto;" bgcolor="#999999"> table-layout: fixed; margin-left: auto; margin-right: auto;" bgcolor="#999999">
<tbody>'; <tbody>';
$columnCloseTemplate = ' $columnCloseTemplate = '
</tbody> </tbody>
</table> </table>

View File

@@ -4,9 +4,13 @@ if(!defined('ABSPATH')) exit;
class Renderer { class Renderer {
public $template = 'NewsletterTemplate.html'; public $template = 'Template.html';
function __construct($newsletterData) { function __construct($newsletterData) {
$this->blocksRenderer = new Blocks\Renderer();
$this->columnsRenderer = new Columns\Renderer();
$this->DOMQuery = new \pQuery();
$this->CSSInliner = new \MailPoet\Util\CSS();
$this->data = $newsletterData; $this->data = $newsletterData;
$this->template = file_get_contents(dirname(__FILE__) . '/' . $this->template); $this->template = file_get_contents(dirname(__FILE__) . '/' . $this->template);
} }
@@ -29,8 +33,8 @@ class Renderer {
foreach ($content['blocks'] as $contentBlock) { foreach ($content['blocks'] as $contentBlock) {
if(isset($contentBlock['blocks']) && is_array($contentBlock['blocks'])) { if(isset($contentBlock['blocks']) && is_array($contentBlock['blocks'])) {
$columnCount = count($contentBlock['blocks']); $columnCount = count($contentBlock['blocks']);
$columnData = Blocks\Renderer::render($contentBlock); $columnData = $this->blocksRenderer->render($contentBlock);
$newsletterContent .= Columns\Renderer::render($columnCount, $columnData); $newsletterContent .= $this->columnsRenderer->render($columnCount, $columnData);
} }
} }
@@ -56,7 +60,7 @@ class Renderer {
} }
$newsletterStyles .= $selector . '{' . PHP_EOL; $newsletterStyles .= $selector . '{' . PHP_EOL;
foreach ($style as $attribute => $individualStyle) { foreach ($style as $attribute => $individualStyle) {
$newsletterStyles .= Blocks\Renderer::translateCSSAttribute($attribute) . ':' . $individualStyle . ';' . PHP_EOL; $newsletterStyles .= $this->blocksRenderer->translateCSSAttribute($attribute) . ':' . $individualStyle . ';' . PHP_EOL;
} }
$newsletterStyles .= '}' . PHP_EOL; $newsletterStyles .= '}' . PHP_EOL;
} }
@@ -71,14 +75,12 @@ class Renderer {
} }
function inlineCSSStyles($template) { function inlineCSSStyles($template) {
$inliner = new \MailPoet\Util\CSS(); return $this->CSSInliner->inlineCSS(null, $template);
return $inliner->inlineCSS(null, $template);
} }
function postProcessRenderedTemplate($template) { function postProcessRenderedTemplate($template) {
// remove padding from last element inside each column // remove padding from last element inside each column
$DOM = \pQuery::parseStr($template); $DOM = $this->DOMQuery->parseStr($template);
$lastColumnElement = $DOM->query('.mailpoet_col > tbody > tr:last-child > td'); $lastColumnElement = $DOM->query('.mailpoet_col > tbody > tr:last-child > td');
foreach ($lastColumnElement as $element) { foreach ($lastColumnElement as $element) {
$element->setAttribute('style', str_replace('padding-bottom:20px;', '', $element->attributes['style'])); $element->setAttribute('style', str_replace('padding-bottom:20px;', '', $element->attributes['style']));