Нема описа

Shortcodes.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Entities\NewsletterEntity;
  5. use MailPoet\Entities\SendingQueueEntity;
  6. use MailPoet\Entities\SubscriberEntity;
  7. use MailPoet\Form\Widget;
  8. use MailPoet\Models\Subscriber;
  9. use MailPoet\Newsletter\NewslettersRepository;
  10. use MailPoet\Newsletter\Url as NewsletterUrl;
  11. use MailPoet\Segments\SegmentSubscribersRepository;
  12. use MailPoet\Subscribers\SubscribersRepository;
  13. use MailPoet\Subscription\Pages;
  14. use MailPoet\WP\Functions as WPFunctions;
  15. class Shortcodes {
  16. /** @var Pages */
  17. private $subscriptionPages;
  18. /** @var WPFunctions */
  19. private $wp;
  20. /** @var SegmentSubscribersRepository */
  21. private $segmentSubscribersRepository;
  22. /** @var SubscribersRepository */
  23. private $subscribersRepository;
  24. /** @var NewsletterUrl */
  25. private $newsletterUrl;
  26. /** @var NewslettersRepository */
  27. private $newslettersRepository;
  28. public function __construct(
  29. Pages $subscriptionPages,
  30. WPFunctions $wp,
  31. SegmentSubscribersRepository $segmentSubscribersRepository,
  32. SubscribersRepository $subscribersRepository,
  33. NewsletterUrl $newsletterUrl,
  34. NewslettersRepository $newslettersRepository
  35. ) {
  36. $this->subscriptionPages = $subscriptionPages;
  37. $this->wp = $wp;
  38. $this->segmentSubscribersRepository = $segmentSubscribersRepository;
  39. $this->subscribersRepository = $subscribersRepository;
  40. $this->newsletterUrl = $newsletterUrl;
  41. $this->newslettersRepository = $newslettersRepository;
  42. }
  43. public function init() {
  44. // form widget shortcode
  45. $this->wp->addShortcode('mailpoet_form', [$this, 'formWidget']);
  46. // subscribers count shortcode
  47. $this->wp->addShortcode('mailpoet_subscribers_count', [
  48. $this, 'getSubscribersCount',
  49. ]);
  50. $this->wp->addShortcode('wysija_subscribers_count', [
  51. $this, 'getSubscribersCount',
  52. ]);
  53. // archives page
  54. $this->wp->addShortcode('mailpoet_archive', [
  55. $this, 'getArchive',
  56. ]);
  57. $this->wp->addFilter('mailpoet_archive_email_processed_date', [
  58. $this, 'renderArchiveDate',
  59. ], 2);
  60. $this->wp->addFilter('mailpoet_archive_email_subject', [
  61. $this, 'renderArchiveSubject',
  62. ], 2, 3);
  63. // This deprecated notice can be removed after 2022-06-01
  64. if ($this->wp->hasFilter('mailpoet_archive_date')) {
  65. $this->wp->deprecatedHook(
  66. 'mailpoet_archive_date',
  67. '3.69.2',
  68. 'mailpoet_archive_email_processed_date',
  69. __('Please note that mailpoet_archive_date no longer runs and that the list of parameters of the new filter is different.', 'mailpoet')
  70. );
  71. }
  72. // This deprecated notice can be removed after 2022-06-01
  73. if ($this->wp->hasFilter('mailpoet_archive_subject')) {
  74. $this->wp->deprecatedHook(
  75. 'mailpoet_archive_subject',
  76. '3.69.2',
  77. 'mailpoet_archive_email_subject',
  78. __('Please note that mailpoet_archive_subject no longer runs and that the list of parameters of the new filter is different.', 'mailpoet')
  79. );
  80. }
  81. // initialize subscription pages data
  82. $this->subscriptionPages->init();
  83. // initialize subscription management shortcodes
  84. $this->subscriptionPages->initShortcodes();
  85. }
  86. public function formWidget($params = []) {
  87. // IMPORTANT: fixes conflict with MagicMember
  88. $this->wp->removeShortcode('user_list');
  89. if (isset($params['id']) && (int)$params['id'] > 0) {
  90. $formWidget = new Widget();
  91. return $formWidget->widget([
  92. 'form' => (int)$params['id'],
  93. 'form_type' => 'shortcode',
  94. ]);
  95. }
  96. }
  97. public function getSubscribersCount($params) {
  98. if (!empty($params['segments'])) {
  99. $segmentIds = array_map(function($segmentId) {
  100. return (int)trim($segmentId);
  101. }, explode(',', $params['segments']));
  102. }
  103. if (empty($segmentIds)) {
  104. return $this->wp->numberFormatI18n(Subscriber::filter('subscribed')->count());
  105. } else {
  106. return $this->wp->numberFormatI18n(
  107. $this->segmentSubscribersRepository->getSubscribersCountBySegmentIds($segmentIds, SubscriberEntity::STATUS_SUBSCRIBED)
  108. );
  109. }
  110. }
  111. public function getArchive($params) {
  112. $segmentIds = [];
  113. if (!empty($params['segments'])) {
  114. $segmentIds = array_map(function($segmentId) {
  115. return (int)trim($segmentId);
  116. }, explode(',', $params['segments']));
  117. }
  118. $html = '';
  119. $newsletters = $this->newslettersRepository->getArchives($segmentIds);
  120. $subscriber = $this->subscribersRepository->getCurrentWPUser();
  121. $subscriber = $subscriber ? Subscriber::findOne($subscriber->getId()) : null;
  122. if (empty($newsletters)) {
  123. return $this->wp->applyFilters(
  124. 'mailpoet_archive_no_newsletters',
  125. $this->wp->__('Oops! There are no newsletters to display.', 'mailpoet')
  126. );
  127. } else {
  128. $title = $this->wp->applyFilters('mailpoet_archive_title', '');
  129. if (!empty($title)) {
  130. $html .= '<h3 class="mailpoet_archive_title">' . $title . '</h3>';
  131. }
  132. $html .= '<ul class="mailpoet_archive">';
  133. foreach ($newsletters as $newsletter) {
  134. $queue = $newsletter->getLatestQueue();
  135. $html .= '<li>' .
  136. '<span class="mailpoet_archive_date">' .
  137. $this->wp->applyFilters('mailpoet_archive_email_processed_date', $newsletter) .
  138. '</span>
  139. <span class="mailpoet_archive_subject">' .
  140. $this->wp->applyFilters('mailpoet_archive_email_subject', $newsletter, $subscriber, $queue) .
  141. '</span>
  142. </li>';
  143. }
  144. $html .= '</ul>';
  145. }
  146. return $html;
  147. }
  148. public function renderArchiveDate(NewsletterEntity $newsletter) {
  149. $timestamp = null;
  150. $processedAt = $newsletter->getProcessedAt();
  151. if (!is_null($processedAt)) {
  152. $timestamp = $processedAt->getTimestamp();
  153. }
  154. return $this->wp->dateI18n(
  155. $this->wp->getOption('date_format'),
  156. $timestamp
  157. );
  158. }
  159. public function renderArchiveSubject(NewsletterEntity $newsletter, $subscriber, SendingQueueEntity $queue) {
  160. $previewUrl = $this->newsletterUrl->getViewInBrowserUrl($newsletter, $subscriber, $queue);
  161. return '<a href="' . esc_attr($previewUrl) . '" target="_blank" title="'
  162. . esc_attr(__('Preview in a new tab', 'mailpoet')) . '">'
  163. . esc_attr((string)$queue->getNewsletterRenderedSubject()) .
  164. '</a>';
  165. }
  166. }