PluginProbe ʕ •ᴥ•ʔ
MailPoet – Newsletters, Email Marketing, and Automation / 5.36.1
MailPoet – Newsletters, Email Marketing, and Automation v5.36.1
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 1 week ago Coupons 2 months ago Endpoints 3 months ago Patterns 2 months ago PersonalizationTags 1 month 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 1 week ago EmailApiController.php 1 month ago EmailEditor.php 3 months ago EmailEditorPreviewEmail.php 10 months ago Logger.php 11 months ago MailPoetCssInliner.php 9 months ago MailpoetCssInlinerFactory.php 1 year ago PersonalizationTagManager.php 1 month ago index.php 2 years ago
EditorPageRenderer.php
357 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\Util\CdnAssetUrl;
26 use MailPoet\Util\FreeDomains;
27 use MailPoet\Util\License\Features\CapabilitiesManager;
28 use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
29 use MailPoet\WP\Functions as WPFunctions;
30
31 class EditorPageRenderer {
32 private WPFunctions $wp;
33
34 private Settings_Controller $settingsController;
35
36 private Theme_Controller $themeController;
37
38 private User_Theme $userTheme;
39
40 private DependencyNotice $dependencyNotice;
41
42 private CdnAssetUrl $cdnAssetUrl;
43
44 private ServicesChecker $servicesChecker;
45
46 private SubscribersFeature $subscribersFeature;
47
48 private MailPoetSettings $mailpoetSettings;
49
50 private NewslettersRepository $newslettersRepository;
51
52 private UserFlagsController $userFlagsController;
53
54 private Analytics $analytics;
55
56 private Bridge $bridge;
57
58 private AuthorizedEmailsController $authorizedEmailsController;
59
60 private AuthorizedSenderDomainController $senderDomainController;
61
62 private FeaturesController $featuresController;
63
64 private CapabilitiesManager $capabilitiesManager;
65
66 public function __construct(
67 WPFunctions $wp,
68 CdnAssetUrl $cdnAssetUrl,
69 ServicesChecker $servicesChecker,
70 SubscribersFeature $subscribersFeature,
71 DependencyNotice $dependencyNotice,
72 MailPoetSettings $mailpoetSettings,
73 NewslettersRepository $newslettersRepository,
74 UserFlagsController $userFlagsController,
75 Analytics $analytics,
76 Bridge $bridge,
77 AuthorizedEmailsController $authorizedEmailsController,
78 AuthorizedSenderDomainController $senderDomainController,
79 FeaturesController $featuresController,
80 CapabilitiesManager $capabilitiesManager
81 ) {
82 $this->wp = $wp;
83 $this->settingsController = Email_Editor_Container::container()->get(Settings_Controller::class);
84 $this->cdnAssetUrl = $cdnAssetUrl;
85 $this->servicesChecker = $servicesChecker;
86 $this->subscribersFeature = $subscribersFeature;
87 $this->themeController = Email_Editor_Container::container()->get(Theme_Controller::class);
88 $this->userTheme = Email_Editor_Container::container()->get(User_Theme::class);
89 $this->dependencyNotice = $dependencyNotice;
90 $this->mailpoetSettings = $mailpoetSettings;
91 $this->newslettersRepository = $newslettersRepository;
92 $this->userFlagsController = $userFlagsController;
93 $this->analytics = $analytics;
94 $this->bridge = $bridge;
95 $this->authorizedEmailsController = $authorizedEmailsController;
96 $this->senderDomainController = $senderDomainController;
97 $this->featuresController = $featuresController;
98 $this->capabilitiesManager = $capabilitiesManager;
99 }
100
101 public function render() {
102 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- is_numeric guard plus int cast is the sanitization
103 $rawPostId = $_GET['post'] ?? null;
104 $postId = is_numeric($rawPostId) ? intval($rawPostId) : 0;
105 $post = $this->wp->getPost($postId);
106 if (!$post instanceof \WP_Post || $post->post_type !== EditorInitController::MAILPOET_EMAIL_POST_TYPE) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
107 return;
108 }
109 $currentPostType = $post->post_type; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
110 $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $postId]);
111 if (!$newsletter instanceof NewsletterEntity) {
112 return;
113 }
114 $this->dependencyNotice->checkDependenciesAndEventuallyShowNotice();
115
116 // load analytics (mixpanel) library
117 if ($this->analytics->isEnabled()) {
118 add_filter('admin_footer', [$this, 'loadAnalyticsModule'], 24);
119 }
120
121 // load mailpoet email editor JS integrations
122 $editorIntegrationAssetsParams = require Env::$assetsPath . '/dist/js/email_editor_integration/email_editor_integration.asset.php';
123 $this->wp->wpEnqueueScript(
124 'email_editor_integration',
125 Env::$assetsUrl . '/dist/js/email_editor_integration/email_editor_integration.js',
126 $editorIntegrationAssetsParams['dependencies'],
127 $editorIntegrationAssetsParams['version'],
128 true
129 );
130 $this->wp->wpSetScriptTranslations('email_editor_integration', 'mailpoet');
131 $this->wp->wpEnqueueStyle(
132 'email_editor_integration',
133 Env::$assetsUrl . '/dist/js/email_editor_integration/email_editor_integration.css',
134 [],
135 $editorIntegrationAssetsParams['version']
136 );
137
138 $styleParams = require Env::$assetsPath . '/dist/js/email-editor/style/style.asset.php';
139 $this->wp->wpEnqueueStyle(
140 'mailpoet_email_editor',
141 Env::$assetsUrl . '/dist/js/email-editor/style/style.css',
142 [],
143 $styleParams['version']
144 );
145
146 // The email editor needs to load block categories to avoid warning and missing category names.
147 // See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L116-L120.
148 wp_add_inline_script(
149 'wp-blocks',
150 sprintf('wp.blocks.setCategories( %s );', wp_json_encode(get_block_categories($post), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES)),
151 'after'
152 );
153
154 // Preload server-registered block schemas to avoid warning about missing block titles.
155 // See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L144C1-L148C3.
156 wp_add_inline_script(
157 'wp-blocks',
158 sprintf('wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode(get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES))
159 );
160
161 $editorSettings = $this->settingsController->get_settings();
162 $editorSettings['displaySendEmailButton'] = true;
163
164 $currentUserEmail = $this->wp->wpGetCurrentUser()->user_email;
165
166 $isAutomationNewsletter = $newsletter->isAutomation() || $newsletter->isAutomationTransactional();
167 $automationId = $newsletter->getOptionValue('automationId');
168
169 $listingUrl = 'page=mailpoet-newsletters';
170 $sendUrl = 'page=mailpoet-newsletters#/send/' . $newsletter->getId();
171 $backUrl = 'page=mailpoet-newsletters';
172
173 if ($isAutomationNewsletter) {
174 $listingUrl = 'page=mailpoet-automation';
175 $sendUrl = 'page=mailpoet-automation-editor&id=' . $automationId;
176 $backUrl = $sendUrl;
177 }
178
179 $this->wp->wpLocalizeScript(
180 'email_editor_integration',
181 'WooCommerceEmailEditor',
182 [
183 'current_post_type' => $currentPostType,
184 'current_post_id' => $post->ID,
185 'current_wp_user_email' => $currentUserEmail,
186 'editor_settings' => $editorSettings,
187 'editor_theme' => $this->themeController->get_base_theme()->get_raw_data(),
188 'user_theme_post_id' => $this->userTheme->get_user_theme_post()->ID,
189 'urls' => [
190 'listings' => admin_url('admin.php?' . $listingUrl),
191 'send' => admin_url('admin.php?' . $sendUrl),
192 'back' => admin_url('admin.php?' . $backUrl),
193 ],
194 ]
195 );
196
197 $installedAtDiff = (new \DateTime($this->mailpoetSettings->get('installed_at')))->diff(new \DateTime());
198 // Survey should be displayed only if there are 2 and more emails and the user hasn't seen it yet
199 $displaySurvey = ($this->newslettersRepository->getCountOfEmailsWithWPPost() > 1) && !$this->userFlagsController->get(UserFlagsController::EMAIL_EDITOR_SURVEY);
200
201 // Renders additional script data that some components require e.g. PremiumModal. This is done here instead of using
202 // PageRenderer since that introduces other dependencies we want to avoid. Used by getUpgradeInfo.
203 // some of these values are used by the powered by mailpoet block: mailpoet/assets/js/src/mailpoet-custom-email-editor-blocks/powered-by-mailpoet/
204 $installer = new Installer(Installer::PREMIUM_PLUGIN_SLUG);
205 $inline_script_data = [
206 'mailpoet_premium_plugin_installed' => Installer::isPluginInstalled(Installer::PREMIUM_PLUGIN_SLUG),
207 'mailpoet_premium_active' => $this->servicesChecker->isPremiumPluginActive(),
208 'mailpoet_premium_plugin_download_url' => $this->subscribersFeature->hasValidPremiumKey() ? $installer->generatePluginDownloadUrl() : null,
209 'mailpoet_premium_plugin_activation_url' => $installer->generatePluginActivationUrl(Installer::PREMIUM_PLUGIN_PATH),
210 'mailpoet_has_valid_api_key' => $this->subscribersFeature->hasValidApiKey(),
211 'mailpoet_has_valid_premium_key' => $this->subscribersFeature->hasValidPremiumKey(),
212 'mailpoet_has_premium_support' => $this->subscribersFeature->hasPremiumSupport(),
213 'mailpoet_mta_method' => $this->mailpoetSettings->get('mta.method'),
214 'mailpoet_mss_active' => $this->bridge->isMailpoetSendingServiceEnabled(),
215 'mailpoet_authorized_emails' => [],
216 'mailpoet_verified_sender_domains' => [],
217 'mailpoet_partially_verified_sender_domains' => [],
218 'mailpoet_all_sender_domains' => [],
219 'mailpoet_sender_restrictions' => [],
220 'mailpoet_free_domains' => FreeDomains::FREE_DOMAINS,
221 'mailpoet_plugin_partial_key' => $this->servicesChecker->generatePartialApiKey(),
222 'mailpoet_subscribers_count' => $this->subscribersFeature->getSubscribersCount(),
223 'mailpoet_subscribers_limit' => $this->subscribersFeature->getSubscribersLimit(),
224 'mailpoet_subscribers_limit_reached' => $this->subscribersFeature->check(),
225 // settings needed for Satismeter tracking
226 'mailpoet_3rd_party_libs_enabled' => $this->mailpoetSettings->get('3rd_party_libs.enabled') === '1',
227 'mailpoet_display_nps_email_editor' => $displaySurvey,
228 'mailpoet_display_nps_poll' => true,
229 'mailpoet_current_wp_user' => $this->wp->wpGetCurrentUser()->to_array(),
230 'mailpoet_current_wp_user_firstname' => $this->wp->wpGetCurrentUser()->user_firstname,
231 'mailpoet_cdn_url' => $this->cdnAssetUrl->generateCdnUrl(""),
232 'mailpoet_site_url' => $this->wp->siteUrl(),
233 'mailpoet_review_request_illustration_url' => $this->cdnAssetUrl->generateCdnUrl('review-request/review-request-illustration.20190815-1427.svg'),
234 'mailpoet_installed_days_ago' => (int)$installedAtDiff->format('%a'),
235 'mailpoet_segments_api' => [
236 'root' => rtrim($this->wp->escUrlRaw($this->wp->restUrl()), '/'),
237 'nonce' => $this->wp->wpCreateNonce('wp_rest'),
238 ],
239 'mailpoet_is_automation_newsletter' => $isAutomationNewsletter,
240 'mailpoet_automation_id' => $automationId,
241 'mailpoet_feature_flags' => $this->featuresController->getAllFlags(),
242 'mailpoet_capabilities' => $this->capabilitiesManager->getCapabilities(),
243 'mailpoet_ai_text_generation_available' => function_exists('wp_ai_client_prompt')
244 && wp_ai_client_prompt('test')->is_supported_for_text_generation(),
245 ];
246 if ($this->bridge->isMailpoetSendingServiceEnabled()) {
247 $inline_script_data['mailpoet_authorized_emails'] = $this->authorizedEmailsController->getAuthorizedEmailAddresses();
248 $inline_script_data['mailpoet_verified_sender_domains'] = $this->senderDomainController->getFullyVerifiedSenderDomains(true);
249 $inline_script_data['mailpoet_partially_verified_sender_domains'] = $this->senderDomainController->getPartiallyVerifiedSenderDomains(true);
250 $inline_script_data['mailpoet_all_sender_domains'] = $this->senderDomainController->getAllSenderDomains();
251 $inline_script_data['mailpoet_sender_restrictions'] = [
252 'lowerLimit' => AuthorizedSenderDomainController::LOWER_LIMIT,
253 'isAuthorizedDomainRequiredForNewCampaigns' => $this->senderDomainController->isAuthorizedDomainRequiredForNewCampaigns(),
254 'campaignTypes' => NewsletterEntity::CAMPAIGN_TYPES,
255 'skipAuthorization' => $this->senderDomainController->shouldSkipAuthorization(),
256 ];
257 }
258 $emailRegexScript = <<<'JS'
259 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,})))/;
260 JS;
261 $inlineScript = implode('', array_map(function ($key) use ($inline_script_data) {
262 return sprintf("var %s=%s;", $key, wp_json_encode($inline_script_data[$key], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES));
263 }, array_keys($inline_script_data)));
264 $inlineScript = $emailRegexScript . $inlineScript;
265 $scriptHandles = [
266 'email_editor_integration',
267 'mailpoet-powered-by-mailpoet-block',
268 ];
269 foreach ($scriptHandles as $scriptHandle) {
270 $this->wp->wpAddInlineScript($scriptHandle, $inlineScript, 'before');
271 }
272
273 // Load CSS from Post Editor
274 $this->wp->wpEnqueueStyle('wp-edit-post');
275 // Load CSS for the format library - used for example in popover
276 $this->wp->wpEnqueueStyle('wp-format-library');
277
278 // Enqueue media library scripts
279 $this->wp->wpEnqueueMedia();
280
281 $this->preloadRestApiData($post);
282
283 require_once ABSPATH . 'wp-admin/admin-header.php';
284 echo '<div id="mailpoet-email-editor" class="block-editor block-editor__container hide-if-no-js"></div>';
285 }
286
287 private function preloadRestApiData(\WP_Post $post): void {
288 $userThemePostId = $this->userTheme->get_user_theme_post()->ID;
289 $templateSlug = get_post_meta($post->ID, '_wp_page_template', true);
290 $routes = [
291 '/wp/v2/mailpoet_email/' . intval($post->ID) . '?context=edit',
292 '/wp/v2/types/mailpoet_email?context=edit',
293 '/wp/v2/global-styles/' . intval($userThemePostId) . '?context=edit', // Global email styles
294 '/wp/v2/block-patterns/patterns',
295 '/wp/v2/templates?context=edit',
296 '/wp/v2/block-patterns/categories',
297 '/wp/v2/settings',
298 '/wp/v2/types?context=view',
299 '/wp/v2/taxonomies?context=view',
300 ];
301
302 if (is_string($templateSlug) && $templateSlug !== '') {
303 $routes[] = '/wp/v2/templates/lookup?slug=' . $templateSlug;
304 } else {
305 // Keep the query in sync with the woocommerce_email_editor_recent_emails_query filter
306 // in assets/js/src/mailpoet-email-editor-integration/index.ts. If the two differ,
307 // the editor asks for a different path and the preloaded data is not used.
308 $routes[] = '/wp/v2/mailpoet_email?context=edit&per_page=30&status=publish,sent,draft';
309 }
310
311 // Preload personalization tags for automation emails
312 // Note: The registry is already extended in render() before preloading.
313 // We include post_id to match the WooCommerce Email Editor's request URL.
314 $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $post->ID]);
315 if ($newsletter && ($newsletter->isAutomation() || $newsletter->isAutomationTransactional())) {
316 $routes[] = '/woocommerce-email-editor/v1/personalization_tags?context=view&per_page=-1&post_id=' . intval($post->ID);
317 }
318
319 // Preload the data for the specified routes
320 $preloadData = array_reduce(
321 $routes,
322 'rest_preload_api_request',
323 []
324 );
325
326 // Add inline script to set up preloading middleware
327 wp_add_inline_script(
328 'wp-blocks',
329 sprintf(
330 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
331 wp_json_encode($preloadData, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES)
332 )
333 );
334 }
335
336 public function loadAnalyticsModule() { // phpcs:ignore -- MissingReturnStatement not required
337 $publicId = $this->analytics->getPublicId();
338 $isPublicIdNew = $this->analytics->isPublicIdNew();
339 // this is required here because of `analytics-event.js` and order of script load and use in `mailpoet-email-editor-integration/index.ts`
340 $libs3rdPartyEnabled = $this->mailpoetSettings->get('3rd_party_libs.enabled') === '1';
341
342 // we need to set this values because they are used in the analytics.html file
343 ?>
344 <script type="text/javascript"> <?php // phpcs:ignore ?>
345 window.mailpoet_analytics_enabled = true;
346 window.mailpoet_analytics_public_id = '<?php echo esc_js($publicId); ?>';
347 window.mailpoet_analytics_new_public_id = <?php echo wp_json_encode($isPublicIdNew, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES); ?>;
348 window.mailpoet_3rd_party_libs_enabled = <?php echo wp_json_encode($libs3rdPartyEnabled, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES); ?>;
349 window.mailpoet_version = '<?php echo esc_js(MAILPOET_VERSION); ?>';
350 window.mailpoet_premium_version = '<?php echo esc_js((defined('MAILPOET_PREMIUM_VERSION')) ? MAILPOET_PREMIUM_VERSION : ''); ?>';
351 </script>
352 <?php
353
354 include_once Env::$viewsPath . '/analytics.html';
355 }
356 }
357