subscriptionPages = $subscriptionPages; $this->wp = $wp; $this->segmentSubscribersRepository = $segmentSubscribersRepository; $this->subscribersRepository = $subscribersRepository; $this->newsletterUrl = $newsletterUrl; $this->newslettersRepository = $newslettersRepository; $this->entityManager = $entityManager; $this->dateCategory = $dateCategory; $this->linkCategory = $linkCategory; $this->newsletterCategory = $newsletterCategory; $this->subscriberCategory = $subscriberCategory; } public function init() { // form widget shortcode $this->wp->addShortcode('mailpoet_form', [$this, 'formWidget']); // subscribers count shortcode $this->wp->addShortcode('mailpoet_subscribers_count', [ $this, 'getSubscribersCount', ]); $this->wp->addShortcode('wysija_subscribers_count', [ $this, 'getSubscribersCount', ]); // archives page $this->wp->addShortcode('mailpoet_archive', [ $this, 'getArchive', ]); $this->wp->addFilter('mailpoet_archive_email_processed_date', [ $this, 'renderArchiveDate', ], 2); $this->wp->addFilter('mailpoet_archive_email_subject', [ $this, 'renderArchiveSubject', ], 2, 3); // This deprecated notice can be removed after 2022-06-01 if ($this->wp->hasFilter('mailpoet_archive_date')) { $this->wp->deprecatedHook( 'mailpoet_archive_date', '3.69.2', 'mailpoet_archive_email_processed_date', __('Please note that mailpoet_archive_date no longer runs and that the list of parameters of the new filter is different.', 'mailpoet') ); } // This deprecated notice can be removed after 2022-06-01 if ($this->wp->hasFilter('mailpoet_archive_subject')) { $this->wp->deprecatedHook( 'mailpoet_archive_subject', '3.69.2', 'mailpoet_archive_email_subject', __('Please note that mailpoet_archive_subject no longer runs and that the list of parameters of the new filter is different.', 'mailpoet') ); } // initialize subscription pages data $this->subscriptionPages->init(); // initialize subscription management shortcodes $this->subscriptionPages->initShortcodes(); } public function formWidget($params = []) { // IMPORTANT: fixes conflict with MagicMember $this->wp->removeShortcode('user_list'); if (isset($params['id']) && (int)$params['id'] > 0) { $formWidget = new Widget(); return $formWidget->widget([ 'form' => (int)$params['id'], 'form_type' => 'shortcode', ]); } } public function getSubscribersCount($params) { if (!empty($params['segments'])) { $segmentIds = array_map(function($segmentId) { return (int)trim($segmentId); }, explode(',', $params['segments'])); } if (empty($segmentIds)) { return $this->wp->numberFormatI18n(Subscriber::filter('subscribed')->count()); } else { return $this->wp->numberFormatI18n( $this->segmentSubscribersRepository->getSubscribersCountBySegmentIds($segmentIds, SubscriberEntity::STATUS_SUBSCRIBED) ); } } public function getArchive($params) { $segmentIds = []; if (!empty($params['segments'])) { $segmentIds = array_map(function($segmentId) { return (int)trim($segmentId); }, explode(',', $params['segments'])); } $html = ''; $newsletters = $this->newslettersRepository->getArchives($segmentIds); $subscriber = $this->subscribersRepository->getCurrentWPUser(); $subscriber = $subscriber ? Subscriber::findOne($subscriber->getId()) : null; if (empty($newsletters)) { return $this->wp->applyFilters( 'mailpoet_archive_no_newsletters', $this->wp->__('Oops! There are no newsletters to display.', 'mailpoet') ); } else { $title = $this->wp->applyFilters('mailpoet_archive_title', ''); if (!empty($title)) { $html .= '

' . $title . '

'; } $html .= ''; } return $html; } public function renderArchiveDate(NewsletterEntity $newsletter) { $timestamp = null; $processedAt = $newsletter->getProcessedAt(); if (!is_null($processedAt)) { $timestamp = $processedAt->getTimestamp(); } return $this->wp->dateI18n( $this->wp->getOption('date_format'), $timestamp ); } public function renderArchiveSubject(NewsletterEntity $newsletter, $subscriber, SendingQueueEntity $queue) { $previewUrl = $this->newsletterUrl->getViewInBrowserUrl($newsletter, $subscriber, $queue); /** * An ugly workaround to make sure state is not shared via NewsletterShortcodes service * This should be replaced with injected service when state is removed from this service * (i.e. after https://mailpoet.atlassian.net/browse/MAILPOET-4087 is done and merged to master) */ $shortcodeProcessor = new NewsletterShortcodes( $this->dateCategory, $this->linkCategory, $this->newsletterCategory, $this->subscriberCategory, $this->wp ); $shortcodeProcessor->setNewsletter($newsletter); if (is_object($subscriber) && !is_a($subscriber, SubscriberEntity::class) && !empty($subscriber->id)) { $subscriberEntity = $this->entityManager->find(SubscriberEntity::class, $subscriber->id); } else { $subscriberEntity = new SubscriberEntity(); } $shortcodeProcessor->setSubscriber($subscriberEntity); $shortcodeProcessor->setQueue($queue); return '' . esc_attr((string)$shortcodeProcessor->replace($queue->getNewsletterRenderedSubject())) . ''; } }