PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.2.10
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.2.10
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 4 years ago Interval 4 years ago Timezone 4 years ago views 4 years ago Config.php 4 years ago Connection.php 4 years ago Export.php 4 years ago LicensingManager.php 4 years ago Scheduling.php 4 years ago SchedulingApi.php 4 years ago
Scheduling.php
227 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 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(!$this->checkLicense()) {
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 $schedulingEnabled = $post['scheduling_enable'];
87
88 if ($schedulingEnabled == 1) {
89
90 $this->userEnableSchedule($id);
91
92 if ($post['scheduling_run_on'] == 'weekly') {
93 $monthly = false;
94 } else {
95 $monthly = true;
96 }
97
98 if($monthly) {
99 $timesArray = self::buildTimesArray($post['scheduling_monthly_days'], $post['scheduling_times']);
100 } else {
101 $timesArray = self::buildTimesArray($post['scheduling_weekly_days'], $post['scheduling_times']);
102 }
103 $this->schedule(
104 $id,
105 new \Wpae\Scheduling\Interval\ScheduleTime($timesArray, $monthly, $post['scheduling_timezone']),
106 $schedulingEnabled
107 );
108 } else {
109 $this->userDisableSchedule($id);
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 public function userDisableSchedule($elementId)
161 {
162 $remoteSchedule = $this->getSchedule($elementId);
163
164 if ($remoteSchedule) {
165 $this->schedulingApi->disableSchedule($remoteSchedule->id);
166 }
167 }
168
169 public function userEnableSchedule($elementId)
170 {
171 $remoteSchedule = $this->getSchedule($elementId);
172
173 if ($remoteSchedule) {
174 $this->schedulingApi->enableSchedule($remoteSchedule->id);
175 }
176 }
177
178 public static function buildTimesArray($schedulingWeeklyDays, $schedulingTimes)
179 {
180
181 $times = array();
182 $days = explode(',', $schedulingWeeklyDays);
183 foreach ($days as $day) {
184 foreach ($schedulingTimes as $time) {
185
186 if (!$time) {
187 break;
188 }
189
190 $timeParts = explode(':', $time);
191 $hour = $timeParts[0];
192 $min = (int)$timeParts[1];
193
194 if (strpos($time, 'pm') !== false && $hour < 12) {
195 $hour = $hour + 12;
196 }
197
198 if($hour == 12) {
199 if(strpos($time, 'am') !== false) {
200 $hour = 0;
201 }
202 }
203
204 $times[] = array(
205 'day' => $day,
206 'hour' => $hour,
207 'min' => $min
208 );
209 }
210 }
211
212 return $times;
213 }
214
215 /**
216 * TODO: Uglier but simpler method, if this gets in the way, extract to a class
217 *
218 * @return Scheduling
219 */
220 public static function create()
221 {
222 $schedulingApi = new SchedulingApi(Config::API_URL);
223 $licensingManager = new LicensingManager();
224
225 return new Scheduling($schedulingApi, $licensingManager);
226 }
227 }