PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
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 / Segments / Edition.php

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

239 lines 8.9 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\Segments;
4
5 use AcyMailing\Classes\SegmentClass;
6 use AcyMailing\Helpers\AutomationHelper;
7
8 trait Edition
9 {
10 public function edit(): void
11 {
12 if (!acym_level(ACYM_ENTERPRISE)) {
13 acym_redirect(acym_completeLink('dashboard&task=upgrade&version=enterprise', false, true));
14 }
15
16 }
17
18 public function apply(): void
19 {
20 $segmentId = $this->store();
21 if (empty($segmentId)) {
22 acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_SAVE_SEGMENT'), 'error');
23 $this->listing();
24 } else {
25 acym_enqueueMessage(acym_translation('ACYM_SEGMENT_WELL_SAVE'));
26 acym_setVar('segmentId', $segmentId);
27 $this->edit();
28 }
29 }
30
31 public function save(): void
32 {
33 $segmentId = $this->store();
34 acym_enqueueMessage(acym_translation(empty($segmentId) ? 'ACYM_COULD_NOT_SAVE_SEGMENT' : 'ACYM_SEGMENT_WELL_SAVE'), empty($segmentId) ? 'error' : 'success');
35 $this->listing();
36 }
37
38 private function store(): int
39 {
40 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
41
42 $segmentId = acym_getVar('int', 'segmentId');
43 $filters = acym_getVar('array', 'acym_action');
44 $segmentRequest = acym_getVar('array', 'segment');
45
46 if (acym_conditionsContainRestrictedFilter($filters) && !acym_hasAdminPermissions()) {
47 acym_enqueueMessage(acym_translation('ACYM_ACCESS_DENIED'), 'error');
48
49 return 0;
50 }
51
52 $segmentClass = new SegmentClass();
53
54 if (empty($segmentId)) {
55 $segment = new \stdClass();
56 $segment->creation_date = acym_date('now', 'Y-m-d H:i:s');
57 } else {
58 $segment = $segmentClass->getOneById($segmentId);
59 if (empty($segment)) {
60 return 0;
61 }
62 }
63
64 $segment->name = $segmentRequest['name'];
65 $segment->active = $segmentRequest['active'];
66 $segment->filters = json_encode($filters['filters'], JSON_FORCE_OBJECT);
67
68 $segmentId = $segmentClass->save($segment);
69
70 return empty($segmentId) ? 0 : $segmentId;
71 }
72
73 public function countResultsTotal(): void
74 {
75 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
76
77 $segmentSelected = acym_getVar('int', 'segment_selected');
78 if (empty($segmentSelected)) {
79 $stepAutomation = acym_getVar('array', 'acym_action', []);
80 acym_blockRestrictedFilterAjax($stepAutomation);
81 } else {
82 $segmentClass = new SegmentClass();
83 $segment = $segmentClass->getOneById($segmentSelected);
84 if (empty($segment)) {
85 $stepAutomation = [];
86 } else {
87 $stepAutomation = ['filters' => $segment->filters];
88 }
89 }
90
91 //if we are in the campaign edition
92 $listsIds = acym_getVar('string', 'list_selected', '[]');
93 $listsIds = empty($listsIds) ? [] : json_decode($listsIds, true);
94
95 $isExclude = acym_getVar('boolean', 'exclude', false);
96 acym_sendAjaxResponse($this->countSegmentByParams($stepAutomation, $listsIds, $isExclude));
97 }
98
99 public function countSegmentByParams(array $segment, array $listsIds, bool $exclude = false): int
100 {
101 $automationHelpers = [];
102
103 if (!empty($segment) && !empty($segment['filters'])) {
104 foreach ($segment['filters'] as $or => $orValues) {
105 if (empty($orValues)) continue;
106
107 $automationHelpers[$or] = new AutomationHelper();
108 foreach ($orValues as $and => $andValues) {
109 $and = intval($and);
110 foreach ($andValues as $filterName => $options) {
111 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
112 }
113 }
114
115 $automationHelpers[$or]->excludeSelected = $exclude;
116 $automationHelpers[$or]->addFlag(self::FLAG_COUNT);
117 }
118 }
119
120 if (!empty($listsIds)) {
121 acym_arrayToInteger($listsIds);
122
123 $join = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id ';
124 $join .= 'AND user_list.list_id IN ('.implode(',', $listsIds).') ';
125 $join .= 'AND user_list.status = 1 ';
126 $join .= 'AND user.active = 1 ';
127 if ($this->config->get('require_confirmation', 1) == 1) {
128 $join .= 'AND user.confirmed = 1 ';
129 }
130 }
131
132 if (empty($automationHelpers)) {
133 $automationHelperBase = new AutomationHelper();
134 if (!empty($listsIds)) {
135 $automationHelperBase->join['user_list'] = $join;
136 }
137
138 return acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
139 } else {
140 $automationHelperBase = array_pop($automationHelpers);
141 if (!empty($listsIds)) {
142 $automationHelperBase->join['user_list'] = $join;
143 }
144 $numberOfRecipients = acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
145 $automationHelperBase->removeFlag(self::FLAG_COUNT);
146
147 return $numberOfRecipients;
148 }
149 }
150
151 public function countResults(): void
152 {
153 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
154
155 $and = acym_getVar('int', 'and');
156 $or = acym_getVar('int', 'or');
157 $stepAutomation = acym_getVar('array', 'acym_action');
158 acym_blockRestrictedFilterAjax($stepAutomation);
159
160 //if we are in the campaign edition
161 $listsIds = acym_getVar('string', 'list_selected', '');
162
163 if (empty($stepAutomation['filters'][$or][$and])) {
164 acym_sendAjaxResponse(acym_translation('ACYM_AUTOMATION_NOT_FOUND'), [], false);
165 }
166
167 $automationHelper = new AutomationHelper();
168
169 if (!empty($listsIds)) {
170 $listsIds = json_decode($listsIds);
171 acym_arrayToInteger($listsIds);
172 $join = $this->config->get('require_confirmation', 1) == 1 ? ' AND user.confirmed = 1' : '';
173 $automationHelper->join['user_list'] = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id AND user_list.list_id IN ('.implode(
174 ',',
175 $listsIds
176 ).') and user_list.status = 1 '.$join;
177 }
178
179 $filterName = key($stepAutomation['filters'][$or][$and]);
180 $options = current($stepAutomation['filters'][$or][$and]);
181 $messages = acym_trigger('onAcymProcessFilterCount_'.$filterName, [&$automationHelper, &$options, &$and]);
182 $messages = $messages[0];
183
184 if (!empty($listsIds)) {
185 ob_start();
186 acym_info(['textShownInTooltip' => 'ACYM_CONDITION_WITH_LISTS_COUNT', 'classText' => 'wysid_tooltip']);
187 $messages .= ob_get_clean();
188 }
189
190 acym_sendAjaxResponse($messages);
191 }
192
193 public function usersSummary(): void
194 {
195 $offset = acym_getVar('int', 'offset', 0);
196 $limit = acym_getVar('int', 'limit', 50);
197 $search = acym_getVar('string', 'modal_search', '');
198
199 $and = acym_getVar('int', 'and');
200 $or = acym_getVar('int', 'or');
201 $stepAutomation = acym_getVar('array', 'acym_action');
202 acym_blockRestrictedFilterAjax($stepAutomation);
203
204 if (empty($stepAutomation['filters'][$or][$and])) {
205 acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_RETRIEVE_DATA'), [], false);
206 }
207
208 $automationHelper = new AutomationHelper();
209
210 //if we are in the campaign edition
211 $listsIds = acym_getVar('string', 'list_selected', '');
212
213 if (!empty($listsIds)) {
214 $listsIds = json_decode($listsIds);
215 acym_arrayToInteger($listsIds);
216 $join = $this->config->get('require_confirmation', 1) == 1 ? ' AND user.confirmed = 1' : '';
217 $automationHelper->join['user_list'] = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id AND user_list.list_id IN ('.implode(
218 ',',
219 $listsIds
220 ).') and user_list.status = 1 '.$join;
221 }
222
223 $filterName = key($stepAutomation['filters'][$or][$and]);
224 $options = current($stepAutomation['filters'][$or][$and]);
225 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelper, &$options, &$and]);
226
227 if (!empty($search)) {
228 $search = acym_escapeDB('%'.$search.'%');
229 $automationHelper->where[] = ' (user.email LIKE '.$search.' OR user.name LIKE '.$search.' OR user.id LIKE '.$search.') ';
230 }
231
232 $automationHelper->limit = intval($offset).', '.intval($limit);
233
234 $users = acym_loadObjectList($automationHelper->getQuery(['user.email', 'user.name', 'user.id']), 'id');
235
236 acym_sendAjaxResponse('', ['users' => $users]);
237 }
238 }
239