PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.3.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.3.8
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / lib / convertkit-api.php

convertkit-api.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 1.3.8, at lib/convertkit-api.php

188 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Establishes API connection to ConvertKit App
5 */
6 class ConvertKitAPI {
7
8 protected $api_key;
9
10 protected $api_version = 2;
11 protected $api_url_base = 'https://api.convertkit.com/';
12 protected $resources = array();
13 protected $markup = array();
14
15 /**
16 * Constructor for ConvertKitAPI instance
17 *
18 * @param String $api_key ConvertKit API Key
19 */
20 public function __construct($api_key) {
21 $this->api_key = $api_key;
22 }
23
24 /**
25 * Gets a resource index
26 *
27 * GET /{$resource}/
28 *
29 * @param string $resource Resource type
30 * @return object API response
31 */
32 public function get_resources($resource) {
33 if(!array_key_exists($resource, $this->resources)) {
34 $api_response = $this->_get_api_response($resource);
35
36 if (is_null($api_response) || is_wp_error($api_response) || isset($api_response['error']) || isset($api_response['error_message'])) {
37 $this->resources[$resource] = array();
38 } else {
39 $this->resources[$resource] = $api_response;
40 }
41 }
42
43 return $this->resources[$resource];
44 }
45
46 /**
47 * Adds a subscriber to a form
48 *
49 * @param string $form_id Form ID
50 * @param array $options Array of user data
51 */
52 public function form_subscribe($form_id, $options) {
53 $request = sprintf('forms/%s/subscribe', $form_id);
54
55 $args = array(
56 'email' => $options['email'],
57 'fname' => $options['fname']
58 );
59
60 return $this->make_request($request, 'POST', $args);
61 }
62
63 /**
64 * Unsubscribes a subscriber from a form
65 *
66 * @param string $form_id Resource ID
67 * @param array $options Array of user data
68 */
69 public function form_unsubscribe($form_id, $options) {
70 $request = sprintf('forms/%s/unsubscribe', $form_id);
71
72 $args = array(
73 'email' => $options['email']
74 );
75
76 return $this->make_request($request, 'POST', $args);
77 }
78
79 public function get_resource($url) {
80 $resource = '';
81
82 if(!empty($url) && isset($this->markup[$url])) {
83 $resource = $this->markup[$url];
84 } else if(!empty($url)) {
85 $response = wp_remote_get($url, array( 'timeout' => 2 ));
86
87 if(!is_wp_error($response)) {
88 if(!function_exists('str_get_html')) {
89 require_once(dirname(__FILE__).'/../vendor/simple-html-dom/simple-html-dom.php');
90 }
91
92 if(!function_exists('url_to_absolute')) {
93 require_once(dirname(__FILE__).'/../vendor/url-to-absolute/url-to-absolute.php');
94 }
95
96 $url_parts = parse_url($url);
97
98 $body = wp_remote_retrieve_body($response);
99 $html = str_get_html($body);
100 foreach($html->find('a, link') as $element) {
101 if(isset($element->href)) {
102 $element->href = url_to_absolute($url, $element->href);
103 }
104 }
105
106 foreach($html->find('img, script') as $element) {
107 if(isset($element->src)) {
108 $element->src = url_to_absolute($url, $element->src);
109 }
110 }
111
112 foreach($html->find('form') as $element) {
113 if(isset($element->action)) {
114 $element->action = url_to_absolute($url, $element->action);
115 } else {
116 $element->action = $url;
117 }
118 }
119
120 $this->markup[$url] = $resource = $html->save();
121 }
122 }
123
124 return $resource;
125 }
126
127 private function _get_api_response($path = '') {
128 $args = array('k' => $this->api_key, 'v' => $this->api_version);
129 $url = add_query_arg($args, path_join($this->api_url_base, $path));
130
131 $response = wp_remote_get($url, array( 'timeout' => 2 ));
132
133 if(is_wp_error($response)) {
134 $data = $response;
135 } else {
136 $data = json_decode(wp_remote_retrieve_body($response), true);
137 }
138
139 return $data;
140 }
141
142 /**
143 * Make a request to the ConvertKit API
144 *
145 * @param string $request Request string
146 * @param string $method HTTP Method
147 * @param array $args Request arguments
148 * @return object Response object
149 */
150 public function make_request($request, $method = 'GET', $args = array()) {
151 $url = $this->build_request_url($request, $args);
152
153 $ch = curl_init();
154 curl_setopt($ch, CURLOPT_URL, $url);
155 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
156 curl_setopt($ch, CURLOPT_HEADER, false);
157 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
158
159 $results = curl_exec($ch);
160
161 curl_close($ch);
162
163 return json_decode($results);
164 }
165
166 /**
167 * Merge default request arguments with those of this request
168 *
169 * @param array $args Request arguments
170 * @return array Request arguments
171 */
172 public function filter_request_arguments($args = array()) {
173 return array_merge($args, array('k' => $this->api_key, 'v' => $this->api_version));
174 }
175
176 /**
177 * Build the full request URL
178 *
179 * @param string $request Request path
180 * @param array $args Request arguments
181 * @return string Request URL
182 */
183 public function build_request_url($request, array $args) {
184 return $this->api_url_base . $request . '?' . http_build_query($this->filter_request_arguments($args));
185 }
186
187 }
188