diff --git a/lib/Newsletter/Blocks/Button.php b/lib/Newsletter/Blocks/Button.php deleted file mode 100644 index 8f82f21756..0000000000 --- a/lib/Newsletter/Blocks/Button.php +++ /dev/null @@ -1,49 +0,0 @@ - - -
- - - - -
- - ' . $element['text'] . ' - -
-
- -'; - - return $template; - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Divider.php b/lib/Newsletter/Blocks/Divider.php deleted file mode 100644 index b19bdc6b9c..0000000000 --- a/lib/Newsletter/Blocks/Divider.php +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - -
-
- -'; - - return $template; - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Footer.php b/lib/Newsletter/Blocks/Footer.php deleted file mode 100644 index 1cd1842e1e..0000000000 --- a/lib/Newsletter/Blocks/Footer.php +++ /dev/null @@ -1,28 +0,0 @@ -getStyles($element['styles'], 'link') . '"', $element['text']); - } - - // apply text styles - if(isset($element['styles']['link'])) { - $element['text'] = str_replace('getStyles($element['styles'], 'block') . '" valign="top"> -
' . $element['text'] . '
- -'; - - return $template; - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Header.php b/lib/Newsletter/Blocks/Header.php deleted file mode 100644 index c4c3359b5f..0000000000 --- a/lib/Newsletter/Blocks/Header.php +++ /dev/null @@ -1,28 +0,0 @@ -getStyles($element['styles'], 'link') . '"', $element['text']); - } - - // apply text styles - if(isset($element['styles']['link'])) { - $element['text'] = str_replace('getBlockStyles($element) . '" valign="top"> -
' . $element['text'] . '
- -'; - - return $template; - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Image.php b/lib/Newsletter/Blocks/Image.php deleted file mode 100644 index 2810983204..0000000000 --- a/lib/Newsletter/Blocks/Image.php +++ /dev/null @@ -1,24 +0,0 @@ - - - - -'; - - return $template; - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Renderer.php b/lib/Newsletter/Blocks/Renderer.php deleted file mode 100644 index 8a2829902a..0000000000 --- a/lib/Newsletter/Blocks/Renderer.php +++ /dev/null @@ -1,72 +0,0 @@ - "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" - ); - - public $cssAtributesTable = array( - 'backgroundColor' => 'background-color', - 'fontColor' => 'color', - 'fontFamily' => 'font-family', - 'textDecoration' => 'text-decoration', - 'textAlign' => 'text-align', - 'fontSize' => 'font-size', - 'borderWidth' => 'border-width', - 'borderStyle' => 'border-style', - 'borderColor' => 'border-color', - 'borderRadius' => 'border-radius', - 'lineHeight' => 'line-height' - ); - - 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) : ''; - } - - function getBlockStyles($element, $ignore = false) { - if(!isset($element['styles']['block'])) { - return; - } - - return $this->getStyles($element['styles'], 'block', $ignore); - } - - function getStyles($data, $type, $ignore = false) { - array_map(function($attribute, $style) use(&$styles, $ignore) { - if(!$ignore || !in_array($attribute, $ignore)) { - $styles .= $this->translateCSSAttribute($attribute) . ': ' . $style . ' !important;'; - } - }, array_keys($data[$type]), $data[$type]); - - return $styles; - } - - function translateCSSAttribute($attribute) { - return (isset($this->cssAtributesTable[$attribute])) ? $this->cssAtributesTable[$attribute] : $attribute; - } -} diff --git a/lib/Newsletter/Blocks/Social.php b/lib/Newsletter/Blocks/Social.php deleted file mode 100644 index 909ab799a4..0000000000 --- a/lib/Newsletter/Blocks/Social.php +++ /dev/null @@ -1,28 +0,0 @@ - - ' . $icon['iconType'] . ' - -'; - } - } - - $template = ' - - -
' . $iconsBlock . '
- -'; - - return $template; - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Text.php b/lib/Newsletter/Blocks/Text.php deleted file mode 100644 index 7cfbd29ed9..0000000000 --- a/lib/Newsletter/Blocks/Text.php +++ /dev/null @@ -1,73 +0,0 @@ - elements to tables - $blockquoteTemplate = ' - - - - - -
- $1 -
-
'; - $element['text'] = preg_replace('/
(.*?)<\/blockquote>/s', $blockquoteTemplate, $element['text']); - - // add line breaks after tags - $element['text'] = preg_replace('/(<\/(ul|ol|h\d)>)/', '$1
', $element['text']); - - $element['text'] = self::removeEmptyHTMLTags($element['text']); - - // convert empty

tags to line breaks - $element['text'] = preg_replace('/<\/p>/', '
', $element['text']); - - // convert

to - $element['text'] = preg_replace('/

(.*?)<\/p>/', '
$1
', $element['text']); - $element['text'] = preg_replace('/(.*?)<\/p>/', '
$2
', $element['text']); - - // remove the last break line - $element['text'] = preg_replace('/
([^
]*)$/s', '', $element['text']); - - $template = ' - - ' . $element['text'] . ' -'; - - return $template; - } - - static function removeEmptyHTMLTags($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 - )*+ - # closing tag - ) # end of the group 1 - ) - ~sxi -EOD; - - return preg_replace($pattern, '', $html); - } - -} \ No newline at end of file diff --git a/lib/Newsletter/Columns/Renderer.php b/lib/Newsletter/Columns/Renderer.php deleted file mode 100644 index af10762303..0000000000 --- a/lib/Newsletter/Columns/Renderer.php +++ /dev/null @@ -1,78 +0,0 @@ - 600, - 2 => 300, - 3 => 200 - ); - - public $columnClasses = array( - 1 => 'mailpoet_col-one', - 2 => 'mailpoet_col-two', - 3 => 'mailpoet_col-three' - ); - - function render($columnsCount, $columnsData) { - - $columnWidth = $this->columnWidths[$columnsCount]; - $columnClass = $this->columnClasses[$columnsCount]; - - // open column container - $columnContainerTemplate = ' - - - - - - -
- '; - - $columnOpenTemplate = ' - - '; - - $columnCloseTemplate = ' - -
-'; - - foreach ($columnsData as $index => $columnData) { - $index++; - $columnContainerTemplate .= $columnOpenTemplate . $columnData; - if($columnsCount > 1 && $index != $columnsCount) { - $columnContainerTemplate .= $columnCloseTemplate; - } - } - - // close column container - $columnContainerTemplate .= ' -
- - - - - -'; - - return $columnContainerTemplate; - } -} diff --git a/lib/Newsletter/Renderer/Blocks/Button.php b/lib/Newsletter/Renderer/Blocks/Button.php new file mode 100644 index 0000000000..fa3b07c1de --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Button.php @@ -0,0 +1,49 @@ + + +

+ + + + +
+ + ' . $element['text'] . ' + +
+
+ + '; + + return $template; + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Blocks/Divider.php b/lib/Newsletter/Renderer/Blocks/Divider.php new file mode 100644 index 0000000000..684494eaf7 --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Divider.php @@ -0,0 +1,25 @@ + + + + + + +
+
+ + '; + + return $template; + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Blocks/Footer.php b/lib/Newsletter/Renderer/Blocks/Footer.php new file mode 100644 index 0000000000..e3387726f2 --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Footer.php @@ -0,0 +1,32 @@ +getStyles($element['styles'], 'link') . '"', $element['text']); + } + + // apply text styles + if(isset($element['styles']['link'])) { + $element['text'] = str_replace('getStyles($element['styles'], 'block') . '" + valign="top"> +
' . $element['text'] . '
+ + '; + + return $template; + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Blocks/Header.php b/lib/Newsletter/Renderer/Blocks/Header.php new file mode 100644 index 0000000000..8bfe1508cf --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Header.php @@ -0,0 +1,32 @@ +getStyles($element['styles'], 'link') . '"', $element['text']); + } + + // apply text styles + if(isset($element['styles']['link'])) { + $element['text'] = str_replace('getBlockStyles($element) . '" + valign="top"> +
' . $element['text'] . '
+ + '; + + return $template; + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Blocks/Image.php b/lib/Newsletter/Renderer/Blocks/Image.php new file mode 100644 index 0000000000..82472fb2c6 --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Image.php @@ -0,0 +1,26 @@ + + + + + '; + + return $template; + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Blocks/Renderer.php b/lib/Newsletter/Renderer/Blocks/Renderer.php new file mode 100644 index 0000000000..f06d8548d3 --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Renderer.php @@ -0,0 +1,25 @@ +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) : ''; + } + +} diff --git a/lib/Newsletter/Renderer/Blocks/Social.php b/lib/Newsletter/Renderer/Blocks/Social.php new file mode 100644 index 0000000000..3285f96111 --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Social.php @@ -0,0 +1,28 @@ + + ' . $icon['iconType'] . ' + + '; + } + } + + $template = ' + + +
' . $iconsBlock . '
+ + '; + + return $template; + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Blocks/Spacer.php b/lib/Newsletter/Renderer/Blocks/Spacer.php similarity index 60% rename from lib/Newsletter/Blocks/Spacer.php rename to lib/Newsletter/Renderer/Blocks/Spacer.php index 14ff428f94..9262001434 100644 --- a/lib/Newsletter/Blocks/Spacer.php +++ b/lib/Newsletter/Renderer/Blocks/Spacer.php @@ -1,12 +1,12 @@ - - -'; + + + '; return $template; } diff --git a/lib/Newsletter/Renderer/Blocks/Text.php b/lib/Newsletter/Renderer/Blocks/Text.php new file mode 100644 index 0000000000..e2a1985434 --- /dev/null +++ b/lib/Newsletter/Renderer/Blocks/Text.php @@ -0,0 +1,116 @@ + "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 = ' + + ' . $html . ' + '; + + return $template; + } + + static function removeLastBreakLine($html) { + return preg_replace('/
([^
]*)$/s', '', $html); + } + + static function convertParagraphsToTables($html) { + $html = preg_replace('/

(.*?)<\/p>/', ' + + + + +
+ + $1 + +
', $html); + + return preg_replace('/(.*?)<\/p>/', ' + + + + +
+ + $2 + +
', $html); + } + + static function convertEmptyParagraphsToLineBreaks($html) { + return preg_replace('/<\/p>/', '
', $html); + } + + static function addLineBreakAfterTags($html) { + return preg_replace('/(<\/(ul|ol|h\d)>)/', '$1
', $html); + } + + static function convertBlockquotesToTables($html) { + $template = ' + + + + + + +
$1
+
'; + + return preg_replace('/

(.*?)<\/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 + )*+ + # closing tag + ) # end of the group 1 + ) + ~sxi +EOD; + + return preg_replace($pattern, '', $html); + } + +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Columns/Renderer.php b/lib/Newsletter/Renderer/Columns/Renderer.php new file mode 100644 index 0000000000..236ee2d9fb --- /dev/null +++ b/lib/Newsletter/Renderer/Columns/Renderer.php @@ -0,0 +1,78 @@ + 600, + 2 => 300, + 3 => 200 + ); + + public $columnClasses = array( + 1 => 'mailpoet_col-one', + 2 => 'mailpoet_col-two', + 3 => 'mailpoet_col-three' + ); + + function render($columnsCount, $columnsData) { + + $columnWidth = $this->columnWidths[$columnsCount]; + $columnClass = $this->columnClasses[$columnsCount]; + + // open column container + $columnContainerTemplate = ' + + + + + + +
+ '; + + $columnOpenTemplate = ' + + '; + + $columnCloseTemplate = ' + +
+ '; + + foreach ($columnsData as $index => $columnData) { + $index++; + $columnContainerTemplate .= $columnOpenTemplate . $columnData; + if($columnsCount > 1 && $index != $columnsCount) { + $columnContainerTemplate .= $columnCloseTemplate; + } + } + + // close column container + $columnContainerTemplate .= ' +
+ + + + + + '; + + return $columnContainerTemplate; + } +} diff --git a/lib/Newsletter/Renderer.php b/lib/Newsletter/Renderer/Renderer.php similarity index 87% rename from lib/Newsletter/Renderer.php rename to lib/Newsletter/Renderer/Renderer.php index 94ef6036b6..963d376eea 100644 --- a/lib/Newsletter/Renderer.php +++ b/lib/Newsletter/Renderer/Renderer.php @@ -1,5 +1,5 @@ blocksRenderer = new Blocks\Renderer(); $this->columnsRenderer = new Columns\Renderer(); + $this->stylesHelper = new StylesHelper(); $this->DOMQuery = new \pQuery(); $this->CSSInliner = new \MailPoet\Util\CSS(); $this->data = $newsletterData; @@ -29,7 +30,7 @@ class Renderer { } function renderContent($content) { - array_map(function($contentBlock) use(&$newsletterContent) { + array_map(function ($contentBlock) use (&$newsletterContent) { $columnCount = count($contentBlock['blocks']); $columnData = $this->blocksRenderer->render($contentBlock); $newsletterContent .= $this->columnsRenderer->render($columnCount, $columnData); @@ -56,7 +57,7 @@ class Renderer { } $newsletterStyles .= $selector . '{' . PHP_EOL; foreach ($style as $attribute => $individualStyle) { - $newsletterStyles .= $this->blocksRenderer->translateCSSAttribute($attribute) . ':' . $individualStyle . ';' . PHP_EOL; + $newsletterStyles .= $this->stylesHelper->translateCSSAttribute($attribute) . ':' . $individualStyle . ';' . PHP_EOL; } $newsletterStyles .= '}' . PHP_EOL; } @@ -65,7 +66,7 @@ class Renderer { } function renderTemplate($template, $data) { - return preg_replace_callback('/{{\w+}}/', function($matches) use(&$data) { + return preg_replace_callback('/{{\w+}}/', function ($matches) use (&$data) { return array_shift($data); }, $template); } diff --git a/lib/Newsletter/Renderer/StylesHelper.php b/lib/Newsletter/Renderer/StylesHelper.php new file mode 100644 index 0000000000..dd0339b568 --- /dev/null +++ b/lib/Newsletter/Renderer/StylesHelper.php @@ -0,0 +1,40 @@ + 'background-color', + 'fontColor' => 'color', + 'fontFamily' => 'font-family', + 'textDecoration' => 'text-decoration', + 'textAlign' => 'text-align', + 'fontSize' => 'font-size', + 'borderWidth' => 'border-width', + 'borderStyle' => 'border-style', + 'borderColor' => 'border-color', + 'borderRadius' => 'border-radius', + 'lineHeight' => 'line-height' + ); + + function getBlockStyles($element, $ignore = false) { + if(!isset($element['styles']['block'])) { + return; + } + + return $this->getStyles($element['styles'], 'block', $ignore); + } + + function getStyles($data, $type, $ignore = false) { + array_map(function ($attribute, $style) use (&$styles, $ignore) { + if(!$ignore || !in_array($attribute, $ignore)) { + $styles .= $this->translateCSSAttribute($attribute) . ': ' . $style . ' !important;'; + } + }, array_keys($data[$type]), $data[$type]); + + return $styles; + } + + function translateCSSAttribute($attribute) { + return (array_key_exists($attribute, $this->cssAtributesTable)) ? $this->cssAtributesTable[$attribute] : $attribute; + } +} diff --git a/lib/Newsletter/Template.html b/lib/Newsletter/Renderer/Template.html similarity index 100% rename from lib/Newsletter/Template.html rename to lib/Newsletter/Renderer/Template.html diff --git a/lib/Newsletter/TestData.json b/lib/Newsletter/Renderer/TestData.json similarity index 100% rename from lib/Newsletter/TestData.json rename to lib/Newsletter/Renderer/TestData.json