AccessControl::PERMISSION_MANAGE_SETTINGS, ]; /** * @var EntityManager */ private $entityManager; /** * @var StatisticsOpensRepository */ private $statisticsOpensRepository; public function __construct( SettingsController $settings, Bridge $bridge, AuthorizedEmailsController $authorizedEmailsController, TransactionalEmails $wcTransactionalEmails, WPFunctions $wp, EntityManager $entityManager, StatisticsOpensRepository $statisticsOpensRepository, ServicesChecker $servicesChecker ) { $this->settings = $settings; $this->bridge = $bridge; $this->authorizedEmailsController = $authorizedEmailsController; $this->wcTransactionalEmails = $wcTransactionalEmails; $this->servicesChecker = $servicesChecker; $this->wp = $wp; $this->entityManager = $entityManager; $this->statisticsOpensRepository = $statisticsOpensRepository; } public function get() { return $this->successResponse($this->settings->getAll()); } public function set($settings = []) { if (empty($settings)) { return $this->badRequest( [ APIError::BAD_REQUEST => WPFunctions::get()->__('You have not specified any settings to be saved.', 'mailpoet'), ]); } else { $oldSettings = $this->settings->getAll(); $signupConfirmation = $this->settings->get('signup_confirmation.enabled'); foreach ($settings as $name => $value) { $this->settings->set($name, $value); } $this->onSettingsChange($oldSettings, $this->settings->getAll()); // when pending approval, leave this to cron / Key Activation tab logic if (!$this->servicesChecker->isMailPoetAPIKeyPendingApproval()) { $this->bridge->onSettingsSave($settings); } $this->authorizedEmailsController->onSettingsSave($settings); if ($signupConfirmation !== $this->settings->get('signup_confirmation.enabled')) { $this->settings->updateSuccessMessages(); } return $this->successResponse($this->settings->getAll()); } } public function recalculateSubscribersScore() { $this->statisticsOpensRepository->resetSubscribersScoreCalculation(); $task = new ScheduledTaskEntity(); $task->setType(SubscribersEngagementScore::TASK_TYPE); $task->setScheduledAt(Carbon::createFromTimestamp($this->wp->currentTime('timestamp'))); $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); $this->entityManager->persist($task); $this->entityManager->flush(); return $this->successResponse(); } public function setAuthorizedFromAddress($data = []) { $address = $data['address'] ?? null; if (!$address) { return $this->badRequest([ APIError::BAD_REQUEST => WPFunctions::get()->__('No email address specified.', 'mailpoet'), ]); } $address = trim($address); try { $this->authorizedEmailsController->setFromEmailAddress($address); } catch (\InvalidArgumentException $e) { return $this->badRequest([ APIError::UNAUTHORIZED => WPFunctions::get()->__('Can’t use this email yet! Please authorize it first.', 'mailpoet'), ]); } if (!$this->servicesChecker->isMailPoetAPIKeyPendingApproval()) { MailerLog::resumeSending(); } return $this->successResponse(); } private function onSettingsChange($oldSettings, $newSettings) { // Recalculate inactive subscribers $oldInactivationInterval = $oldSettings['deactivate_subscriber_after_inactive_days']; $newInactivationInterval = $newSettings['deactivate_subscriber_after_inactive_days']; if ($oldInactivationInterval !== $newInactivationInterval) { $this->settings->onInactiveSubscribersIntervalChange(); } $oldSendingMethod = $oldSettings['mta_group']; $newSendingMethod = $newSettings['mta_group']; if (($oldSendingMethod !== $newSendingMethod) && ($newSendingMethod === 'mailpoet')) { $this->onMSSActivate($newSettings); } // Sync WooCommerce Customers list $oldSubscribeOldWoocommerceCustomers = isset($oldSettings['mailpoet_subscribe_old_woocommerce_customers']['enabled']) ? $oldSettings['mailpoet_subscribe_old_woocommerce_customers']['enabled'] : '0'; $newSubscribeOldWoocommerceCustomers = isset($newSettings['mailpoet_subscribe_old_woocommerce_customers']['enabled']) ? $newSettings['mailpoet_subscribe_old_woocommerce_customers']['enabled'] : '0'; if ($oldSubscribeOldWoocommerceCustomers !== $newSubscribeOldWoocommerceCustomers) { $this->settings->onSubscribeOldWoocommerceCustomersChange(); } if (!empty($newSettings['woocommerce']['use_mailpoet_editor'])) { $this->wcTransactionalEmails->init(); } } private function onMSSActivate($newSettings) { // see mailpoet/assets/js/src/wizard/create_sender_settings.jsx:freeAddress $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']); if ( isset($newSettings['sender']['address']) && !empty($newSettings['reply_to']['address']) && ($newSettings['sender']['address'] === ('wordpress@' . $domain)) ) { $sender = [ 'name' => $newSettings['reply_to']['name'] ?? '', 'address' => $newSettings['reply_to']['address'], ]; $this->settings->set('sender', $sender); $this->settings->set('reply_to', null); } } }