-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCron.php
More file actions
110 lines (98 loc) · 2.76 KB
/
Cron.php
File metadata and controls
110 lines (98 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
*
*/
class MdtCron
{
static private function findMsgFromTime($value, $style = null)
{
if(!$value){
return $value;
}
$value = intval($value);
$weeks = intval($value/(60*60*24*7));
$value = $value%(60*60*24*7);
$days = intval($value/(60*60*24));
$value = $value%(60*60*24);
$hours = intval($value/(60*60));
$value = $value%(60*60);
$minutes = intval($value/(60));
$value = $value%(60);
$seconds = intval($value);
if($style === 'array'){
return [
'weeks' => $weeks,
'days' => $days,
'hours' => $hours,
'minutes' => $minutes,
'seconds' => $seconds
];
}
$weeks = $weeks.' '.__('weeks');
$days = $days.' '.__('days');
$hours = $hours.' '.__('hours');
$minutes = $minutes.' '.__('minutes');
$seconds = $seconds.' '.__('seconds');
$and = __('and');
return "$weeks, $days, $hours, $minutes $and $seconds";
}
static public function registerMdtSchedule($schedule = [])
{
// Adds variable time to the existing schedules.
$time = (new MdtSharedOption('schedule'))->getValue();
$time = $time? $time : (7 * 24 * 60 * 60);
$schedules['mdt'] = [
'interval' => $time,
'display' => self::findMsgFromTime($time)
];
return $schedules;
}
static public function setScheduleFrequency($value, $start = null)
{
(new MdtSharedOption('schedule'))->setValue($value);
return self::setSchedule($start);
}
static public function getScheduleFrequency($pretty = null)
{
return $pretty? self::findMsgFromTime(self::getScheduleFrequency(), $pretty) : (new MdtSharedOption('schedule'))->getValue();
}
static public function setSchedule($start = null)
{
self::clearSchedule();
wp_schedule_event( $start? $start : time(), 'mdt', 'mdt_cron' );
return self::getNextScheduleTime();
}
static public function clearSchedule()
{
return wp_clear_scheduled_hook( 'mdt_cron' );
}
static public function getNextScheduleTime($pretty = null)
{
if(!$pretty)
return wp_next_scheduled( 'mdt_cron' );
$time = self::getNextScheduleTime();
return self::findMsgFromTime($time? $time - time() : $time, $pretty);
}
static public function setOneShotSchedule($start = null)
{
self::clearOneShotSchedule();
wp_schedule_single_event( $start? $start : time(), 'mdt_cron_once', ['hasarguments'=>true] );
return self::getNextOneShotScheduleTime();
}
static public function clearOneShotSchedule()
{
return wp_clear_scheduled_hook( 'mdt_cron_once', ['hasarguments'=>true] );
}
static public function getNextOneShotScheduleTime($pretty = null)
{
if(!$pretty)
return wp_next_scheduled( 'mdt_cron_once', ['hasarguments'=>true] );
$time = self::getNextOneShotScheduleTime();
return self::findMsgFromTime($time? $time - time() : $time, $pretty);
}
private function __construct($argument)
{
# code...
}
}
?>