From 4b55bb5bb3ad69486e46fc372c339be18f132455 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Wed, 17 Jan 2024 14:35:05 +0100 Subject: [PATCH] Move default core/button styles configuration to integration [MAILPOET-5814] --- mailpoet/lib/EmailEditor/Engine/theme.json | 17 -------- .../Integrations/Core/Initializer.php | 12 ++++++ .../EmailEditor/Integrations/Core/theme.json | 23 +++++++++++ .../Engine/Renderer/RendererTest.php | 21 ---------- .../Core/Renderer/RendererTest.php | 40 +++++++++++++++++++ 5 files changed, 75 insertions(+), 38 deletions(-) create mode 100644 mailpoet/lib/EmailEditor/Integrations/Core/theme.json create mode 100644 mailpoet/tests/integration/EmailEditor/Integration/Core/Renderer/RendererTest.php diff --git a/mailpoet/lib/EmailEditor/Engine/theme.json b/mailpoet/lib/EmailEditor/Engine/theme.json index 4b29aead67..60010a4eed 100644 --- a/mailpoet/lib/EmailEditor/Engine/theme.json +++ b/mailpoet/lib/EmailEditor/Engine/theme.json @@ -149,23 +149,6 @@ "typography": { "fontFamily": "Arial, 'Helvetica Neue', Helvetica, sans-serif", "fontSize": "16px" - }, - "blocks": { - "core/button": { - "variations": {}, - "color": { - "background": "#32373c", - "text": "#ffffff" - }, - "spacing": { - "padding": { - "bottom": "0.7em", - "left": "1.4em", - "right": "1.4em", - "top": "0.7em" - } - } - } } } } diff --git a/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php b/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php index 592f545612..e773d78446 100644 --- a/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php +++ b/mailpoet/lib/EmailEditor/Integrations/Core/Initializer.php @@ -8,6 +8,7 @@ use MailPoet\EmailEditor\Engine\Renderer\Layout\FlexLayoutRenderer; class Initializer { public function initialize(): void { add_action('mailpoet_blocks_renderer_initialized', [$this, 'registerCoreBlocksRenderers'], 10, 1); + add_filter('mailpoet_email_editor_theme_json', [$this, 'adjustThemeJson'], 10, 1); } /** @@ -23,4 +24,15 @@ class Initializer { $blocksRegistry->addBlockRenderer('core/buttons', new Renderer\Blocks\Buttons(new FlexLayoutRenderer())); $blocksRegistry->addBlockRenderer('core/button', new Renderer\Blocks\Button()); } + + /** + * Adjusts the editor's theme to add blocks specific settings for core blocks. + */ + public function adjustThemeJson(\WP_Theme_JSON $editorThemeJson): \WP_Theme_JSON { + $themeJson = (string)file_get_contents(dirname(__FILE__) . '/theme.json'); + $themeJson = json_decode($themeJson, true); + /** @var array $themeJson */ + $editorThemeJson->merge(new \WP_Theme_JSON($themeJson, 'default')); + return $editorThemeJson; + } } diff --git a/mailpoet/lib/EmailEditor/Integrations/Core/theme.json b/mailpoet/lib/EmailEditor/Integrations/Core/theme.json new file mode 100644 index 0000000000..6383c0dac3 --- /dev/null +++ b/mailpoet/lib/EmailEditor/Integrations/Core/theme.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://schemas.wp.org/trunk/theme.json", + "version": 2, + "styles": { + "blocks": { + "core/button": { + "variations": {}, + "color": { + "background": "#32373c", + "text": "#ffffff" + }, + "spacing": { + "padding": { + "bottom": "0.7em", + "left": "1.4em", + "right": "1.4em", + "top": "0.7em" + } + } + } + } + } +} diff --git a/mailpoet/tests/integration/EmailEditor/Engine/Renderer/RendererTest.php b/mailpoet/tests/integration/EmailEditor/Engine/Renderer/RendererTest.php index 93f778d474..8549bf1fa8 100644 --- a/mailpoet/tests/integration/EmailEditor/Engine/Renderer/RendererTest.php +++ b/mailpoet/tests/integration/EmailEditor/Engine/Renderer/RendererTest.php @@ -140,25 +140,4 @@ class RendererTest extends \MailPoetTest { verify($style)->stringContainsString('padding-top:3px;'); verify($style)->stringContainsString('padding-bottom:4px;'); } - - public function testItInlinesButtonDefaultStyles() { - $this->emailPost = new \WP_Post((object)[ - 'post_content' => '
Button
', - ]); - $rendered = $this->renderer->render($this->emailPost, 'Subject', '', 'en'); - $doc = new \DOMDocument(); - $doc->loadHTML($rendered['html']); - $xpath = new \DOMXPath($doc); - $nodes = $xpath->query('//td[contains(@class, "wp-block-button")]'); - $button = null; - if (($nodes instanceof \DOMNodeList) && $nodes->length > 0) { - $button = $nodes->item(0); - } - $this->assertInstanceOf(\DOMElement::class, $button); - $this->assertInstanceOf(\DOMDocument::class, $button->ownerDocument); - $buttonHtml = $button->ownerDocument->saveHTML($button); - verify($buttonHtml)->stringContainsString('color:#ffffff'); - verify($buttonHtml)->stringContainsString('padding:.7em 1.4em'); - verify($buttonHtml)->stringContainsString('background:#32373c'); - } } diff --git a/mailpoet/tests/integration/EmailEditor/Integration/Core/Renderer/RendererTest.php b/mailpoet/tests/integration/EmailEditor/Integration/Core/Renderer/RendererTest.php new file mode 100644 index 0000000000..f7424a0726 --- /dev/null +++ b/mailpoet/tests/integration/EmailEditor/Integration/Core/Renderer/RendererTest.php @@ -0,0 +1,40 @@ +renderer = $this->diContainer->get(Renderer::class); + $this->diContainer->get(EmailEditor::class)->initialize(); + $this->diContainer->get(Initializer::class)->initialize(); + } + + public function testItInlinesButtonDefaultStyles() { + $emailPost = new \WP_Post((object)[ + 'post_content' => '
Button
', + ]); + $rendered = $this->renderer->render($emailPost, 'Subject', '', 'en'); + $doc = new \DOMDocument(); + $doc->loadHTML($rendered['html']); + $xpath = new \DOMXPath($doc); + $nodes = $xpath->query('//td[contains(@class, "wp-block-button")]'); + $button = null; + if (($nodes instanceof \DOMNodeList) && $nodes->length > 0) { + $button = $nodes->item(0); + } + $this->assertInstanceOf(\DOMElement::class, $button); + $this->assertInstanceOf(\DOMDocument::class, $button->ownerDocument); + $buttonHtml = $button->ownerDocument->saveHTML($button); + verify($buttonHtml)->stringContainsString('color:#ffffff'); + verify($buttonHtml)->stringContainsString('padding:.7em 1.4em'); + verify($buttonHtml)->stringContainsString('background:#32373c'); + } +}