PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.2
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 / dynamics / time / TimeAutomationTriggers.php

TimeAutomationTriggers.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.2, at back/dynamics/time/TimeAutomationTriggers.php

441 lines 19.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 trait TimeAutomationTriggers
4 {
5 public function onAcymDeclareTriggers(&$triggers, &$defaultValues)
6 {
7 $dailyHour = $this->config->get('daily_hour', '12');
8 $dailyMinute = $this->config->get('daily_minute', '00');
9
10 $triggers['classic']['asap'] = new stdClass();
11 $triggers['classic']['asap']->name = acym_translation('ACYM_EACH_TIME');
12 $triggers['classic']['asap']->option = '<input type="hidden" name="[triggers][classic][asap]" value="y">';
13
14 $hour = [];
15 $minutes = [];
16 $i = 0;
17 while ($i <= 59) {
18 if ($i <= 23) {
19 $hour[$i] = $i < 10 ? '0'.$i : $i;
20 }
21 $minutes[$i] = $i < 10 ? '0'.$i : $i;
22 $i++;
23 }
24
25
26 $triggers['classic']['day'] = new stdClass();
27 $triggers['classic']['day']->name = acym_translation('ACYM_EVERY_DAY_AT');
28 $triggers['classic']['day']->option = '<div class="grid-x grid-margin-x" style="height: 40px;">';
29 $triggers['classic']['day']->option .= '<div class="cell medium-shrink">'.acym_select(
30 $hour,
31 '[triggers][classic][day][hour]',
32 empty($defaultValues['day']) ? acym_date('now', 'H') : $defaultValues['day']['hour'],
33 ['data-class' => 'intext_select acym__select']
34 ).'</div>';
35 $triggers['classic']['day']->option .= '<div class="cell medium-shrink acym_vcenter">:</div>';
36 $triggers['classic']['day']->option .= '<div class="cell medium-auto">'.acym_select(
37 $minutes,
38 '[triggers][classic][day][minutes]',
39 empty($defaultValues['day']) ? acym_date('now', 'i') : $defaultValues['day']['minutes'],
40 ['data-class' => 'intext_select acym__select']
41 ).'</div>';
42 $triggers['classic']['day']->option .= '</div>';
43
44 $days = [
45 'monday' => acym_translation('ACYM_MONDAY'),
46 'tuesday' => acym_translation('ACYM_TUESDAY'),
47 'wednesday' => acym_translation('ACYM_WEDNESDAY'),
48 'thursday' => acym_translation('ACYM_THURSDAY'),
49 'friday' => acym_translation('ACYM_FRIDAY'),
50 'saturday' => acym_translation('ACYM_SATURDAY'),
51 'sunday' => acym_translation('ACYM_SUNDAY'),
52 ];
53
54 $triggers['classic']['weeks_on'] = new stdClass();
55 $triggers['classic']['weeks_on']->name = acym_translation('ACYM_EVERY_WEEK_ON');
56 $triggers['classic']['weeks_on']->option = '<div class="grid-x">';
57 $triggers['classic']['weeks_on']->option .= '<div class="cell">'.acym_selectMultiple(
58 $days,
59 '[triggers][classic][weeks_on][day]',
60 empty($defaultValues['weeks_on']) ? ['monday'] : $defaultValues['weeks_on']['day'],
61 ['data-class' => 'acym__select']
62 ).'</div>';
63 $triggers['classic']['weeks_on']->option .= '<div class="cell margin-top-1 acym_vcenter">';
64 $triggers['classic']['weeks_on']->option .= acym_translationSprintf(
65 'ACYM_AT_DATE_TIME',
66 '<div class="margin-left-1 margin-right-1">'.acym_select(
67 $hour,
68 '[triggers][classic][weeks_on][hour]',
69 !isset($defaultValues['weeks_on']['hour']) ? $dailyHour : $defaultValues['weeks_on']['hour'],
70 ['data-class' => 'intext_select acym__select']
71 ).'</div>',
72 '<div class="margin-left-1 margin-right-1">'.acym_select(
73 $minutes,
74 '[triggers][classic][weeks_on][minutes]',
75 !isset($defaultValues['weeks_on']['minutes']) ? $dailyMinute : $defaultValues['weeks_on']['minutes'],
76 ['data-class' => 'intext_select acym__select']
77 ).'</div>'
78 );
79 $triggers['classic']['weeks_on']->option .= '</div>';
80 $triggers['classic']['weeks_on']->option .= '</div>';
81
82
83 $triggers['classic']['on_day_month'] = new stdClass();
84 $triggers['classic']['on_day_month']->name = acym_translation('ACYM_ONTHE');
85 $triggers['classic']['on_day_month']->option = '<div class="grid-x grid-margin-x margin-y">';
86 $triggers['classic']['on_day_month']->option .= '<div class="cell medium-4">'.acym_select(
87 [
88 'first' => acym_translation('ACYM_FIRST'),
89 'second' => acym_translation('ACYM_SECOND'),
90 'third' => acym_translation('ACYM_THIRD'),
91 'fourth' => acym_translation('ACYM_FOURTH'),
92 'last' => acym_translation('ACYM_LAST'),
93 ],
94 '[triggers][classic][on_day_month][number]',
95 empty($defaultValues['on_day_month']) ? null : $defaultValues['on_day_month']['number'],
96 ['data-class' => 'acym__select']
97 ).'</div>';
98 $triggers['classic']['on_day_month']->option .= '<div class="cell medium-4">'.acym_select(
99 $days,
100 '[triggers][classic][on_day_month][day]',
101 empty($defaultValues['on_day_month']) ? null : $defaultValues['on_day_month']['day'],
102 [
103 'data-class' => 'acym__select',
104 'style' => 'margin: 0 10px;',
105 ]
106 ).'</div>';
107 $triggers['classic']['on_day_month']->option .= '<div class="cell medium-4 acym_vcenter">'.acym_translation('ACYM_DAYOFMONTH').'</div>';
108 $triggers['classic']['on_day_month']->option .= '<div class="cell acym_vcenter">';
109 $triggers['classic']['on_day_month']->option .= acym_translationSprintf(
110 'ACYM_AT_DATE_TIME',
111 '<div class="margin-left-1 margin-right-1">'.acym_select(
112 $hour,
113 '[triggers][classic][on_day_month][hour]',
114 !isset($defaultValues['on_day_month']['hour']) ? $dailyHour : $defaultValues['on_day_month']['hour'],
115 ['data-class' => 'intext_select acym__select']
116 ).'</div>',
117 '<div class="margin-left-1 margin-right-1">'.acym_select(
118 $minutes,
119 '[triggers][classic][on_day_month][minutes]',
120 !isset($defaultValues['on_day_month']['minutes']) ? $dailyMinute : $defaultValues['on_day_month']['minutes'],
121 ['data-class' => 'intext_select acym__select']
122 ).'</div>'
123 );
124 $triggers['classic']['on_day_month']->option .= '</div>';
125 $triggers['classic']['on_day_month']->option .= '</div>';
126
127
128 $every = [
129 '3600' => acym_translation('ACYM_HOURS'),
130 '86400' => acym_translation('ACYM_DAYS'),
131 '604800' => acym_translation('ACYM_WEEKS'),
132 '2628000' => acym_translation('ACYM_MONTHS'),
133 ];
134
135 $defaultEvery = empty($defaultValues['every']['number']) ? '1' : $defaultValues['every']['number'];
136 $triggers['classic']['every'] = new stdClass();
137 $triggers['classic']['every']->name = acym_translation('ACYM_EVERY');
138 $triggers['classic']['every']->option = '<div class="grid-x grid-margin-x">';
139 $triggers['classic']['every']->option .= '<div class="cell medium-shrink">';
140 $triggers['classic']['every']->option .= '<input type="number" min="1" name="[triggers][classic][every][number]" class="intext_input" value="'.intval($defaultEvery).'">';
141 $triggers['classic']['every']->option .= '</div>';
142 $triggers['classic']['every']->option .= '<div class="cell medium-auto">'.acym_select(
143 $every,
144 '[triggers][classic][every][type]',
145 empty($defaultValues['every']) ? '604800' : $defaultValues['every']['type'],
146 ['data-class' => 'intext_select acym__select']
147 ).'</div>';
148 $triggers['classic']['every']->option .= '</div>';
149 }
150
151 public function onAcymExecuteTrigger(&$step, &$execute, &$data)
152 {
153 if (!empty($step->is_scenario)) {
154 return;
155 }
156
157 $time = $data['time'];
158 $triggers = $step->triggers;
159
160 // For each trigger of the automation, we'll calculate the next execution date. In the end we take the closest one
161 $nextExecutionDate = [];
162
163 // Get the time the auto tasks should be triggered
164 $dailyHour = $this->config->get('daily_hour', '12');
165 $dailyMinute = $this->config->get('daily_minute', '00');
166
167
168 // Each time the cron is triggered
169 if (!empty($triggers['asap'])) {
170 $execute = true;
171 $nextExecutionDate[] = $time;
172 }
173
174 // Every day at xx:xx
175 if (!empty($triggers['day'])) {
176 // The day it is currently based on the timezone specified in the CMS configuration
177 $dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d');
178
179 $hour = $triggers['day']['hour'];
180 $minutes = $triggers['day']['minutes'];
181
182 if (strlen($hour) < 2) $hour = '0'.$hour;
183 if (strlen($minutes) < 2) $minutes = '0'.$minutes;
184
185 // The UTC timestamp of the current day based on the CMS timezone, at the specified hour
186 $dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$hour.':'.$minutes);
187
188 if ($time < $dayBasedOnCMSTimezoneAtSpecifiedHour) {
189 $nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour;
190 } else {
191 $nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour + 86400;
192
193 // First trigger: if the hour is passed we execute
194 if (empty($step->last_execution)) $execute = true;
195 }
196 }
197
198 // Each week on Mondays and Wednesdays for example
199 if (!empty($triggers['weeks_on'])) {
200 if (isset($triggers['weeks_on']['hour'])) {
201 $hour = $triggers['weeks_on']['hour'];
202 $minutes = $triggers['weeks_on']['minutes'];
203 } else {
204 $hour = $dailyHour;
205 $minutes = $dailyMinute;
206 }
207
208 if (strlen($hour) < 2) $hour = '0'.$hour;
209 if (strlen($minutes) < 2) $minutes = '0'.$minutes;
210
211 foreach ($triggers['weeks_on']['day'] as $day) {
212 // The day it is currently based on the timezone specified in the CMS configuration
213 $dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d');
214
215 // The UTC timestamp of the current day based on the CMS timezone, at the specified hour
216 $dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$hour.':'.$minutes);
217
218 // Only store the next Execution date if it's in the future
219 if ($day == strtolower(acym_date('now', 'l', true, false))) {
220 if ($time < $dayBasedOnCMSTimezoneAtSpecifiedHour) {
221 $nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour;
222 } else {
223 $nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour + 604800;
224
225 // Current day is selected, the time is passed, and it's the first trigger
226 $lastExecutionIsNotToday = acym_date($step->last_execution, 'Y-m-d') !== $dayBasedOnCMSTimezone;
227 $nextExecutionIsToday = acym_date($step->next_execution, 'Y-m-d') === $dayBasedOnCMSTimezone;
228 if (empty($step->last_execution) || ($lastExecutionIsNotToday && $nextExecutionIsToday)) $execute = true;
229 }
230 } else {
231 $days = [
232 'monday',
233 'tuesday',
234 'wednesday',
235 'thursday',
236 'friday',
237 'saturday',
238 'sunday',
239 ];
240 $currentDayOfWeek = acym_date('now', 'N') - 1;
241 $wantedDayOfWeek = array_search($day, $days);
242
243 $shift = $wantedDayOfWeek - $currentDayOfWeek;
244 if ($shift < 0) $shift += 7;
245
246 $nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour + 86400 * $shift;
247 }
248 }
249 }
250
251 // On first Friday of the month for example
252 if (!empty($triggers['on_day_month'])) {
253 if (isset($triggers['on_day_month']['hour'])) {
254 $hour = $triggers['on_day_month']['hour'];
255 $minutes = $triggers['on_day_month']['minutes'];
256 } else {
257 $hour = $dailyHour;
258 $minutes = $dailyMinute;
259 }
260
261 if (strlen($hour) < 2) $hour = '0'.$hour;
262 if (strlen($minutes) < 2) $minutes = '0'.$minutes;
263
264 $today = acym_getTime('today '.$hour.':'.$minutes);
265
266 // Get the current month's day
267 $execution = acym_getTime($triggers['on_day_month']['number'].' '.$triggers['on_day_month']['day'].' of this month '.$hour.':'.$minutes);
268
269 //If it's before today, get the next date
270 if ($execution < $today) {
271 $execution = acym_getTime($triggers['on_day_month']['number'].' '.$triggers['on_day_month']['day'].' of next month '.$hour.':'.$minutes);
272 }
273
274 // The next execution date is in the future
275 if ($execution > $time) {
276 $nextExecutionDate[] = $execution;
277 } else {
278 // The next execution is today and is passed
279
280
281 // If it's the first trigger we execute
282 if (empty($step->last_execution)) {
283 $execute = true;
284 }
285
286 // Set the next execution time: recompute the real "next weekday of next month" occurrence.
287 $nextExecutionDate[] = acym_getTime($triggers['on_day_month']['number'].' '.$triggers['on_day_month']['day'].' of next month '.$hour.':'.$minutes);
288 }
289 }
290
291 // WARNING : KEEP THIS TRIGGER AT THE END, ACTION PERFORMED IF WE EXECUTE
292 // Every X hours/days/weeks/months
293 if (!empty($triggers['every'])) {
294 // First trigger: we execute and set the next execution in X hours/days/weeks/months
295 if (empty($step->last_execution)) {
296 $execute = true;
297 } else {
298 if ($triggers['every']['type'] == 2628000) {
299 $nextDate = new \DateTime(acym_date($step->last_execution, 'Y-m-d H:i:s', false), new \DateTimeZone('UTC'));
300 $nextDate = $nextDate->add(new \DateInterval('P'.$triggers['every']['number'].'M'));
301 $nextDate = $nextDate->getTimestamp();
302 } else {
303 $nextDate = $step->last_execution + ($triggers['every']['number'] * $triggers['every']['type']);
304 }
305
306 if ($nextDate > $time) {
307 $nextExecutionDate[] = $nextDate;
308 } else {
309 $execute = true;
310 }
311 }
312
313 if ($execute) {
314 $nextExecutionDate[] = $time + ($triggers['every']['number'] * $triggers['every']['type']);
315 }
316 }
317
318 if (!empty($nextExecutionDate)) {
319 $step->next_execution = min($nextExecutionDate);
320 }
321 }
322
323 public function onAcymDeclareSummary_triggers(object $automation): void
324 {
325 if (!empty($automation->triggers['type_trigger'])) {
326 unset($automation->triggers['type_trigger']);
327 }
328
329 $days = [
330 'monday' => acym_translation('ACYM_MONDAY'),
331 'tuesday' => acym_translation('ACYM_TUESDAY'),
332 'wednesday' => acym_translation('ACYM_WEDNESDAY'),
333 'thursday' => acym_translation('ACYM_THURSDAY'),
334 'friday' => acym_translation('ACYM_FRIDAY'),
335 'saturday' => acym_translation('ACYM_SATURDAY'),
336 'sunday' => acym_translation('ACYM_SUNDAY'),
337 ];
338
339 $this->summaryAsap($automation);
340 $this->summaryDay($automation);
341 $this->summaryWeeksOn($automation, $days);
342 $this->summaryOnDayMonth($automation, $days);
343 $this->summaryEvery($automation);
344 }
345
346 private function summaryAsap(object $automation): void
347 {
348 if (!empty($automation->triggers['asap'])) {
349 $automation->triggers['asap'] = acym_translation('ACYM_EACH_TIME');
350 }
351 }
352
353 private function summaryDay(object $automation): void
354 {
355 if (empty($automation->triggers['day']) || !is_array($automation->triggers['day'])) {
356 return;
357 }
358
359 $hour = sprintf('%02d', $automation->triggers['day']['hour']);
360 $minutes = sprintf('%02d', $automation->triggers['day']['minutes']);
361
362 $automation->triggers['day'] = acym_translationSprintf('ACYM_TRIGGER_DAY_SUMMARY', $hour, $minutes);
363 }
364
365 private function summaryWeeksOn(object $automation, array $days): void
366 {
367 if (empty($automation->triggers['weeks_on']) || !is_array($automation->triggers['weeks_on'])) {
368 return;
369 }
370
371 foreach ($automation->triggers['weeks_on']['day'] as $i => $oneDay) {
372 $automation->triggers['weeks_on']['day'][$i] = $days[$oneDay];
373 }
374
375 if (isset($automation->triggers['weeks_on']['hour'])) {
376 $hour = $automation->triggers['weeks_on']['hour'];
377 $minutes = $automation->triggers['weeks_on']['minutes'];
378 } else {
379 $hour = $this->config->get('daily_hour', '12');
380 $minutes = $this->config->get('daily_minute', '00');
381 }
382
383 $hour = sprintf('%02d', $hour);
384 $minutes = sprintf('%02d', $minutes);
385
386 $automation->triggers['weeks_on'] = acym_translationSprintf(
387 'ACYM_TRIGGER_WEEKS_ON_SUMMARY',
388 implode(', ', $automation->triggers['weeks_on']['day'])
389 ).' '.acym_translationSprintf('ACYM_AT_DATE_TIME', $hour, $minutes);
390 }
391
392 private function summaryOnDayMonth(object $automation, array $days): void
393 {
394 if (empty($automation->triggers['on_day_month']) || !is_array($automation->triggers['on_day_month'])) {
395 return;
396 }
397
398 $numbers = [
399 'first' => acym_translation('ACYM_FIRST'),
400 'second' => acym_translation('ACYM_SECOND'),
401 'third' => acym_translation('ACYM_THIRD'),
402 'fourth' => acym_translation('ACYM_FOURTH'),
403 'last' => acym_translation('ACYM_LAST'),
404 ];
405
406 if (isset($automation->triggers['on_day_month']['hour'])) {
407 $hour = $automation->triggers['on_day_month']['hour'];
408 $minutes = $automation->triggers['on_day_month']['minutes'];
409 } else {
410 $hour = $this->config->get('daily_hour', '12');
411 $minutes = $this->config->get('daily_minute', '00');
412 }
413
414 $hour = sprintf('%02d', $hour);
415 $minutes = sprintf('%02d', $minutes);
416
417 $automation->triggers['on_day_month'] = acym_translationSprintf(
418 'ACYM_TRIGGER_ON_DAY_MONTH_SUMMARY',
419 $numbers[$automation->triggers['on_day_month']['number']],
420 $days[$automation->triggers['on_day_month']['day']]
421 ).' '.acym_translationSprintf('ACYM_AT_DATE_TIME', $hour, $minutes);
422 }
423
424 private function summaryEvery(object $automation): void
425 {
426 if (empty($automation->triggers['every']) || !is_array($automation->triggers['every'])) {
427 return;
428 }
429
430 if ($automation->triggers['every']['type'] == 3600) $automation->triggers['every']['type'] = acym_translation('ACYM_HOURS');
431 if ($automation->triggers['every']['type'] == 86400) $automation->triggers['every']['type'] = acym_translation('ACYM_DAYS');
432 if ($automation->triggers['every']['type'] == 604800) $automation->triggers['every']['type'] = acym_translation('ACYM_WEEKS');
433 if ($automation->triggers['every']['type'] == 2628000) $automation->triggers['every']['type'] = acym_translation('ACYM_MONTHS');
434 $automation->triggers['every'] = acym_translationSprintf(
435 'ACYM_TRIGGER_EVERY_SUMMARY',
436 $automation->triggers['every']['number'],
437 $automation->triggers['every']['type']
438 );
439 }
440 }
441