Blocks
10 months ago
Coupons
1 month ago
Endpoints
2 months ago
Patterns
1 month ago
PersonalizationTags
3 days ago
ProductCollection
1 month ago
Templates
2 months ago
AutomationEmailContextProvider.php
1 month ago
AutomationEmailPreviewOrderProvider.php
1 month ago
BlockEmailContentDetector.php
1 month ago
Cli.php
2 weeks ago
DependencyNotice.php
10 months ago
EditorPageRenderer.php
1 week ago
EmailApiController.php
1 week ago
EmailEditor.php
2 months ago
EmailEditorPreviewEmail.php
9 months ago
Logger.php
10 months ago
MailPoetCssInliner.php
8 months ago
MailpoetCssInlinerFactory.php
1 year ago
PersonalizationTagManager.php
4 days ago
index.php
2 years ago
EmailEditor.php
188 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\API; |
| 9 | use MailPoet\EmailEditor\Integrations\MailPoet\Endpoints\GenerateSubjectSuggestionsEndpoint; |
| 10 | use MailPoet\EmailEditor\Integrations\MailPoet\Patterns\PatternsController; |
| 11 | use MailPoet\EmailEditor\Integrations\MailPoet\Templates\TemplatesController; |
| 12 | use MailPoet\Newsletter\NewslettersRepository; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 15 | |
| 16 | class EmailEditor { |
| 17 | const MAILPOET_EMAIL_POST_TYPE = 'mailpoet_email'; |
| 18 | |
| 19 | private WPFunctions $wp; |
| 20 | |
| 21 | private EmailApiController $emailApiController; |
| 22 | |
| 23 | private EditorPageRenderer $editorPageRenderer; |
| 24 | |
| 25 | private PatternsController $patternsController; |
| 26 | |
| 27 | private Cli $cli; |
| 28 | |
| 29 | private EmailEditorPreviewEmail $emailEditorPreviewEmail; |
| 30 | |
| 31 | private PersonalizationTagManager $personalizationTagManager; |
| 32 | |
| 33 | private TemplatesController $templatesController; |
| 34 | |
| 35 | private DotcomHelperFunctions $dotcomHelperFunctions; |
| 36 | |
| 37 | private NewslettersRepository $newslettersRepository; |
| 38 | |
| 39 | public function __construct( |
| 40 | WPFunctions $wp, |
| 41 | EmailApiController $emailApiController, |
| 42 | EditorPageRenderer $editorPageRenderer, |
| 43 | EmailEditorPreviewEmail $emailEditorPreviewEmail, |
| 44 | PatternsController $patternsController, |
| 45 | TemplatesController $templatesController, |
| 46 | Cli $cli, |
| 47 | DotcomHelperFunctions $dotcomHelperFunctions, |
| 48 | PersonalizationTagManager $personalizationTagManager, |
| 49 | NewslettersRepository $newslettersRepository |
| 50 | ) { |
| 51 | $this->wp = $wp; |
| 52 | $this->emailApiController = $emailApiController; |
| 53 | $this->editorPageRenderer = $editorPageRenderer; |
| 54 | $this->patternsController = $patternsController; |
| 55 | $this->templatesController = $templatesController; |
| 56 | $this->cli = $cli; |
| 57 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 58 | $this->emailEditorPreviewEmail = $emailEditorPreviewEmail; |
| 59 | $this->personalizationTagManager = $personalizationTagManager; |
| 60 | $this->newslettersRepository = $newslettersRepository; |
| 61 | } |
| 62 | |
| 63 | public function initialize(): void { |
| 64 | $this->cli->initialize(); |
| 65 | $this->wp->addFilter('woocommerce_email_editor_post_types', [$this, 'addEmailPostType']); |
| 66 | $this->wp->addAction('rest_delete_mailpoet_email', [$this->emailApiController, 'trashEmail'], 10, 1); |
| 67 | $this->wp->addFilter('woocommerce_is_email_editor_page', [$this, 'isEditorPage'], 10, 1); |
| 68 | $this->wp->addFilter('replace_editor', [$this, 'replaceEditor'], 10, 2); |
| 69 | $this->wp->addFilter('woocommerce_email_editor_send_preview_email', [$this->emailEditorPreviewEmail, 'sendPreviewEmail'], 10, 1); |
| 70 | $this->wp->addFilter('woocommerce_email_editor_send_preview_email_personalizer_context', [$this, 'extendPreviewPersonalizerContext'], 10, 1); |
| 71 | $this->wp->addFilter('rest_pre_insert_mailpoet_email', [$this, 'preserveAutomationEmailStatus'], 10, 2); |
| 72 | $this->patternsController->registerPatterns(); |
| 73 | // Skip classic templates in Garden environment. |
| 74 | if (!$this->dotcomHelperFunctions->isGarden()) { |
| 75 | $this->templatesController->initialize(); |
| 76 | } |
| 77 | $this->extendEmailPostApi(); |
| 78 | $this->registerApiRoutes(); |
| 79 | $this->personalizationTagManager->initialize(); |
| 80 | } |
| 81 | |
| 82 | public function addEmailPostType(array $postTypes): array { |
| 83 | $postTypes[] = [ |
| 84 | 'name' => self::MAILPOET_EMAIL_POST_TYPE, |
| 85 | 'args' => [ |
| 86 | 'labels' => [ |
| 87 | 'name' => __('Emails', 'mailpoet'), |
| 88 | 'singular_name' => __('Email', 'mailpoet'), |
| 89 | ], |
| 90 | 'rewrite' => ['slug' => self::MAILPOET_EMAIL_POST_TYPE], |
| 91 | ], |
| 92 | ]; |
| 93 | return $postTypes; |
| 94 | } |
| 95 | |
| 96 | public function isEditorPage(bool $isEditorPage): bool { |
| 97 | if ($isEditorPage) { |
| 98 | return $isEditorPage; |
| 99 | } |
| 100 | // We need to check early if we are on the email editor page. The check runs early so we can't use current_screen() here. |
| 101 | if ($this->wp->isAdmin() && isset($_GET['post']) && isset($_GET['action']) && $_GET['action'] === 'edit' && is_numeric($_GET['post'])) { |
| 102 | $post = $this->wp->getPost((int)$_GET['post']); |
| 103 | return $post && $post->post_type === self::MAILPOET_EMAIL_POST_TYPE; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | private function registerApiRoutes(): void { |
| 109 | $this->wp->addAction(API::REST_API_INIT_ACTION, function (API $api) { |
| 110 | $api->registerPostRoute( |
| 111 | 'email/generate-subject-suggestions', |
| 112 | GenerateSubjectSuggestionsEndpoint::class |
| 113 | ); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | public function extendEmailPostApi() { |
| 118 | $this->wp->registerRestField(self::MAILPOET_EMAIL_POST_TYPE, 'mailpoet_data', [ |
| 119 | 'get_callback' => [$this->emailApiController, 'getEmailData'], |
| 120 | 'update_callback' => [$this->emailApiController, 'saveEmailData'], |
| 121 | 'schema' => $this->emailApiController->getEmailDataSchema(), |
| 122 | ]); |
| 123 | } |
| 124 | |
| 125 | public function replaceEditor($replace, $post) { |
| 126 | $currentScreen = get_current_screen(); |
| 127 | if ($post->post_type === self::MAILPOET_EMAIL_POST_TYPE && $currentScreen) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 128 | $this->editorPageRenderer->render(); |
| 129 | return true; |
| 130 | } |
| 131 | return $replace; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Extend preview personalizer context with automation sample data. |
| 136 | * This is called by WooCommerce Email Editor when rendering preview in browser. |
| 137 | * |
| 138 | * @param array<string, mixed> $context |
| 139 | * @return array<string, mixed> |
| 140 | */ |
| 141 | public function extendPreviewPersonalizerContext(array $context): array { |
| 142 | // Get the current post being previewed |
| 143 | $post = $this->wp->getPost(); |
| 144 | if (!$post || $post->post_type !== self::MAILPOET_EMAIL_POST_TYPE) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 145 | return $context; |
| 146 | } |
| 147 | |
| 148 | $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $post->ID]); |
| 149 | if (!$newsletter || (!$newsletter->isAutomation() && !$newsletter->isAutomationTransactional())) { |
| 150 | return $context; |
| 151 | } |
| 152 | |
| 153 | // Get automation ID and extend tags |
| 154 | $automationId = $newsletter->getOptionValue('automationId'); |
| 155 | if (!$automationId) { |
| 156 | return $context; |
| 157 | } |
| 158 | |
| 159 | // Extend personalization tags based on automation subjects |
| 160 | $this->personalizationTagManager->extendPersonalizationTagsByAutomationSubjects((int)$automationId); |
| 161 | |
| 162 | // Context is populated via woocommerce_email_editor_send_preview_email_personalizer_context filter |
| 163 | // which extensions can hook into to add their sample data |
| 164 | return $context; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Preserve the 'private' status for automation emails when updating via REST API. |
| 169 | * This prevents the block editor from changing the status to 'draft' during autosave. |
| 170 | * |
| 171 | * @param \stdClass $preparedPost The prepared post data for insertion. |
| 172 | * @param \WP_REST_Request $request The REST request object. |
| 173 | * @return \stdClass The modified post data. |
| 174 | */ |
| 175 | public function preserveAutomationEmailStatus($preparedPost, $request) { |
| 176 | if (empty($preparedPost->ID)) { |
| 177 | return $preparedPost; |
| 178 | } |
| 179 | |
| 180 | $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $preparedPost->ID]); |
| 181 | if ($newsletter && ($newsletter->isAutomation() || $newsletter->isAutomationTransactional())) { |
| 182 | $preparedPost->post_status = 'private'; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 183 | } |
| 184 | |
| 185 | return $preparedPost; |
| 186 | } |
| 187 | } |
| 188 |