PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.5
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.5
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
acymailing / back / Controllers / Users / Edition.php

Edition.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.5, at back/Controllers/Users/Edition.php

373 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Users;
4
5 use AcyMailing\Classes\FieldClass;
6 use AcyMailing\Classes\HistoryClass;
7 use AcyMailing\Classes\UrlClickClass;
8 use AcyMailing\Classes\UserClass;
9 use AcyMailing\Classes\UserStatClass;
10 use AcyMailing\Helpers\EntitySelectHelper;
11 use AcyMailing\Helpers\TabHelper;
12
13 trait Edition
14 {
15 public function edit(): void
16 {
17 acym_setVar('layout', 'edit');
18
19 $data = [];
20 $data['tab'] = new TabHelper();
21
22 $userId = acym_getVar('int', 'userId', 0);
23
24 if (!$this->prepareUserEdit($data, $userId)) return;
25 $this->prepareEntitySelectEdit($data, $userId);
26 $this->prepareUserFieldsEdit($data, $userId);
27 $this->prepareSubscriptionsEdit($data, $userId);
28 $this->prepareStatsEdit($data, $userId);
29 $this->prepareHistoryEdit($data, $userId);
30 $this->prepareMailHistory($data, $userId);
31 $this->prepareFieldsEdit($data);
32
33 parent::display($data);
34 }
35
36 private function prepareUserEdit(array &$data, int $userId): bool
37 {
38 if (empty($userId)) {
39 $data['user-information'] = new \stdClass();
40 $data['user-information']->name = '';
41 $data['user-information']->email = '';
42 $data['user-information']->active = '1';
43 $data['user-information']->confirmed = '1';
44 $data['user-information']->cms_id = null;
45 $data['user-information']->tracking = 1;
46 $data['user-information']->language = '';
47
48 $this->breadcrumb[acym_translation('ACYM_NEW_SUBSCRIBER')] = acym_completeLink('users&task=edit');
49 } else {
50 $userClass = new UserClass();
51 $data['user-information'] = $userClass->getOneById($userId);
52
53 if (empty($data['user-information']) || !$userClass->hasUserAccess($userId)) {
54 acym_enqueueMessage(acym_translation('ACYM_USER_NOT_FOUND'), 'error');
55 $this->listing();
56
57 return false;
58 }
59
60 $this->breadcrumb[$data['user-information']->email] = acym_completeLink('users&task=edit&userId='.$userId);
61 }
62
63 if (empty($data['user-information']->language)) {
64 $data['user-information']->language = acym_getLanguageTag();
65 }
66
67 return true;
68 }
69
70 private function prepareEntitySelectEdit(array &$data, int $userId): void
71 {
72 if (empty($userId)) {
73 return;
74 }
75
76 $entityHelper = new EntitySelectHelper();
77
78 $data['entityselectContent'] = $entityHelper->entitySelect(
79 [
80 'entity' => 'list',
81 'entityParams' => ['join' => 'join_user-'.$userId],
82 'columnsToDisplay' => $entityHelper->getColumnsForList('userlist.list_id'),
83 'buttonSubmit' => ['text' => acym_translation('ACYM_CONFIRM'), 'action' => 'apply'],
84 ]
85 );
86 }
87
88 private function prepareUserFieldsEdit(array &$data, int $userId): void
89 {
90 $data['fieldsValues'] = [];
91
92 if (empty($userId)) return;
93
94 $fieldClass = new FieldClass();
95 $fieldsValues = $fieldClass->getFieldsValueByUserId($userId);
96 foreach ($fieldsValues as $one) {
97 $data['fieldsValues'][$one->field_id] = $one->value;
98 }
99 }
100
101 private function prepareSubscriptionsEdit(array &$data, int $userId): void
102 {
103 $data['subscriptionsIds'] = [];
104 $data['subscriptions'] = [];
105 $data['unsubscribe'] = [];
106
107 if (empty($userId)) return;
108
109 $userClass = new UserClass();
110 $data['allSubscriptions'] = $userClass->getUserSubscriptionById($userId);
111
112 foreach ($data['allSubscriptions'] as $sub) {
113 if ($sub->status == 1) {
114 $data['subscriptions'][] = $sub;
115 } else {
116 $data['unsubscribe'][] = $sub;
117 }
118 }
119
120 $data['subscriptionsIds'] = [];
121
122 if (!empty($data['subscriptions'])) {
123 $data['subscriptionsIds'] = [];
124 foreach ($data['subscriptions'] as $list) {
125 $data['subscriptionsIds'][] = $list->id;
126 }
127
128 acym_arrayToInteger($data['subscriptionsIds']);
129 }
130 }
131
132 private function prepareStatsEdit(array &$data, int $userId): void
133 {
134 $data['percentageOpen'] = 0;
135 $data['percentageClick'] = 0;
136 $data['anonymousStats'] = !empty($this->config->get('anonymous_stats', 0));
137
138 // Individual open/click rates are an individual statistic: don't compute them when anonymizing
139 if (empty($userId) || $data['anonymousStats']) return;
140
141 $userStatClass = new UserStatClass();
142 $userStatFromDB = $userStatClass->getAllUserStatByUserId($userId);
143
144 if (empty($userStatFromDB)) return;
145
146 $userStat = new \stdClass();
147 $userStat->totalSent = 0;
148 $userStat->open = 0;
149 $userStat->click = 0;
150
151 foreach ($userStatFromDB as $oneStat) {
152 if ($oneStat->sent > 0) $userStat->totalSent++;
153 if ($oneStat->open > 0) $userStat->open++;
154 }
155
156 $urlClickClass = new UrlClickClass();
157 $clickStats = $urlClickClass->getUserMailsClicked($userId);
158
159 foreach ($clickStats as $oneStat) {
160 if (!empty($oneStat->click)) {
161 $userStat->click++;
162 }
163 }
164
165 $userStat->percentageOpen = empty($userStat->open) || empty($userStat->totalSent) ? 0 : intval(($userStat->open * 100) / $userStat->totalSent);
166 $userStat->percentageClick = empty($userStat->click) || empty($userStat->totalSent) ? 0 : intval(($userStat->click * 100) / $userStat->totalSent);
167
168 $data['percentageOpen'] = number_format($userStat->percentageOpen, 2);
169 $data['percentageClick'] = number_format($userStat->percentageClick, 2);
170 }
171
172 private function prepareMailHistory(array &$data, int $userId): void
173 {
174 if (empty($userId)) {
175 return;
176 }
177
178 $userClass = new UserClass();
179 $data['userMailHistory'] = $userClass->getMailHistory($userId);
180 foreach ($data['userMailHistory'] as $mailId => $oneMailHistory) {
181 if (!empty($oneMailHistory->bounce_rule) && preg_match('/(.*) \[(.*)]/Us', $oneMailHistory->bounce_rule, $matches)) {
182 $ruleName = acym_translation($matches[1]).' ['.$matches[2].']';
183 } else {
184 $ruleName = $data['userMailHistory'][$mailId]->bounce_rule;
185 }
186 $data['userMailHistory'][$mailId]->ruleName = $ruleName;
187 }
188 }
189
190 private function prepareHistoryEdit(array &$data, int $userId): void
191 {
192 if (empty($userId)) {
193 return;
194 }
195
196 $historyClass = new HistoryClass();
197 $data['userHistory'] = $historyClass->getHistoryOfOneById($userId);
198 $data['unsubReasons'] = $historyClass->getAllMainLanguageUnsubReasons();
199 $data['historyClass'] = $historyClass;
200 }
201
202 protected function prepareFieldsEdit(array &$data, string $fieldVisibility = 'backend_edition'): void
203 {
204 $data['allFields'] = [];
205
206 $fieldClass = new FieldClass();
207 $fieldsElements = $fieldClass->getMatchingElements();
208 $allFields = $fieldsElements['elements'];
209 $languageFieldId = $fieldClass->getLanguageFieldId();
210
211 foreach ($allFields as $one) {
212 $one->option = empty($one->option) ? new \stdClass() : json_decode($one->option);
213 $one->value = empty($one->value) ? '' : json_decode($one->value);
214 $fieldDB = empty($one->option->fieldDB) ? '' : json_decode($one->option->fieldDB);
215
216 // Keep this code, search for data-display-optional for more info
217 //$displayIf = empty($one->option->display) ? '' : 'data-display-optional="'.esc_attr($one->option->display).'"';
218
219 $valuesArray = [];
220 if (!empty($one->value)) {
221 foreach ($one->value as $value) {
222 $valueTmp = new \stdClass();
223 $valueTmp->text = $value->title;
224 $valueTmp->value = $value->value;
225 if ($value->disabled == 'y') $valueTmp->disable = true;
226 $valuesArray[$value->value] = $valueTmp;
227 }
228 }
229 if (!empty($fieldDB) && !empty($fieldDB->value)) {
230 $fromDB = $fieldClass->getValueFromDB($fieldDB);
231 foreach ($fromDB as $value) {
232 $valuesArray[$value->value] = $value->title;
233 }
234 }
235
236 $one->display = empty($one->option->display) ? '' : json_decode($one->option->display);
237 $data['allFields'][$one->id] = $one;
238 if ($one->id == 1) {
239 $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->name;
240 } elseif ($one->id == 2) {
241 $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->email;
242 } elseif ($one->id == $languageFieldId) {
243 $defaultValue = empty($data['user-information']->id) ? acym_getLanguageTag() : $data['user-information']->language;
244 } elseif (
245 isset($data['fieldsValues'][$one->id])
246 && (
247 (
248 (
249 is_array($data['fieldsValues'][$one->id])
250 || $data['fieldsValues'][$one->id] instanceof \Countable
251 )
252 && count($data['fieldsValues'][$one->id]) > 0
253 )
254 || (
255 is_string($data['fieldsValues'][$one->id])
256 && strlen($data['fieldsValues'][$one->id]) > 0
257 )
258 )
259 ) {
260 $decoded = json_decode($data['fieldsValues'][$one->id]);
261 $defaultValue = is_array($decoded) ? $decoded : $data['fieldsValues'][$one->id];
262 } else {
263 $defaultValue = $one->default_value;
264 }
265
266 ob_start();
267 $fieldClass->displayField(
268 $one,
269 $defaultValue,
270 $valuesArray,
271 true,
272 !acym_isAdmin(),
273 $data['user-information'],
274 $one->$fieldVisibility != 0
275 );
276 $data['allFields'][$one->id]->html = ob_get_clean();
277 }
278 }
279
280 public function save(): void
281 {
282 $this->apply(true);
283 }
284
285 public function apply(bool $listing = false): void
286 {
287 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
288
289 $userInformation = acym_getVar('array', 'user');
290
291 $user = new \stdClass();
292 $user->name = $userInformation['name'];
293 $user->email = $userInformation['email'];
294 if (!empty($userInformation['language'])) {
295 $user->language = $userInformation['language'];
296 }
297 $user->active = $userInformation['active'];
298 if ($this->config->get('require_confirmation', '0') == '1') {
299 $user->confirmed = $userInformation['confirmed'];
300 }
301 $user->tracking = $userInformation['tracking'];
302 $customFields = acym_getVar('array', 'customField', []);
303
304 preg_match('/'.acym_getEmailRegex().'/i', $user->email, $matches);
305
306 if (empty($matches)) {
307 $this->edit();
308 acym_enqueueMessage(acym_translationSprintf('ACYM_VALID_EMAIL', $user->email), 'error');
309
310 return;
311 }
312
313 $userClass = new UserClass();
314 $frontCreation = false;
315 $existingUser = $userClass->getOneByEmail($user->email);
316 $userId = acym_getVar('int', 'userId');
317
318 if (empty($userId)) {
319 if (!empty($existingUser) && acym_isAdmin()) {
320 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
321
322 $this->edit();
323
324 return;
325 } elseif (!empty($existingUser)) {
326 $userId = $existingUser->id;
327 } else {
328 $user->creation_date = acym_date('now', 'Y-m-d H:i:s', false);
329 $userClass->checkVisitor = false;
330 $userId = $userClass->save($user, $customFields);
331 if (empty($userId)) {
332 acym_enqueueMessage(empty($userClass->errors) ? acym_translation('ACYM_ERROR') : $userClass->errors, 'error');
333 $this->edit();
334
335 return;
336 }
337 $frontCreation = true;
338 }
339 acym_setVar('userId', $userId);
340 } else {
341 if (!empty($existingUser) && $existingUser->id != $userId) {
342 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
343 $this->edit();
344
345 return;
346 }
347
348 if (!$userClass->hasUserAccess($userId)) {
349 die('Access denied for the modification of this user');
350 }
351
352 $user->id = $userId;
353 $userClass->save($user, $customFields);
354 }
355
356 $listsToAdd = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true);
357 if (!empty($listsToAdd)) {
358 $this->subscribeUser(false, $listsToAdd, $frontCreation);
359 }
360
361 $listsToUnsub = json_decode(acym_getVar('string', 'acym__entity_select__unselected', '[]'), true);
362 if (!empty($listsToUnsub)) {
363 $userClass->unsubscribeOnSubscriptions($userId, $listsToUnsub);
364 }
365
366 if ($listing) {
367 $this->listing();
368 } else {
369 $this->edit();
370 }
371 }
372 }
373