PluginProbe ʕ •ᴥ•ʔ
MailPoet – Newsletters, Email Marketing, and Automation / trunk
MailPoet – Newsletters, Email Marketing, and Automation vtrunk
5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 4.44.0 4.44.1 4.45.0 4.46.0 4.47.0 4.48.0 4.48.1 4.48.2 4.49.0 4.49.1 4.5.0 4.5.1 4.5.2 4.50.0 4.50.1 4.51.0 4.51.1 4.51.2 4.52.0 4.53.0 4.54.0 4.55.0 4.56.0 4.57.0 4.58.0 4.58.1 4.58.2 4.6.0 4.6.1 4.6.2 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 5.0.0 5.0.1 5.0.2 5.1.0 5.1.1 5.10.0 5.10.1 5.11.0 5.12.0 5.12.1 5.12.10 5.12.11 5.12.12 5.12.13 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.12.7 5.12.8 5.12.9 5.13.0 5.13.1 5.13.2 5.14.0 5.14.1 5.14.2 5.14.3 5.15.0 5.15.1 5.16.0 5.16.1 5.16.2 5.16.3 5.16.4 5.17.0 5.17.1 5.17.2 5.17.3 5.17.4 5.17.5 5.17.6 5.18.0 5.19.0 5.2.0 5.2.1 5.2.2 5.2.3 5.20.0 5.21.0 5.21.1 5.21.2 5.21.3 5.22.0 5.22.1 5.22.2 5.22.3 5.22.4 5.23.0 5.23.1 5.23.2 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 5.5.2 5.6.0 5.6.1 5.6.2 5.6.3 5.6.4 5.7.0 5.7.1 5.8.0 5.8.1 5.9.0 3.0.0-beta.15 3.7.1 3.0.0-beta.16 3.7.2 3.0.0-beta.17 3.7.3 3.0.0-beta.18 3.7.4 3.0.0-beta.19 3.7.5 3.0.0-beta.2 3.7.6 3.0.0-beta.20 3.7.8 3.0.0-beta.21 3.70.0 3.0.0-beta.22 3.71.0 3.0.0-beta.23 3.71.1 3.0.0-beta.23.1 3.71.2 3.0.0-beta.23.2 3.71.3 3.0.0-beta.24 3.72.0 3.0.0-beta.25 3.73.0 3.0.0-beta.26 3.73.1 3.0.0-beta.27 3.73.2 3.0.0-beta.28 3.74.0 3.0.0-beta.29 3.74.1 3.0.0-beta.3 3.74.2 3.0.0-beta.30 3.74.3 3.0.0-beta.31 3.75.0 3.0.0-beta.32 3.75.1 3.0.0-beta.33 3.76.0 3.0.0-beta.33.1 3.77.0 3.0.0-beta.34.0.0 3.77.1 3.0.0-beta.36.0.0 3.78.0 3.0.0-beta.36.0.1 3.79.0 3.0.0-beta.36.2.0 3.8 3.0.0-beta.36.3.0 3.8.1 3.0.0-beta.36.3.1 3.8.2 3.0.0-beta.37.0.0 3.8.3 3.0.0-beta.4 3.8.4 3.0.0-beta.5 3.8.5 3.0.0-beta.6 3.8.6 3.0.0-beta.7 3.80.0 3.0.0-beta.7.1 3.81.0 3.0.0-beta.8 3.82.0 3.0.0-beta.9 3.83.0 3.0.0-rc.1.0.0 3.84.0 3.0.0-rc.1.0.1 3.84.1 3.0.0-rc.1.0.2 3.85.0 3.0.0-rc.1.0.3 3.85.1 3.0.0-rc.1.0.4 3.86.0 3.0.0-rc.2.0.0 3.87.0 3.0.0-rc.2.0.1 3.87.1 3.0.0-rc.2.0.2 3.87.2 3.0.0-rc.2.0.3 3.88.0 3.0.1 3.88.1 3.0.2 3.88.2 3.0.3 3.89.0 3.0.4 3.89.1 3.0.5 3.89.2 3.0.6 3.89.3 3.0.7 3.89.4 3.0.8 3.9.0 3.0.9 3.9.1 3.1.0 3.90.0 3.10 3.90.1 3.10.1 3.90.2 3.100.0 3.91.0 3.100.1 3.91.1 3.100.2 3.92.0 3.101.0 3.92.1 3.101.1 3.93.0 3.102.0 3.93.1 3.102.1 3.94.0 3.103.0 3.95.0 3.103.1 3.95.1 3.11.0 3.96.0 3.11.1 3.96.1 3.11.2 3.97.0 3.11.3 3.98.0 3.11.4 3.98.1 3.11.5 3.99.0 3.12.0 3.99.1 3.12.1 4.0.0 3.13.0 4.0.1 3.14.0 4.1.0 3.14.1 4.1.1 3.15.0 4.10.0 3.16.0 4.11.0 3.16.1 4.11.1 3.16.2 4.12.0 3.16.3 4.12.1 3.17.0 4.12.2 3.17.1 4.13.0 3.17.2 4.14.0 3.18.0 4.15.0 3.18.1 4.16.0 3.18.2 4.17.0 3.19.0 4.17.1 3.19.1 4.18.0 3.19.2 4.18.1 3.19.3 4.19.0 3.2.0 4.2.0 3.2.1 4.20.0 3.2.2 4.20.1 3.2.3 4.20.2 3.2.4 4.21.0 3.2.5 4.22.0 3.20.0 4.22.1 3.21.0 4.22.2 3.21.1 4.23.0 3.22.0 4.24.0 3.23.0 4.25.0 3.23.1 4.26.0 3.23.2 4.26.1 3.24.0 4.27.0 3.25.0 4.28.0 3.25.1 4.29.0 3.26.0 4.3.0 3.26.1 4.3.1 3.27.0 4.30.0 3.28.0 4.31.0 3.29.0 4.31.1 3.3.0 4.32.0 3.3.1 4.33.0 3.3.2 4.34.0 3.3.3 4.35.0 3.3.4 4.35.1 3.3.5 4.36.0 3.3.6 4.37.0 3.30.0 4.38.0 3.31.0 4.39.0 3.31.1 4.4.0 3.32.0 4.40.0 3.32.1 4.41.0 3.32.2 4.41.1 3.33.0 4.41.2 3.34.0 4.41.3 3.34.1 4.42.0 3.34.2 4.42.1 3.34.3 3.34.4 3.35.0 3.35.1 3.35.3 3.35.4 3.36.0 3.37.0 3.37.1 3.37.2 3.37.3 3.38.0 3.38.1 3.39.0 3.39.1 3.39.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.40.0 3.40.1 3.41.0 3.41.1 3.41.2 3.42.0 3.42.1 3.42.2 3.42.3 3.43.0 3.43.1 3.44.0 3.45.0 3.45.1 3.46.0 3.46.1 3.46.10 3.46.11 3.46.12 3.46.13 3.46.14 3.46.2 3.46.3 3.46.4 3.46.5 3.46.6 3.46.7 3.46.8 3.46.9 3.47.0 3.47.1 3.47.10 3.47.11 3.47.2 3.47.3 3.47.5 3.47.6 3.47.7 3.47.9 3.48.0 3.48.1 3.49.0 3.49.1 3.5.0 3.5.1 3.50.0 3.51.0 3.51.1 3.51.2 3.52.0 3.53.0 3.54.0 3.54.1 3.54.2 3.54.3 3.55.0 3.55.1 3.56.0 3.56.1 3.56.2 3.57.0 3.57.1 3.58.0 3.59.0 3.59.1 3.59.2 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.60.0 3.60.1 3.60.10 3.60.11 3.60.12 3.60.2 3.60.3 3.60.4 3.60.6 3.60.7 3.60.8 3.60.9 3.61.0 3.62.0 3.62.1 3.63.0 3.64.0 3.64.1 3.64.2 3.64.3 3.65.0 trunk 3.65.1 3.0.0 3.66.0 3.0.0-beta.1 3.67.0 3.0.0-beta.10 3.67.1 3.0.0-beta.11 3.68.0 3.0.0-beta.12 3.69.0 3.0.0-beta.13 3.69.1 3.0.0-beta.14 3.7.0
mailpoet / lib / EmailEditor / Integrations / MailPoet / EditorPageRenderer.php
mailpoet / lib / EmailEditor / Integrations / MailPoet Last commit date
Blocks 12 hours ago Coupons 2 months ago Endpoints 3 months ago Patterns 2 months ago PersonalizationTags 12 hours ago ProductCollection 2 months ago Templates 2 weeks ago AutomationEmailContextProvider.php 3 months ago AutomationEmailPreviewOrderProvider.php 3 months ago BlockEmailContentDetector.php 3 months ago Cli.php 1 month ago DependencyNotice.php 1 year ago EditorPageRenderer.php 12 hours ago EmailApiController.php 1 month ago EmailEditor.php 12 hours ago EmailEditorPreviewEmail.php 10 months ago Logger.php 11 months ago MailPoetCssInliner.php 9 months ago MailpoetCssInlinerFactory.php 1 year ago PersonalizationTagManager.php 12 hours ago index.php 2 years ago
EditorPageRenderer.php
365 lines
1 <?php declare(strict_types = 1);
2
3 namespace MailPoet\EmailEditor\Integrations\MailPoet;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 use Automattic\WooCommerce\EmailEditor\Email_Editor_Container;
9 use Automattic\WooCommerce\EmailEditor\Engine\Settings_Controller;
10 use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller;
11 use Automattic\WooCommerce\EmailEditor\Engine\User_Theme;
12 use MailPoet\Analytics\Analytics;
13 use MailPoet\Config\Env;
14 use MailPoet\Config\Installer;
15 use MailPoet\Config\ServicesChecker;
16 use MailPoet\EmailEditor\Integrations\MailPoet\EmailEditor as EditorInitController;
17 use MailPoet\Entities\NewsletterEntity;
18 use MailPoet\Features\FeaturesController;
19 use MailPoet\Newsletter\NewslettersRepository;
20 use MailPoet\Services\AuthorizedEmailsController;
21 use MailPoet\Services\AuthorizedSenderDomainController;
22 use MailPoet\Services\Bridge;
23 use MailPoet\Settings\SettingsController as MailPoetSettings;
24 use MailPoet\Settings\UserFlagsController;
25 use MailPoet\Subscribers\TrackingConsentController;
26 use MailPoet\Util\CdnAssetUrl;
27 use MailPoet\Util\FreeDomains;
28 use MailPoet\Util\License\Features\CapabilitiesManager;
29 use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
30 use MailPoet\WP\Functions as WPFunctions;
31
32 class EditorPageRenderer {
33 private WPFunctions $wp;
34
35 private Settings_Controller $settingsController;
36
37 private Theme_Controller $themeController;
38
39 private User_Theme $userTheme;
40
41 private DependencyNotice $dependencyNotice;
42
43 private CdnAssetUrl $cdnAssetUrl;
44
45 private ServicesChecker $servicesChecker;
46
47 private SubscribersFeature $subscribersFeature;
48
49 private MailPoetSettings $mailpoetSettings;
50
51 private NewslettersRepository $newslettersRepository;
52
53 private UserFlagsController $userFlagsController;
54
55 private Analytics $analytics;
56
57 private Bridge $bridge;
58
59 private AuthorizedEmailsController $authorizedEmailsController;
60
61 private AuthorizedSenderDomainController $senderDomainController;
62
63 private FeaturesController $featuresController;
64
65 private CapabilitiesManager $capabilitiesManager;
66
67 private TrackingConsentController $trackingConsentController;
68
69 public function __construct(
70 WPFunctions $wp,
71 CdnAssetUrl $cdnAssetUrl,
72 ServicesChecker $servicesChecker,
73 SubscribersFeature $subscribersFeature,
74 DependencyNotice $dependencyNotice,
75 MailPoetSettings $mailpoetSettings,
76 NewslettersRepository $newslettersRepository,
77 UserFlagsController $userFlagsController,
78 Analytics $analytics,
79 Bridge $bridge,
80 AuthorizedEmailsController $authorizedEmailsController,
81 AuthorizedSenderDomainController $senderDomainController,
82 FeaturesController $featuresController,
83 CapabilitiesManager $capabilitiesManager,
84 TrackingConsentController $trackingConsentController
85 ) {
86 $this->wp = $wp;
87 $this->settingsController = Email_Editor_Container::container()->get(Settings_Controller::class);
88 $this->cdnAssetUrl = $cdnAssetUrl;
89 $this->servicesChecker = $servicesChecker;
90 $this->subscribersFeature = $subscribersFeature;
91 $this->themeController = Email_Editor_Container::container()->get(Theme_Controller::class);
92 $this->userTheme = Email_Editor_Container::container()->get(User_Theme::class);
93 $this->dependencyNotice = $dependencyNotice;
94 $this->mailpoetSettings = $mailpoetSettings;
95 $this->newslettersRepository = $newslettersRepository;
96 $this->userFlagsController = $userFlagsController;
97 $this->analytics = $analytics;
98 $this->bridge = $bridge;
99 $this->authorizedEmailsController = $authorizedEmailsController;
100 $this->senderDomainController = $senderDomainController;
101 $this->featuresController = $featuresController;
102 $this->capabilitiesManager = $capabilitiesManager;
103 $this->trackingConsentController = $trackingConsentController;
104 }
105
106 public function render() {
107 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- is_numeric guard plus int cast is the sanitization
108 $rawPostId = $_GET['post'] ?? null;
109 $postId = is_numeric($rawPostId) ? intval($rawPostId) : 0;
110 $post = $this->wp->getPost($postId);
111 if (!$post instanceof \WP_Post || $post->post_type !== EditorInitController::MAILPOET_EMAIL_POST_TYPE) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
112 return;
113 }
114 $currentPostType = $post->post_type; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
115 $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $postId]);
116 if (!$newsletter instanceof NewsletterEntity) {
117 return;
118 }
119 $this->dependencyNotice->checkDependenciesAndEventuallyShowNotice();
120
121 // load analytics (mixpanel) library
122 if ($this->analytics->isEnabled()) {
123 add_filter('admin_footer', [$this, 'loadAnalyticsModule'], 24);
124 }
125
126 // load mailpoet email editor JS integrations
127 $editorIntegrationAssetsParams = require Env::$assetsPath . '/dist/js/email_editor_integration/email_editor_integration.asset.php';
128 $this->wp->wpEnqueueScript(
129 'email_editor_integration',
130 Env::$assetsUrl . '/dist/js/email_editor_integration/email_editor_integration.js',
131 $editorIntegrationAssetsParams['dependencies'],
132 $editorIntegrationAssetsParams['version'],
133 true
134 );
135 $this->wp->wpSetScriptTranslations('email_editor_integration', 'mailpoet');
136 $this->wp->wpEnqueueStyle(
137 'email_editor_integration',
138 Env::$assetsUrl . '/dist/js/email_editor_integration/email_editor_integration.css',
139 [],
140 $editorIntegrationAssetsParams['version']
141 );
142
143 $styleParams = require Env::$assetsPath . '/dist/js/email-editor/style/style.asset.php';
144 $this->wp->wpEnqueueStyle(
145 'mailpoet_email_editor',
146 Env::$assetsUrl . '/dist/js/email-editor/style/style.css',
147 [],
148 $styleParams['version']
149 );
150
151 // The email editor needs to load block categories to avoid warning and missing category names.
152 // See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L116-L120.
153 wp_add_inline_script(
154 'wp-blocks',
155 sprintf('wp.blocks.setCategories( %s );', wp_json_encode(get_block_categories($post), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES)),
156 'after'
157 );
158
159 // Preload server-registered block schemas to avoid warning about missing block titles.
160 // See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L144C1-L148C3.
161 wp_add_inline_script(
162 'wp-blocks',
163 sprintf('wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode(get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES))
164 );
165
166 $editorSettings = $this->settingsController->get_settings();
167 $editorSettings['displaySendEmailButton'] = true;
168
169 $currentUserEmail = $this->wp->wpGetCurrentUser()->user_email;
170
171 $isAutomationNewsletter = $newsletter->isAutomation() || $newsletter->isAutomationTransactional();
172 $automationId = $newsletter->getOptionValue('automationId');
173
174 $listingUrl = 'page=mailpoet-newsletters';
175 $sendUrl = 'page=mailpoet-newsletters#/send/' . $newsletter->getId();
176 $backUrl = 'page=mailpoet-newsletters';
177
178 if ($isAutomationNewsletter) {
179 $listingUrl = 'page=mailpoet-automation';
180 $sendUrl = 'page=mailpoet-automation-editor&id=' . $automationId;
181 $backUrl = $sendUrl;
182 }
183
184 $this->wp->wpLocalizeScript(
185 'email_editor_integration',
186 'WooCommerceEmailEditor',
187 [
188 'current_post_type' => $currentPostType,
189 'current_post_id' => $post->ID,
190 'current_wp_user_email' => $currentUserEmail,
191 'editor_settings' => $editorSettings,
192 'editor_theme' => $this->themeController->get_base_theme()->get_raw_data(),
193 'user_theme_post_id' => $this->userTheme->get_user_theme_post()->ID,
194 'urls' => [
195 'listings' => admin_url('admin.php?' . $listingUrl),
196 'send' => admin_url('admin.php?' . $sendUrl),
197 'back' => admin_url('admin.php?' . $backUrl),
198 ],
199 ]
200 );
201
202 $installedAtDiff = (new \DateTime($this->mailpoetSettings->get('installed_at')))->diff(new \DateTime());
203 // Survey should be displayed only if there are 2 and more emails and the user hasn't seen it yet
204 $displaySurvey = ($this->newslettersRepository->getCountOfEmailsWithWPPost() > 1) && !$this->userFlagsController->get(UserFlagsController::EMAIL_EDITOR_SURVEY);
205
206 // Renders additional script data that some components require e.g. PremiumModal. This is done here instead of using
207 // PageRenderer since that introduces other dependencies we want to avoid. Used by getUpgradeInfo.
208 // some of these values are used by the powered by mailpoet block: mailpoet/assets/js/src/mailpoet-custom-email-editor-blocks/powered-by-mailpoet/
209 $installer = new Installer(Installer::PREMIUM_PLUGIN_SLUG);
210 $inline_script_data = [
211 'mailpoet_premium_plugin_installed' => Installer::isPluginInstalled(Installer::PREMIUM_PLUGIN_SLUG),
212 'mailpoet_premium_active' => $this->servicesChecker->isPremiumPluginActive(),
213 'mailpoet_premium_plugin_download_url' => $this->subscribersFeature->hasValidPremiumKey() ? $installer->generatePluginDownloadUrl() : null,
214 'mailpoet_premium_plugin_activation_url' => $installer->generatePluginActivationUrl(Installer::PREMIUM_PLUGIN_PATH),
215 'mailpoet_has_valid_api_key' => $this->subscribersFeature->hasValidApiKey(),
216 'mailpoet_has_valid_premium_key' => $this->subscribersFeature->hasValidPremiumKey(),
217 'mailpoet_has_premium_support' => $this->subscribersFeature->hasPremiumSupport(),
218 'mailpoet_mta_method' => $this->mailpoetSettings->get('mta.method'),
219 'mailpoet_mss_active' => $this->bridge->isMailpoetSendingServiceEnabled(),
220 'mailpoet_authorized_emails' => [],
221 'mailpoet_verified_sender_domains' => [],
222 'mailpoet_partially_verified_sender_domains' => [],
223 'mailpoet_all_sender_domains' => [],
224 'mailpoet_sender_restrictions' => [],
225 'mailpoet_free_domains' => FreeDomains::FREE_DOMAINS,
226 'mailpoet_plugin_partial_key' => $this->servicesChecker->generatePartialApiKey(),
227 'mailpoet_subscribers_count' => $this->subscribersFeature->getSubscribersCount(),
228 'mailpoet_subscribers_limit' => $this->subscribersFeature->getSubscribersLimit(),
229 'mailpoet_subscribers_limit_reached' => $this->subscribersFeature->check(),
230 // settings needed for Satismeter tracking
231 'mailpoet_3rd_party_libs_enabled' => $this->mailpoetSettings->get('3rd_party_libs.enabled') === '1',
232 'mailpoet_display_nps_email_editor' => $displaySurvey,
233 'mailpoet_display_nps_poll' => true,
234 'mailpoet_current_wp_user' => $this->wp->wpGetCurrentUser()->to_array(),
235 'mailpoet_current_wp_user_firstname' => $this->wp->wpGetCurrentUser()->user_firstname,
236 'mailpoet_cdn_url' => $this->cdnAssetUrl->generateCdnUrl(""),
237 'mailpoet_site_url' => $this->wp->siteUrl(),
238 'mailpoet_review_request_illustration_url' => $this->cdnAssetUrl->generateCdnUrl('review-request/review-request-illustration.20190815-1427.svg'),
239 'mailpoet_installed_days_ago' => (int)$installedAtDiff->format('%a'),
240 'mailpoet_segments_api' => [
241 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'),
242 'nonce' => $this->wp->wpCreateNonce('wp_rest'),
243 ],
244 'mailpoet_is_automation_newsletter' => $isAutomationNewsletter,
245 // Only a site that asks EVERY subscriber for consent needs an opt-out
246 // link in its emails, so only then does content validation ask for one.
247 'mailpoet_tracking_consent_ask_all' => $this->trackingConsentController->getSubscriberChoice() === TrackingConsentController::CHOICE_ASK_ALL,
248 'mailpoet_automation_id' => $automationId,
249 'mailpoet_feature_flags' => $this->featuresController->getAllFlags(),
250 'mailpoet_capabilities' => $this->capabilitiesManager->getCapabilities(),
251 'mailpoet_ai_text_generation_available' => function_exists('wp_ai_client_prompt')
252 && wp_ai_client_prompt('test')->is_supported_for_text_generation(),
253 ];
254 if ($this->bridge->isMailpoetSendingServiceEnabled()) {
255 $inline_script_data['mailpoet_authorized_emails'] = $this->authorizedEmailsController->getAuthorizedEmailAddresses();
256 $inline_script_data['mailpoet_verified_sender_domains'] = $this->senderDomainController->getFullyVerifiedSenderDomains(true);
257 $inline_script_data['mailpoet_partially_verified_sender_domains'] = $this->senderDomainController->getPartiallyVerifiedSenderDomains(true);
258 $inline_script_data['mailpoet_all_sender_domains'] = $this->senderDomainController->getAllSenderDomains();
259 $inline_script_data['mailpoet_sender_restrictions'] = [
260 'lowerLimit' => AuthorizedSenderDomainController::LOWER_LIMIT,
261 'isAuthorizedDomainRequiredForNewCampaigns' => $this->senderDomainController->isAuthorizedDomainRequiredForNewCampaigns(),
262 'campaignTypes' => NewsletterEntity::CAMPAIGN_TYPES,
263 'skipAuthorization' => $this->senderDomainController->shouldSkipAuthorization(),
264 ];
265 }
266 $emailRegexScript = <<<'JS'
267 var mailpoet_email_regex = /(?=^[+a-zA-Z0-9_.!#$%&'*\/=?^`{|}~-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,63}$)(?=^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})))/;
268 JS;
269 $inlineScript = implode('', array_map(function ($key) use ($inline_script_data) {
270 return sprintf("var %s=%s;", $key, wp_json_encode($inline_script_data[$key], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES));
271 }, array_keys($inline_script_data)));
272 $inlineScript = $emailRegexScript . $inlineScript;
273 $scriptHandles = [
274 'email_editor_integration',
275 'mailpoet-powered-by-mailpoet-block',
276 ];
277 foreach ($scriptHandles as $scriptHandle) {
278 $this->wp->wpAddInlineScript($scriptHandle, $inlineScript, 'before');
279 }
280
281 // Load CSS from Post Editor
282 $this->wp->wpEnqueueStyle('wp-edit-post');
283 // Load CSS for the format library - used for example in popover
284 $this->wp->wpEnqueueStyle('wp-format-library');
285
286 // Enqueue media library scripts
287 $this->wp->wpEnqueueMedia();
288
289 $this->preloadRestApiData($post);
290
291 require_once ABSPATH . 'wp-admin/admin-header.php';
292 echo '<div id="mailpoet-email-editor" class="block-editor block-editor__container hide-if-no-js"></div>';
293 }
294
295 private function preloadRestApiData(\WP_Post $post): void {
296 $userThemePostId = $this->userTheme->get_user_theme_post()->ID;
297 $templateSlug = get_post_meta($post->ID, '_wp_page_template', true);
298 $routes = [
299 '/wp/v2/mailpoet_email/' . intval($post->ID) . '?context=edit',
300 '/wp/v2/types/mailpoet_email?context=edit',
301 '/wp/v2/global-styles/' . intval($userThemePostId) . '?context=edit', // Global email styles
302 '/wp/v2/block-patterns/patterns',
303 '/wp/v2/templates?context=edit',
304 '/wp/v2/block-patterns/categories',
305 '/wp/v2/settings',
306 '/wp/v2/types?context=view',
307 '/wp/v2/taxonomies?context=view',
308 ];
309
310 if (is_string($templateSlug) && $templateSlug !== '') {
311 $routes[] = '/wp/v2/templates/lookup?slug=' . $templateSlug;
312 } else {
313 // Keep the query in sync with the woocommerce_email_editor_recent_emails_query filter
314 // in assets/js/src/mailpoet-email-editor-integration/index.ts. If the two differ,
315 // the editor asks for a different path and the preloaded data is not used.
316 $routes[] = '/wp/v2/mailpoet_email?context=edit&per_page=30&status=publish,sent,draft';
317 }
318
319 // Preload personalization tags for automation emails
320 // Note: The registry is already extended in render() before preloading.
321 // We include post_id to match the WooCommerce Email Editor's request URL.
322 $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $post->ID]);
323 if ($newsletter && ($newsletter->isAutomation() || $newsletter->isAutomationTransactional())) {
324 $routes[] = '/woocommerce-email-editor/v1/personalization_tags?context=view&per_page=-1&post_id=' . intval($post->ID);
325 }
326
327 // Preload the data for the specified routes
328 $preloadData = array_reduce(
329 $routes,
330 'rest_preload_api_request',
331 []
332 );
333
334 // Add inline script to set up preloading middleware
335 wp_add_inline_script(
336 'wp-blocks',
337 sprintf(
338 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
339 wp_json_encode($preloadData, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES)
340 )
341 );
342 }
343
344 public function loadAnalyticsModule() { // phpcs:ignore -- MissingReturnStatement not required
345 $publicId = $this->analytics->getPublicId();
346 $isPublicIdNew = $this->analytics->isPublicIdNew();
347 // this is required here because of `analytics-event.js` and order of script load and use in `mailpoet-email-editor-integration/index.ts`
348 $libs3rdPartyEnabled = $this->mailpoetSettings->get('3rd_party_libs.enabled') === '1';
349
350 // we need to set this values because they are used in the analytics.html file
351 ?>
352 <script type="text/javascript"> <?php // phpcs:ignore ?>
353 window.mailpoet_analytics_enabled = true;
354 window.mailpoet_analytics_public_id = '<?php echo esc_js($publicId); ?>';
355 window.mailpoet_analytics_new_public_id = <?php echo wp_json_encode($isPublicIdNew, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES); ?>;
356 window.mailpoet_3rd_party_libs_enabled = <?php echo wp_json_encode($libs3rdPartyEnabled, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES); ?>;
357 window.mailpoet_version = '<?php echo esc_js(MAILPOET_VERSION); ?>';
358 window.mailpoet_premium_version = '<?php echo esc_js((defined('MAILPOET_PREMIUM_VERSION')) ? MAILPOET_PREMIUM_VERSION : ''); ?>';
359 </script>
360 <?php
361
362 include_once Env::$viewsPath . '/analytics.html';
363 }
364 }
365