SchedulingApi.php
4.43 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Wpae\Scheduling;
use Wpae\Scheduling\Exception\SchedulingHttpException;
class SchedulingApi
{
private $apiUrl;
public function __construct($apiUrl)
{
$this->apiUrl = $apiUrl;
}
public function checkConnection()
{
if (is_multisite()) {
$siteUrl = get_site_url(get_current_blog_id());
} else {
$siteUrl = get_site_url();
}
$pingBackUrl = $this->getApiUrl('connection') . '?url=' . urlencode($siteUrl);
$response = wp_remote_request(
$pingBackUrl,
array(
'method' => 'GET'
)
);
if ($response instanceof \WP_Error) {
return false;
}
if ($response['response']['code'] == 200) {
return true;
} else {
return false;
}
}
public function getSchedules($elementId, $elementType)
{
$response = wp_remote_request(
$this->getApiUrl('schedules?forElement=' . $elementId .
'&type=' . $elementType .
'&endpoint=' . urlencode(get_site_url())),
array(
'method' => 'GET',
'headers' => $this->getHeaders()
)
);
if ($response instanceof \WP_Error) {
return false;
}
return json_decode($response['body']);
}
public function getSchedule($scheduleId)
{
wp_remote_request(
$this->getApiUrl('schedules/' . $scheduleId),
array(
'method' => 'GET',
'headers' => $this->getHeaders()
)
);
}
public function createSchedule($scheduleData)
{
$response = wp_remote_request(
$this->getApiUrl('schedules'),
array(
'method' => 'PUT',
'headers' => $this->getHeaders(),
'body' => json_encode($scheduleData)
)
);
if ($response instanceof \WP_Error) {
throw new SchedulingHttpException('There was a problem saving the schedule');
}
return $response;
}
public function deleteSchedule($remoteScheduleId)
{
wp_remote_request(
$this->getApiUrl('schedules/' . $remoteScheduleId),
array(
'method' => 'DELETE',
'headers' => $this->getHeaders()
)
);
}
public function disableSchedule($remoteScheduleId)
{
wp_remote_request(
$this->getApiUrl('schedules/' . $remoteScheduleId . '/disable'),
array(
'method' => 'DELETE',
'headers' => $this->getHeaders()
)
);
}
public function enableSchedule($scheduleId)
{
wp_remote_request(
$this->getApiUrl('schedules/' . $scheduleId . '/enable'),
array(
'method' => 'POST',
'headers' => $this->getHeaders()
)
);
}
public function updateSchedule($scheduleId, $scheduleTime)
{
$response = wp_remote_request(
$this->getApiUrl('schedules/' . $scheduleId),
array(
'method' => 'POST',
'headers' => $this->getHeaders(),
'body' => json_encode($scheduleTime)
));
if ($response instanceof \WP_Error) {
throw new SchedulingHttpException('There was a problem saving the schedule');
}
return $response;
}
public function updateScheduleKey($remoteScheduleId, $newKey)
{
wp_remote_request(
$this->getApiUrl('schedules/' . $remoteScheduleId . '/key'),
array(
'method' => 'POST',
'headers' => $this->getHeaders(),
'body' => json_encode(['key' => $newKey])
)
);
}
private function getHeaders()
{
$options = \PMXE_Plugin::getInstance()->getOption();
if (!empty($options['scheduling_license'])) {
return array(
'Authorization' => 'License ' . \PMXE_Plugin::decode($options['scheduling_license'])
);
} else {
//TODO: Throw custom exception
throw new \Exception('No license present');
}
}
/**
* @return string
*/
private function getApiUrl($resource)
{
return $this->apiUrl . '/' . $resource;
}
}