PluginProbe ʕ •ᴥ•ʔ
MailPoet – Newsletters, Email Marketing, and Automation / 5.37.0
MailPoet – Newsletters, Email Marketing, and Automation v5.37.0
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 / Config / Initializer.php
mailpoet / lib / Config Last commit date
PopulatorData 4 months ago AccessControl.php 2 years ago Activator.php 3 months ago AssetsLoader.php 3 weeks ago Capabilities.php 4 months ago Changelog.php 4 months ago DeactivationPoll.php 3 years ago DeferredAdminNotices.php 3 months ago Env.php 7 months ago Hooks.php 5 days ago HooksWooCommerce.php 2 months ago Initializer.php 5 days ago Installer.php 1 year ago Localizer.php 1 month ago Menu.php 2 months ago PersonalDataErasers.php 2 months ago PersonalDataExporters.php 2 months ago PluginActivatedHook.php 3 years ago Populator.php 1 month ago PrivacyPolicy.php 2 months ago Renderer.php 2 years ago RendererFactory.php 3 years ago RequirementsChecker.php 3 months ago Router.php 4 months ago ServicesChecker.php 3 years ago Shortcodes.php 1 month ago SilentUpgraderSkin.php 3 years ago SubscriberChangesNotifier.php 3 months ago TranslationUpdater.php 4 months ago TwigEnvironment.php 1 year ago TwigFileSystemCache.php 3 years ago Updater.php 1 month ago index.php 3 years ago
Initializer.php
699 lines
1 <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
2
3 namespace MailPoet\Config;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 use Automattic\WooCommerce\EmailEditor\Bootstrap as EmailEditorBootstrap;
9 use Automattic\WooCommerce\EmailEditor\Email_Editor_Container;
10 use Automattic\WooCommerce\EmailEditor\Engine\Logger\Email_Editor_Logger;
11 use MailPoet\Abilities\Abilities;
12 use MailPoet\API\JSON\API;
13 use MailPoet\API\REST\API as RestApi;
14 use MailPoet\AutomaticEmails\AutomaticEmails;
15 use MailPoet\Automation\Engine\Engine;
16 use MailPoet\Automation\Engine\Hooks as AutomationHooks;
17 use MailPoet\Automation\Integrations\MailPoet\MailPoetIntegration;
18 use MailPoet\Automation\Integrations\WooCommerce\WooCommerceIntegration;
19 use MailPoet\Cron\CliCommands\Cli as CronCli;
20 use MailPoet\Cron\CronTrigger;
21 use MailPoet\Cron\DaemonActionSchedulerRunner;
22 use MailPoet\CustomFields\RestApi\Api as CustomFieldsRestApi;
23 use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController;
24 use MailPoet\EmailEditor\Integrations\MailPoet\EmailEditor as MailpoetEmailEditorIntegration;
25 use MailPoet\EmailEditor\Integrations\MailPoet\Logger;
26 use MailPoet\Form\RestApi\Api as FormsRestApi;
27 use MailPoet\InvalidStateException;
28 use MailPoet\Logging\LogsDownload;
29 use MailPoet\Logging\RestApi\Api as LogsRestApi;
30 use MailPoet\Migrator\Cli as MigratorCli;
31 use MailPoet\Newsletter\RestApi\Api as NewslettersRestApi;
32 use MailPoet\Newsletter\Sharing\PublicEmailRoute;
33 use MailPoet\PostEditorBlocks\PostEditorBlock;
34 use MailPoet\PostEditorBlocks\WooCommerceBlocksIntegration;
35 use MailPoet\Router;
36 use MailPoet\Segments\RestApi\Api as SegmentsRestApi;
37 use MailPoet\Settings\SettingsController;
38 use MailPoet\Statistics\Track\SubscriberActivityTracker;
39 use MailPoet\Subscribers\ImportExport\Import\Cli as ImportCli;
40 use MailPoet\Subscribers\RestApi\Api as SubscribersRestApi;
41 use MailPoet\Tags\RestApi\Api as TagsRestApi;
42 use MailPoet\Util\ConflictResolver;
43 use MailPoet\Util\LegacyDatabase;
44 use MailPoet\Util\Notices\PermanentNotices;
45 use MailPoet\Util\Url;
46 use MailPoet\WooCommerce\Helper as WooCommerceHelper;
47 use MailPoet\WooCommerce\TransactionalEmailHooks as WCTransactionalEmails;
48 use MailPoet\WP\Functions as WPFunctions;
49 use MailPoet\WP\Notice as WPNotice;
50
51 class Initializer {
52 /** @var AccessControl */
53 private $accessControl;
54
55 /** @var Renderer */
56 private $renderer;
57
58 /** @var RendererFactory */
59 private $rendererFactory;
60
61 /** @var API */
62 private $api;
63
64 /** @var RestApi */
65 private $restApi;
66
67 /** @var Activator */
68 private $activator;
69
70 /** @var SettingsController */
71 private $settings;
72
73 /** @var MigratorCli */
74 private $migratorCli;
75
76 /** @var ImportCli */
77 private $importCli;
78
79 /** @var CronCli */
80 private $cronCli;
81
82 /** @var Router\Router */
83 private $router;
84
85 /** @var Hooks */
86 private $hooks;
87
88 /** @var Changelog */
89 private $changelog;
90
91 /** @var PublicEmailRoute */
92 private $publicEmailRoute;
93
94 /** @var Menu */
95 private $menu;
96
97 /** @var CronTrigger */
98 private $cronTrigger;
99
100 /** @var PermanentNotices */
101 private $permanentNotices;
102
103 /** @var Shortcodes */
104 private $shortcodes;
105
106 /** @var WCTransactionalEmails */
107 private $wcTransactionalEmails;
108
109 /** @var WooCommerceHelper */
110 private $wcHelper;
111
112 /** @var \MailPoet\PostEditorBlocks\PostEditorBlock */
113 private $postEditorBlock;
114
115 /** @var \MailPoet\PostEditorBlocks\WooCommerceBlocksIntegration */
116 private $woocommerceBlocksIntegration;
117
118 /** @var Localizer */
119 private $localizer;
120
121 /** @var AutomaticEmails */
122 private $automaticEmails;
123
124 /** @var WPFunctions */
125 private $wpFunctions;
126
127 /** @var AssetsLoader */
128 private $assetsLoader;
129
130 /** @var SubscriberActivityTracker */
131 private $subscriberActivityTracker;
132
133 /** @var Engine */
134 private $automationEngine;
135
136 /** @var TagsRestApi */
137 private $tagsRestApi;
138
139 /** @var CustomFieldsRestApi */
140 private $customFieldsRestApi;
141
142 /** @var FormsRestApi */
143 private $formsRestApi;
144
145 /** @var SegmentsRestApi */
146 private $segmentsRestApi;
147
148 /** @var LogsRestApi */
149 private $logsRestApi;
150
151 /** @var SubscribersRestApi */
152 private $subscribersRestApi;
153
154 /** @var NewslettersRestApi */
155 private $newslettersRestApi;
156
157 /** @var LogsDownload */
158 private $logsDownload;
159
160 /** @var MailPoetIntegration */
161 private $automationMailPoetIntegration;
162
163 /** @var WooCommerceIntegration */
164 private $woocommerceIntegration;
165
166 /** @var PersonalDataExporters */
167 private $personalDataExporters;
168
169 /** @var DaemonActionSchedulerRunner */
170 private $actionSchedulerRunner;
171
172 /** @var MailpoetEmailEditorIntegration */
173 private $mailpoetEmailEditorIntegration;
174
175 /** @var BlockTypesController */
176 private $blockTypesController;
177
178 /** @var Url */
179 private $urlHelper;
180
181 private EmailEditorBootstrap $emailEditorBootstrap;
182
183 private Email_Editor_Logger $emailEditorLogger;
184
185 const INITIALIZED = 'MAILPOET_INITIALIZED';
186
187 const PLUGIN_ACTIVATED = 'mailpoet_plugin_activated';
188
189 public function __construct(
190 RendererFactory $rendererFactory,
191 AccessControl $accessControl,
192 API $api,
193 RestApi $restApi,
194 Activator $activator,
195 SettingsController $settings,
196 MigratorCli $migratorCli,
197 ImportCli $importCli,
198 CronCli $cronCli,
199 Router\Router $router,
200 Hooks $hooks,
201 Changelog $changelog,
202 Menu $menu,
203 CronTrigger $cronTrigger,
204 PermanentNotices $permanentNotices,
205 Shortcodes $shortcodes,
206 WCTransactionalEmails $wcTransactionalEmails,
207 PostEditorBlock $postEditorBlock,
208 WooCommerceBlocksIntegration $woocommerceBlocksIntegration,
209 WooCommerceHelper $wcHelper,
210 Localizer $localizer,
211 AutomaticEmails $automaticEmails,
212 SubscriberActivityTracker $subscriberActivityTracker,
213 WPFunctions $wpFunctions,
214 AssetsLoader $assetsLoader,
215 Engine $automationEngine,
216 MailPoetIntegration $automationMailPoetIntegration,
217 WooCommerceIntegration $woocommerceIntegration,
218 PersonalDataExporters $personalDataExporters,
219 DaemonActionSchedulerRunner $actionSchedulerRunner,
220 BlockTypesController $blockTypesController,
221 MailpoetEmailEditorIntegration $mailpoetEmailEditorIntegration,
222 Url $urlHelper,
223 TagsRestApi $tagsRestApi,
224 CustomFieldsRestApi $customFieldsRestApi,
225 FormsRestApi $formsRestApi,
226 SegmentsRestApi $segmentsRestApi,
227 LogsRestApi $logsRestApi,
228 SubscribersRestApi $subscribersRestApi,
229 NewslettersRestApi $newslettersRestApi,
230 LogsDownload $logsDownload,
231 PublicEmailRoute $publicEmailRoute
232 ) {
233 $this->rendererFactory = $rendererFactory;
234 $this->accessControl = $accessControl;
235 $this->api = $api;
236 $this->restApi = $restApi;
237 $this->activator = $activator;
238 $this->settings = $settings;
239 $this->migratorCli = $migratorCli;
240 $this->importCli = $importCli;
241 $this->cronCli = $cronCli;
242 $this->router = $router;
243 $this->hooks = $hooks;
244 $this->changelog = $changelog;
245 $this->menu = $menu;
246 $this->cronTrigger = $cronTrigger;
247 $this->permanentNotices = $permanentNotices;
248 $this->shortcodes = $shortcodes;
249 $this->wcTransactionalEmails = $wcTransactionalEmails;
250 $this->wcHelper = $wcHelper;
251 $this->postEditorBlock = $postEditorBlock;
252 $this->woocommerceBlocksIntegration = $woocommerceBlocksIntegration;
253 $this->localizer = $localizer;
254 $this->automaticEmails = $automaticEmails;
255 $this->subscriberActivityTracker = $subscriberActivityTracker;
256 $this->wpFunctions = $wpFunctions;
257 $this->assetsLoader = $assetsLoader;
258 $this->automationEngine = $automationEngine;
259 $this->automationMailPoetIntegration = $automationMailPoetIntegration;
260 $this->woocommerceIntegration = $woocommerceIntegration;
261 $this->personalDataExporters = $personalDataExporters;
262 $this->actionSchedulerRunner = $actionSchedulerRunner;
263 $this->mailpoetEmailEditorIntegration = $mailpoetEmailEditorIntegration;
264 $this->blockTypesController = $blockTypesController;
265 $this->urlHelper = $urlHelper;
266 $this->tagsRestApi = $tagsRestApi;
267 $this->customFieldsRestApi = $customFieldsRestApi;
268 $this->formsRestApi = $formsRestApi;
269 $this->segmentsRestApi = $segmentsRestApi;
270 $this->logsRestApi = $logsRestApi;
271 $this->subscribersRestApi = $subscribersRestApi;
272 $this->newslettersRestApi = $newslettersRestApi;
273 $this->logsDownload = $logsDownload;
274 $this->publicEmailRoute = $publicEmailRoute;
275
276 $emailEditorContainer = Email_Editor_Container::container();
277 $this->emailEditorBootstrap = $emailEditorContainer->get(EmailEditorBootstrap::class);
278 $this->emailEditorLogger = $emailEditorContainer->get(Email_Editor_Logger::class);
279 }
280
281 public function init() {
282 // Initialize Action Scheduler. It needs to be called early because it hooks into `plugins_loaded`.
283 require_once __DIR__ . '/../../vendor/woocommerce/action-scheduler/action-scheduler.php';
284
285 // define legacy constants for DB tables - for back compatibility
286 LegacyDatabase::defineTableConstants();
287
288 // load translations and setup translations update/download
289 $this->setupLocalizer();
290
291 // activation function
292 $this->wpFunctions->registerActivationHook(
293 Env::$file,
294 [
295 $this,
296 'runActivator',
297 ]
298 );
299
300 // deactivation function
301 $this->wpFunctions->registerDeactivationHook(
302 Env::$file,
303 [
304 $this,
305 'runDeactivation',
306 ]
307 );
308
309 $this->emailEditorBootstrap->init();
310 $this->setupAbilities();
311
312 $this->wpFunctions->addAction('activated_plugin', [
313 new PluginActivatedHook(new DeferredAdminNotices),
314 'action',
315 ], 10, 2);
316
317 $this->wpFunctions->addAction('plugins_loaded', [
318 $this,
319 'pluginsLoaded',
320 ], 0);
321
322 $this->wpFunctions->addAction('init', [
323 $this,
324 'preInitialize',
325 ], 0);
326
327 $this->wpFunctions->addAction('init', [
328 $this,
329 'initialize',
330 ]);
331
332 $this->wpFunctions->addAction(
333 'init',
334 [$this, 'maybeRunActivator'],
335 PHP_INT_MIN
336 );
337
338 $this->wpFunctions->addAction('admin_init', [
339 $this,
340 'setupPrivacyPolicy',
341 ]);
342
343 $this->wpFunctions->addAction('wp_loaded', [
344 $this,
345 'postInitialize',
346 ]);
347
348 $this->wpFunctions->addAction('admin_init', [
349 new DeferredAdminNotices,
350 'printAndClean',
351 ]);
352
353 $this->wpFunctions->addFilter('wpmu_drop_tables', [
354 $this,
355 'multisiteDropTables',
356 ]);
357
358 $this->wpFunctions->addFilter('woocommerce_email_editor_initialized', [
359 $this,
360 'setupEmailEditorIntegrations',
361 ]);
362
363 $this->wpFunctions->addAction('woocommerce_init', [
364 $this,
365 'setupMarketingConfirmationEmail',
366 ]);
367
368 WPFunctions::get()->addAction(AutomationHooks::INITIALIZE, [
369 $this->automationMailPoetIntegration,
370 'register',
371 ]);
372 WPFunctions::get()->addAction(AutomationHooks::INITIALIZE, [
373 $this->woocommerceIntegration,
374 'register',
375 ]);
376
377 WPFunctions::get()->addAction('admin_init', [
378 $this,
379 'afterPluginActivation',
380 ]);
381
382 $this->hooks->initEarlyHooks();
383 }
384
385 private function setupAbilities(): void {
386 require_once __DIR__ . '/../Abilities/Abilities.php';
387 Abilities::init();
388 }
389
390 public function runActivator() {
391 try {
392 $this->wpFunctions->addOption(self::PLUGIN_ACTIVATED, true); // used in afterPluginActivation
393 $this->activator->activate();
394 } catch (InvalidStateException $e) {
395 return $this->handleRunningMigration($e);
396 } catch (\Exception $e) {
397 return $this->handleFailedInitialization($e);
398 }
399 }
400
401 public function pluginsLoaded() {
402 $this->hooks->init();
403 $this->publicEmailRoute->init();
404 }
405
406 public function preInitialize() {
407 try {
408 $this->renderer = $this->rendererFactory->getRenderer();
409 $this->setupWidget();
410 $this->setupWoocommerceTransactionalEmails();
411 $this->assetsLoader->loadStyles();
412 $this->emailEditorLogger->set_logger(new Logger());
413 } catch (\Exception $e) {
414 $this->handleFailedInitialization($e);
415 }
416 }
417
418 public function setupWidget() {
419 $this->wpFunctions->registerWidget('\MailPoet\Form\Widget');
420 }
421
422 public function initialize() {
423 try {
424 $this->migratorCli->initialize();
425 $this->importCli->initialize();
426 $this->cronCli->initialize();
427 $this->setupInstaller();
428 $this->setupUpdater();
429
430 $this->setupCapabilities();
431 $this->menu->init();
432 $this->setupShortcodes();
433 $this->setupImages();
434 $this->setupPersonalDataExporters();
435 $this->setupPersonalDataErasers();
436
437 $this->changelog->init();
438 $this->setupCronTrigger();
439 $this->setupConflictResolver();
440
441 $this->setupPages();
442
443 $this->setupPermanentNotices();
444 $this->setupAutomaticEmails();
445 $this->setupWoocommerceBlocksIntegration();
446 $this->setupDeactivationPoll();
447 try {
448 $this->subscriberActivityTracker->trackActivity();
449 } catch (\Throwable $e) {
450 // Page-view tracking failing must never stop the rest of initialize(), which
451 // includes registering the mailpoet/v1 REST namespace. One throw here took the
452 // whole namespace down for a customer, twice over: it aborted the remaining
453 // calls in this block AND left INITIALIZED undefined, so postInitialize()
454 // skipped restApi->init() as well.
455 //
456 // Log to the PHP error log, not the MailPoet logger: the logger writes to the
457 // database, and a database that is missing, unmigrated or unreachable is exactly
458 // the kind of failure this catch exists to survive.
459 if (function_exists('error_log')) {
460 // phpcs:disable QITStandard.PHP.DebugCode.DebugFunctionFound
461 error_log('[MailPoet] Subscriber activity tracking failed: ' . (string)$e); // phpcs:ignore Squiz.PHP.DiscouragedFunctions
462 // phpcs:enable QITStandard.PHP.DebugCode.DebugFunctionFound
463 }
464 }
465 $this->postEditorBlock->init();
466 $this->automationEngine->initialize();
467 $this->tagsRestApi->initialize();
468 $this->customFieldsRestApi->initialize();
469 $this->formsRestApi->initialize();
470 $this->segmentsRestApi->initialize();
471 $this->logsRestApi->initialize();
472 $this->subscribersRestApi->initialize();
473 $this->newslettersRestApi->initialize();
474 $this->logsDownload->initialize();
475 $this->blockTypesController->initialize();
476 $this->wpFunctions->doAction('mailpoet_initialized', MAILPOET_VERSION);
477 } catch (InvalidStateException $e) {
478 return $this->handleRunningMigration($e);
479 } catch (\Exception $e) {
480 return $this->handleFailedInitialization($e);
481 }
482
483 define(self::INITIALIZED, true);
484 }
485
486 /**
487 * Walk around for getting this to work correctly
488 *
489 * Read more here: https://developer.wordpress.org/reference/functions/register_activation_hook/
490 * and https://github.com/mailpoet/mailpoet/pull/4620#discussion_r1058210174
491 * @return void
492 */
493 public function afterPluginActivation() {
494 if (!$this->wpFunctions->isAdmin() || !defined(self::INITIALIZED) || !$this->wpFunctions->getOption(self::PLUGIN_ACTIVATED)) return;
495
496 $currentUrl = $this->urlHelper->getCurrentUrl();
497
498 // wp automatically redirect to `wp-admin/plugins.php?activate=true&...` after plugin activation
499 $activatedByWpAdmin = !empty(strpos($currentUrl, 'plugins.php')) && isset($_GET['activate']) && (bool)$_GET['activate'];
500
501 // We want to run this only once immediately after activation.
502 // Delete the flag to prevent triggering on subsequent page loads.
503 $this->wpFunctions->deleteOption(self::PLUGIN_ACTIVATED);
504
505 // If not activated by wp. Do not redirect e.g WooCommerce NUX
506 if ($activatedByWpAdmin) {
507 $this->changelog->redirectToLandingPage();
508 }
509 }
510
511 /**
512 * Checks if the plugin was updated and runs the activator if needed. The activator
513 * will run the database migrations and update the db version among a few other things.
514 *
515 * @return void
516 * @throws InvalidStateException
517 */
518 public function maybeRunActivator() {
519 try {
520 $currentDbVersion = $this->settings->get('db_version');
521 } catch (\Exception $e) {
522 $currentDbVersion = null;
523 }
524
525 if (version_compare((string)$currentDbVersion, Env::$version) === 0) {
526 return;
527 }
528
529 // if current db version and plugin version differ
530 try {
531 $this->activator->activate();
532 } catch (InvalidStateException $e) {
533 $this->handleRunningMigration($e);
534 } catch (\Exception $e) {
535 $this->handleFailedInitialization($e);
536 }
537 }
538
539 public function setupInstaller() {
540 $installer = new Installer(
541 Installer::PREMIUM_PLUGIN_SLUG
542 );
543 $installer->init();
544 }
545
546 public function setupUpdater() {
547 $premiumSlug = Installer::PREMIUM_PLUGIN_SLUG;
548 $premiumPluginFile = Installer::getPluginFile($premiumSlug);
549 $premiumVersion = defined('MAILPOET_PREMIUM_VERSION') ? MAILPOET_PREMIUM_VERSION : null;
550
551 if (empty($premiumPluginFile) || !$premiumVersion) {
552 return false;
553 }
554 $updater = new Updater(
555 $premiumPluginFile,
556 $premiumSlug,
557 MAILPOET_PREMIUM_VERSION
558 );
559 $updater->init();
560 }
561
562 public function setupLocalizer() {
563 $this->localizer->init($this->wpFunctions);
564 }
565
566 public function setupCapabilities() {
567 $caps = new Capabilities($this->renderer);
568 $caps->init();
569 }
570
571 public function setupShortcodes() {
572 $this->shortcodes->init();
573 }
574
575 public function setupImages() {
576 $this->wpFunctions->addImageSize('mailpoet_newsletter_max', Env::NEWSLETTER_CONTENT_WIDTH);
577 }
578
579 public function setupCronTrigger() {
580 $this->cronTrigger->init((string)php_sapi_name());
581 }
582
583 public function setupConflictResolver() {
584 $conflictResolver = new ConflictResolver();
585 $conflictResolver->init();
586 }
587
588 public function postInitialize() {
589 if (!defined(self::INITIALIZED)) return;
590 try {
591 $this->api->init();
592 $this->restApi->init();
593 $this->router->init();
594 $this->setupUserLocale();
595 } catch (\Exception $e) {
596 $this->handleFailedInitialization($e);
597 }
598 }
599
600 public function setupUserLocale() {
601 if (get_user_locale() === $this->wpFunctions->getLocale()) return;
602 $this->wpFunctions->unloadTextdomain(Env::$pluginName);
603 $this->localizer->init($this->wpFunctions);
604 }
605
606 public function setupPages() {
607 $pages = new \MailPoet\Settings\Pages();
608 $pages->init();
609 }
610
611 public function setupPrivacyPolicy() {
612 $privacyPolicy = new PrivacyPolicy();
613 $privacyPolicy->init();
614 }
615
616 public function setupPersonalDataExporters() {
617 $this->personalDataExporters->init();
618 }
619
620 public function setupPersonalDataErasers() {
621 $erasers = new PersonalDataErasers();
622 $erasers->init();
623 }
624
625 public function setupPermanentNotices() {
626 $this->permanentNotices->init();
627 }
628
629 public function handleFailedInitialization($exception) {
630 // check if we are able to add pages at this point
631 if (function_exists('wp_get_current_user')) {
632 Menu::addErrorPage($this->accessControl);
633 }
634 return WPNotice::displayError($exception);
635 }
636
637 private function handleRunningMigration(InvalidStateException $exception) {
638 if (function_exists('wp_get_current_user')) {
639 Menu::addErrorPage($this->accessControl);
640 }
641 return WPNotice::displayWarning($exception->getMessage());
642 }
643
644 public function setupAutomaticEmails() {
645 $this->automaticEmails->init();
646 $this->automaticEmails->getAutomaticEmails();
647 }
648
649 public function multisiteDropTables($tables) {
650 global $wpdb;
651 $tablePrefix = $wpdb->prefix . Env::$pluginPrefix;
652 $mailpoetTables = $wpdb->get_col(
653 $wpdb->prepare(
654 "SHOW TABLES LIKE %s",
655 $wpdb->esc_like($tablePrefix) . '%'
656 )
657 );
658 return array_merge($tables, $mailpoetTables);
659 }
660
661 public function setupEmailEditorIntegrations() {
662 $this->mailpoetEmailEditorIntegration->initialize();
663 }
664
665 public function runDeactivation() {
666 $this->actionSchedulerRunner->deactivate();
667 }
668
669 private function setupWoocommerceTransactionalEmails() {
670 $wcEnabled = $this->wcHelper->isWooCommerceActive();
671 $optInEnabled = $this->settings->get('woocommerce.use_mailpoet_editor', false);
672 if ($wcEnabled && $optInEnabled) {
673 $this->wcTransactionalEmails->overrideStylesForWooEmails();
674 $this->wcTransactionalEmails->useTemplateForWoocommerceEmails();
675 }
676 }
677
678 public function setupMarketingConfirmationEmail() {
679 $wcEnabled = $this->wcHelper->isWooCommerceActive();
680 if ($wcEnabled) {
681 $emails = new \MailPoet\WooCommerce\Emails();
682 $emails->init();
683 }
684 }
685
686 private function setupWoocommerceBlocksIntegration() {
687 $wcEnabled = $this->wcHelper->isWooCommerceActive();
688 $wcBlocksEnabled = $this->wcHelper->isWooCommerceBlocksActive('8.0.0');
689 if ($wcEnabled && $wcBlocksEnabled) {
690 $this->woocommerceBlocksIntegration->init();
691 }
692 }
693
694 private function setupDeactivationPoll(): void {
695 $deactivationPoll = new DeactivationPoll($this->wpFunctions, $this->renderer);
696 $deactivationPoll->init();
697 }
698 }
699