PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.4
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.4
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.4, at back/Controllers/Users/Edition.php

371 lines 13.2 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
137 if (empty($userId)) return;
138
139 $userStatClass = new UserStatClass();
140 $userStatFromDB = $userStatClass->getAllUserStatByUserId($userId);
141
142 if (empty($userStatFromDB)) return;
143
144 $userStat = new \stdClass();
145 $userStat->totalSent = 0;
146 $userStat->open = 0;
147 $userStat->click = 0;
148
149 foreach ($userStatFromDB as $oneStat) {
150 if ($oneStat->sent > 0) $userStat->totalSent++;
151 if ($oneStat->open > 0) $userStat->open++;
152 }
153
154 $urlClickClass = new UrlClickClass();
155 $clickStats = $urlClickClass->getUserMailsClicked($userId);
156
157 foreach ($clickStats as $oneStat) {
158 if (!empty($oneStat->click)) {
159 $userStat->click++;
160 }
161 }
162
163 $userStat->percentageOpen = empty($userStat->open) || empty($userStat->totalSent) ? 0 : intval(($userStat->open * 100) / $userStat->totalSent);
164 $userStat->percentageClick = empty($userStat->click) || empty($userStat->totalSent) ? 0 : intval(($userStat->click * 100) / $userStat->totalSent);
165
166 $data['percentageOpen'] = number_format($userStat->percentageOpen, 2);
167 $data['percentageClick'] = number_format($userStat->percentageClick, 2);
168 }
169
170 private function prepareMailHistory(array &$data, int $userId): void
171 {
172 if (empty($userId)) {
173 return;
174 }
175
176 $userClass = new UserClass();
177 $data['userMailHistory'] = $userClass->getMailHistory($userId);
178 foreach ($data['userMailHistory'] as $mailId => $oneMailHistory) {
179 if (!empty($oneMailHistory->bounce_rule) && preg_match('/(.*) \[(.*)]/Us', $oneMailHistory->bounce_rule, $matches)) {
180 $ruleName = acym_translation($matches[1]).' ['.$matches[2].']';
181 } else {
182 $ruleName = $data['userMailHistory'][$mailId]->bounce_rule;
183 }
184 $data['userMailHistory'][$mailId]->ruleName = $ruleName;
185 }
186 }
187
188 private function prepareHistoryEdit(array &$data, int $userId): void
189 {
190 if (empty($userId)) {
191 return;
192 }
193
194 $historyClass = new HistoryClass();
195 $data['userHistory'] = $historyClass->getHistoryOfOneById($userId);
196 $data['unsubReasons'] = $historyClass->getAllMainLanguageUnsubReasons();
197 $data['historyClass'] = $historyClass;
198 }
199
200 protected function prepareFieldsEdit(array &$data, string $fieldVisibility = 'backend_edition'): void
201 {
202 $data['allFields'] = [];
203
204 $fieldClass = new FieldClass();
205 $fieldsElements = $fieldClass->getMatchingElements();
206 $allFields = $fieldsElements['elements'];
207 $languageFieldId = $fieldClass->getLanguageFieldId();
208
209 foreach ($allFields as $one) {
210 $one->option = empty($one->option) ? new \stdClass() : json_decode($one->option);
211 $one->value = empty($one->value) ? '' : json_decode($one->value);
212 $fieldDB = empty($one->option->fieldDB) ? '' : json_decode($one->option->fieldDB);
213
214 // Keep this code, search for data-display-optional for more info
215 //$displayIf = empty($one->option->display) ? '' : 'data-display-optional="'.esc_attr($one->option->display).'"';
216
217 $valuesArray = [];
218 if (!empty($one->value)) {
219 foreach ($one->value as $value) {
220 $valueTmp = new \stdClass();
221 $valueTmp->text = $value->title;
222 $valueTmp->value = $value->value;
223 if ($value->disabled == 'y') $valueTmp->disable = true;
224 $valuesArray[$value->value] = $valueTmp;
225 }
226 }
227 if (!empty($fieldDB) && !empty($fieldDB->value)) {
228 $fromDB = $fieldClass->getValueFromDB($fieldDB);
229 foreach ($fromDB as $value) {
230 $valuesArray[$value->value] = $value->title;
231 }
232 }
233
234 $one->display = empty($one->option->display) ? '' : json_decode($one->option->display);
235 $data['allFields'][$one->id] = $one;
236 if ($one->id == 1) {
237 $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->name;
238 } elseif ($one->id == 2) {
239 $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->email;
240 } elseif ($one->id == $languageFieldId) {
241 $defaultValue = empty($data['user-information']->id) ? acym_getLanguageTag() : $data['user-information']->language;
242 } elseif (
243 isset($data['fieldsValues'][$one->id])
244 && (
245 (
246 (
247 is_array($data['fieldsValues'][$one->id])
248 || $data['fieldsValues'][$one->id] instanceof \Countable
249 )
250 && count($data['fieldsValues'][$one->id]) > 0
251 )
252 || (
253 is_string($data['fieldsValues'][$one->id])
254 && strlen($data['fieldsValues'][$one->id]) > 0
255 )
256 )
257 ) {
258 $decoded = json_decode($data['fieldsValues'][$one->id]);
259 $defaultValue = is_array($decoded) ? $decoded : $data['fieldsValues'][$one->id];
260 } else {
261 $defaultValue = $one->default_value;
262 }
263
264 ob_start();
265 $fieldClass->displayField(
266 $one,
267 $defaultValue,
268 $valuesArray,
269 true,
270 !acym_isAdmin(),
271 $data['user-information'],
272 $one->$fieldVisibility != 0
273 );
274 $data['allFields'][$one->id]->html = ob_get_clean();
275 }
276 }
277
278 public function save(): void
279 {
280 $this->apply(true);
281 }
282
283 public function apply(bool $listing = false): void
284 {
285 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
286
287 $userInformation = acym_getVar('array', 'user');
288
289 $user = new \stdClass();
290 $user->name = $userInformation['name'];
291 $user->email = $userInformation['email'];
292 if (!empty($userInformation['language'])) {
293 $user->language = $userInformation['language'];
294 }
295 $user->active = $userInformation['active'];
296 if ($this->config->get('require_confirmation', '0') == '1') {
297 $user->confirmed = $userInformation['confirmed'];
298 }
299 $user->tracking = $userInformation['tracking'];
300 $customFields = acym_getVar('array', 'customField', []);
301
302 preg_match('/'.acym_getEmailRegex().'/i', $user->email, $matches);
303
304 if (empty($matches)) {
305 $this->edit();
306 acym_enqueueMessage(acym_translationSprintf('ACYM_VALID_EMAIL', $user->email), 'error');
307
308 return;
309 }
310
311 $userClass = new UserClass();
312 $frontCreation = false;
313 $existingUser = $userClass->getOneByEmail($user->email);
314 $userId = acym_getVar('int', 'userId');
315
316 if (empty($userId)) {
317 if (!empty($existingUser) && acym_isAdmin()) {
318 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
319
320 $this->edit();
321
322 return;
323 } elseif (!empty($existingUser)) {
324 $userId = $existingUser->id;
325 } else {
326 $user->creation_date = acym_date('now', 'Y-m-d H:i:s', false);
327 $userClass->checkVisitor = false;
328 $userId = $userClass->save($user, $customFields);
329 if (empty($userId)) {
330 acym_enqueueMessage(empty($userClass->errors) ? acym_translation('ACYM_ERROR') : $userClass->errors, 'error');
331 $this->edit();
332
333 return;
334 }
335 $frontCreation = true;
336 }
337 acym_setVar('userId', $userId);
338 } else {
339 if (!empty($existingUser) && $existingUser->id != $userId) {
340 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
341 $this->edit();
342
343 return;
344 }
345
346 if (!$userClass->hasUserAccess($userId)) {
347 die('Access denied for the modification of this user');
348 }
349
350 $user->id = $userId;
351 $userClass->save($user, $customFields);
352 }
353
354 $listsToAdd = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true);
355 if (!empty($listsToAdd)) {
356 $this->subscribeUser(false, $listsToAdd, $frontCreation);
357 }
358
359 $listsToUnsub = json_decode(acym_getVar('string', 'acym__entity_select__unselected', '[]'), true);
360 if (!empty($listsToUnsub)) {
361 $userClass->unsubscribeOnSubscriptions($userId, $listsToUnsub);
362 }
363
364 if ($listing) {
365 $this->listing();
366 } else {
367 $this->edit();
368 }
369 }
370 }
371