PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.0
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.0
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
← All changes | front/FrontControllers/FronturlController.php +138 -0 10.1.1 → 11.1.0 View file →
@@ -1,0 +1,138 @@
1 +<?php
2 +
3 +namespace AcyMailing\FrontControllers;
4 +
5 +use AcyMailing\Classes\MailClass;
6 +use AcyMailing\Classes\MailStatClass;
7 +use AcyMailing\Classes\UrlClass;
8 +use AcyMailing\Classes\UrlClickClass;
9 +use AcyMailing\Classes\UserClass;
10 +use AcyMailing\Classes\UserStatClass;
11 +use AcyMailing\Core\AcymController;
12 +
13 +class FronturlController extends AcymController
14 +{
15 + public function __construct()
16 + {
17 + parent::__construct();
18 +
19 + $this->publicFrontTasks = [
20 + 'click',
21 + ];
22 + }
23 +
24 + public function click(): void
25 + {
26 + $urlId = acym_getVar('int', 'urlid');
27 + $mailId = acym_getVar('int', 'mailid');
28 + $userId = acym_getVar('int', 'userid');
29 +
30 + $urlClass = new UrlClass();
31 + $urlObject = $urlClass->getOneUrlById($urlId);
32 +
33 + if (empty($urlObject->id)) {
34 + acym_raiseError(404, acym_translation('ACYM_PAGE_NOT_FOUND'));
35 + }
36 +
37 + $urlObject->url = preg_replace(
38 + [
39 + '#&idU=[0-9]+#i',
40 + '#idU=[0-9]+&#i',
41 + '#\?idU=[0-9]+#i',
42 + ],
43 + '',
44 + $urlObject->url
45 + );
46 +
47 + $mailClass = new MailClass();
48 + $mail = $mailClass->getOneById($mailId);
49 +
50 + $userStatClass = new UserStatClass();
51 + $userStat = $userStatClass->getOneByMailAndUserId($mailId, $userId);
52 +
53 + // The mail has been deleted, or we didn't send this email to this user, or it's a bot
54 + $delay = $this->config->get('tracking_delay', 0);
55 + if (empty($mail) || empty($userStat) || acym_isRobot() || (!empty($delay) && acym_getTimeFromUTCDate($userStat->send_date) > time() - $delay)) {
56 + acym_redirect($this->resolveSubscriberTags($urlObject->url, $userId), '', 'message', false);
57 + }
58 +
59 + $anonymousStats = !empty($this->config->get('anonymous_stats', 0));
60 +
61 + $urlClick = new \stdClass();
62 + $urlClick->mail_id = $mailId;
63 + $urlClick->url_id = $urlObject->id;
64 + $urlClick->click = 1;
65 + $urlClick->user_id = $anonymousStats ? 0 : $userId;
66 + $urlClick->date_click = acym_date('now', 'Y-m-d H:i:s', false);
67 +
68 + $mailStatClass = new MailStatClass();
69 + $urlClickClass = new UrlClickClass();
70 + $urlClickClass->save($urlClick);
71 +
72 + UserStatClass::triggerActivity('user_click', $userId, $mailId);
73 +
74 + if (!$anonymousStats) {
75 + if (empty($userStat->open)) {
76 + $userStatToInsert = new \stdClass();
77 + $userStatToInsert->user_id = $userId;
78 + $userStatToInsert->mail_id = $mailId;
79 + $userStatToInsert->open = 1;
80 + $userStatToInsert->open_date = acym_date('now', 'Y-m-d H:i:s', false);
81 + $userStatClass->save($userStatToInsert);
82 +
83 + $mailStat = new \stdClass();
84 + $mailStat->mail_id = $mailId;
85 + $mailStat->open_unique = 1;
86 + $mailStat->open_total = 1;
87 + $mailStatClass->save($mailStat);
88 +
89 + UserStatClass::triggerActivity('user_open', $userId, $mailId);
90 + }
91 +
92 + $clickStats = $urlClickClass->getOneByMailIdAndUserId($mailId, $userId);
93 + $mailStatClass->incrementClicks($mailId, $clickStats->click == 1);
94 + } else {
95 + $mailStatClass->incrementClicks($mailId, true);
96 +
97 + // Anonymous mode tracks totals only and keeps no per-recipient open state, so this is not deduplicated.
98 + $mailStat = new \stdClass();
99 + $mailStat->mail_id = $mailId;
100 + $mailStat->open_unique = 1;
101 + $mailStat->open_total = 1;
102 + $mailStatClass->save($mailStat);
103 + $mailStatClass->incrementDetail($mailId, 'open_time', acym_date('now', 'Y-m-d H', false).':00:00');
104 + }
105 +
106 + $userClass = new UserClass();
107 + $subscriber = $userClass->getOneById($userId);
108 + if (!empty($subscriber)) {
109 + $subscriber->last_open_date = acym_date('now', 'Y-m-d H:i:s', false);
110 + $subscriber->last_click_date = acym_date('now', 'Y-m-d H:i:s', false);
111 + $userClass->triggers = false;
112 + $userClass->sendConf = false;
113 + $userClass->save($subscriber);
114 + }
115 +
116 + acym_redirect($this->resolveSubscriberTags($urlObject->url, $userId), '', 'message', false);
117 + }
118 +
119 + private function resolveSubscriberTags(string $url, int $userId): string
120 + {
121 + if (!preg_match('#\{|%7B#i', $url)) {
122 + return $url;
123 + }
124 +
125 + $userClass = new UserClass();
126 + $subscriber = $userClass->identify(true, 'userid', 'userkey');
127 +
128 + if (empty($subscriber) || (int)$subscriber->id !== $userId) {
129 + return $url;
130 + }
131 +
132 + $tempEmail = new \stdClass();
133 + $tempEmail->body = $url;
134 + acym_trigger('replaceUserInformation', [&$tempEmail, &$subscriber, true]);
135 +
136 + return $tempEmail->body;
137 + }
138 +}