diff --git a/mailpoet/lib/EmailEditor/Core/EmailEditor.php b/mailpoet/lib/EmailEditor/Core/EmailEditor.php index e314272af4..1fef030207 100644 --- a/mailpoet/lib/EmailEditor/Core/EmailEditor.php +++ b/mailpoet/lib/EmailEditor/Core/EmailEditor.php @@ -7,8 +7,11 @@ namespace MailPoet\EmailEditor\Core; * See register_post_type for details about EmailPostType args. */ class EmailEditor { + private const ALLOWED_BLOCK_TYPES = ['core/paragraph', 'core/heading', 'core/column', 'core/columns']; + public function initialize(): void { $this->registerEmailPostTypes(); + add_filter('allowed_block_types_all', [$this, 'setAllowedBlocksInEmails'], 100, 2); } private function registerEmailPostTypes() { @@ -40,4 +43,17 @@ class EmailEditor { 'show_in_rest' => true, // Important to enable Gutenberg editor ]; } + + /** + * @param string[]|bool $allowedBlockTypes + * @param \WP_Block_Editor_Context $blockEditorContext + * @return array|bool + */ + public function setAllowedBlocksInEmails($allowedBlockTypes, \WP_Block_Editor_Context $blockEditorContext) { + $emailPostTypes = array_column($this->getPostTypes(), 'name'); + if (!$blockEditorContext->post || !in_array($blockEditorContext->post->post_type, $emailPostTypes, true)) { + return $allowedBlockTypes; + } + return self::ALLOWED_BLOCK_TYPES; + } } diff --git a/mailpoet/tests/integration/EmailEditor/Core/EmailEditorTest.php b/mailpoet/tests/integration/EmailEditor/Core/EmailEditorTest.php index bd80606efe..78ad7efe09 100644 --- a/mailpoet/tests/integration/EmailEditor/Core/EmailEditorTest.php +++ b/mailpoet/tests/integration/EmailEditor/Core/EmailEditorTest.php @@ -3,19 +3,47 @@ namespace MailPoet\EmailEditor\Core; class EmailEditorTest extends \MailPoetTest { - public function testItRegistersHookedCustomPostTypes() { - $callback = function ($postTypes) { + /** @var EmailEditor */ + private $emailEditor; + + /** @var callable */ + private $postRegisterCallback; + + public function _before() { + parent::_before(); + $this->emailEditor = $this->diContainer->get(EmailEditor::class); + $this->postRegisterCallback = function ($postTypes) { $postTypes[] = [ 'name' => 'custom_email_type', 'args' => [], ]; return $postTypes; }; - add_filter('mailpoet_email_editor_post_types', $callback); - $emailEditor = $this->diContainer->get(EmailEditor::class); - $emailEditor->initialize(); + add_filter('mailpoet_email_editor_post_types', $this->postRegisterCallback); + $this->emailEditor->initialize(); + } + + public function testItRegistersCustomPostTypeAddedViaHook() { $postTypes = get_post_types(); $this->assertArrayHasKey('custom_email_type', $postTypes); - remove_filter('mailpoet_email_editor_post_types', $callback); + } + + public function testItRestrictsAllowedBlockTypes() { + $noEmailContext = new \WP_Block_Editor_Context(); + $blockTypes = get_allowed_block_types($noEmailContext); + expect($blockTypes)->equals(true); // true means all blocks are allowed + + $noEmailContext = new \WP_Block_Editor_Context($settings = ['post' => (object)['post_type' => 'custom_email_type']]); + $blockTypes = get_allowed_block_types($noEmailContext); + expect($blockTypes)->contains('core/paragraph'); + expect($blockTypes)->contains('core/heading'); + expect($blockTypes)->contains('core/columns'); + expect($blockTypes)->contains('core/column'); + expect(count((array)$blockTypes))->equals(4); + } + + public function _after() { + parent::_after(); + remove_filter('mailpoet_email_editor_post_types', $this->postRegisterCallback); } }