Restrict allowed blocks to paragraph, heading and columns

[MAILPOET-5365]
This commit is contained in:
Rostislav Wolny
2023-06-16 14:53:51 +02:00
committed by Aschepikov
parent c7f1ecfb5e
commit 6be97e665b
2 changed files with 50 additions and 6 deletions

View File

@@ -7,8 +7,11 @@ namespace MailPoet\EmailEditor\Core;
* See register_post_type for details about EmailPostType args. * See register_post_type for details about EmailPostType args.
*/ */
class EmailEditor { class EmailEditor {
private const ALLOWED_BLOCK_TYPES = ['core/paragraph', 'core/heading', 'core/column', 'core/columns'];
public function initialize(): void { public function initialize(): void {
$this->registerEmailPostTypes(); $this->registerEmailPostTypes();
add_filter('allowed_block_types_all', [$this, 'setAllowedBlocksInEmails'], 100, 2);
} }
private function registerEmailPostTypes() { private function registerEmailPostTypes() {
@@ -40,4 +43,17 @@ class EmailEditor {
'show_in_rest' => true, // Important to enable Gutenberg editor '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;
}
} }

View File

@@ -3,19 +3,47 @@
namespace MailPoet\EmailEditor\Core; namespace MailPoet\EmailEditor\Core;
class EmailEditorTest extends \MailPoetTest { class EmailEditorTest extends \MailPoetTest {
public function testItRegistersHookedCustomPostTypes() { /** @var EmailEditor */
$callback = function ($postTypes) { private $emailEditor;
/** @var callable */
private $postRegisterCallback;
public function _before() {
parent::_before();
$this->emailEditor = $this->diContainer->get(EmailEditor::class);
$this->postRegisterCallback = function ($postTypes) {
$postTypes[] = [ $postTypes[] = [
'name' => 'custom_email_type', 'name' => 'custom_email_type',
'args' => [], 'args' => [],
]; ];
return $postTypes; return $postTypes;
}; };
add_filter('mailpoet_email_editor_post_types', $callback); add_filter('mailpoet_email_editor_post_types', $this->postRegisterCallback);
$emailEditor = $this->diContainer->get(EmailEditor::class); $this->emailEditor->initialize();
$emailEditor->initialize(); }
public function testItRegistersCustomPostTypeAddedViaHook() {
$postTypes = get_post_types(); $postTypes = get_post_types();
$this->assertArrayHasKey('custom_email_type', $postTypes); $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);
} }
} }