Move preprocessing logic into Preprocessor class

[MAILPOET-2286]

Additional preprocessing logic is needed to handle WooCommerce
blocks, so instead of making the Renderer class even bigger,
I am creating a new Preprocessor class. I wanted to move the
`addMailpoetLogoContentBlock` logic as well but it will break
tests and require a lot of refactor to fix them so I am leaving
it here for now.
This commit is contained in:
Amine Ben hammou
2019-11-07 21:44:57 +01:00
committed by Jack Kitterhing
parent 9e27afc994
commit 5f5a254548
2 changed files with 42 additions and 26 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace MailPoet\Newsletter\Renderer;
use MailPoet\Newsletter\Renderer\Blocks\Renderer as BlocksRenderer;
class Preprocessor {
/** @var BlocksRenderer */
private $blocks_renderer;
public function __construct(BlocksRenderer $blocks_renderer) {
$this->blocks_renderer = $blocks_renderer;
}
/**
* @param array $content
* @return array
*/
public function process($content) {
if (!array_key_exists('blocks', $content)) {
return $content;
}
$blocks = [];
foreach ($content['blocks'] as $block) {
if ($block['type'] === 'automatedLatestContentLayout') {
$blocks = array_merge(
$blocks,
$this->blocks_renderer->automatedLatestContentTransformedPosts($block)
);
} else {
$blocks[] = $block;
}
}
$content['blocks'] = $blocks;
return $content;
}
}

View File

@ -13,6 +13,7 @@ use MailPoet\WP\Functions as WPFunctions;
class Renderer {
public $blocks_renderer;
public $columns_renderer;
private $preprocessor;
public $CSS_inliner;
public $newsletter;
public $preview;
@ -30,7 +31,8 @@ class Renderer {
$this->preview = $preview;
$this->blocks_renderer = new Blocks\Renderer($this->newsletter);
$this->columns_renderer = new Columns\Renderer();
$this->CSS_inliner = new \MailPoetVendor\CSS();
$this->preprocessor = new Preprocessor($this->blocks_renderer);
$this->CSS_inliner = new \MailPoet\Util\CSS();
$this->template = file_get_contents(dirname(__FILE__) . '/' . self::NEWSLETTER_TEMPLATE);
$this->premium_activated = License::getLicense();
$bridge = new Bridge();
@ -53,7 +55,7 @@ class Renderer {
$content = $this->addMailpoetLogoContentBlock($content, $styles);
}
$content = $this->preProcessALC($content);
$content = $this->preprocessor->process($content);
$rendered_body = $this->renderBody($content);
$rendered_styles = $this->renderStyles($styles);
$custom_fonts_links = StylesHelper::getCustomFontsLinks($styles);
@ -78,30 +80,6 @@ class Renderer {
$rendered_newsletter;
}
/**
* @param array $content
* @return array
*/
private function preProcessALC(array $content) {
$blocks = [];
$content_blocks = (array_key_exists('blocks', $content))
? $content['blocks']
: [];
foreach ($content_blocks as $block) {
if ($block['type'] === 'automatedLatestContentLayout') {
$blocks = array_merge(
$blocks,
$this->blocks_renderer->automatedLatestContentTransformedPosts($block)
);
} else {
$blocks[] = $block;
}
}
$content['blocks'] = $blocks;
return $content;
}
/**
* @param array $content
* @return string