diff --git a/mailpoet/lib/EmailEditor/Engine/EmailEditor.php b/mailpoet/lib/EmailEditor/Engine/EmailEditor.php index e601658626..e512112b2d 100644 --- a/mailpoet/lib/EmailEditor/Engine/EmailEditor.php +++ b/mailpoet/lib/EmailEditor/Engine/EmailEditor.php @@ -3,12 +3,15 @@ namespace MailPoet\EmailEditor\Engine; use MailPoet\Entities\NewsletterEntity; +use MailPoet\Validator\Builder; /** * @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 { + public const MAILPOET_EMAIL_META_THEME_TYPE = 'mailpoet_email_theme'; + /** @var EmailApiController */ private $emailApiController; @@ -21,6 +24,7 @@ class EmailEditor { public function initialize(): void { do_action('mailpoet_email_editor_initialized'); $this->registerEmailPostTypes(); + $this->registerEmailMetaFields(); $this->registerEmailPostSendStatus(); $this->extendEmailPostApi(); } @@ -35,13 +39,23 @@ class EmailEditor { $postType['name'], array_merge($this->getDefaultEmailPostArgs(), $postType['args']) ); - foreach ($postType['meta'] as $postMeta) { - register_post_meta( - $postType['name'], - $postMeta['key'], - $postMeta['args'] - ); - } + } + } + + private function registerEmailMetaFields(): void { + foreach ($this->getPostTypes() as $postType) { + register_post_meta( + $postType['name'], + self::MAILPOET_EMAIL_META_THEME_TYPE, + [ + 'show_in_rest' => [ + 'schema' => $this->getEmailThemeDataSchema(), + ], + 'single' => true, + 'type' => 'object', + 'default' => ['version' => 2], // The version 2 is important to merge themes correctly + ] + ); } } @@ -60,7 +74,7 @@ class EmailEditor { 'show_ui' => true, 'show_in_menu' => false, 'show_in_nav_menus' => false, - 'supports' => ['editor', 'title'], + 'supports' => ['editor', 'title', 'custom-fields'], // 'custom-fields' is required for loading meta fields via API 'has_archive' => true, 'show_in_rest' => true, // Important to enable Gutenberg editor ]; @@ -84,4 +98,21 @@ class EmailEditor { 'schema' => $this->emailApiController->getEmailDataSchema(), ]); } + + public function getEmailThemeDataSchema(): array { + return Builder::object([ + 'version' => Builder::integer(), + 'styles' => Builder::object([ + 'spacing' => Builder::object([ + 'padding' => Builder::object([ + 'top' => Builder::string(), + 'right' => Builder::string(), + 'bottom' => Builder::string(), + 'left' => Builder::string(), + ])->nullable(), + 'blockGap' => Builder::string()->nullable(), + ])->nullable(), + ])->nullable(), + ])->toArray(); + } } diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php index a4be82475a..0d6dd341af 100644 --- a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailApiController.php @@ -34,7 +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), + 'theme' => get_post_meta($postEmailData['id'], \MailPoet\EmailEditor\Engine\EmailEditor::MAILPOET_EMAIL_META_THEME_TYPE, true), ]; } @@ -50,7 +50,7 @@ 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']); + update_post_meta($emailPost->ID, \MailPoet\EmailEditor\Engine\EmailEditor::MAILPOET_EMAIL_META_THEME_TYPE, $data['theme']); $newsletter->setSubject($data['subject']); $newsletter->setPreheader($data['preheader']); diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php index 0e93dcdbd4..212e366f94 100644 --- a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EmailEditor.php @@ -10,7 +10,6 @@ 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; @@ -55,17 +54,6 @@ 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; } @@ -102,7 +90,7 @@ 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); + $emailTheme = get_post_meta($post->ID, \MailPoet\EmailEditor\Engine\EmailEditor::MAILPOET_EMAIL_META_THEME_TYPE, true); if ($emailTheme && is_array($emailTheme)) { $theme->merge(new WP_Theme_JSON($emailTheme)); }