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
SchedulingApi.php
172 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 | private function getHeaders() |
| 151 | { |
| 152 | |
| 153 | $options = \PMXE_Plugin::getInstance()->getOption(); |
| 154 | |
| 155 | if (!empty($options['scheduling_license'])) { |
| 156 | return array( |
| 157 | 'Authorization' => 'License ' . \PMXE_Plugin::decode($options['scheduling_license']) |
| 158 | ); |
| 159 | } else { |
| 160 | //TODO: Throw custom exception |
| 161 | throw new \Exception('No license present'); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return string |
| 167 | */ |
| 168 | private function getApiUrl($resource) |
| 169 | { |
| 170 | return $this->apiUrl . '/' . $resource; |
| 171 | } |
| 172 | } |