Introduce block to handle email content

This commit is contained in:
Mike Jolley
2024-04-09 14:03:56 +01:00
committed by Rostislav Wolný
parent 37e901c809
commit 3f2b96f3a6
10 changed files with 153 additions and 23 deletions

View File

@@ -353,6 +353,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\EmailEditor\Integrations\Core\Initializer::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\EmailEditor::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\EmailApiController::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController::class)->setPublic(true);
// Features
$container->autowire(\MailPoet\Features\FeaturesController::class)->setPublic(true);
$container->autowire(\MailPoet\Features\FeatureFlagsController::class)->setPublic(true);

View File

@@ -1 +1 @@
<!-- wp:post-content /-->
<!-- wp:mailpoet/email-content /-->

View File

@@ -24,7 +24,6 @@ class Initializer {
$blocksRegistry->addBlockRenderer('core/image', new Renderer\Blocks\Image());
$blocksRegistry->addBlockRenderer('core/buttons', new Renderer\Blocks\Buttons(new FlexLayoutRenderer()));
$blocksRegistry->addBlockRenderer('core/button', new Renderer\Blocks\Button());
$blocksRegistry->addBlockRenderer('core/post-content', new Renderer\Blocks\PostContent());
}
/**

View File

@@ -1,18 +0,0 @@
<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\Core\Renderer\Blocks;
use MailPoet\EmailEditor\Engine\SettingsController;
class PostContent extends AbstractBlockRenderer {
public function render(string $blockContent, array $parsedBlock, SettingsController $settingsController): string {
return $this->renderContent($blockContent, $parsedBlock, $settingsController);
}
protected function renderContent(string $blockContent, array $parsedBlock, SettingsController $settingsController): string {
global $post;
/** This filter is documented in wp-includes/post-template.php */
return apply_filters('the_content', str_replace(']]>', ']]&gt;', $post->post_content)); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
}
}

View File

@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypes;
use WP_Block;
abstract class AbstractBlock {
protected $namespace = 'mailpoet';
protected $blockName = '';
public function __construct() {
$this->registerBlockType();
}
/**
* Get the block type.
*
* @return string
*/
protected function getBlockType() {
return $this->namespace . '/' . $this->blockName;
}
protected function parseRenderCallbackAttributes($attributes): array {
return is_a($attributes, 'WP_Block') ? $attributes->attributes : $attributes;
}
/**
* The default render_callback for all blocks.
*/
public function renderCallback($attributes = [], $content = '', $block = null): string {
$render_callback_attributes = $this->parseRenderCallbackAttributes($attributes);
return $this->render($render_callback_attributes, $content, $block);
}
/**
* Registers the block type with WordPress.
*/
protected function registerBlockType() {
$metadata_path = __DIR__ . '/' . $this->blockName . '/block.json';
$block_settings = [
'render_callback' => [$this, 'renderCallback'],
'api_version' => '2',
];
register_block_type_from_metadata(
$metadata_path,
$block_settings
);
}
/**
* Render the block. Extended by children.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param WP_Block $block Block instance.
* @return string Rendered block type output.
*/
protected function render($attributes, $content, $block) {
return $content;
}
}

View File

@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypes;
class EmailContent extends AbstractBlock {
/**
* Block name.
*
* @var string
*/
protected $blockName = 'email-content';
/**
* Render the block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param \WP_Block $block Block instance.
*
* @return string | void Rendered block output.
*/
protected function render($attributes, $content, $block) {
global $post;
/** This filter is documented in wp-includes/post-template.php */
$content = apply_filters('the_content', str_replace(']]>', ']]&gt;', $post->post_content)); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
return sprintf(
'<div class="%1$s">%2$s</div>',
esc_attr('wp-block-' . $this->blockName),
$content
);
}
}

View File

@@ -0,0 +1,19 @@
{
"name": "mailpoet/email-content",
"version": "1.0.0",
"title": "Email Content",
"description": "Shows email content within the email editor.",
"category": "mailpoet",
"keywords": ["MailPoet"],
"textdomain": "mailpoet",
"attributes": {},
"supports": {
"align": false,
"html": false,
"multiple": false,
"reusable": false,
"inserter": false
},
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}

View File

@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\MailPoet\Blocks;
class BlockTypesController {
public function initialize(): void {
$this->registerBlockTypes();
}
public function registerBlockTypes() {
foreach ($this->getBlockTypes() as $type) {
$block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $type;
new $block_type_class();
}
}
private function getBlockTypes() {
return [
'EmailContent',
];
}
}

View File

@@ -2,6 +2,7 @@
namespace MailPoet\EmailEditor\Integrations\MailPoet;
use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController;
use MailPoet\Features\FeaturesController;
use MailPoet\Util\CdnAssetUrl;
use MailPoet\WP\Functions as WPFunctions;
@@ -21,16 +22,21 @@ class EmailEditor {
/** @var CdnAssetUrl */
private $cdnAssetUrl;
/** @var BlockTypesController */
private $blockTypesController;
public function __construct(
WPFunctions $wp,
FeaturesController $featuresController,
EmailApiController $emailApiController,
CdnAssetUrl $cdnAssetUrl
CdnAssetUrl $cdnAssetUrl,
BlockTypesController $blockTypesController
) {
$this->wp = $wp;
$this->featuresController = $featuresController;
$this->emailApiController = $emailApiController;
$this->cdnAssetUrl = $cdnAssetUrl;
$this->blockTypesController = $blockTypesController;
}
public function initialize(): void {
@@ -38,6 +44,7 @@ class EmailEditor {
return;
}
$this->wp->addFilter('mailpoet_email_editor_post_types', [$this, 'addEmailPostType']);
$this->blockTypesController->initialize();
$this->extendEmailPostApi();
}

View File

@@ -3,6 +3,7 @@
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
use MailPoet\EmailEditor\Engine\EmailEditor;
use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController;
require_once __DIR__ . '/DummyBlockRenderer.php';
@@ -14,6 +15,7 @@ class ContentRendererTest extends \MailPoetTest {
public function _before(): void {
parent::_before();
$this->diContainer->get(EmailEditor::class)->initialize();
$this->diContainer->get(BlockTypesController::class)->initialize();
$this->renderer = $this->diContainer->get(ContentRenderer::class);
$this->emailPost = new \WP_Post((object)[
'ID' => 1,
@@ -24,7 +26,7 @@ class ContentRendererTest extends \MailPoetTest {
public function testItRendersContent(): void {
$template = new \WP_Block_Template();
$template->id = 'template-id';
$template->content = '<!-- wp:post-content /-->';
$template->content = '<!-- wp:mailpoet/email-content /-->';
$content = $this->renderer->render(
$this->emailPost,
$template
@@ -35,7 +37,7 @@ class ContentRendererTest extends \MailPoetTest {
public function testItInlinesContentStyles(): void {
$template = new \WP_Block_Template();
$template->id = 'template-id';
$template->content = '<!-- wp:post-content /-->';
$template->content = '<!-- wp:mailpoet/email-content /-->';
$rendered = $this->renderer->render($this->emailPost, $template);
$paragraphStyles = $this->getStylesValueForTag($rendered, 'p');
verify($paragraphStyles)->stringContainsString('margin:0');