Use DI in block renderer

[MAILPOET-2899]
This commit is contained in:
Pavel Dohnal
2020-05-25 13:51:37 +02:00
committed by Veljko V
parent 7561730407
commit 5f73f9ee3e
5 changed files with 32 additions and 31 deletions

View File

@ -2,7 +2,6 @@
namespace MailPoet\Newsletter\Renderer\Blocks; namespace MailPoet\Newsletter\Renderer\Blocks;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterPost; use MailPoet\Models\NewsletterPost;
use MailPoet\Newsletter\AutomatedLatestContent; use MailPoet\Newsletter\AutomatedLatestContent;
@ -10,37 +9,35 @@ use MailPoet\Newsletter\Renderer\Columns\ColumnsHelper;
use MailPoet\Newsletter\Renderer\StylesHelper; use MailPoet\Newsletter\Renderer\StylesHelper;
class Renderer { class Renderer {
public $newsletter;
public $posts; public $posts;
public $ALC; public $ALC;
public function __construct(array $newsletter) { public function __construct(AutomatedLatestContent $ALC) {
$this->newsletter = $newsletter;
$this->posts = []; $this->posts = [];
$this->ALC = ContainerWrapper::getInstance()->get(AutomatedLatestContent::class); $this->ALC = $ALC;
} }
public function render($data) { public function render($newsletter, $data) {
$columnCount = count($data['blocks']); $columnCount = count($data['blocks']);
$columnsLayout = isset($data['columnLayout']) ? $data['columnLayout'] : null; $columnsLayout = isset($data['columnLayout']) ? $data['columnLayout'] : null;
$columnWidths = ColumnsHelper::columnWidth($columnCount, $columnsLayout); $columnWidths = ColumnsHelper::columnWidth($columnCount, $columnsLayout);
$columnContent = []; $columnContent = [];
foreach ($data['blocks'] as $index => $columnBlocks) { foreach ($data['blocks'] as $index => $columnBlocks) {
$renderedBlockElement = $this->renderBlocksInColumn($columnBlocks, $columnWidths[$index]); $renderedBlockElement = $this->renderBlocksInColumn($newsletter, $columnBlocks, $columnWidths[$index]);
$columnContent[] = $renderedBlockElement; $columnContent[] = $renderedBlockElement;
} }
return $columnContent; return $columnContent;
} }
private function renderBlocksInColumn($block, $columnBaseWidth) { private function renderBlocksInColumn($newsletter, $block, $columnBaseWidth) {
$blockContent = ''; $blockContent = '';
$_this = $this; $_this = $this;
array_map(function($block) use (&$blockContent, $columnBaseWidth, $_this) { array_map(function($block) use (&$blockContent, $columnBaseWidth, $newsletter, $_this) {
$renderedBlockElement = $_this->createElementFromBlockType($block, $columnBaseWidth); $renderedBlockElement = $_this->createElementFromBlockType($newsletter, $block, $columnBaseWidth);
if (isset($block['blocks'])) { if (isset($block['blocks'])) {
$renderedBlockElement = $_this->renderBlocksInColumn($block, $columnBaseWidth); $renderedBlockElement = $_this->renderBlocksInColumn($newsletter, $block, $columnBaseWidth);
// nested vertical column container is rendered as an array // nested vertical column container is rendered as an array
if (is_array($renderedBlockElement)) { if (is_array($renderedBlockElement)) {
$renderedBlockElement = implode('', $renderedBlockElement); $renderedBlockElement = implode('', $renderedBlockElement);
@ -52,9 +49,9 @@ class Renderer {
return $blockContent; return $blockContent;
} }
public function createElementFromBlockType($block, $columnBaseWidth) { public function createElementFromBlockType($newsletter, $block, $columnBaseWidth) {
if ($block['type'] === 'automatedLatestContent') { if ($block['type'] === 'automatedLatestContent') {
$content = $this->processAutomatedLatestContent($block, $columnBaseWidth); $content = $this->processAutomatedLatestContent($newsletter, $block, $columnBaseWidth);
return $content; return $content;
} }
$block = StylesHelper::applyTextAlignment($block); $block = StylesHelper::applyTextAlignment($block);
@ -65,11 +62,11 @@ class Renderer {
return $blockClass::render($block, $columnBaseWidth); return $blockClass::render($block, $columnBaseWidth);
} }
public function automatedLatestContentTransformedPosts($args) { public function automatedLatestContentTransformedPosts($newsletter, $args) {
$newerThanTimestamp = false; $newerThanTimestamp = false;
$newsletterId = false; $newsletterId = false;
if ($this->newsletter['type'] === Newsletter::TYPE_NOTIFICATION_HISTORY) { if ($newsletter['type'] === Newsletter::TYPE_NOTIFICATION_HISTORY) {
$newsletterId = $this->newsletter['parent_id']; $newsletterId = $newsletter['parent_id'];
$lastPost = NewsletterPost::getNewestNewsletterPost($newsletterId); $lastPost = NewsletterPost::getNewestNewsletterPost($newsletterId);
if ($lastPost) { if ($lastPost) {
@ -85,13 +82,12 @@ class Renderer {
return $this->ALC->transformPosts($args, $aLCPosts); return $this->ALC->transformPosts($args, $aLCPosts);
} }
public function processAutomatedLatestContent($args, $columnBaseWidth) { public function processAutomatedLatestContent($newsletter, $args, $columnBaseWidth) {
$transformedPosts = [ $transformedPosts = [
'blocks' => $this->automatedLatestContentTransformedPosts($args), 'blocks' => $this->automatedLatestContentTransformedPosts($newsletter, $args),
]; ];
$transformedPosts = StylesHelper::applyTextAlignment($transformedPosts); $transformedPosts = StylesHelper::applyTextAlignment($transformedPosts);
$renderedPosts = $this->renderBlocksInColumn($transformedPosts, $columnBaseWidth); return $this->renderBlocksInColumn($newsletter, $transformedPosts, $columnBaseWidth);
return $renderedPosts;
} }
public function getPosts() { public function getPosts() {

View File

@ -32,15 +32,16 @@ class Preprocessor {
/** /**
* @param array $content * @param array $content
* @param array $newsletter
* @return array * @return array
*/ */
public function process($content) { public function process($newsletter, $content) {
if (!array_key_exists('blocks', $content)) { if (!array_key_exists('blocks', $content)) {
return $content; return $content;
} }
$blocks = []; $blocks = [];
foreach ($content['blocks'] as $block) { foreach ($content['blocks'] as $block) {
$blocks = array_merge($blocks, $this->processBlock($block)); $blocks = array_merge($blocks, $this->processBlock($newsletter, $block));
} }
$content['blocks'] = $blocks; $content['blocks'] = $blocks;
return $content; return $content;
@ -48,12 +49,13 @@ class Preprocessor {
/** /**
* @param array $block * @param array $block
* @param array $newsletter
* @return array * @return array
*/ */
public function processBlock($block) { public function processBlock($newsletter, $block) {
switch ($block['type']) { switch ($block['type']) {
case 'automatedLatestContentLayout': case 'automatedLatestContentLayout':
return $this->blocksRenderer->automatedLatestContentTransformedPosts($block); return $this->blocksRenderer->automatedLatestContentTransformedPosts($newsletter, $block);
case 'woocommerceHeading': case 'woocommerceHeading':
$wcEmailSettings = $this->transactionalEmails->getWCEmailSettings(); $wcEmailSettings = $this->transactionalEmails->getWCEmailSettings();
$content = self::WC_HEADING_BEFORE . '<h1 style="color:' . $wcEmailSettings['base_text_color'] . ';">' . self::WC_HEADING_PLACEHOLDER . '</h1>' . self::WC_HEADING_AFTER; $content = self::WC_HEADING_BEFORE . '<h1 style="color:' . $wcEmailSettings['base_text_color'] . ';">' . self::WC_HEADING_PLACEHOLDER . '</h1>' . self::WC_HEADING_AFTER;

View File

@ -5,6 +5,7 @@ namespace MailPoet\Newsletter\Renderer;
use MailPoet\Config\Env; use MailPoet\Config\Env;
use MailPoet\DI\ContainerWrapper; use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Newsletter\AutomatedLatestContent;
use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper; use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Util\License\License; use MailPoet\Util\License\License;
@ -31,7 +32,9 @@ class Renderer {
public function __construct($newsletter, $preview = false) { public function __construct($newsletter, $preview = false) {
$this->newsletter = ($newsletter instanceof Newsletter) ? $newsletter->asArray() : $newsletter; $this->newsletter = ($newsletter instanceof Newsletter) ? $newsletter->asArray() : $newsletter;
$this->preview = $preview; $this->preview = $preview;
$this->blocksRenderer = new Blocks\Renderer($this->newsletter); $this->blocksRenderer = new Blocks\Renderer(
ContainerWrapper::getInstance()->get(AutomatedLatestContent::class)
);
$this->columnsRenderer = new Columns\Renderer(); $this->columnsRenderer = new Columns\Renderer();
$this->preprocessor = new Preprocessor( $this->preprocessor = new Preprocessor(
$this->blocksRenderer, $this->blocksRenderer,
@ -60,7 +63,7 @@ class Renderer {
$content = $this->addMailpoetLogoContentBlock($content, $styles); $content = $this->addMailpoetLogoContentBlock($content, $styles);
} }
$content = $this->preprocessor->process($content); $content = $this->preprocessor->process($newsletter, $content);
$renderedBody = $this->renderBody($content); $renderedBody = $this->renderBody($content);
$renderedStyles = $this->renderStyles($styles); $renderedStyles = $this->renderStyles($styles);
$customFontsLinks = StylesHelper::getCustomFontsLinks($styles); $customFontsLinks = StylesHelper::getCustomFontsLinks($styles);
@ -100,7 +103,7 @@ class Renderer {
$_this = $this; $_this = $this;
$renderedContent = array_map(function($contentBlock) use($_this) { $renderedContent = array_map(function($contentBlock) use($_this) {
$columnsData = $_this->blocksRenderer->render($contentBlock); $columnsData = $_this->blocksRenderer->render($_this->newsletter, $contentBlock);
return $_this->columnsRenderer->render( return $_this->columnsRenderer->render(
$contentBlock, $contentBlock,

View File

@ -7,7 +7,7 @@ use MailPoet\WP\Functions as WPFunctions;
class AutomatedLatestContentTest extends \MailPoetTest { class AutomatedLatestContentTest extends \MailPoetTest {
public function testItGetsPostTypes() { public function testItGetsPostTypes() {
$endpoint = new AutomatedLatestContent(new \MailPoet\Newsletter\AutomatedLatestContent(), new WPFunctions); $endpoint = $this->diContainer->get(AutomatedLatestContent::class);
$response = $endpoint->getPostTypes(); $response = $endpoint->getPostTypes();
expect($response->data)->notEmpty(); expect($response->data)->notEmpty();
foreach ($response->data as $postType) { foreach ($response->data as $postType) {
@ -18,7 +18,7 @@ class AutomatedLatestContentTest extends \MailPoetTest {
} }
public function testItDoesNotGetPostTypesExludedFromSearch() { public function testItDoesNotGetPostTypesExludedFromSearch() {
$endpoint = new AutomatedLatestContent(new \MailPoet\Newsletter\AutomatedLatestContent(), new WPFunctions); $endpoint = $this->diContainer->get(AutomatedLatestContent::class);
$response = $endpoint ->getPostTypes(); $response = $endpoint ->getPostTypes();
// WP's default post type 'revision' is excluded from search // WP's default post type 'revision' is excluded from search
// https://codex.wordpress.org/Post_Types // https://codex.wordpress.org/Post_Types

View File

@ -17,7 +17,7 @@ class PreprocessorTest extends \MailPoetUnitTest {
], ],
]); ]);
$preprocessor = new Preprocessor($renderer, $transactionalEmails); $preprocessor = new Preprocessor($renderer, $transactionalEmails);
expect($preprocessor->processBlock(['type' => 'woocommerceHeading']))->equals([[ expect($preprocessor->processBlock([], ['type' => 'woocommerceHeading']))->equals([[
'type' => 'container', 'type' => 'container',
'orientation' => 'horizontal', 'orientation' => 'horizontal',
'styles' => [ 'styles' => [
@ -42,7 +42,7 @@ class PreprocessorTest extends \MailPoetUnitTest {
public function testProcessWooCommerceContentBlock() { public function testProcessWooCommerceContentBlock() {
$renderer = Stub::make(Renderer::class); $renderer = Stub::make(Renderer::class);
$preprocessor = new Preprocessor($renderer, Stub::make(TransactionalEmails::class)); $preprocessor = new Preprocessor($renderer, Stub::make(TransactionalEmails::class));
expect($preprocessor->processBlock(['type' => 'woocommerceContent']))->equals([[ expect($preprocessor->processBlock([], ['type' => 'woocommerceContent']))->equals([[
'type' => 'container', 'type' => 'container',
'orientation' => 'horizontal', 'orientation' => 'horizontal',
'styles' => [ 'styles' => [