diff --git a/mailpoet/assets/js/src/email-editor/engine/store/actions.ts b/mailpoet/assets/js/src/email-editor/engine/store/actions.ts index 79811bb74b..1b3ede5772 100644 --- a/mailpoet/assets/js/src/email-editor/engine/store/actions.ts +++ b/mailpoet/assets/js/src/email-editor/engine/store/actions.ts @@ -6,7 +6,7 @@ import { store as noticesStore } from '@wordpress/notices'; import { __ } from '@wordpress/i18n'; import { apiFetch } from '@wordpress/data-controls'; import { storeName, mainSidebarEmailTab } from './constants'; -import { SendingPreviewStatus, State, Feature } from './types'; +import { SendingPreviewStatus, State, Feature, EmailStyles } from './types'; export const toggleFeature = (feature: Feature) => @@ -124,6 +124,30 @@ export function* updateEmailMailPoetProperty(name: string, value: string) { ); } +export function* updateEmailMailPoetTheme(theme: EmailStyles) { + const postId = select(storeName).getEmailPostId(); + // There can be a better way how to get the edited post data + const editedPost = select(coreDataStore).getEditedEntityRecord( + 'postType', + 'mailpoet_email', + postId, + ); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + const mailpoetData = editedPost?.mailpoet_data || {}; + yield dispatch(coreDataStore).editEntityRecord( + 'postType', + 'mailpoet_email', + postId, + { + mailpoet_data: { + ...mailpoetData, + theme, + }, + }, + ); +} + export function* requestSendingNewsletterPreview( newsletterId: number, email: string, diff --git a/mailpoet/lib/EmailEditor/Engine/EmailEditor.php b/mailpoet/lib/EmailEditor/Engine/EmailEditor.php index 25b6ec98ca..e601658626 100644 --- a/mailpoet/lib/EmailEditor/Engine/EmailEditor.php +++ b/mailpoet/lib/EmailEditor/Engine/EmailEditor.php @@ -5,7 +5,7 @@ namespace MailPoet\EmailEditor\Engine; use MailPoet\Entities\NewsletterEntity; /** - * @phpstan-type EmailPostType array{name: string, args: array} + * @phpstan-type EmailPostType array{name: string, args: array, meta: array{key: string, args: array}[]} * See register_post_type for details about EmailPostType args. */ class EmailEditor { @@ -35,6 +35,13 @@ class EmailEditor { $postType['name'], array_merge($this->getDefaultEmailPostArgs(), $postType['args']) ); + foreach ($postType['meta'] as $postMeta) { + register_post_meta( + $postType['name'], + $postMeta['key'], + $postMeta['args'] + ); + } } } diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php index 081d0a765c..a4be82475a 100644 --- a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php @@ -34,6 +34,7 @@ class EmailApiController { 'subject' => $newsletter ? $newsletter->getSubject() : '', 'preheader' => $newsletter ? $newsletter->getPreheader() : '', 'preview_url' => $this->newsletterUrl->getViewInBrowserUrl($newsletter), + 'theme' => get_post_meta($postEmailData['id'], EmailEditor::MAILPOET_EMAIL_META_THEME_TYPE, true), ]; } @@ -49,6 +50,8 @@ class EmailApiController { throw new UnexpectedValueException('Newsletter ID does not match the post ID'); } + update_post_meta($emailPost->ID, EmailEditor::MAILPOET_EMAIL_META_THEME_TYPE, $data['theme']); + $newsletter->setSubject($data['subject']); $newsletter->setPreheader($data['preheader']); $this->newsletterRepository->flush(); @@ -60,6 +63,7 @@ class EmailApiController { 'subject' => Builder::string(), 'preheader' => Builder::string(), 'preview_url' => Builder::string(), + 'theme' => Builder::object(), ])->toArray(); } } diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php index 014f2330ac..0e93dcdbd4 100644 --- a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php @@ -5,9 +5,12 @@ namespace MailPoet\EmailEditor\Integrations\MailPoet; use MailPoet\Features\FeaturesController; use MailPoet\Util\CdnAssetUrl; use MailPoet\WP\Functions as WPFunctions; +use WP_Post; +use WP_Theme_JSON; class EmailEditor { const MAILPOET_EMAIL_POST_TYPE = 'mailpoet_email'; + const MAILPOET_EMAIL_META_THEME_TYPE = 'mailpoet_email_theme'; /** @var WPFunctions */ private $wp; @@ -38,6 +41,7 @@ class EmailEditor { return; } $this->wp->addFilter('mailpoet_email_editor_post_types', [$this, 'addEmailPostType']); + $this->wp->addFilter('mailpoet_email_editor_rendering_theme_styles', [$this, 'extendEmailThemeStyles'], 10, 2); $this->extendEmailPostApi(); } @@ -51,6 +55,17 @@ class EmailEditor { ], 'rewrite' => ['slug' => self::MAILPOET_EMAIL_POST_TYPE], ], + 'meta' => [ + [ + 'key' => self::MAILPOET_EMAIL_META_THEME_TYPE, + 'args' => [ + 'show_in_rest' => true, + 'single' => true, + 'type' => 'object', + 'default' => ['version' => 2], + ], + ], + ], ]; return $postTypes; } @@ -85,4 +100,12 @@ class EmailEditor { '; } + + public function extendEmailThemeStyles(WP_Theme_JSON $theme, WP_Post $post): WP_Theme_JSON { + $emailTheme = get_post_meta($post->ID, self::MAILPOET_EMAIL_META_THEME_TYPE, true); + if ($emailTheme && is_array($emailTheme)) { + $theme->merge(new WP_Theme_JSON($emailTheme)); + } + return $theme; + } }