| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906 |
- <?php
- namespace MailPoet\Config;
- if (!defined('ABSPATH')) exit;
- use MailPoet\Cron\CronTrigger;
- use MailPoet\Cron\Workers\AuthorizedSendingEmailsCheck;
- use MailPoet\Cron\Workers\Beamer;
- use MailPoet\Cron\Workers\InactiveSubscribers;
- use MailPoet\Cron\Workers\StatsNotifications\Worker;
- use MailPoet\Cron\Workers\SubscriberLinkTokens;
- use MailPoet\Cron\Workers\SubscribersLastEngagement;
- use MailPoet\Cron\Workers\UnsubscribeTokens;
- use MailPoet\Entities\FormEntity;
- use MailPoet\Entities\NewsletterEntity;
- use MailPoet\Entities\ScheduledTaskEntity;
- use MailPoet\Entities\SendingQueueEntity;
- use MailPoet\Entities\StatisticsFormEntity;
- use MailPoet\Entities\UserFlagEntity;
- use MailPoet\Form\FormsRepository;
- use MailPoet\Mailer\MailerLog;
- use MailPoet\Models\Newsletter;
- use MailPoet\Models\NewsletterLink;
- use MailPoet\Models\ScheduledTask;
- use MailPoet\Models\Segment;
- use MailPoet\Models\SendingQueue;
- use MailPoet\Models\Subscriber;
- use MailPoet\Referrals\ReferralDetector;
- use MailPoet\Segments\WP;
- use MailPoet\Services\Bridge;
- use MailPoet\Settings\Pages;
- use MailPoet\Settings\SettingsController;
- use MailPoet\Settings\UserFlagsRepository;
- use MailPoet\Subscribers\NewSubscriberNotificationMailer;
- use MailPoet\Subscribers\Source;
- use MailPoet\Subscription\Captcha;
- use MailPoet\Util\Helpers;
- use MailPoet\WP\Functions as WPFunctions;
- use MailPoetVendor\Carbon\Carbon;
- use MailPoetVendor\Doctrine\ORM\EntityManager;
- class Populator {
- public $prefix;
- public $models;
- public $templates;
- /** @var SettingsController */
- private $settings;
- /** @var WPFunctions */
- private $wp;
- /** @var Captcha */
- private $captcha;
- /** @var ReferralDetector */
- private $referralDetector;
- const TEMPLATES_NAMESPACE = '\MailPoet\Config\PopulatorData\Templates\\';
- /** @var FormsRepository */
- private $formsRepository;
- /** @var WP */
- private $wpSegment;
- /** @var EntityManager */
- private $entityManager;
- public function __construct(
- SettingsController $settings,
- WPFunctions $wp,
- Captcha $captcha,
- ReferralDetector $referralDetector,
- FormsRepository $formsRepository,
- EntityManager $entityManager,
- WP $wpSegment
- ) {
- $this->settings = $settings;
- $this->wp = $wp;
- $this->captcha = $captcha;
- $this->wpSegment = $wpSegment;
- $this->referralDetector = $referralDetector;
- $this->prefix = Env::$dbPrefix;
- $this->models = [
- 'newsletter_option_fields',
- 'newsletter_templates',
- ];
- $this->templates = [
- 'WelcomeBlank1Column',
- 'WelcomeBlank12Column',
- 'GiftWelcome',
- 'Minimal',
- 'Phone',
- 'Sunglasses',
- 'RealEstate',
- 'AppWelcome',
- 'FoodBox',
- 'Poet',
- 'PostNotificationsBlank1Column',
- 'ModularStyleStories',
- 'RssSimpleNews',
- 'NotSoMedium',
- 'WideStoryLayout',
- 'IndustryConference',
- 'ScienceWeekly',
- 'NewspaperTraditional',
- 'ClearNews',
- 'DogFood',
- 'KidsClothing',
- 'RockBand',
- 'WineCity',
- 'Fitness',
- 'Motor',
- 'Avocado',
- 'BookStoreWithCoupon',
- 'FlowersWithCoupon',
- 'NewsletterBlank1Column',
- 'NewsletterBlank12Column',
- 'NewsletterBlank121Column',
- 'NewsletterBlank13Column',
- 'SimpleText',
- 'TakeAHike',
- 'NewsDay',
- 'WorldCup',
- 'FestivalEvent',
- 'RetroComputingMagazine',
- 'Shoes',
- 'Music',
- 'Hotels',
- 'PieceOfCake',
- 'BuddhistTemple',
- 'Mosque',
- 'Synagogue',
- 'Faith',
- 'College',
- 'RenewableEnergy',
- 'PrimarySchool',
- 'ComputerRepair',
- 'YogaStudio',
- 'Retro',
- 'Charity',
- 'CityLocalNews',
- 'Coffee',
- 'Vlogger',
- 'Birds',
- 'Engineering',
- 'BrandingAgencyNews',
- 'WordPressTheme',
- 'Drone',
- 'FashionBlog',
- 'FashionStore',
- 'FashionBlogA',
- 'Photography',
- 'JazzClub',
- 'Guitarist',
- 'HealthyFoodBlog',
- 'Software',
- 'LifestyleBlogA',
- 'FashionShop',
- 'LifestyleBlogB',
- 'Painter',
- 'FarmersMarket',
- ];
- $this->formsRepository = $formsRepository;
- $this->entityManager = $entityManager;
- }
- public function up() {
- $localizer = new Localizer();
- $localizer->forceLoadWebsiteLocaleText();
- array_map([$this, 'populate'], $this->models);
- $this->createDefaultSegment();
- $this->createDefaultSettings();
- $this->createDefaultUsersFlags();
- $this->createMailPoetPage();
- $this->createSourceForSubscribers();
- $this->updateMetaFields();
- $this->scheduleInitialInactiveSubscribersCheck();
- $this->scheduleAuthorizedSendingEmailsCheck();
- $this->scheduleBeamer();
- $this->updateLastSubscribedAt();
- $this->enableStatsNotificationsForAutomatedEmails();
- $this->updateSentUnsubscribeLinksToInstantUnsubscribeLinks();
- $this->pauseTasksForPausedNewsletters();
- $this->scheduleUnsubscribeTokens();
- $this->scheduleSubscriberLinkTokens();
- $this->detectReferral();
- $this->moveGoogleAnalyticsFromPremium();
- $this->addPlacementStatusToForms();
- $this->migrateFormPlacement();
- $this->scheduleSubscriberLastEngagementDetection();
- }
- private function createMailPoetPage() {
- $page = Pages::getDefaultMailPoetPage();
- if ($page === null) {
- $mailpoetPageId = Pages::createMailPoetPage();
- } else {
- $mailpoetPageId = (int)$page->ID;
- }
- $subscription = $this->settings->get('subscription.pages', []);
- if (empty($subscription)) {
- $this->settings->set('subscription.pages', [
- 'unsubscribe' => $mailpoetPageId,
- 'manage' => $mailpoetPageId,
- 'confirmation' => $mailpoetPageId,
- 'captcha' => $mailpoetPageId,
- 'confirm_unsubscribe' => $mailpoetPageId,
- ]);
- } else {
- // For existing installations
- $captchaPageSetting = (empty($subscription['captcha']) || $subscription['captcha'] !== $mailpoetPageId)
- ? $mailpoetPageId : $subscription['captcha'];
- $confirmUnsubPageSetting = empty($subscription['confirm_unsubscribe'])
- ? $mailpoetPageId : $subscription['confirm_unsubscribe'];
- $this->settings->set('subscription.pages', array_merge($subscription, [
- 'captcha' => $captchaPageSetting,
- 'confirm_unsubscribe' => $confirmUnsubPageSetting,
- ]));
- }
- }
- private function createDefaultSettings() {
- $currentUser = $this->wp->wpGetCurrentUser();
- $settingsDbVersion = $this->settings->fetch('db_version');
- // set cron trigger option to default method
- if (!$this->settings->fetch(CronTrigger::SETTING_NAME)) {
- $this->settings->set(CronTrigger::SETTING_NAME, [
- 'method' => CronTrigger::DEFAULT_METHOD,
- ]);
- }
- // set default sender info based on current user
- $sender = [
- 'name' => $currentUser->display_name, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
- 'address' => $currentUser->user_email, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
- ];
- // set default from name & address
- if (!$this->settings->fetch('sender')) {
- $this->settings->set('sender', $sender);
- }
- // enable signup confirmation by default
- if (!$this->settings->fetch('signup_confirmation')) {
- $this->settings->set('signup_confirmation', [
- 'enabled' => true,
- ]);
- }
- // set installation date
- if (!$this->settings->fetch('installed_at')) {
- $this->settings->set('installed_at', date("Y-m-d H:i:s"));
- }
- // set captcha settings
- $captcha = $this->settings->fetch('captcha');
- $reCaptcha = $this->settings->fetch('re_captcha');
- if (empty($captcha)) {
- $captchaType = Captcha::TYPE_DISABLED;
- if (!empty($reCaptcha['enabled'])) {
- $captchaType = Captcha::TYPE_RECAPTCHA;
- } elseif ($this->captcha->isSupported()) {
- $captchaType = Captcha::TYPE_BUILTIN;
- }
- $this->settings->set('captcha', [
- 'type' => $captchaType,
- 'recaptcha_site_token' => !empty($reCaptcha['site_token']) ? $reCaptcha['site_token'] : '',
- 'recaptcha_secret_token' => !empty($reCaptcha['secret_token']) ? $reCaptcha['secret_token'] : '',
- ]);
- }
- $subscriberEmailNotification = $this->settings->fetch(NewSubscriberNotificationMailer::SETTINGS_KEY);
- if (empty($subscriberEmailNotification)) {
- $sender = $this->settings->fetch('sender', []);
- $this->settings->set('subscriber_email_notification', [
- 'enabled' => true,
- 'automated' => true,
- 'address' => isset($sender['address']) ? $sender['address'] : null,
- ]);
- }
- $statsNotifications = $this->settings->fetch(Worker::SETTINGS_KEY);
- if (empty($statsNotifications)) {
- $sender = $this->settings->fetch('sender', []);
- $this->settings->set(Worker::SETTINGS_KEY, [
- 'enabled' => true,
- 'address' => isset($sender['address']) ? $sender['address'] : null,
- ]);
- }
- $woocommerceOptinOnCheckout = $this->settings->fetch('woocommerce.optin_on_checkout');
- $legacyLabelText = $this->wp->_x('Yes, I would like to be added to your mailing list', "default email opt-in message displayed on checkout page for ecommerce websites", 'mailpoet');
- $currentLabelText = $this->wp->_x('I would like to receive exclusive emails with discounts and product information', "default email opt-in message displayed on checkout page for ecommerce websites", 'mailpoet');
- if (empty($woocommerceOptinOnCheckout)) {
- $this->settings->set('woocommerce.optin_on_checkout', [
- 'enabled' => empty($settingsDbVersion), // enable on new installs only
- 'message' => $currentLabelText,
- ]);
- } elseif (isset($woocommerceOptinOnCheckout['message']) && $woocommerceOptinOnCheckout['message'] === $legacyLabelText ) {
- $this->settings->set('woocommerce.optin_on_checkout.message', $currentLabelText);
- }
- // reset mailer log
- MailerLog::resetMailerLog();
- $thirdPartyScriptsEnabled = $this->settings->get('3rd_party_libs');
- if (is_null($thirdPartyScriptsEnabled)) {
- // keep loading 3rd party libraries for existing users so the functionality is not broken
- $this->settings->set('3rd_party_libs.enabled', '1');
- }
- }
- private function createDefaultUsersFlags() {
- $lastAnnouncementSeen = $this->settings->fetch('last_announcement_seen');
- if (!empty($lastAnnouncementSeen)) {
- foreach ($lastAnnouncementSeen as $userId => $value) {
- $this->createOrUpdateUserFlag($userId, 'last_announcement_seen', $value);
- }
- $this->settings->delete('last_announcement_seen');
- }
- $prefix = 'user_seen_editor_tutorial';
- $prefixLength = strlen($prefix);
- foreach ($this->settings->getAll() as $name => $value) {
- if (substr($name, 0, $prefixLength) === $prefix) {
- $userId = substr($name, $prefixLength);
- $this->createOrUpdateUserFlag($userId, 'editor_tutorial_seen', $value);
- $this->settings->delete($name);
- }
- }
- }
- private function createOrUpdateUserFlag($userId, $name, $value) {
- $userFlagsRepository = \MailPoet\DI\ContainerWrapper::getInstance(WP_DEBUG)->get(UserFlagsRepository::class);
- $flag = $userFlagsRepository->findOneBy([
- 'userId' => $userId,
- 'name' => $name,
- ]);
- if (!$flag) {
- $flag = new UserFlagEntity();
- $flag->setUserId($userId);
- $flag->setName($name);
- $userFlagsRepository->persist($flag);
- }
- $flag->setValue($value);
- $userFlagsRepository->flush();
- }
- private function createDefaultSegment() {
- // WP Users segment
- Segment::getWPSegment();
- // WooCommerce customers segment
- Segment::getWooCommerceSegment();
- // Synchronize WP Users
- $this->wpSegment->synchronizeUsers();
- // Default segment
- $defaultSegment = Segment::where('type', 'default')->orderByAsc('id')->limit(1)->findOne();
- if (!$defaultSegment instanceof Segment) {
- $defaultSegment = Segment::create();
- $newList = [
- 'name' => $this->wp->__('Newsletter mailing list', 'mailpoet'),
- 'description' =>
- $this->wp->__('This list is automatically created when you install MailPoet.', 'mailpoet'),
- ];
- $defaultSegment->hydrate($newList);
- $defaultSegment->save();
- }
- return $defaultSegment;
- }
- protected function newsletterOptionFields() {
- $optionFields = [
- [
- 'name' => 'isScheduled',
- 'newsletter_type' => 'standard',
- ],
- [
- 'name' => 'scheduledAt',
- 'newsletter_type' => 'standard',
- ],
- [
- 'name' => 'event',
- 'newsletter_type' => 'welcome',
- ],
- [
- 'name' => 'segment',
- 'newsletter_type' => 'welcome',
- ],
- [
- 'name' => 'role',
- 'newsletter_type' => 'welcome',
- ],
- [
- 'name' => 'afterTimeNumber',
- 'newsletter_type' => 'welcome',
- ],
- [
- 'name' => 'afterTimeType',
- 'newsletter_type' => 'welcome',
- ],
- [
- 'name' => 'intervalType',
- 'newsletter_type' => 'notification',
- ],
- [
- 'name' => 'timeOfDay',
- 'newsletter_type' => 'notification',
- ],
- [
- 'name' => 'weekDay',
- 'newsletter_type' => 'notification',
- ],
- [
- 'name' => 'monthDay',
- 'newsletter_type' => 'notification',
- ],
- [
- 'name' => 'nthWeekDay',
- 'newsletter_type' => 'notification',
- ],
- [
- 'name' => 'schedule',
- 'newsletter_type' => 'notification',
- ],
- [
- 'name' => 'group',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'event',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'sendTo',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'segment',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'afterTimeNumber',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'afterTimeType',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'meta',
- 'newsletter_type' => NewsletterEntity::TYPE_AUTOMATIC,
- ],
- [
- 'name' => 'afterTimeNumber',
- 'newsletter_type' => NewsletterEntity::TYPE_RE_ENGAGEMENT,
- ],
- [
- 'name' => 'afterTimeType',
- 'newsletter_type' => NewsletterEntity::TYPE_RE_ENGAGEMENT,
- ],
- ];
- return [
- 'rows' => $optionFields,
- 'identification_columns' => [
- 'name',
- 'newsletter_type',
- ],
- ];
- }
- protected function newsletterTemplates() {
- $templates = [];
- foreach ($this->templates as $template) {
- $template = self::TEMPLATES_NAMESPACE . $template;
- $template = new $template(Env::$assetsUrl);
- $templates[] = $template->get();
- }
- return [
- 'rows' => $templates,
- 'identification_columns' => [
- 'name',
- ],
- 'remove_duplicates' => true,
- ];
- }
- protected function populate($model) {
- $modelMethod = Helpers::underscoreToCamelCase($model);
- $table = $this->prefix . $model;
- $dataDescriptor = $this->$modelMethod();
- $rows = $dataDescriptor['rows'];
- $identificationColumns = array_fill_keys(
- $dataDescriptor['identification_columns'],
- ''
- );
- $removeDuplicates =
- isset($dataDescriptor['remove_duplicates']) && $dataDescriptor['remove_duplicates'];
- foreach ($rows as $row) {
- $existenceComparisonFields = array_intersect_key(
- $row,
- $identificationColumns
- );
- if (!$this->rowExists($table, $existenceComparisonFields)) {
- $this->insertRow($table, $row);
- } else {
- if ($removeDuplicates) {
- $this->removeDuplicates($table, $row, $existenceComparisonFields);
- }
- $this->updateRow($table, $row, $existenceComparisonFields);
- }
- }
- }
- private function rowExists($table, $columns) {
- global $wpdb;
- $conditions = array_map(function($key) {
- return $key . '=%s';
- }, array_keys($columns));
- return $wpdb->get_var($wpdb->prepare(
- "SELECT COUNT(*) FROM $table WHERE " . implode(' AND ', $conditions),
- array_values($columns)
- )) > 0;
- }
- private function insertRow($table, $row) {
- global $wpdb;
- return $wpdb->insert(
- $table,
- $row
- );
- }
- private function updateRow($table, $row, $where) {
- global $wpdb;
- return $wpdb->update(
- $table,
- $row,
- $where
- );
- }
- private function removeDuplicates($table, $row, $where) {
- global $wpdb;
- $conditions = ['1=1'];
- $values = [];
- foreach ($where as $field => $value) {
- $conditions[] = "`t1`.`$field` = `t2`.`$field`";
- $conditions[] = "`t1`.`$field` = %s";
- $values[] = $value;
- }
- $conditions = implode(' AND ', $conditions);
- $sql = "DELETE FROM `$table` WHERE $conditions";
- return $wpdb->query(
- $wpdb->prepare(
- "DELETE t1 FROM $table t1, $table t2 WHERE t1.id < t2.id AND $conditions",
- $values
- )
- );
- }
- private function createSourceForSubscribers() {
- $statisticsFormTable = $this->entityManager->getClassMetadata(StatisticsFormEntity::class)->getTableName();
- Subscriber::rawExecute(
- ' UPDATE LOW_PRIORITY `' . Subscriber::$_table . '` subscriber ' .
- ' JOIN `' . $statisticsFormTable . '` stats ON stats.subscriber_id=subscriber.id ' .
- ' SET `source` = "' . Source::FORM . '"' .
- ' WHERE `source` = "' . Source::UNKNOWN . '"'
- );
- Subscriber::rawExecute(
- 'UPDATE LOW_PRIORITY `' . Subscriber::$_table . '`' .
- ' SET `source` = "' . Source::WORDPRESS_USER . '"' .
- ' WHERE `source` = "' . Source::UNKNOWN . '"' .
- ' AND `wp_user_id` IS NOT NULL'
- );
- Subscriber::rawExecute(
- 'UPDATE LOW_PRIORITY `' . Subscriber::$_table . '`' .
- ' SET `source` = "' . Source::WOOCOMMERCE_USER . '"' .
- ' WHERE `source` = "' . Source::UNKNOWN . '"' .
- ' AND `is_woocommerce_user` = 1'
- );
- }
- private function updateMetaFields() {
- global $wpdb;
- // perform once for versions below or equal to 3.26.0
- if (version_compare($this->settings->get('db_version', '3.26.1'), '3.26.0', '>')) {
- return false;
- }
- $tables = [ScheduledTask::$_table, SendingQueue::$_table];
- foreach ($tables as $table) {
- $query = "UPDATE `%s` SET meta = NULL WHERE meta = 'null'";
- $wpdb->query(sprintf($query, $table));
- }
- return true;
- }
- private function scheduleInitialInactiveSubscribersCheck() {
- $this->scheduleTask(
- InactiveSubscribers::TASK_TYPE,
- Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))->addHour()
- );
- }
- private function scheduleAuthorizedSendingEmailsCheck() {
- if (!Bridge::isMPSendingServiceEnabled()) {
- return;
- }
- $this->scheduleTask(
- AuthorizedSendingEmailsCheck::TASK_TYPE,
- Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))
- );
- }
- private function scheduleBeamer() {
- if (!$this->settings->get('last_announcement_date')) {
- $this->scheduleTask(
- Beamer::TASK_TYPE,
- Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))
- );
- }
- }
- private function updateLastSubscribedAt() {
- global $wpdb;
- // perform once for versions below or equal to 3.42.0
- if (version_compare($this->settings->get('db_version', '3.42.1'), '3.42.0', '>')) {
- return false;
- }
- $query = "UPDATE `%s` SET last_subscribed_at = GREATEST(COALESCE(confirmed_at, 0), COALESCE(created_at, 0)) WHERE status != '%s' AND last_subscribed_at IS NULL;";
- $wpdb->query(sprintf(
- $query,
- Subscriber::$_table,
- Subscriber::STATUS_UNCONFIRMED
- ));
- return true;
- }
- private function scheduleUnsubscribeTokens() {
- $this->scheduleTask(
- UnsubscribeTokens::TASK_TYPE,
- Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))
- );
- }
- private function scheduleSubscriberLinkTokens() {
- $this->scheduleTask(
- SubscriberLinkTokens::TASK_TYPE,
- Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))
- );
- }
- private function scheduleTask($type, $datetime) {
- $task = ScheduledTask::where('type', $type)
- ->whereRaw('status = ? OR status IS NULL', [ScheduledTask::STATUS_SCHEDULED])
- ->findOne();
- if ($task) {
- return true;
- }
- $task = ScheduledTask::create();
- $task->type = $type;
- $task->status = ScheduledTask::STATUS_SCHEDULED;
- $task->scheduledAt = $datetime;
- $task->save();
- }
- private function enableStatsNotificationsForAutomatedEmails() {
- if (version_compare($this->settings->get('db_version', '3.31.2'), '3.31.1', '>')) {
- return;
- }
- $settings = $this->settings->get(Worker::SETTINGS_KEY);
- $settings['automated'] = true;
- $this->settings->set(Worker::SETTINGS_KEY, $settings);
- }
- private function updateSentUnsubscribeLinksToInstantUnsubscribeLinks() {
- if (version_compare($this->settings->get('db_version', '3.46.14'), '3.46.13', '>')) {
- return;
- }
- $query = "UPDATE `%s` SET `url` = '%s' WHERE `url` = '%s';";
- global $wpdb;
- $wpdb->query(sprintf(
- $query,
- NewsletterLink::$_table,
- NewsletterLink::INSTANT_UNSUBSCRIBE_LINK_SHORT_CODE,
- NewsletterLink::UNSUBSCRIBE_LINK_SHORT_CODE
- ));
- }
- private function pauseTasksForPausedNewsletters() {
- if (version_compare($this->settings->get('db_version', '3.60.5'), '3.60.4', '>')) {
- return;
- }
- $scheduledTaskTable = $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName();
- $sendingQueueTable = $this->entityManager->getClassMetadata(SendingQueueEntity::class)->getTableName();
- $newsletterTable = $this->entityManager->getClassMetadata(NewsletterEntity::class)->getTableName();
- $query = "
- UPDATE $scheduledTaskTable as t
- JOIN $sendingQueueTable as q ON t.id = q.task_id
- JOIN $newsletterTable as n ON n.id = q.newsletter_id
- SET t.status = :tStatusPaused
- WHERE
- t.status = :tStatusScheduled
- AND n.status = :nStatusDraft
- ";
- $this->entityManager->getConnection()->executeUpdate(
- $query,
- [
- 'tStatusPaused' => ScheduledTaskEntity::STATUS_PAUSED,
- 'tStatusScheduled' => ScheduledTaskEntity::STATUS_SCHEDULED,
- 'nStatusDraft' => NewsletterEntity::STATUS_DRAFT,
- ]
- );
- }
- private function addPlacementStatusToForms() {
- if (version_compare($this->settings->get('db_version', '3.49.0'), '3.48.1', '>')) {
- return;
- }
- $forms = $this->formsRepository->findAll();
- foreach ($forms as $form) {
- $settings = $form->getSettings();
- if (
- (isset($settings['place_form_bellow_all_posts']) && $settings['place_form_bellow_all_posts'] === '1')
- || (isset($settings['place_form_bellow_all_pages']) && $settings['place_form_bellow_all_pages'] === '1')
- ) {
- $settings['form_placement_bellow_posts_enabled'] = '1';
- } else {
- $settings['form_placement_bellow_posts_enabled'] = '';
- }
- if (
- (isset($settings['place_popup_form_on_all_posts']) && $settings['place_popup_form_on_all_posts'] === '1')
- || (isset($settings['place_popup_form_on_all_pages']) && $settings['place_popup_form_on_all_pages'] === '1')
- ) {
- $settings['form_placement_popup_enabled'] = '1';
- } else {
- $settings['form_placement_popup_enabled'] = '';
- }
- if (
- (isset($settings['place_fixed_bar_form_on_all_posts']) && $settings['place_fixed_bar_form_on_all_posts'] === '1')
- || (isset($settings['place_fixed_bar_form_on_all_pages']) && $settings['place_fixed_bar_form_on_all_pages'] === '1')
- ) {
- $settings['form_placement_fixed_bar_enabled'] = '1';
- } else {
- $settings['form_placement_fixed_bar_enabled'] = '';
- }
- if (
- (isset($settings['place_slide_in_form_on_all_posts']) && $settings['place_slide_in_form_on_all_posts'] === '1')
- || (isset($settings['place_slide_in_form_on_all_pages']) && $settings['place_slide_in_form_on_all_pages'] === '1')
- ) {
- $settings['form_placement_slide_in_enabled'] = '1';
- } else {
- $settings['form_placement_slide_in_enabled'] = '';
- }
- $form->setSettings($settings);
- }
- $this->formsRepository->flush();
- }
- private function migrateFormPlacement() {
- if (version_compare($this->settings->get('db_version', '3.50.0'), '3.49.1', '>')) {
- return;
- }
- $forms = $this->formsRepository->findAll();
- foreach ($forms as $form) {
- $settings = $form->getSettings();
- if (!is_array($settings)) continue;
- $settings['form_placement'] = [
- FormEntity::DISPLAY_TYPE_POPUP => [
- 'enabled' => $settings['form_placement_popup_enabled'],
- 'delay' => $settings['popup_form_delay'] ?? 0,
- 'styles' => $settings['popup_styles'] ?? [],
- 'posts' => [
- 'all' => $settings['place_popup_form_on_all_posts'] ?? '',
- ],
- 'pages' => [
- 'all' => $settings['place_popup_form_on_all_pages'] ?? '',
- ],
- ],
- FormEntity::DISPLAY_TYPE_FIXED_BAR => [
- 'enabled' => $settings['form_placement_fixed_bar_enabled'],
- 'delay' => $settings['fixed_bar_form_delay'] ?? 0,
- 'styles' => $settings['fixed_bar_styles'] ?? [],
- 'position' => $settings['fixed_bar_form_position'] ?? 'top',
- 'posts' => [
- 'all' => $settings['place_fixed_bar_form_on_all_posts'] ?? '',
- ],
- 'pages' => [
- 'all' => $settings['place_fixed_bar_form_on_all_pages'] ?? '',
- ],
- ],
- FormEntity::DISPLAY_TYPE_BELOW_POST => [
- 'enabled' => $settings['form_placement_bellow_posts_enabled'],
- 'styles' => $settings['below_post_styles'] ?? [],
- 'posts' => [
- 'all' => $settings['place_form_bellow_all_posts'] ?? '',
- ],
- 'pages' => [
- 'all' => $settings['place_form_bellow_all_pages'] ?? '',
- ],
- ],
- FormEntity::DISPLAY_TYPE_SLIDE_IN => [
- 'enabled' => $settings['form_placement_slide_in_enabled'],
- 'delay' => $settings['slide_in_form_delay'] ?? 0,
- 'position' => $settings['slide_in_form_position'] ?? 'right',
- 'styles' => $settings['slide_in_styles'] ?? [],
- 'posts' => [
- 'all' => $settings['place_slide_in_form_on_all_posts'] ?? '',
- ],
- 'pages' => [
- 'all' => $settings['place_slide_in_form_on_all_pages'] ?? '',
- ],
- ],
- FormEntity::DISPLAY_TYPE_OTHERS => [
- 'styles' => $settings['other_styles'] ?? [],
- ],
- ];
- if (isset($settings['form_placement_slide_in_enabled'])) unset($settings['form_placement_slide_in_enabled']);
- if (isset($settings['form_placement_fixed_bar_enabled'])) unset($settings['form_placement_fixed_bar_enabled']);
- if (isset($settings['form_placement_popup_enabled'])) unset($settings['form_placement_popup_enabled']);
- if (isset($settings['form_placement_bellow_posts_enabled'])) unset($settings['form_placement_bellow_posts_enabled']);
- if (isset($settings['place_form_bellow_all_pages'])) unset($settings['place_form_bellow_all_pages']);
- if (isset($settings['place_form_bellow_all_posts'])) unset($settings['place_form_bellow_all_posts']);
- if (isset($settings['place_popup_form_on_all_pages'])) unset($settings['place_popup_form_on_all_pages']);
- if (isset($settings['place_popup_form_on_all_posts'])) unset($settings['place_popup_form_on_all_posts']);
- if (isset($settings['popup_form_delay'])) unset($settings['popup_form_delay']);
- if (isset($settings['place_fixed_bar_form_on_all_pages'])) unset($settings['place_fixed_bar_form_on_all_pages']);
- if (isset($settings['place_fixed_bar_form_on_all_posts'])) unset($settings['place_fixed_bar_form_on_all_posts']);
- if (isset($settings['fixed_bar_form_delay'])) unset($settings['fixed_bar_form_delay']);
- if (isset($settings['fixed_bar_form_position'])) unset($settings['fixed_bar_form_position']);
- if (isset($settings['place_slide_in_form_on_all_pages'])) unset($settings['place_slide_in_form_on_all_pages']);
- if (isset($settings['place_slide_in_form_on_all_posts'])) unset($settings['place_slide_in_form_on_all_posts']);
- if (isset($settings['slide_in_form_delay'])) unset($settings['slide_in_form_delay']);
- if (isset($settings['slide_in_form_position'])) unset($settings['slide_in_form_position']);
- if (isset($settings['other_styles'])) unset($settings['other_styles']);
- if (isset($settings['slide_in_styles'])) unset($settings['slide_in_styles']);
- if (isset($settings['below_post_styles'])) unset($settings['below_post_styles']);
- if (isset($settings['fixed_bar_styles'])) unset($settings['fixed_bar_styles']);
- if (isset($settings['popup_styles'])) unset($settings['popup_styles']);
- $form->setSettings($settings);
- }
- $this->formsRepository->flush();
- }
- private function moveGoogleAnalyticsFromPremium() {
- global $wpdb;
- if (version_compare($this->settings->get('db_version', '3.38.2'), '3.38.1', '>')) {
- return;
- }
- $premiumTableName = $wpdb->prefix . 'mailpoet_premium_newsletter_extra_data';
- $premiumTableExists = (int)$wpdb->get_var(
- $wpdb->prepare(
- "SELECT COUNT(1) FROM information_schema.tables WHERE table_schema=%s AND table_name=%s;",
- $wpdb->dbname,
- $premiumTableName
- )
- );
- if ($premiumTableExists) {
- $query = "
- UPDATE
- `%s` as n
- JOIN %s as ped ON n.id=ped.newsletter_id
- SET n.ga_campaign = ped.ga_campaign
- ";
- $wpdb->query(
- sprintf(
- $query,
- Newsletter::$_table,
- $premiumTableName
- )
- );
- }
- return true;
- }
- private function detectReferral() {
- $this->referralDetector->detect();
- }
- private function scheduleSubscriberLastEngagementDetection() {
- if (version_compare($this->settings->get('db_version', '3.68.1'), '3.68.0', '>')) {
- return;
- }
- $this->scheduleTask(
- SubscribersLastEngagement::TASK_TYPE,
- Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))
- );
- }
- }
|