- 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 {
static function render($element) {
$blocksRenderer = new Renderer();
$template = '
<tr>
<td class="mailpoet_col mailpoet_button mailpoet_padded" valign = "top" >
@@ -32,7 +34,7 @@ class Button {
<![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;' . 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>
</td>
</tr>

View File

@@ -1,23 +1,23 @@
<?php namespace MailPoet\Newsletter\Blocks;
use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Footer {
static function render($element) {
$blocksRenderer = new Renderer();
// apply link styles
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
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 = '
<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>
</td>
</tr>';

View File

@@ -1,23 +1,23 @@
<?php namespace MailPoet\Newsletter\Blocks;
use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Header {
static function render($element) {
$blocksRenderer = new Renderer();
// apply link styles
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
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 = '
<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>
</td>
</tr>';

View File

@@ -5,11 +5,13 @@ use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Image {
static function render($element) {
$blocksRenderer = new Renderer();
$element['width'] = (int) $element['width'];
$template = '
<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%;"
src="' . $element['src'] . '"
width="' . (($element['padded'] === true) ? $element['width'] - (20 * 2) : $element['width']) . '">

View File

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

View File

@@ -4,11 +4,17 @@ 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;">';
$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">

View File

@@ -5,6 +5,9 @@ use MailPoet\Newsletter\Blocks\Renderer as BlocksRenderer;
class Spacer {
static function render($element) {
$blocksRenderer = new Renderer();
// 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') {
@@ -13,7 +16,7 @@ class Spacer {
$template = '
<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>';
return $template;

View File

@@ -2,19 +2,22 @@
class Renderer {
static function render($columnsCount, $columnsData) {
$columnWidths = array(
public $columnWidths = array(
1 => 600,
2 => 300,
3 => 200
);
$columnClasses = array(
public $columnClasses = array(
1 => 'mailpoet_col-one',
2 => 'mailpoet_col-two',
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
$columnContainerTemplate = '
@@ -30,12 +33,14 @@ class Renderer {
<tr>
<td width="' . $columnWidth . '" style="width: ' . $columnWidth . 'px;" valign="top">
<![endif]-->';
$columnOpenTemplate = '
<table width="' . $columnWidth . '"
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;
table-layout: fixed; margin-left: auto; margin-right: auto;" bgcolor="#999999">
<tbody>';
$columnCloseTemplate = '
</tbody>
</table>

View File

@@ -4,9 +4,13 @@ if(!defined('ABSPATH')) exit;
class Renderer {
public $template = 'NewsletterTemplate.html';
public $template = 'Template.html';
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->template = file_get_contents(dirname(__FILE__) . '/' . $this->template);
}
@@ -29,8 +33,8 @@ class Renderer {
foreach ($content['blocks'] as $contentBlock) {
if(isset($contentBlock['blocks']) && is_array($contentBlock['blocks'])) {
$columnCount = count($contentBlock['blocks']);
$columnData = Blocks\Renderer::render($contentBlock);
$newsletterContent .= Columns\Renderer::render($columnCount, $columnData);
$columnData = $this->blocksRenderer->render($contentBlock);
$newsletterContent .= $this->columnsRenderer->render($columnCount, $columnData);
}
}
@@ -56,7 +60,7 @@ class Renderer {
}
$newsletterStyles .= $selector . '{' . PHP_EOL;
foreach ($style as $attribute => $individualStyle) {
$newsletterStyles .= Blocks\Renderer::translateCSSAttribute($attribute) . ':' . $individualStyle . ';' . PHP_EOL;
$newsletterStyles .= $this->blocksRenderer->translateCSSAttribute($attribute) . ':' . $individualStyle . ';' . PHP_EOL;
}
$newsletterStyles .= '}' . PHP_EOL;
}
@@ -71,14 +75,12 @@ class Renderer {
}
function inlineCSSStyles($template) {
$inliner = new \MailPoet\Util\CSS();
return $inliner->inlineCSS(null, $template);
return $this->CSSInliner->inlineCSS(null, $template);
}
function postProcessRenderedTemplate($template) {
// 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');
foreach ($lastColumnElement as $element) {
$element->setAttribute('style', str_replace('padding-bottom:20px;', '', $element->attributes['style']));