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 | back/Classes/ScenarioClass.php +59 -22 10.1.4 → 11.1.0 View file →
@@ -6,16 +6,17 @@
6 6 use AcyMailing\Core\AcymClass;
7 7
8 8 class ScenarioClass extends AcymClass
9 9 {
10 - private ScenarioStepClass $scenarioStepClass;
11 - private ScenarioHistoryLineClass $scenarioHistoryLineClass;
12 10 const TYPE_DELAY = 'delay';
13 11 const TYPE_CONDITION = 'condition';
14 12 const TYPE_ACTION = 'action';
15 13
16 - private $stepIdsToKeep = [];
14 + private ScenarioStepClass $scenarioStepClass;
17 15
16 + // This is a list of all the step ids we have to save when saving a scenario, this way we can delete all the steps that are not in this list
17 + private array $stepIdsToKeep = [];
18 +
18 19 public function __construct()
19 20 {
20 21 parent::__construct();
21 22
@@ -21,9 +22,8 @@
21 22
22 23 $this->table = 'scenario';
23 24 $this->pkey = 'id';
24 25 $this->scenarioStepClass = new ScenarioStepClass();
25 - $this->scenarioHistoryLineClass = new ScenarioHistoryLineClass();
26 26 }
27 27
28 28 public function getMatchingElements(array $settings = []): array
29 29 {
@@ -50,8 +50,9 @@
50 50 $filters[] = 'scenario.active = 0';
51 51 }
52 52 }
53 53
54 + // We don't filter by status on the status query this way we can select other statuses
54 55 if (!empty($filters)) {
55 56 $query .= ' WHERE ('.implode(') AND (', $filters).')';
56 57 $queryCount .= ' WHERE ('.implode(') AND (', $filters).')';
57 58 }
@@ -76,14 +77,15 @@
76 77 'scenariosNumberStatus' => acym_loadObjectList($queryStatus)[0],
77 78 ];
78 79 }
79 80
80 - public function save($element): int
81 + public function save(object $element): ?int
81 82 {
82 - $flow = '';
83 + $flow = [];
83 84 if (!empty($element->flow)) {
84 85 $flow = json_decode($element->flow, true);
85 86
87 + // The first step should the trigger
86 88 if (empty($flow[0]['params'])) {
87 89 return 0;
88 90 }
89 91
@@ -104,9 +106,9 @@
104 106 unset($element->flow);
105 107
106 108 $element->id = parent::save($element);
107 109
108 - if (!empty($flow)) {
110 + if (!empty($flow) && !empty($element->id)) {
109 111 $this->saveFlow($flow, $element->id);
110 112 }
111 113
112 114 return $element->id;
@@ -111,9 +113,9 @@
111 113
112 114 return $element->id;
113 115 }
114 116
115 - public function getOneById($id)
117 + public function getOneById(int $id): ?object
116 118 {
117 119 $scenario = parent::getOneById($id);
118 120
119 121 if (empty($scenario->trigger_params)) {
@@ -124,18 +126,20 @@
124 126
125 127 return $scenario;
126 128 }
127 129
128 - private function saveFlow($flow, $scenarioId)
130 + private function saveFlow(array $flow, int $scenarioId): void
129 131 {
130 132 if (!empty($flow[0]['children'][0])) {
133 + // We save all the step
131 134 $this->saveStep($flow[0]['children'][0], $scenarioId);
132 135 }
133 136 $this->cleanSteps($scenarioId);
134 137 }
135 138
136 - private function saveStep($currentStep, $scenarioId, $previousStepId = null, $conditionValid = null)
139 + private function saveStep(array $currentStep, int $scenarioId, ?string $previousStepId = null, ?int $conditionValid = null): void
137 140 {
141 + // If this is a direct child of a condition we pass it, it's a hidden object for the javascript
138 142 if (isset($currentStep['conditionEnd'])) {
139 143 if ($currentStep['conditionEnd'] && !empty($currentStep['children'])) {
140 144 $isConditionValid = empty($currentStep['conditionValid']) ? 0 : 1;
141 145 $this->saveStep($currentStep['children'][0], $scenarioId, $previousStepId, $isConditionValid);
@@ -191,9 +195,9 @@
191 195 $scenarioQueueClass->deleteByStepIds($stepIdsToDelete);
192 196 $this->scenarioStepClass->delete($stepIdsToDelete);
193 197 }
194 198
195 - private function formatScenario($scenario)
199 + private function formatScenario(object $scenario): ?object
196 200 {
197 201 if (empty($scenario)) {
198 202 return null;
199 203 }
@@ -209,28 +213,21 @@
209 213 return [];
210 214 }
211 215
212 216 $triggerTypes = array_map('acym_escapeDB', $triggerTypes);
213 - $query = 'SELECT * FROM #__acym_scenario WHERE active = 1 AND `trigger` IN ('.implode(', ', $triggerTypes).')';
217 + $query = 'SELECT * FROM #__acym_scenario WHERE active = 1 AND `trigger` IN ('.implode(', ', $triggerTypes).') ORDER BY `ordering` ASC';
214 218
215 219 return array_map([$this, 'formatScenario'], acym_loadObjectList($query));
216 220 }
217 221
218 - public function delete($elements)
222 + public function delete(array $elements): int
219 223 {
220 224 if (empty($elements)) {
221 225 return 0;
222 226 }
223 227
224 - if (!is_array($elements)) {
225 - $elements = [$elements];
226 - }
228 + $escapedElements = array_map('acym_escapeDB', $elements);
227 229
228 - $escapedElements = [];
229 - foreach ($elements as $key => $val) {
230 - $escapedElements[$key] = acym_escapeDB($val);
231 - }
232 -
233 230 $scenarioHistoryLineQuery = 'DELETE FROM #__acym_scenario_history_line WHERE scenario_process_id IN (SELECT id FROM #__acym_scenario_process WHERE scenario_id IN ('.implode(
234 231 ',',
235 232 $escapedElements
236 233 ).'))';
@@ -247,9 +244,9 @@
247 244
248 245 return parent::delete($elements);
249 246 }
250 247
251 - public function duplicate(int $scenarioId)
248 + public function duplicate(int $scenarioId): void
252 249 {
253 250 $scenarioStepClass = new ScenarioStepClass();
254 251
255 252 $scenario = $this->getOneById($scenarioId);
@@ -283,6 +280,46 @@
283 280 $scenarioStep->previous_id = $stepCorrespondence[$scenarioStep->previous_id] ?? null;
284 281 $scenarioStep->scenario_id = $scenario->id;
285 282 $scenarioStepClass->save($scenarioStep);
286 283 }
284 + }
285 +
286 + public function markUserUnsubscribeFromScenario(int $userId, int $scenarioId): void
287 + {
288 + $query = 'UPDATE #__acym_scenario_process SET unsubscribed = 1 WHERE scenario_id = '.intval($scenarioId).' AND user_id = '.intval($userId);
289 +
290 +
291 + acym_query($query);
292 + }
293 +
294 + public function getScenarioIdByMailId(int $mailId): ?int
295 + {
296 + $query = 'SELECT scenario_id
297 + FROM #__acym_scenario_step
298 + WHERE type = '.acym_escapeDB(self::TYPE_ACTION).'
299 + AND params LIKE '.acym_escapeDB('%[mail_id]":"'.$mailId.'"%').'
300 + LIMIT 1';
301 +
302 + return acym_loadResult($query);
303 + }
304 +
305 + public function checkUserMarkedUnsubscribed(int $userId, int $scenarioProcessId): bool
306 + {
307 + $queryScenId = 'SELECT scenario_id
308 + FROM #__acym_scenario_process
309 + WHERE id = '.intval($scenarioProcessId).'
310 + AND user_id = '.intval($userId).'
311 + LIMIT 1';
312 +
313 + $scenarioId = acym_loadResult($queryScenId);
314 +
315 + $queryUnsubscribed = 'SELECT unsubscribed
316 + FROM #__acym_scenario_process
317 + WHERE scenario_id = '.intval($scenarioId).'
318 + AND user_id = '.intval($userId).'
319 + LIMIT 1';
320 +
321 + $result = acym_loadResult($queryUnsubscribed);
322 +
323 + return !empty($result);
287 324 }
288 325 }