handler.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | // Namespace |
| 4 | namespace BMI\Plugin\CRON; |
| 5 | |
| 6 | // Use |
| 7 | use BMI\Plugin\BMI_Logger AS Logger; |
| 8 | |
| 9 | // Exit on direct access |
| 10 | if (!defined('ABSPATH')) exit; |
| 11 | |
| 12 | /** |
| 13 | * BMI_Crons |
| 14 | */ |
| 15 | class BMI_Crons { |
| 16 | |
| 17 | function __construct() { |
| 18 | |
| 19 | } |
| 20 | |
| 21 | public static function calculate_date($base, $curr = false) { |
| 22 | |
| 23 | $type = $base['type']; |
| 24 | $week = intval($base['week']); |
| 25 | $day = intval($base['day']); |
| 26 | $hour = intval($base['hour']); |
| 27 | $minutes = intval($base['minute']); |
| 28 | |
| 29 | if (isset($curr) && $curr != false) { |
| 30 | $timestamp = $curr; |
| 31 | } else $timestamp = time(); |
| 32 | |
| 33 | $current = new \DateTime(); |
| 34 | $current->setTimestamp($timestamp); |
| 35 | |
| 36 | $future = new \DateTime(); |
| 37 | $future->setTimestamp($timestamp); |
| 38 | $future->setTime($hour, $minutes, 0); |
| 39 | |
| 40 | $c_minute = intval($current->format('i')); |
| 41 | $c_hour = intval($current->format('G')); |
| 42 | $c_month_day = intval($current->format('j')); |
| 43 | $c_month = intval($current->format('n')); |
| 44 | $c_week = intval($current->format('N')); |
| 45 | |
| 46 | if ($type == 'day') { |
| 47 | |
| 48 | if ($current->getTimestamp() >= $future->getTimestamp()) { |
| 49 | |
| 50 | $future->modify('+1 day'); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | } else if ($type == 'week') { |
| 55 | |
| 56 | if ($c_week >= $week) { |
| 57 | |
| 58 | if ($c_week == $week) { |
| 59 | |
| 60 | if ($c_hour >= $hour && (($c_hour > $hour) || ($c_hour == $hour && $c_minute >= $minutes))) { |
| 61 | // if () { |
| 62 | $offset = (7 - ($c_week - $week)); |
| 63 | $future->modify("+$offset day"); |
| 64 | // } |
| 65 | } |
| 66 | |
| 67 | } else { |
| 68 | |
| 69 | // if ($c_hour <= $hour) { |
| 70 | $offset = (7 - ($c_week - $week)); |
| 71 | $future->modify("+$offset day"); |
| 72 | // } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | } else { |
| 78 | |
| 79 | $offset = ($week - $c_week); |
| 80 | $future->modify("+$offset day"); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | } else if ($type == 'month') { |
| 85 | |
| 86 | if ($c_month_day >= $day) { |
| 87 | |
| 88 | if ($c_month_day == $day && $c_hour <= $hour) { |
| 89 | if ($c_minute >= $minutes) { |
| 90 | |
| 91 | $days = \cal_days_in_month(CAL_GREGORIAN, $c_month, $current->format('Y')); |
| 92 | $offset = ($days - ($c_month_day - $day)); |
| 93 | $future->modify("+$offset day"); |
| 94 | |
| 95 | } |
| 96 | } else { |
| 97 | |
| 98 | $offset = ($day - $c_month_day); |
| 99 | $future->modify("+1 month"); |
| 100 | $future->modify("+$offset day"); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | } else { |
| 105 | |
| 106 | $offset = ($day - $c_month_day); |
| 107 | $future->modify("+$offset day"); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | return $future->getTimestamp(); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | } |
| 118 |