|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Docket CronWP. |
| 4 | + * |
| 5 | + * @author Nawawi Jamili |
| 6 | + * @license MIT |
| 7 | + * |
| 8 | + * @see https://github.com/nawawi/docket-cronwp |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Nawawi\DocketCronWP; |
| 12 | + |
| 13 | +\defined('DOCKET_CRONWP') || exit; |
| 14 | + |
| 15 | +/* |
| 16 | + * Reference: |
| 17 | + * wp-includes/cron.php. |
| 18 | + */ |
| 19 | +final class Cron |
| 20 | +{ |
| 21 | + public static function getdata() |
| 22 | + { |
| 23 | + $file = apply_filters('docketcronwp/lockfile', false); |
| 24 | + if (empty($file) || !is_file($file)) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + $data = include $file; |
| 29 | + if (!empty($data) && \is_array($data)) { |
| 30 | + return $data; |
| 31 | + } |
| 32 | + |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + public static function putdata($data) |
| 37 | + { |
| 38 | + $file = apply_filters('docketcronwp/lockfile', false); |
| 39 | + if (empty($data) || !\is_array($data)) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + $code = '<?php return '.var_export($data, 1).';'; |
| 44 | + |
| 45 | + if (file_put_contents($file, $code, \LOCK_EX)) { |
| 46 | + chmod($file, 0666); |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // wp_schedule_event |
| 55 | + public static function schedule_event($timestamp, $recurrence, $hook, $args = []) |
| 56 | + { |
| 57 | + if (!is_numeric($timestamp) || $timestamp <= 0) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + $schedules = self::get_schedules(); |
| 62 | + |
| 63 | + if (!isset($schedules[$recurrence])) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + $event = (object) [ |
| 68 | + 'hook' => $hook, |
| 69 | + 'timestamp' => $timestamp, |
| 70 | + 'schedule' => $recurrence, |
| 71 | + 'args' => $args, |
| 72 | + 'interval' => $schedules[$recurrence]['interval'], |
| 73 | + ]; |
| 74 | + |
| 75 | + $pre = apply_filters('pre_schedule_event', null, $event, false); |
| 76 | + |
| 77 | + if (null !== $pre) { |
| 78 | + if (is_wp_error($pre)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return $pre; |
| 83 | + } |
| 84 | + |
| 85 | + $event = apply_filters('schedule_event', $event); |
| 86 | + |
| 87 | + if (!$event) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + $key = md5(serialize($event->args)); |
| 92 | + |
| 93 | + $crons = self::get_cron_array(); |
| 94 | + $crons[$event->timestamp][$event->hook][$key] = [ |
| 95 | + 'schedule' => $event->schedule, |
| 96 | + 'args' => $event->args, |
| 97 | + 'interval' => $event->interval, |
| 98 | + ]; |
| 99 | + uksort($crons, 'strnatcasecmp'); |
| 100 | + |
| 101 | + return self::set_cron_array($crons); |
| 102 | + } |
| 103 | + |
| 104 | + // wp_reschedule_event |
| 105 | + public static function reschedule_event($timestamp, $recurrence, $hook, $args = []) |
| 106 | + { |
| 107 | + if (!is_numeric($timestamp) || $timestamp <= 0) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + $schedules = self::get_schedules(); |
| 112 | + $interval = 0; |
| 113 | + |
| 114 | + if (isset($schedules[$recurrence])) { |
| 115 | + $interval = $schedules[$recurrence]['interval']; |
| 116 | + } |
| 117 | + |
| 118 | + if (0 === $interval) { |
| 119 | + $scheduled_event = self::get_scheduled_event($hook, $args, $timestamp); |
| 120 | + if ($scheduled_event && isset($scheduled_event->interval)) { |
| 121 | + $interval = $scheduled_event->interval; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + $event = (object) [ |
| 126 | + 'hook' => $hook, |
| 127 | + 'timestamp' => $timestamp, |
| 128 | + 'schedule' => $recurrence, |
| 129 | + 'args' => $args, |
| 130 | + 'interval' => $interval, |
| 131 | + ]; |
| 132 | + |
| 133 | + $pre = apply_filters('pre_reschedule_event', null, $event, false); |
| 134 | + |
| 135 | + if (null !== $pre) { |
| 136 | + if (is_wp_error($pre)) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + return $pre; |
| 141 | + } |
| 142 | + |
| 143 | + if (0 == $interval) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + $now = time(); |
| 148 | + |
| 149 | + if ($timestamp >= $now) { |
| 150 | + $timestamp = $now + $interval; |
| 151 | + } else { |
| 152 | + $timestamp = $now + ($interval - (($now - $timestamp) % $interval)); |
| 153 | + } |
| 154 | + |
| 155 | + return self::schedule_event($timestamp, $recurrence, $hook, $args); |
| 156 | + } |
| 157 | + |
| 158 | + // wp_unschedule_event |
| 159 | + public static function unschedule_event($timestamp, $hook, $args = []) |
| 160 | + { |
| 161 | + if (!is_numeric($timestamp) || $timestamp <= 0) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + $pre = apply_filters('pre_unschedule_event', null, $timestamp, $hook, $args, false); |
| 166 | + |
| 167 | + if (null !== $pre) { |
| 168 | + if (is_wp_error($pre)) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + return $pre; |
| 173 | + } |
| 174 | + |
| 175 | + $crons = self::get_cron_array(); |
| 176 | + $key = md5(serialize($args)); |
| 177 | + unset($crons[$timestamp][$hook][$key]); |
| 178 | + if (empty($crons[$timestamp][$hook])) { |
| 179 | + unset($crons[$timestamp][$hook]); |
| 180 | + } |
| 181 | + if (empty($crons[$timestamp])) { |
| 182 | + unset($crons[$timestamp]); |
| 183 | + } |
| 184 | + |
| 185 | + return self::set_cron_array($crons); |
| 186 | + } |
| 187 | + |
| 188 | + // wp_get_scheduled_event |
| 189 | + public static function get_scheduled_event($hook, $args = [], $timestamp = null) |
| 190 | + { |
| 191 | + $pre = apply_filters('pre_get_scheduled_event', null, $hook, $args, $timestamp); |
| 192 | + if (null !== $pre) { |
| 193 | + return $pre; |
| 194 | + } |
| 195 | + |
| 196 | + if (null !== $timestamp && !is_numeric($timestamp)) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + $crons = self::get_cron_array(); |
| 201 | + if (empty($crons)) { |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + $key = md5(serialize($args)); |
| 206 | + |
| 207 | + if (!$timestamp) { |
| 208 | + $next = false; |
| 209 | + foreach ($crons as $timestamp => $cron) { |
| 210 | + if (isset($cron[$hook][$key])) { |
| 211 | + $next = $timestamp; |
| 212 | + break; |
| 213 | + } |
| 214 | + } |
| 215 | + if (!$next) { |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + $timestamp = $next; |
| 220 | + } elseif (!isset($crons[$timestamp][$hook][$key])) { |
| 221 | + return false; |
| 222 | + } |
| 223 | + |
| 224 | + $event = (object) [ |
| 225 | + 'hook' => $hook, |
| 226 | + 'timestamp' => $timestamp, |
| 227 | + 'schedule' => $crons[$timestamp][$hook][$key]['schedule'], |
| 228 | + 'args' => $args, |
| 229 | + ]; |
| 230 | + |
| 231 | + if (isset($crons[$timestamp][$hook][$key]['interval'])) { |
| 232 | + $event->interval = $crons[$timestamp][$hook][$key]['interval']; |
| 233 | + } |
| 234 | + |
| 235 | + return $event; |
| 236 | + } |
| 237 | + |
| 238 | + // wp_get_schedules |
| 239 | + public static function get_schedules() |
| 240 | + { |
| 241 | + return apply_filters('docketcronwp/upstream_get_schedules', []); |
| 242 | + } |
| 243 | + |
| 244 | + // _get_cron_array |
| 245 | + public static function get_cron_array() |
| 246 | + { |
| 247 | + $cron = self::getdata(); |
| 248 | + if (!\is_array($cron)) { |
| 249 | + return false; |
| 250 | + } |
| 251 | + |
| 252 | + unset($cron['version']); |
| 253 | + |
| 254 | + return $cron; |
| 255 | + } |
| 256 | + |
| 257 | + // __set_cron_array |
| 258 | + public static function set_cron_array($cron) |
| 259 | + { |
| 260 | + $cron['version'] = 2; |
| 261 | + $result = self::putdata($cron); |
| 262 | + |
| 263 | + return $result; |
| 264 | + } |
| 265 | +} |
0 commit comments