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