SGIScheduleAdapter.php
5 years ago
SGSchedule.php
5 years ago
SGScheduleAdapterWordpress.php
5 years ago
SGScheduleAdapterWordpress.php
125 lines
| 1 | <?php |
| 2 | require_once(SG_SCHEDULE_PATH.'SGIScheduleAdapter.php'); |
| 3 | |
| 4 | class SGScheduleAdapterWordpress implements SGIScheduleAdapter |
| 5 | { |
| 6 | public static function create($cron, $id = SG_SCHEDULER_DEFAULT_ID) |
| 7 | { |
| 8 | if (!self::isCronAvailable()) { |
| 9 | return false; |
| 10 | } |
| 11 | |
| 12 | $cronExecutionData = self::getCronExecutionData($cron); |
| 13 | $time = $cronExecutionData['time']; |
| 14 | $recurrence = $cronExecutionData['recurrence']; |
| 15 | |
| 16 | $args = array((int)$id); |
| 17 | |
| 18 | $dateString = backupGuardConvertDateTimezone(@date("Y-m-d H:i:s", $time)); |
| 19 | $time = strtotime($dateString); |
| 20 | $res = wp_schedule_event($time, $recurrence, SG_SCHEDULE_ACTION, $args); |
| 21 | } |
| 22 | |
| 23 | public static function getCronExecutionData($cron) |
| 24 | { |
| 25 | $recurrence = ''; |
| 26 | $tmpTime = self::getTmpTime($cron['intervalHour']); |
| 27 | |
| 28 | if ($cron['interval'] == BG_SCHEDULE_INTERVAL_HOURLY) { |
| 29 | $recurrence = 'hourly'; |
| 30 | $time = time() + 3600; |
| 31 | } |
| 32 | else if ($cron['interval'] == BG_SCHEDULE_INTERVAL_DAILY) { |
| 33 | $recurrence = 'daily'; |
| 34 | |
| 35 | if ($tmpTime < time()) { |
| 36 | $time = strtotime('Next day '.sprintf("%02d:00", $cron['intervalHour'])); |
| 37 | } |
| 38 | else { |
| 39 | $time = $tmpTime; |
| 40 | } |
| 41 | } |
| 42 | else if ($cron['interval'] == BG_SCHEDULE_INTERVAL_WEEKLY) { |
| 43 | $recurrence = 'weekly'; |
| 44 | $dayOfInterval = $cron['dayOfInterval']; |
| 45 | |
| 46 | switch ($dayOfInterval) { |
| 47 | case 1: |
| 48 | $dayOfInterval = 'Monday'; |
| 49 | break; |
| 50 | case 2: |
| 51 | $dayOfInterval = 'Tuesday'; |
| 52 | break; |
| 53 | case 3: |
| 54 | $dayOfInterval = 'Wednesday'; |
| 55 | break; |
| 56 | case 4: |
| 57 | $dayOfInterval = 'Thursday'; |
| 58 | break; |
| 59 | case 5: |
| 60 | $dayOfInterval = 'Friday'; |
| 61 | break; |
| 62 | case 6: |
| 63 | $dayOfInterval = 'Saturday'; |
| 64 | break; |
| 65 | case 7: |
| 66 | $dayOfInterval = 'Sunday'; |
| 67 | break; |
| 68 | default: |
| 69 | $dayOfInterval = 'Monday'; |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if ($tmpTime < time()) { |
| 74 | $time = strtotime('Next '.$dayOfInterval.' '.sprintf("%02d:00", $cron['intervalHour'])); |
| 75 | } |
| 76 | else { |
| 77 | $time = strtotime('this '.$dayOfInterval.' '.sprintf("%02d:00", $cron['intervalHour'])); |
| 78 | } |
| 79 | } |
| 80 | else if ($cron['interval'] == BG_SCHEDULE_INTERVAL_MONTHLY) { |
| 81 | $recurrence = 'monthly'; |
| 82 | $dayOfInterval = $cron['dayOfInterval']; |
| 83 | $today = (int)date('d'); |
| 84 | |
| 85 | if ($today < $dayOfInterval) { |
| 86 | $time = $tmpTime + ($dayOfInterval - $today) * SG_ONE_DAY_IN_SECONDS; |
| 87 | } |
| 88 | else { |
| 89 | if ($tmpTime > time() && $today == $dayOfInterval) { |
| 90 | $time = $tmpTime; |
| 91 | } |
| 92 | else { |
| 93 | $time = strtotime('first day of next month '.sprintf("%02d:00", $cron['intervalHour'])); |
| 94 | $time += ($dayOfInterval - 1) * SG_ONE_DAY_IN_SECONDS; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | else { |
| 99 | $recurrence = 'yearly'; |
| 100 | $time = strtotime('Next year today '.sprintf("%02d:00", $cron['intervalHour'])); |
| 101 | } |
| 102 | |
| 103 | return array( |
| 104 | 'time' => $time, |
| 105 | 'recurrence' => $recurrence |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | public static function remove($id = SG_SCHEDULER_DEFAULT_ID) |
| 110 | { |
| 111 | $args = array((int)$id); |
| 112 | wp_clear_scheduled_hook(SG_SCHEDULE_ACTION, $args); |
| 113 | } |
| 114 | |
| 115 | public static function getTmpTime($hours) |
| 116 | { |
| 117 | return strtotime('Today '.sprintf("%02d:00", $hours)); |
| 118 | } |
| 119 | |
| 120 | public static function isCronAvailable() |
| 121 | { |
| 122 | return defined('DISABLE_WP_CRON')?!DISABLE_WP_CRON:true; |
| 123 | } |
| 124 | } |
| 125 |