Нема описа

API.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace MailPoet\Services\Release;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class API {
  6. private $apiKey;
  7. private $wp;
  8. public $urlProducts = 'https://release.mailpoet.com/products/';
  9. public function __construct(
  10. $apiKey
  11. ) {
  12. $this->setKey($apiKey);
  13. $this->wp = new WPFunctions();
  14. }
  15. public function getPluginInformation($pluginName) {
  16. $result = $this->request(
  17. $this->urlProducts . $pluginName
  18. );
  19. $code = $this->wp->wpRemoteRetrieveResponseCode($result);
  20. switch ($code) {
  21. case 200:
  22. $body = $this->wp->wpRemoteRetrieveBody($result);
  23. if ($body) {
  24. $body = json_decode($body);
  25. }
  26. break;
  27. default:
  28. $body = null;
  29. break;
  30. }
  31. return $body;
  32. }
  33. public function setKey($apiKey) {
  34. $this->apiKey = $apiKey;
  35. }
  36. public function getKey() {
  37. return $this->apiKey;
  38. }
  39. private function request($url, $params = []) {
  40. $params['license'] = $this->apiKey;
  41. $url = WPFunctions::get()->addQueryArg($params, $url);
  42. $args = [
  43. 'timeout' => 10,
  44. 'httpversion' => '1.0',
  45. ];
  46. return $this->wp->wpRemoteGet($url, $args);
  47. }
  48. }