diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index 1071780947..189366dc40 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -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); diff --git a/mailpoet/lib/EmailEditor/Engine/Renderer/Templates/email-general.html b/mailpoet/lib/EmailEditor/Engine/Renderer/Templates/email-general.html index 888d7460ff..4ab0ef8971 100644 --- a/mailpoet/lib/EmailEditor/Engine/Renderer/Templates/email-general.html +++ b/mailpoet/lib/EmailEditor/Engine/Renderer/Templates/email-general.html @@ -1 +1 @@ - + diff --git a/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php b/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php index 0049f7cb90..8ce09ee9ad 100644 --- a/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php +++ b/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php @@ -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()); } /** diff --git a/mailpoet/lib/EmailEditor/Integrations/Core/Renderer/Blocks/PostContent.php b/mailpoet/lib/EmailEditor/Integrations/Core/Renderer/Blocks/PostContent.php deleted file mode 100644 index 72f0e5616f..0000000000 --- a/mailpoet/lib/EmailEditor/Integrations/Core/Renderer/Blocks/PostContent.php +++ /dev/null @@ -1,18 +0,0 @@ -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(']]>', ']]>', $post->post_content)); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps - } -} diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/AbstractBlock.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/AbstractBlock.php new file mode 100644 index 0000000000..f7eebacc37 --- /dev/null +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/AbstractBlock.php @@ -0,0 +1,63 @@ +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; + } +} diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/EmailContent.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/EmailContent.php new file mode 100644 index 0000000000..b7d35cf168 --- /dev/null +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/EmailContent.php @@ -0,0 +1,35 @@ +', ']]>', $post->post_content)); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + + return sprintf( + '
%2$s
', + esc_attr('wp-block-' . $this->blockName), + $content + ); + } +} diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/email-content/block.json b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/email-content/block.json new file mode 100644 index 0000000000..90efb02234 --- /dev/null +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypes/email-content/block.json @@ -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" +} diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypesController.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypesController.php new file mode 100644 index 0000000000..9eefe305ad --- /dev/null +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/Blocks/BlockTypesController.php @@ -0,0 +1,22 @@ +registerBlockTypes(); + } + + public function registerBlockTypes() { + foreach ($this->getBlockTypes() as $type) { + $block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $type; + new $block_type_class(); + } + } + + private function getBlockTypes() { + return [ + 'EmailContent', + ]; + } +} diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php index 014f2330ac..9b8629030b 100644 --- a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php @@ -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(); } diff --git a/mailpoet/tests/integration/EmailEditor/Engine/Renderer/ContentRenderer/ContentRendererTest.php b/mailpoet/tests/integration/EmailEditor/Engine/Renderer/ContentRenderer/ContentRendererTest.php index 27563eaa98..924d242c6a 100644 --- a/mailpoet/tests/integration/EmailEditor/Engine/Renderer/ContentRenderer/ContentRendererTest.php +++ b/mailpoet/tests/integration/EmailEditor/Engine/Renderer/ContentRenderer/ContentRendererTest.php @@ -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 = ''; + $template->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 = ''; + $template->content = ''; $rendered = $this->renderer->render($this->emailPost, $template); $paragraphStyles = $this->getStylesValueForTag($rendered, 'p'); verify($paragraphStyles)->stringContainsString('margin:0');