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 / SchedulingApi.php
wp-all-export / src / Scheduling Last commit date
Exception 8 years ago Interval 8 years ago Timezone 7 years ago views 4 weeks ago Config.php 8 years ago Connection.php 8 years ago Export.php 4 weeks ago LicensingManager.php 1 year ago Scheduling.php 1 year ago SchedulingApi.php 3 years ago
SchedulingApi.php
185 lines
1 <?php
2
3 namespace Wpae\Scheduling;
4
5
6 use Wpae\Scheduling\Exception\SchedulingHttpException;
7
8 class SchedulingApi
9 {
10 private $apiUrl;
11
12 public function __construct($apiUrl)
13 {
14 $this->apiUrl = $apiUrl;
15 }
16
17 public function checkConnection()
18 {
19 if (is_multisite()) {
20 $siteUrl = get_site_url(get_current_blog_id());
21 } else {
22 $siteUrl = get_site_url();
23 }
24
25 $pingBackUrl = $this->getApiUrl('connection') . '?url=' . urlencode($siteUrl);
26
27 $response = wp_remote_request(
28 $pingBackUrl,
29 array(
30 'method' => 'GET'
31 )
32 );
33
34 if ($response instanceof \WP_Error) {
35 return false;
36 }
37
38 if ($response['response']['code'] == 200) {
39 return true;
40 } else {
41 return false;
42 }
43 }
44
45 public function getSchedules($elementId, $elementType)
46 {
47 $response = wp_remote_request(
48
49 $this->getApiUrl('schedules?forElement=' . $elementId .
50 '&type=' . $elementType .
51 '&endpoint=' . urlencode(get_site_url())),
52 array(
53 'method' => 'GET',
54 'headers' => $this->getHeaders()
55 )
56 );
57
58 if ($response instanceof \WP_Error) {
59 return false;
60 }
61
62 return json_decode($response['body']);
63 }
64
65 public function getSchedule($scheduleId)
66 {
67 wp_remote_request(
68
69 $this->getApiUrl('schedules/' . $scheduleId),
70 array(
71 'method' => 'GET',
72 'headers' => $this->getHeaders()
73 )
74 );
75 }
76
77 public function createSchedule($scheduleData)
78 {
79
80 $response = wp_remote_request(
81 $this->getApiUrl('schedules'),
82 array(
83 'method' => 'PUT',
84 'headers' => $this->getHeaders(),
85 'body' => json_encode($scheduleData)
86 )
87 );
88
89 if ($response instanceof \WP_Error) {
90 throw new SchedulingHttpException('There was a problem saving the schedule');
91 }
92
93 return $response;
94 }
95
96 public function deleteSchedule($remoteScheduleId)
97 {
98
99 wp_remote_request(
100 $this->getApiUrl('schedules/' . $remoteScheduleId),
101 array(
102 'method' => 'DELETE',
103 'headers' => $this->getHeaders()
104 )
105 );
106 }
107
108 public function disableSchedule($remoteScheduleId)
109 {
110 wp_remote_request(
111 $this->getApiUrl('schedules/' . $remoteScheduleId . '/disable'),
112 array(
113 'method' => 'DELETE',
114 'headers' => $this->getHeaders()
115 )
116 );
117 }
118
119
120 public function enableSchedule($scheduleId)
121 {
122 wp_remote_request(
123 $this->getApiUrl('schedules/' . $scheduleId . '/enable'),
124 array(
125 'method' => 'POST',
126 'headers' => $this->getHeaders()
127 )
128 );
129 }
130
131
132 public function updateSchedule($scheduleId, $scheduleTime)
133 {
134
135 $response = wp_remote_request(
136 $this->getApiUrl('schedules/' . $scheduleId),
137 array(
138 'method' => 'POST',
139 'headers' => $this->getHeaders(),
140 'body' => json_encode($scheduleTime)
141 ));
142
143 if ($response instanceof \WP_Error) {
144 throw new SchedulingHttpException('There was a problem saving the schedule');
145 }
146
147 return $response;
148 }
149
150 public function updateScheduleKey($remoteScheduleId, $newKey)
151 {
152 wp_remote_request(
153 $this->getApiUrl('schedules/' . $remoteScheduleId . '/key'),
154 array(
155 'method' => 'POST',
156 'headers' => $this->getHeaders(),
157 'body' => json_encode(['key' => $newKey])
158
159 )
160 );
161 }
162
163 private function getHeaders()
164 {
165
166 $options = \PMXE_Plugin::getInstance()->getOption();
167
168 if (!empty($options['scheduling_license'])) {
169 return array(
170 'Authorization' => 'License ' . \PMXE_Plugin::decode($options['scheduling_license'])
171 );
172 } else {
173 //TODO: Throw custom exception
174 throw new \Exception('No license present');
175 }
176 }
177
178 /**
179 * @return string
180 */
181 private function getApiUrl($resource)
182 {
183 return $this->apiUrl . '/' . $resource;
184 }
185 }