Exception
7 years ago
Interval
7 years ago
Timezone
7 years ago
views
7 years ago
Config.php
7 years ago
Connection.php
7 years ago
Export.php
7 years ago
LicensingManager.php
7 years ago
Scheduling.php
7 years ago
SchedulingApi.php
7 years ago
SchedulingApi.php
147 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($scheduleId) |
| 97 | { |
| 98 | wp_remote_request( |
| 99 | $this->getApiUrl('schedules/' . $scheduleId), |
| 100 | array( |
| 101 | 'method' => 'DELETE', |
| 102 | 'headers' => $this->getHeaders() |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | public function updateSchedule($scheduleId, $scheduleTime) |
| 108 | { |
| 109 | |
| 110 | $response = wp_remote_request( |
| 111 | $this->getApiUrl('schedules/' . $scheduleId), |
| 112 | array( |
| 113 | 'method' => 'POST', |
| 114 | 'headers' => $this->getHeaders(), |
| 115 | 'body' => json_encode($scheduleTime) |
| 116 | )); |
| 117 | |
| 118 | if($response instanceof \WP_Error) { |
| 119 | throw new SchedulingHttpException('There was a problem saving the schedule'); |
| 120 | } |
| 121 | |
| 122 | return $response; |
| 123 | } |
| 124 | |
| 125 | private function getHeaders() |
| 126 | { |
| 127 | |
| 128 | $options = \PMXE_Plugin::getInstance()->getOption(); |
| 129 | |
| 130 | if (!empty($options['scheduling_license'])) { |
| 131 | return array( |
| 132 | 'Authorization' => 'License ' . \PMXE_Plugin::decode($options['scheduling_license']) |
| 133 | ); |
| 134 | } else { |
| 135 | //TODO: Throw custom exception |
| 136 | throw new \Exception('No license present'); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @return string |
| 142 | */ |
| 143 | private function getApiUrl($resource) |
| 144 | { |
| 145 | return $this->apiUrl . '/' . $resource; |
| 146 | } |
| 147 | } |