PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.2.2
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.2.2
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / src / Scheduling / Scheduling.php
wp-all-export / src / Scheduling Last commit date
Exception 7 years ago Interval 7 years ago Timezone 7 years ago views 7 years ago Config.php 7 years ago Connection.php 7 years ago Export.php 7 years ago LicensingManager.php 7 years ago Scheduling.php 7 years ago SchedulingApi.php 7 years ago
Scheduling.php
214 lines
1 <?php
2
3 namespace Wpae\Scheduling;
4
5
6 use Wpae\Scheduling\Interval\ScheduleTime;
7
8 class Scheduling
9 {
10 /**
11 * @var SchedulingApi
12 */
13 private $schedulingApi;
14 /**
15 * @var LicensingManager
16 */
17 private $licensingManager;
18
19 public function __construct(SchedulingApi $schedulingApi, LicensingManager $licensingManager)
20 {
21 $this->schedulingApi = $schedulingApi;
22 $this->licensingManager = $licensingManager;
23 }
24
25 public function schedule($elementId, ScheduleTime $scheduleTime, $schedulingEnabled)
26 {
27 $elementId = intval($elementId);
28
29 if ($schedulingEnabled == 1) {
30 $this->enableSchedule($elementId, $scheduleTime);
31 } else {
32 $this->disableSchedule($elementId);
33 }
34 }
35
36 public function scheduleExists($elementId)
37 {
38 $response = $this->schedulingApi->getSchedules($elementId, Config::TYPE);
39
40 return count($response);
41 }
42
43 public function getSchedule($elementId)
44 {
45 $response = $this->schedulingApi->getSchedules($elementId, Config::TYPE);
46
47 if (count($response)) {
48 return $response[0];
49 } else {
50 return false;
51 }
52 }
53
54 public function checkLicense()
55 {
56 $options = \PMXE_Plugin::getInstance()->getOption();
57
58 if (empty($options['scheduling_license'])) {
59 return false;
60 }
61
62 return $this->licensingManager->checkLicense($options['scheduling_license'], \PMXE_Plugin::getSchedulingName());
63 }
64
65 public function checkConnection()
66 {
67 return $this->schedulingApi->checkConnection();
68 }
69
70 public function deleteScheduleIfExists($id) {
71
72 if(!$this->checkLicense()) {
73 return true;
74 }
75
76 $schedule = $this->getSchedule($id);
77 if($schedule) {
78 $this->deleteSchedule($schedule->id);
79 }
80
81 return true;
82 }
83
84 /**
85 * @param $post
86 */
87 public function handleScheduling($id, $post)
88 {
89 $schedulingEnabled = $post['scheduling_enable'];
90
91 if ($schedulingEnabled == 1) {
92
93
94 if ($post['scheduling_run_on'] == 'weekly') {
95 $monthly = false;
96 } else {
97 $monthly = true;
98 }
99
100 if($monthly) {
101 $timesArray = self::buildTimesArray($post['scheduling_monthly_days'], $post['scheduling_times']);
102 } else {
103 $timesArray = self::buildTimesArray($post['scheduling_weekly_days'], $post['scheduling_times']);
104 }
105 $this->schedule(
106 $id,
107 new \Wpae\Scheduling\Interval\ScheduleTime($timesArray, $monthly, $post['scheduling_timezone']),
108 $schedulingEnabled
109 );
110 }
111 }
112
113 private function enableSchedule($elementId, ScheduleTime $scheduleTime)
114 {
115 $schedule = $this->getSchedule($elementId);
116
117 if ($schedule) {
118 $this->updateSchedule($schedule->id, $scheduleTime);
119 } else {
120 $this->createSchedule($elementId, $scheduleTime);
121 }
122 }
123
124 private function deleteSchedule($elementId)
125 {
126 $this->schedulingApi->deleteSchedule($elementId);
127 }
128
129 private function createSchedule($elementId, ScheduleTime $scheduleTime)
130 {
131 $scheduleData = array(
132 "scheduled_job_id" => $elementId,
133 "scheduled_job_type" => Config::TYPE,
134 "endpoint" => get_site_url(),
135 "key" => \PMXE_Plugin::getInstance()->getOption('cron_job_key'),
136 'schedule' => array(
137 'monthly' => $scheduleTime->isMonthly(),
138 'timezone' => $scheduleTime->getTimezone(),
139 'times' => $scheduleTime->getTime()
140 )
141 );
142
143 $this->schedulingApi->createSchedule($scheduleData);
144 }
145
146 private function updateSchedule($scheduleId, ScheduleTime $scheduleTime, $enabled = true)
147 {
148
149 $scheduleTime = array(
150 'enabled' => $enabled,
151 'schedule' => array(
152 'timezone' => $scheduleTime->getTimezone(),
153 'monthly' => $scheduleTime->isMonthly(),
154 'times' => $scheduleTime->getTime()
155 ));
156
157 $this->schedulingApi->updateSchedule($scheduleId, $scheduleTime);
158 }
159
160 private function disableSchedule($elementId)
161 {
162 $this->deleteSchedule($elementId);
163 }
164
165 public static function buildTimesArray($schedulingWeeklyDays, $schedulingTimes)
166 {
167
168 $times = array();
169 $days = explode(',', $schedulingWeeklyDays);
170 foreach ($days as $day) {
171 foreach ($schedulingTimes as $time) {
172
173 if (!$time) {
174 break;
175 }
176
177 $timeParts = explode(':', $time);
178 $hour = $timeParts[0];
179 $min = (int)$timeParts[1];
180
181 if (strpos($time, 'pm') !== false && $hour < 12) {
182 $hour = $hour + 12;
183 }
184
185 if($hour == 12) {
186 if(strpos($time, 'am') !== false) {
187 $hour = 0;
188 }
189 }
190
191 $times[] = array(
192 'day' => $day,
193 'hour' => $hour,
194 'min' => $min
195 );
196 }
197 }
198
199 return $times;
200 }
201
202 /**
203 * TODO: Uglier but simpler method, if this gets in the way, extract to a class
204 *
205 * @return Scheduling
206 */
207 public static function create()
208 {
209 $schedulingApi = new SchedulingApi(Config::API_URL);
210 $licensingManager = new LicensingManager();
211
212 return new Scheduling($schedulingApi, $licensingManager);
213 }
214 }