Use DI in block renderer
[MAILPOET-2899]
This commit is contained in:
@ -2,7 +2,6 @@
|
||||
|
||||
namespace MailPoet\Newsletter\Renderer\Blocks;
|
||||
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\NewsletterPost;
|
||||
use MailPoet\Newsletter\AutomatedLatestContent;
|
||||
@ -10,37 +9,35 @@ use MailPoet\Newsletter\Renderer\Columns\ColumnsHelper;
|
||||
use MailPoet\Newsletter\Renderer\StylesHelper;
|
||||
|
||||
class Renderer {
|
||||
public $newsletter;
|
||||
public $posts;
|
||||
public $ALC;
|
||||
|
||||
public function __construct(array $newsletter) {
|
||||
$this->newsletter = $newsletter;
|
||||
public function __construct(AutomatedLatestContent $ALC) {
|
||||
$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']);
|
||||
$columnsLayout = isset($data['columnLayout']) ? $data['columnLayout'] : null;
|
||||
$columnWidths = ColumnsHelper::columnWidth($columnCount, $columnsLayout);
|
||||
$columnContent = [];
|
||||
|
||||
foreach ($data['blocks'] as $index => $columnBlocks) {
|
||||
$renderedBlockElement = $this->renderBlocksInColumn($columnBlocks, $columnWidths[$index]);
|
||||
$renderedBlockElement = $this->renderBlocksInColumn($newsletter, $columnBlocks, $columnWidths[$index]);
|
||||
$columnContent[] = $renderedBlockElement;
|
||||
}
|
||||
|
||||
return $columnContent;
|
||||
}
|
||||
|
||||
private function renderBlocksInColumn($block, $columnBaseWidth) {
|
||||
private function renderBlocksInColumn($newsletter, $block, $columnBaseWidth) {
|
||||
$blockContent = '';
|
||||
$_this = $this;
|
||||
array_map(function($block) use (&$blockContent, $columnBaseWidth, $_this) {
|
||||
$renderedBlockElement = $_this->createElementFromBlockType($block, $columnBaseWidth);
|
||||
array_map(function($block) use (&$blockContent, $columnBaseWidth, $newsletter, $_this) {
|
||||
$renderedBlockElement = $_this->createElementFromBlockType($newsletter, $block, $columnBaseWidth);
|
||||
if (isset($block['blocks'])) {
|
||||
$renderedBlockElement = $_this->renderBlocksInColumn($block, $columnBaseWidth);
|
||||
$renderedBlockElement = $_this->renderBlocksInColumn($newsletter, $block, $columnBaseWidth);
|
||||
// nested vertical column container is rendered as an array
|
||||
if (is_array($renderedBlockElement)) {
|
||||
$renderedBlockElement = implode('', $renderedBlockElement);
|
||||
@ -52,9 +49,9 @@ class Renderer {
|
||||
return $blockContent;
|
||||
}
|
||||
|
||||
public function createElementFromBlockType($block, $columnBaseWidth) {
|
||||
public function createElementFromBlockType($newsletter, $block, $columnBaseWidth) {
|
||||
if ($block['type'] === 'automatedLatestContent') {
|
||||
$content = $this->processAutomatedLatestContent($block, $columnBaseWidth);
|
||||
$content = $this->processAutomatedLatestContent($newsletter, $block, $columnBaseWidth);
|
||||
return $content;
|
||||
}
|
||||
$block = StylesHelper::applyTextAlignment($block);
|
||||
@ -65,11 +62,11 @@ class Renderer {
|
||||
return $blockClass::render($block, $columnBaseWidth);
|
||||
}
|
||||
|
||||
public function automatedLatestContentTransformedPosts($args) {
|
||||
public function automatedLatestContentTransformedPosts($newsletter, $args) {
|
||||
$newerThanTimestamp = false;
|
||||
$newsletterId = false;
|
||||
if ($this->newsletter['type'] === Newsletter::TYPE_NOTIFICATION_HISTORY) {
|
||||
$newsletterId = $this->newsletter['parent_id'];
|
||||
if ($newsletter['type'] === Newsletter::TYPE_NOTIFICATION_HISTORY) {
|
||||
$newsletterId = $newsletter['parent_id'];
|
||||
|
||||
$lastPost = NewsletterPost::getNewestNewsletterPost($newsletterId);
|
||||
if ($lastPost) {
|
||||
@ -85,13 +82,12 @@ class Renderer {
|
||||
return $this->ALC->transformPosts($args, $aLCPosts);
|
||||
}
|
||||
|
||||
public function processAutomatedLatestContent($args, $columnBaseWidth) {
|
||||
public function processAutomatedLatestContent($newsletter, $args, $columnBaseWidth) {
|
||||
$transformedPosts = [
|
||||
'blocks' => $this->automatedLatestContentTransformedPosts($args),
|
||||
'blocks' => $this->automatedLatestContentTransformedPosts($newsletter, $args),
|
||||
];
|
||||
$transformedPosts = StylesHelper::applyTextAlignment($transformedPosts);
|
||||
$renderedPosts = $this->renderBlocksInColumn($transformedPosts, $columnBaseWidth);
|
||||
return $renderedPosts;
|
||||
return $this->renderBlocksInColumn($newsletter, $transformedPosts, $columnBaseWidth);
|
||||
}
|
||||
|
||||
public function getPosts() {
|
||||
|
@ -32,15 +32,16 @@ class Preprocessor {
|
||||
|
||||
/**
|
||||
* @param array $content
|
||||
* @param array $newsletter
|
||||
* @return array
|
||||
*/
|
||||
public function process($content) {
|
||||
public function process($newsletter, $content) {
|
||||
if (!array_key_exists('blocks', $content)) {
|
||||
return $content;
|
||||
}
|
||||
$blocks = [];
|
||||
foreach ($content['blocks'] as $block) {
|
||||
$blocks = array_merge($blocks, $this->processBlock($block));
|
||||
$blocks = array_merge($blocks, $this->processBlock($newsletter, $block));
|
||||
}
|
||||
$content['blocks'] = $blocks;
|
||||
return $content;
|
||||
@ -48,12 +49,13 @@ class Preprocessor {
|
||||
|
||||
/**
|
||||
* @param array $block
|
||||
* @param array $newsletter
|
||||
* @return array
|
||||
*/
|
||||
public function processBlock($block) {
|
||||
public function processBlock($newsletter, $block) {
|
||||
switch ($block['type']) {
|
||||
case 'automatedLatestContentLayout':
|
||||
return $this->blocksRenderer->automatedLatestContentTransformedPosts($block);
|
||||
return $this->blocksRenderer->automatedLatestContentTransformedPosts($newsletter, $block);
|
||||
case 'woocommerceHeading':
|
||||
$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;
|
||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Newsletter\Renderer;
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Newsletter\AutomatedLatestContent;
|
||||
use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper;
|
||||
use MailPoet\Services\Bridge;
|
||||
use MailPoet\Util\License\License;
|
||||
@ -31,7 +32,9 @@ class Renderer {
|
||||
public function __construct($newsletter, $preview = false) {
|
||||
$this->newsletter = ($newsletter instanceof Newsletter) ? $newsletter->asArray() : $newsletter;
|
||||
$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->preprocessor = new Preprocessor(
|
||||
$this->blocksRenderer,
|
||||
@ -60,7 +63,7 @@ class Renderer {
|
||||
$content = $this->addMailpoetLogoContentBlock($content, $styles);
|
||||
}
|
||||
|
||||
$content = $this->preprocessor->process($content);
|
||||
$content = $this->preprocessor->process($newsletter, $content);
|
||||
$renderedBody = $this->renderBody($content);
|
||||
$renderedStyles = $this->renderStyles($styles);
|
||||
$customFontsLinks = StylesHelper::getCustomFontsLinks($styles);
|
||||
@ -100,7 +103,7 @@ class Renderer {
|
||||
$_this = $this;
|
||||
$renderedContent = array_map(function($contentBlock) use($_this) {
|
||||
|
||||
$columnsData = $_this->blocksRenderer->render($contentBlock);
|
||||
$columnsData = $_this->blocksRenderer->render($_this->newsletter, $contentBlock);
|
||||
|
||||
return $_this->columnsRenderer->render(
|
||||
$contentBlock,
|
||||
|
@ -7,7 +7,7 @@ use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class AutomatedLatestContentTest extends \MailPoetTest {
|
||||
public function testItGetsPostTypes() {
|
||||
$endpoint = new AutomatedLatestContent(new \MailPoet\Newsletter\AutomatedLatestContent(), new WPFunctions);
|
||||
$endpoint = $this->diContainer->get(AutomatedLatestContent::class);
|
||||
$response = $endpoint->getPostTypes();
|
||||
expect($response->data)->notEmpty();
|
||||
foreach ($response->data as $postType) {
|
||||
@ -18,7 +18,7 @@ class AutomatedLatestContentTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
public function testItDoesNotGetPostTypesExludedFromSearch() {
|
||||
$endpoint = new AutomatedLatestContent(new \MailPoet\Newsletter\AutomatedLatestContent(), new WPFunctions);
|
||||
$endpoint = $this->diContainer->get(AutomatedLatestContent::class);
|
||||
$response = $endpoint ->getPostTypes();
|
||||
// WP's default post type 'revision' is excluded from search
|
||||
// https://codex.wordpress.org/Post_Types
|
||||
|
@ -17,7 +17,7 @@ class PreprocessorTest extends \MailPoetUnitTest {
|
||||
],
|
||||
]);
|
||||
$preprocessor = new Preprocessor($renderer, $transactionalEmails);
|
||||
expect($preprocessor->processBlock(['type' => 'woocommerceHeading']))->equals([[
|
||||
expect($preprocessor->processBlock([], ['type' => 'woocommerceHeading']))->equals([[
|
||||
'type' => 'container',
|
||||
'orientation' => 'horizontal',
|
||||
'styles' => [
|
||||
@ -42,7 +42,7 @@ class PreprocessorTest extends \MailPoetUnitTest {
|
||||
public function testProcessWooCommerceContentBlock() {
|
||||
$renderer = Stub::make(Renderer::class);
|
||||
$preprocessor = new Preprocessor($renderer, Stub::make(TransactionalEmails::class));
|
||||
expect($preprocessor->processBlock(['type' => 'woocommerceContent']))->equals([[
|
||||
expect($preprocessor->processBlock([], ['type' => 'woocommerceContent']))->equals([[
|
||||
'type' => 'container',
|
||||
'orientation' => 'horizontal',
|
||||
'styles' => [
|
||||
|
Reference in New Issue
Block a user