ConfirmationEmailTemplate
3 years ago
ImportExport
2 weeks ago
Statistics
1 year ago
BulkConfirmationEmailResender.php
4 weeks ago
ConfirmationEmailCustomizer.php
4 weeks ago
ConfirmationEmailMailer.php
4 weeks ago
ConfirmationEmailResolver.php
4 weeks ago
EngagementDataBackfiller.php
4 weeks ago
InactiveSubscribersController.php
4 weeks ago
LinkTokens.php
4 weeks ago
NewSubscriberNotificationMailer.php
4 weeks ago
RequiredCustomFieldValidator.php
4 weeks ago
Source.php
3 weeks ago
SubscriberActions.php
3 weeks ago
SubscriberCustomFieldRepository.php
3 years ago
SubscriberIPsRepository.php
2 years ago
SubscriberLimitNotificationEvaluator.php
3 weeks ago
SubscriberLimitNotificationMailer.php
3 weeks ago
SubscriberLimitNotificationScheduler.php
3 weeks ago
SubscriberListingRepository.php
4 weeks ago
SubscriberPersonalDataEraser.php
4 weeks ago
SubscriberSaveController.php
3 weeks ago
SubscriberSegmentRepository.php
1 month ago
SubscriberSubscribeController.php
3 weeks ago
SubscriberTagRepository.php
3 years ago
SubscribersCountsController.php
1 month ago
SubscribersEmailCountsController.php
1 year ago
SubscribersRepository.php
3 weeks ago
index.php
3 years ago
NewSubscriberNotificationMailer.php
121 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscribers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Renderer; |
| 9 | use MailPoet\Entities\SegmentEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Mailer\MailerFactory; |
| 12 | use MailPoet\Mailer\MetaInfo; |
| 13 | use MailPoet\Settings\SettingsController; |
| 14 | use MailPoet\WP\Functions as WPFunctions; |
| 15 | |
| 16 | class NewSubscriberNotificationMailer { |
| 17 | const SETTINGS_KEY = 'subscriber_email_notification'; |
| 18 | |
| 19 | /** @var MailerFactory */ |
| 20 | private $mailerFactory; |
| 21 | |
| 22 | /** @var Renderer */ |
| 23 | private $renderer; |
| 24 | |
| 25 | /** @var SettingsController */ |
| 26 | private $settings; |
| 27 | |
| 28 | /** @var MetaInfo */ |
| 29 | private $mailerMetaInfo; |
| 30 | |
| 31 | public function __construct( |
| 32 | MailerFactory $mailerFactory, |
| 33 | Renderer $renderer, |
| 34 | SettingsController $settings |
| 35 | ) { |
| 36 | $this->mailerFactory = $mailerFactory; |
| 37 | $this->renderer = $renderer; |
| 38 | $this->settings = $settings; |
| 39 | $this->mailerMetaInfo = new MetaInfo(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param SubscriberEntity $subscriber |
| 44 | * @param SegmentEntity[] $segments |
| 45 | * |
| 46 | * @throws \Exception |
| 47 | */ |
| 48 | public function send(SubscriberEntity $subscriber, array $segments): void { |
| 49 | $settings = $this->settings->get(NewSubscriberNotificationMailer::SETTINGS_KEY); |
| 50 | if ($this->isDisabled($settings)) { |
| 51 | return; |
| 52 | } |
| 53 | try { |
| 54 | $extraParams = [ |
| 55 | 'meta' => $this->mailerMetaInfo->getNewSubscriberNotificationMetaInfo(), |
| 56 | ]; |
| 57 | $this->mailerFactory->getDefaultMailer()->send($this->constructNewsletter($subscriber, $segments), $settings['address'], $extraParams); |
| 58 | } catch (\Exception $e) { |
| 59 | if (WP_DEBUG) { |
| 60 | throw $e; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public static function isDisabled($settings) { |
| 66 | if (!is_array($settings)) { |
| 67 | return true; |
| 68 | } |
| 69 | if (!isset($settings['enabled'])) { |
| 70 | return true; |
| 71 | } |
| 72 | if (!isset($settings['address']) || !is_string($settings['address'])) { |
| 73 | return true; |
| 74 | } |
| 75 | if (empty(trim($settings['address']))) { |
| 76 | return true; |
| 77 | } |
| 78 | return !(bool)$settings['enabled']; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param SubscriberEntity $subscriber |
| 83 | * @param SegmentEntity[] $segments |
| 84 | * |
| 85 | * @return array |
| 86 | * @throws \Exception |
| 87 | */ |
| 88 | private function constructNewsletter(SubscriberEntity $subscriber, array $segments) { |
| 89 | $segmentNames = $this->getSegmentNames($segments); |
| 90 | $context = [ |
| 91 | 'subscriber_email' => $subscriber->getEmail(), |
| 92 | 'segments_names' => $segmentNames, |
| 93 | 'link_settings' => WPFunctions::get()->applyFilters( |
| 94 | 'mailpoet_new_subscriber_notification_link_settings', |
| 95 | WPFunctions::get()->getSiteUrl(null, '/wp-admin/admin.php?page=mailpoet-settings') |
| 96 | ), |
| 97 | 'link_premium' => WPFunctions::get()->getSiteUrl(null, '/wp-admin/admin.php?page=mailpoet-upgrade'), |
| 98 | ]; |
| 99 | return [ |
| 100 | // translators: %s is name of the segment. |
| 101 | 'subject' => sprintf(__('New subscriber to %s', 'mailpoet'), $segmentNames), |
| 102 | 'body' => [ |
| 103 | 'html' => $this->renderer->render('emails/newSubscriberNotification.html', $context), |
| 104 | 'text' => $this->renderer->render('emails/newSubscriberNotification.txt', $context), |
| 105 | ], |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param SegmentEntity[] $segments |
| 111 | * @return string |
| 112 | */ |
| 113 | private function getSegmentNames(array $segments): string { |
| 114 | $names = []; |
| 115 | foreach ($segments as $segment) { |
| 116 | $names[] = $segment->getName(); |
| 117 | } |
| 118 | return implode(', ', $names); |
| 119 | } |
| 120 | } |
| 121 |