ConvertKit.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Establishes API connection to ConvertKit App
  4. */
  5. class ConvertKit {
  6. protected $api_key;
  7. protected $api_version = 3;
  8. protected $api_url_base = 'https://api.convertkit.com/';
  9. protected $resources = array();
  10. protected $markup = array();
  11. static $response = array();
  12. /**
  13. * Constructor for ConvertKit instance
  14. *
  15. * @param String $api_key ConvertKit API Key
  16. */
  17. public function __construct($api_key) {
  18. $this->api_key = $api_key;
  19. $this->api_url_base .= 'v' . $this->api_version . '/';
  20. }
  21. /**
  22. * Check if the API connection is authenticated
  23. *
  24. * @param string $resource
  25. * @return boolean
  26. */
  27. public function is_authenticated() {
  28. $api_response = $this->_get_api_response('forms');
  29. self::$response = $api_response;
  30. if (is_wp_error($api_response) || isset($api_response['error']) || isset($api_response['error_message'])) {
  31. return false;
  32. }
  33. return true;
  34. }
  35. /**
  36. * Gets a resource index
  37. *
  38. * GET /{$resource}/
  39. *
  40. * @param string $resource Resource type
  41. * @return object API response
  42. */
  43. public function get_resources($resource) {
  44. if(!array_key_exists($resource, $this->resources)) {
  45. $api_response = $this->_get_api_response($resource);
  46. self::$response = $api_response;
  47. if (is_wp_error($api_response) || isset($api_response['error']) || isset($api_response['error_message'])) {
  48. $this->resources[$resource] = array();
  49. } else {
  50. $this->resources[$resource] = $api_response;
  51. }
  52. }
  53. return $this->resources[$resource];
  54. }
  55. /**
  56. * Adds a subscriber to a form
  57. *
  58. * @param string $form_id Form ID
  59. * @param array $options Array of user data
  60. */
  61. public function form_subscribe($form_id, $options) {
  62. $request = sprintf('forms/%s/subscribe', $form_id);
  63. $args = array(
  64. 'email' => $options['email'],
  65. 'first_name' => isset( $options['fname'] ) ? $options['fname'] : '',
  66. );
  67. return $this->make_request($request, 'POST', $args);
  68. }
  69. /**
  70. * Unsubscribes a subscriber from a form
  71. *
  72. * @param string $form_id Resource ID
  73. * @param array $options Array of user data
  74. */
  75. public function form_unsubscribe($form_id, $options) {
  76. $request = sprintf('forms/%s/unsubscribe', $form_id);
  77. $args = array(
  78. 'email' => $options['email']
  79. );
  80. return $this->make_request($request, 'POST', $args);
  81. }
  82. /**
  83. * Get API response
  84. * @param string $path
  85. * @return array|object
  86. */
  87. private function _get_api_response($path = '') {
  88. $args = array('api_key' => $this->api_key);
  89. $url = add_query_arg($args, path_join($this->api_url_base, $path));
  90. $response = wp_remote_get($url);
  91. if(is_wp_error($response)) {
  92. $data = $response;
  93. } else {
  94. $data = json_decode(wp_remote_retrieve_body($response), true);
  95. }
  96. return $data;
  97. }
  98. /**
  99. * Make a request to the ConvertKit API
  100. *
  101. * @param string $request Request string
  102. * @param string $method HTTP Method
  103. * @param array $args Request arguments
  104. * @return object Response object
  105. */
  106. public function make_request($request, $method = 'GET', $args = array()) {
  107. $url = $this->build_request_url($request, $args);
  108. $ch = curl_init();
  109. curl_setopt($ch, CURLOPT_URL, $url);
  110. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  111. curl_setopt($ch, CURLOPT_HEADER, false);
  112. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  113. $results = curl_exec($ch);
  114. curl_close($ch);
  115. return json_decode($results);
  116. }
  117. /**
  118. * Build the full request URL
  119. *
  120. * @param string $request Request path
  121. * @param array $args Request arguments
  122. * @return string Request URL
  123. */
  124. public function build_request_url($request, array $args) {
  125. return $this->api_url_base . $request . '?' . http_build_query($this->filter_request_arguments($args));
  126. }
  127. /**
  128. * Merge default request arguments with those of this request
  129. *
  130. * @param array $args Request arguments
  131. * @return array Request arguments
  132. */
  133. public function filter_request_arguments($args = array()) {
  134. return array_merge($args, array('api_key' => $this->api_key));
  135. }
  136. }