PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.1
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 | back/Classes/OverrideClass.php +0 -227 11.0.1 → 11.1.1 View file →
@@ -1,227 +1,0 @@
1 -<?php
2 -
3 -namespace AcyMailing\Classes;
4 -
5 -use AcyMailing\Core\AcymClass;
6 -use AcyMailing\Helpers\PaginationHelper;
7 -
8 -class OverrideClass extends AcymClass
9 -{
10 - public function __construct()
11 - {
12 - parent::__construct();
13 -
14 - $this->table = 'mail_override';
15 - $this->pkey = 'id';
16 - }
17 -
18 - /**
19 - * Get mails depending on filters (search, ordering, pagination)
20 - */
21 - public function getMatchingElements(array $settings = []): array
22 - {
23 - $query = 'SELECT override.*, mail.name, mail.subject FROM #__acym_mail_override AS override JOIN #__acym_mail AS mail ON override.mail_id = mail.id';
24 - $queryCount = 'SELECT COUNT(override.mail_id) AS total, SUM(override.active) AS totalActive FROM #__acym_mail_override AS override INNER JOIN #__acym_mail AS mail ON override.mail_id = mail.id';
25 -
26 - $filters = [];
27 -
28 - if (!empty($settings['search'])) {
29 - $filters[] = 'mail.name LIKE '.acym_escapeDB('%'.$settings['search'].'%');
30 - }
31 -
32 - if (!empty($settings['source'])) {
33 - $filters[] = 'override.source LIKE '.acym_escapeDB('%'.$settings['source'].'%');
34 - }
35 -
36 - $filters[] = 'mail.parent_id IS NULL';
37 -
38 - if (!empty($filters)) {
39 - $query .= ' WHERE ('.implode(') AND (', $filters).')';
40 - $queryCount .= ' WHERE ('.implode(') AND (', $filters).')';
41 - }
42 -
43 - if (!empty($settings['status'])) {
44 - $query .= empty($filters) ? ' WHERE ' : ' AND ';
45 - $query .= 'active = '.($settings['status'] == 'active' ? '1' : '0');
46 - }
47 -
48 - if (!empty($settings['ordering']) && !empty($settings['ordering_sort_order'])) {
49 - $query .= ' ORDER BY override.'.acym_secureDBColumn($settings['ordering']).' '.acym_secureDBColumn(strtoupper($settings['ordering_sort_order']));
50 - $query .= ', override.mail_id ASC';
51 - }
52 -
53 - if (empty($settings['offset']) || $settings['offset'] < 0) {
54 - $settings['offset'] = 0;
55 - }
56 -
57 - if (empty($settings['elementsPerPage']) || $settings['elementsPerPage'] < 1) {
58 - $pagination = new PaginationHelper();
59 - $settings['elementsPerPage'] = $pagination->getListLimit();
60 - }
61 -
62 - $mailClass = new MailClass();
63 -
64 - return [
65 - 'elements' => $mailClass->decode(acym_loadObjectList($query, '', $settings['offset'], $settings['elementsPerPage'])),
66 - 'total' => acym_loadObject($queryCount),
67 - 'status' => [],
68 - ];
69 - }
70 -
71 - public function cleanEmailsOverride(): void
72 - {
73 - $overrideId = acym_loadResultArray('SELECT id FROM #__acym_mail_override');
74 - $this->delete($overrideId);
75 - }
76 -
77 - public function areOverrideMailsInstalled(): bool
78 - {
79 - return (bool)acym_loadResult('SELECT COUNT(id) FROM #__acym_mail_override');
80 - }
81 -
82 - public function getAllSources(): array
83 - {
84 - return acym_loadResultArray('SELECT DISTINCT `source` FROM #__acym_mail_override');
85 - }
86 -
87 - public function getActiveOverrides(string $key = ''): array
88 - {
89 - $query = 'SELECT override.*, mail.name FROM #__acym_mail_override AS override JOIN #__acym_mail AS mail ON mail.id = override.mail_id WHERE `active` = 1';
90 -
91 - return acym_loadObjectList($query, $key);
92 - }
93 -
94 - /**
95 - * Returns the override email corresponding to the base email content provided
96 - */
97 - public function getMailByBaseContent(string $subject, string $body): ?object
98 - {
99 - $translatepressIsActive = acym_isExtensionActive('translatepress-multilingual/index.php');
100 - $activeOverrides = $this->getActiveOverrides('name');
101 -
102 - $joomlaMailStyle = 'plaintext';
103 - if ('joomla' === ACYM_CMS) {
104 - acym_loadLanguageFile('com_contact');
105 - if (ACYM_J40) {
106 - $params = \Joomla\CMS\Component\ComponentHelper::getParams('com_mails');
107 - $joomlaMailStyle = $params->get('mail_style', 'plaintext');
108 - }
109 - }
110 -
111 - foreach ($activeOverrides as $oneOverride) {
112 - $parameters = [];
113 - $matches = true;
114 - foreach (['subject', 'body'] as $part) {
115 - $identifier = 'base_'.$part;
116 - if (empty($oneOverride->$identifier)) continue;
117 -
118 - // The identifier is the default email's text, for example "User coucou registered to the site https://www.acymailing.com"
119 - // We use it to check if the current email matches
120 - $decodedValue = json_decode($oneOverride->$identifier, true);
121 - if (empty($decodedValue)) continue;
122 -
123 - $oneOverride->$identifier = '';
124 - foreach ($decodedValue as $partialTrad) {
125 - $oneOverride->$identifier .= acym_translation($partialTrad, false, true, '');
126 - if ($oneOverride->source === 'woocommerce') {
127 - $oneOverride->$identifier = acym_translation($oneOverride->$identifier, false, true, 'woocommerce');
128 - if ($translatepressIsActive) {
129 - $oneOverride->$identifier = preg_replace('/#!trpst#trp-gettext data-trpgettextoriginal=\d+#!trpen#/i', '', $oneOverride->$identifier);
130 - $oneOverride->$identifier = preg_replace('/#!trpst#\/trp-gettext#!trpen#/i', '', $oneOverride->$identifier);
131 - }
132 - }
133 - }
134 -
135 - // Replace the %s / %1$s by (.*) to get the params from the email content
136 - // So: User %1$s registered to the site %2$s
137 - // Becomes: User (.*) registered to the site (.*)
138 - $oneOverride->$identifier = preg_replace(
139 - [
140 - '/%([0-9].?\$)?s/',
141 - '/\\\{[A-Z_]+\\\}/i',
142 - '/###[A-Z_-]+###/i',
143 - '/\\\#\\\#\\\#[A-Z_-]+\\\#\\\#\\\#/i',
144 - ],
145 - '(.*)',
146 - preg_quote($oneOverride->$identifier, '/')
147 - );
148 -
149 - $oneOverride->$identifier = str_replace('&amp;', '&', $oneOverride->$identifier);
150 -
151 - if ('plaintext' !== $joomlaMailStyle) {
152 - $$part = str_replace('<br>', '', $$part);
153 - }
154 - $$part = str_replace("\r\n", "\n", $$part);
155 -
156 - $matches = preg_match('/'.trim($oneOverride->$identifier).'/', $$part, $params) === 1 && $matches;
157 -
158 - if (empty($parameters)) {
159 - $parameters = $params;
160 - } else {
161 - for ($i = 1; $i < count($params); $i++) {
162 - $parameters[] = trim($params[$i]);
163 - }
164 - }
165 - }
166 -
167 - // The content didn't match the identifier
168 - if (!$matches) continue;
169 -
170 - // We found the override
171 - $mailClass = new MailClass();
172 - $mail = $mailClass->getOneById($oneOverride->mail_id);
173 -
174 - if ($oneOverride->source == 'woocommerce' && $translatepressIsActive) {
175 - $subjectTranslated = $params[0];
176 - for ($i = 1; $i < count($params); $i++) {
177 - $pattern = '/'.$params[$i].'/i';
178 - $param = '{param'.$i.'}';
179 - $subjectTranslated = preg_replace($pattern, $param, $subjectTranslated);
180 - }
181 - $mail->subject = $subjectTranslated;
182 - }
183 -
184 - // Include the found parameters
185 - $mail->parameters = $parameters;
186 -
187 - if (empty($oneOverride->base_body)) {
188 - $mail->body = $body;
189 - }
190 -
191 - return $mail;
192 - }
193 -
194 - return null;
195 - }
196 -
197 - public function delete(array $elements): int
198 - {
199 - if (empty($elements)) return 0;
200 -
201 - acym_arrayToInteger($elements);
202 - $mailIds = acym_loadResultArray('SELECT `mail_id` FROM #__acym_mail_override WHERE `id` IN ('.implode(',', $elements).')');
203 -
204 - $result = parent::delete($elements);
205 -
206 - $mailClass = new MailClass();
207 - $mailClass->delete($mailIds);
208 -
209 - return $result;
210 - }
211 -
212 - public function getParamsByMailId(?int $mailId): array
213 - {
214 - if (empty($mailId)) {
215 - return [];
216 - }
217 -
218 - $mailClass = new MailClass();
219 - $mail = $mailClass->getOneById($mailId);
220 -
221 - if (empty($mail)) {
222 - return [];
223 - }
224 -
225 - return acym_getOverrideParamsByName($mail->name);
226 - }
227 -}