AutomaticEmailScheduler.php
2 years ago
AutomationEmailScheduler.php
1 year ago
PostNotificationScheduler.php
2 years ago
ReEngagementScheduler.php
1 year ago
Scheduler.php
1 year ago
WelcomeScheduler.php
2 years ago
index.php
3 years ago
Scheduler.php
113 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Scheduler; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Newsletter\NewslettersRepository; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | use MailPoetVendor\Carbon\Carbon; |
| 12 | |
| 13 | class Scheduler { |
| 14 | const MYSQL_TIMESTAMP_MAX = '2038-01-19 03:14:07'; |
| 15 | |
| 16 | /** @var WPFunctions */ |
| 17 | private $wp; |
| 18 | |
| 19 | /** @var NewslettersRepository */ |
| 20 | private $newslettersRepository; |
| 21 | |
| 22 | public function __construct( |
| 23 | WPFunctions $wp, |
| 24 | NewslettersRepository $newslettersRepository |
| 25 | ) { |
| 26 | $this->wp = $wp; |
| 27 | $this->newslettersRepository = $newslettersRepository; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return string|false |
| 32 | */ |
| 33 | public function getNextRunDate($schedule) { |
| 34 | $nextRunDateTime = $this->getNextRunDateTime($schedule); |
| 35 | return $nextRunDateTime ? $nextRunDateTime->format('Y-m-d H:i:s') : $nextRunDateTime; |
| 36 | } |
| 37 | |
| 38 | public function getPreviousRunDate($schedule) { |
| 39 | // User enters time in WordPress site timezone, but we need to calculate it in UTC before we save it to DB |
| 40 | // 1) As the initial time we use time in site timezone via current_datetime |
| 41 | // 2) We use CronExpression to calculate previous run (still in site's timezone) |
| 42 | // 3) We convert the calculated time to UTC |
| 43 | $from = $this->wp->currentDatetime(); |
| 44 | try { |
| 45 | $schedule = new \Cron\CronExpression((string)$schedule); |
| 46 | $previousRunDate = $schedule->getPreviousRunDate(Carbon::instance($from)); |
| 47 | $previousRunDate->setTimezone(new \DateTimeZone('UTC')); |
| 48 | $previousRunDate = $previousRunDate->format('Y-m-d H:i:s'); |
| 49 | } catch (\Exception $e) { |
| 50 | $previousRunDate = false; |
| 51 | } |
| 52 | return $previousRunDate; |
| 53 | } |
| 54 | |
| 55 | public function getScheduledTimeWithDelay($afterTimeType, $afterTimeNumber): Carbon { |
| 56 | $currentTime = Carbon::now()->millisecond(0); |
| 57 | switch ($afterTimeType) { |
| 58 | case 'minutes': |
| 59 | $currentTime->addMinutes($afterTimeNumber); |
| 60 | break; |
| 61 | case 'hours': |
| 62 | $currentTime->addHours($afterTimeNumber); |
| 63 | break; |
| 64 | case 'days': |
| 65 | $currentTime->addDays($afterTimeNumber); |
| 66 | break; |
| 67 | case 'weeks': |
| 68 | $currentTime->addWeeks($afterTimeNumber); |
| 69 | break; |
| 70 | } |
| 71 | $maxScheduledTime = Carbon::createFromFormat('Y-m-d H:i:s', self::MYSQL_TIMESTAMP_MAX); |
| 72 | if ($maxScheduledTime && $currentTime > $maxScheduledTime) { |
| 73 | return $maxScheduledTime; |
| 74 | } |
| 75 | return $currentTime; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return NewsletterEntity[] |
| 80 | */ |
| 81 | public function getNewsletters(string $type, ?string $group = null): array { |
| 82 | return $this->newslettersRepository->findActiveByTypeAndGroup($type, $group); |
| 83 | } |
| 84 | |
| 85 | public function formatDatetimeString($datetimeString) { |
| 86 | return Carbon::parse($datetimeString)->format('Y-m-d H:i:s'); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return \DateTime|false |
| 91 | */ |
| 92 | public function getNextRunDateTime($schedule) { |
| 93 | // User enters time in WordPress site timezone, but we need to calculate it in UTC before we save it to DB |
| 94 | // 1) As the initial time we use time in site timezone via current_datetime |
| 95 | // 2) We use CronExpression to calculate next run (still in site's timezone) |
| 96 | // 3) We convert the calculated time to UTC |
| 97 | //$fromTimestamp = $this->wp->currentTime('timestamp', false); |
| 98 | $from = $this->wp->currentDatetime(); |
| 99 | try { |
| 100 | $schedule = new \Cron\CronExpression((string)$schedule); |
| 101 | $nextRunDate = $schedule->getNextRunDate(Carbon::instance($from)); |
| 102 | $nextRunDate->setTimezone(new \DateTimeZone('UTC')); |
| 103 | // Work around CronExpression transforming Carbon into DateTime |
| 104 | if (!$nextRunDate instanceof Carbon) { |
| 105 | $nextRunDate = new Carbon($nextRunDate); |
| 106 | } |
| 107 | } catch (\Exception $e) { |
| 108 | $nextRunDate = false; |
| 109 | } |
| 110 | return $nextRunDate; |
| 111 | } |
| 112 | } |
| 113 |