PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / trunk
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel vtrunk
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 8 years ago Interval 8 years ago Timezone 7 years ago views 3 weeks ago Config.php 8 years ago Connection.php 8 years ago Export.php 3 weeks ago LicensingManager.php 1 year ago Scheduling.php 1 year ago SchedulingApi.php 3 years ago
Scheduling.php
241 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 private function schedule($elementId, ScheduleTime $scheduleTime)
26 {
27 $elementId = intval($elementId);
28
29 $this->enableSchedule($elementId, $scheduleTime);
30
31 }
32
33 public function scheduleExists($elementId)
34 {
35 $response = $this->schedulingApi->getSchedules($elementId, Config::TYPE);
36
37 return count($response);
38 }
39
40 public function getSchedule($elementId)
41 {
42 $response = $this->schedulingApi->getSchedules($elementId, Config::TYPE);
43
44 if (count($response)) {
45 return $response[0];
46 } else {
47 return false;
48 }
49 }
50
51 public function checkLicense()
52 {
53 $options = \PMXE_Plugin::getInstance()->getOption();
54
55 if (empty($options['scheduling_license'])) {
56 return ['success' => false];
57 }
58
59 return $this->licensingManager->checkLicense($options['scheduling_license'], \PMXE_Plugin::getSchedulingName());
60 }
61
62 public function checkConnection()
63 {
64 return $this->schedulingApi->checkConnection();
65 }
66
67 public function deleteScheduleIfExists($id) {
68
69 if(empty($this->checkLicense()['success'])) {
70 return true;
71 }
72
73 $schedule = $this->getSchedule($id);
74 if($schedule) {
75 $this->deleteSchedule($schedule->id);
76 }
77
78 return true;
79 }
80
81 /**
82 * @param $post
83 */
84 public function handleScheduling($id, $post)
85 {
86
87 if (empty($this->checkLicense()['success'])) {
88 return false;
89 }
90
91 $schedulingEnabled = $post['scheduling_enable'];
92
93 if ($schedulingEnabled == 1) {
94
95 $this->userEnableSchedule($id);
96
97 if ($post['scheduling_run_on'] == 'weekly') {
98 $monthly = false;
99 } else {
100 $monthly = true;
101 }
102
103 if($monthly) {
104 $timesArray = self::buildTimesArray($post['scheduling_monthly_days'], $post['scheduling_times']);
105 } else {
106 $timesArray = self::buildTimesArray($post['scheduling_weekly_days'], $post['scheduling_times']);
107 }
108 $this->schedule(
109 $id,
110 new \Wpae\Scheduling\Interval\ScheduleTime($timesArray, $monthly, $post['scheduling_timezone']),
111 $schedulingEnabled
112 );
113 } else {
114 $this->userDisableSchedule($id);
115 }
116 }
117
118 private function enableSchedule($elementId, ScheduleTime $scheduleTime)
119 {
120 $schedule = $this->getSchedule($elementId);
121
122 if ($schedule) {
123 $this->updateSchedule($schedule->id, $scheduleTime);
124 } else {
125 $this->createSchedule($elementId, $scheduleTime);
126 }
127 }
128
129 private function deleteSchedule($elementId)
130 {
131 $this->schedulingApi->deleteSchedule($elementId);
132 }
133
134 private function createSchedule($elementId, ScheduleTime $scheduleTime)
135 {
136 $scheduleData = array(
137 "scheduled_job_id" => $elementId,
138 "scheduled_job_type" => Config::TYPE,
139 "endpoint" => get_site_url(),
140 "key" => \PMXE_Plugin::getInstance()->getOption('cron_job_key'),
141 'schedule' => array(
142 'monthly' => $scheduleTime->isMonthly(),
143 'timezone' => $scheduleTime->getTimezone(),
144 'times' => $scheduleTime->getTime()
145 )
146 );
147
148 $this->schedulingApi->createSchedule($scheduleData);
149 }
150
151 private function updateSchedule($scheduleId, ScheduleTime $scheduleTime, $enabled = true)
152 {
153
154 $scheduleTime = array(
155 'enabled' => $enabled,
156 'schedule' => array(
157 'timezone' => $scheduleTime->getTimezone(),
158 'monthly' => $scheduleTime->isMonthly(),
159 'times' => $scheduleTime->getTime()
160 ));
161
162 $this->schedulingApi->updateSchedule($scheduleId, $scheduleTime);
163 }
164
165 public function userDisableSchedule($elementId)
166 {
167 $remoteSchedule = $this->getSchedule($elementId);
168
169 if ($remoteSchedule) {
170 $this->schedulingApi->disableSchedule($remoteSchedule->id);
171 }
172 }
173
174 public function userEnableSchedule($elementId)
175 {
176 $remoteSchedule = $this->getSchedule($elementId);
177
178 if ($remoteSchedule) {
179 $this->schedulingApi->enableSchedule($remoteSchedule->id);
180 }
181 }
182
183 public static function buildTimesArray($schedulingWeeklyDays, $schedulingTimes)
184 {
185
186 $times = array();
187 $days = explode(',', $schedulingWeeklyDays);
188 foreach ($days as $day) {
189 foreach ($schedulingTimes as $time) {
190
191 if (!$time) {
192 break;
193 }
194
195 $timeParts = explode(':', $time);
196 $hour = (int)$timeParts[0];
197 $min = (int)$timeParts[1];
198
199 if (strpos($time, 'pm') !== false && $hour < 12) {
200 $hour = $hour + 12;
201 }
202
203 if($hour == 12) {
204 if(strpos($time, 'am') !== false) {
205 $hour = 0;
206 }
207 }
208
209 $times[] = array(
210 'day' => $day,
211 'hour' => $hour,
212 'min' => $min
213 );
214 }
215 }
216
217 return $times;
218 }
219
220 public function updateApiKey($elementId, $newKey) {
221
222 $remoteSchedule = $this->getSchedule($elementId);
223
224 if ($remoteSchedule) {
225 $this->schedulingApi->updateScheduleKey($remoteSchedule->id, $newKey);
226 }
227 }
228
229 /**
230 * TODO: Uglier but simpler method, if this gets in the way, extract to a class
231 *
232 * @return Scheduling
233 */
234 public static function create()
235 {
236 $schedulingApi = new SchedulingApi(Config::API_URL);
237 $licensingManager = new LicensingManager();
238
239 return new Scheduling($schedulingApi, $licensingManager);
240 }
241 }