rval' => $this->checkPeriod * 3600, 'display' => sprintf('Every %d hours', $this->checkPeriod), ); } return $schedules; } } class Wslm_LicenseManagerApi { private $apiUrl; public function __construct($apiUrl) { $this->apiUrl = $apiUrl; } public function getLicense($product, $key, $siteUrl = null) { $params = ($siteUrl !== null) ? array('site_url' => $siteUrl) : array(); return $this->get($this->endpoint($product, $key), $params); } public function licenseSite($product, $key, $siteUrl) { $params = array('site_url' => $siteUrl); return $this->post($this->endpoint($product, $key, null, 'license_site'), $params); } public function unlicenseSite($product, $key, $siteUrl) { $params = array('site_url' => $siteUrl); return $this->post($this->endpoint($product, $key, null, 'unlicense_site'), $params); } public function getLicenseByToken($product, $token, $siteUrl = null) { $params = ($siteUrl !== null) ? array('site_url' => $siteUrl) : array(); return $this->get($this->endpoint($product, null, $token), $params); } public function unlicenseSiteByToken($product, $token, $siteUrl) { $params = array('site_url' => $siteUrl); return $this->post($this->endpoint($product, null, $token, 'unlicense_site'), $params); } public function get($endpoint, $params = array()) { return $this->request('get', $endpoint, $params); } public function post($endpoint, $params = array()) { return $this->request('post', $endpoint, $params); } private function endpoint($product, $license = null, $token = null, $action = null) { $endpoint = '/products/' . urlencode($product) . '/licenses/'; $endpoint .= ($token !== null) ? 'bytoken/' . urlencode($token) : urlencode($license); if ( $action !== null ) { $endpoint .= '/' . $action; } return $endpoint; } /** * Send an API request. * * @param string $method * @param string $endpoint * @param array $params * @return Wslm_LicenseManagerApiResponse */ public function request($method, $endpoint, $params = array()) { $url = $this->getApiUrl($endpoint); $method = strtoupper($method); $args = array('method' => $method, 'timeout' => 30); if ( !empty($params) ) { if ( ($method === 'POST') || ($method === 'PUT') ) { $args['body'] = $params; } else { $url .= '?' . http_build_query($params, '', '&'); } } $response = wp_remote_request($url, $args); return new Wslm_LicenseManagerApiResponse($response); } private function getApiUrl($endpoint) { return rtrim($this->apiUrl, '/') . '/' . ltrim($endpoint, '/'); } } class Wslm_LicenseManagerApiResponse { public $response = null; public $httpCode; public $httpResponse; private $error = null; /** * @param array|WP_Error $httpResponse */ public function __construct($httpResponse) { $this->httpResponse = $httpResponse; $this->httpCode = intval(wp_remote_retrieve_response_code($httpResponse)); if ( is_wp_error($httpResponse) ) { $this->error = new WP_Error('api_request_failed', $httpResponse->get_error_message(), $this); $this->response = null; } else { //Attempt to parse the API response. Expect a JSON document. $data = json_decode(wp_remote_retrieve_body($httpResponse)); if ( $data === null ) { $this->error = new WP_Error( 'api_request_failed', 'Failed to parse the response returned by the licensing API (expected JSON).', $this ); } $this->response = $data; } } public function success() { return empty($this->error) && ($this->httpCode >= 200 && $this->httpCode < 400); } public function asWpError() { if ( $this->success() ) { return null; } if ( !empty($this->error) ) { return $this->error; } else if ( isset($this->response->error) ) { return new WP_Error($this->response->error->code, $this->response->error->message, $this); } else { return new WP_Error('http_' . $this->httpCode, 'HTTP error ' . $this->httpCode, $this); } } }