Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
NewsletterLinks.php
152 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\Config\AccessControl; |
| 10 | use MailPoet\Cron\Workers\StatsNotifications\NewsletterLinkRepository; |
| 11 | use MailPoet\Entities\NewsletterEntity; |
| 12 | use MailPoet\Newsletter\Links\Links as NewsletterLinksService; |
| 13 | use MailPoet\Newsletter\NewslettersRepository; |
| 14 | |
| 15 | class NewsletterLinks extends APIEndpoint { |
| 16 | |
| 17 | public $permissions = [ |
| 18 | 'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS, |
| 19 | ]; |
| 20 | |
| 21 | private const AUTOMATION_EMAIL_TYPES = [ |
| 22 | NewsletterEntity::TYPE_AUTOMATION, |
| 23 | NewsletterEntity::TYPE_AUTOMATION_TRANSACTIONAL, |
| 24 | ]; |
| 25 | |
| 26 | /** @var NewsletterLinkRepository */ |
| 27 | private $newsletterLinkRepository; |
| 28 | |
| 29 | /** @var NewslettersRepository */ |
| 30 | private $newslettersRepository; |
| 31 | |
| 32 | /** @var NewsletterLinksService */ |
| 33 | private $newsletterLinks; |
| 34 | |
| 35 | public function __construct( |
| 36 | NewsletterLinkRepository $newsletterLinkRepository, |
| 37 | NewslettersRepository $newslettersRepository, |
| 38 | NewsletterLinksService $newsletterLinks |
| 39 | ) { |
| 40 | $this->newsletterLinkRepository = $newsletterLinkRepository; |
| 41 | $this->newslettersRepository = $newslettersRepository; |
| 42 | $this->newsletterLinks = $newsletterLinks; |
| 43 | } |
| 44 | |
| 45 | public function get($data = []) { |
| 46 | $newsletterId = (int)($data['newsletterId'] ?? 0); |
| 47 | $newsletter = $this->newslettersRepository->findOneById($newsletterId); |
| 48 | if (!$newsletter instanceof NewsletterEntity) { |
| 49 | return $this->successResponse([]); |
| 50 | } |
| 51 | |
| 52 | if (in_array($newsletter->getType(), self::AUTOMATION_EMAIL_TYPES, true)) { |
| 53 | return $this->successResponse($this->getAutomationLinks($newsletter)); |
| 54 | } |
| 55 | |
| 56 | $links = $this->newsletterLinkRepository->findBy(['newsletter' => $newsletterId]); |
| 57 | $response = []; |
| 58 | foreach ($links as $link) { |
| 59 | $response[] = [ |
| 60 | 'id' => $link->getId(), |
| 61 | 'url' => $link->getUrl(), |
| 62 | ]; |
| 63 | } |
| 64 | return $this->successResponse($response); |
| 65 | } |
| 66 | |
| 67 | private function getAutomationLinks(NewsletterEntity $newsletter): array { |
| 68 | $newsletterId = $newsletter->getId(); |
| 69 | if (!$newsletterId) { |
| 70 | return []; |
| 71 | } |
| 72 | |
| 73 | $urls = []; |
| 74 | foreach ($this->newsletterLinkRepository->findUrlsByNewsletterId($newsletterId) as $url) { |
| 75 | $this->addUrl($urls, $url); |
| 76 | } |
| 77 | foreach ($this->extractUrlsFromNewsletterBody($newsletter) as $url) { |
| 78 | $this->addUrl($urls, $url); |
| 79 | } |
| 80 | |
| 81 | return array_map(function(string $url): array { |
| 82 | return [ |
| 83 | 'id' => $url, |
| 84 | 'url' => $url, |
| 85 | ]; |
| 86 | }, array_values($urls)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return string[] |
| 91 | */ |
| 92 | private function extractUrlsFromNewsletterBody(NewsletterEntity $newsletter): array { |
| 93 | $body = $newsletter->getBody(); |
| 94 | if (!is_array($body)) { |
| 95 | return []; |
| 96 | } |
| 97 | |
| 98 | $urls = []; |
| 99 | $this->collectUrlsFromBody($body['content'] ?? $body, $urls); |
| 100 | return array_values($urls); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param mixed $bodyPart |
| 105 | * @param array<string, string> $urls |
| 106 | */ |
| 107 | private function collectUrlsFromBody($bodyPart, array &$urls): void { |
| 108 | if (!is_array($bodyPart)) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | foreach ($bodyPart as $key => $value) { |
| 113 | if (is_string($value)) { |
| 114 | if (in_array($key, ['url', 'link'], true)) { |
| 115 | $this->addUrl($urls, $value); |
| 116 | } |
| 117 | // Links::extract parses HTML and scans for shortcodes; skip when neither marker is present |
| 118 | // to avoid running a DOM parse on every plain-text leaf of the body tree. |
| 119 | if (strpos($value, '<') !== false || strpos($value, '[') !== false) { |
| 120 | foreach ($this->newsletterLinks->extract($value) as $link) { |
| 121 | $this->addUrl($urls, $link['link'] ?? ''); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (is_array($value)) { |
| 127 | $this->collectUrlsFromBody($value, $urls); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param array<string, string> $urls |
| 134 | */ |
| 135 | private function addUrl(array &$urls, string $url): void { |
| 136 | $url = trim($url); |
| 137 | if (!$this->isSelectableUrl($url)) { |
| 138 | return; |
| 139 | } |
| 140 | $urls[$url] = $url; |
| 141 | } |
| 142 | |
| 143 | private function isSelectableUrl(string $url): bool { |
| 144 | if ($url === '') { |
| 145 | return false; |
| 146 | } |
| 147 | // strpos returns false when '[' is absent and 0 when it's the first char; !== 0 covers both |
| 148 | // "no leading bracket" cases. Allow [link:…] shortcodes through; reject other tokens like [postLink]. |
| 149 | return strpos($url, '[') !== 0 || strpos($url, '[link:') === 0; |
| 150 | } |
| 151 | } |
| 152 |