PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.4.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.4.1
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.4.1, at lib/convertkit-api.php

241 lines 5.9 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 /** @var string */
9 protected $api_key;
10
11 /** @var string */
12 protected $api_secret;
13
14 /** @var string */
15 protected $api_version = 'v3';
16
17 /** @var string */
18 protected $api_url_base = 'http://api.convertkit.com/';
19
20 /** @var array */
21 protected $resources = array();
22
23 /** @var array */
24 protected $markup = array();
25
26 /**
27 * Constructor for ConvertKitAPI instance
28 *
29 * @param string $api_key ConvertKit API Key
30 * @param string $api_secret ConvertKit API Secret
31 */
32 public function __construct($api_key, $api_secret) {
33 $this->api_key = $api_key;
34 $this->api_secret = $api_secret;
35 }
36
37 /**
38 * Gets a resource index
39 *
40 * GET /{$resource}/
41 *
42 * @param string $resource Resource type
43 * @return object API response
44 */
45 public function get_resources($resource) {
46
47 if(!array_key_exists($resource, $this->resources)) {
48
49 if ( $resource == 'landing_pages' ) {
50 $api_response = $this->_get_api_response( 'forms' );
51 } else {
52 $api_response = $this->_get_api_response( $resource );
53 }
54
55 if (is_null($api_response) || is_wp_error($api_response) || isset($api_response['error']) || isset($api_response['error_message'])) {
56 $this->resources[$resource] = array( array('id' => '-2', 'name' => 'Error contacting API' ) );
57 } else {
58 $_resource = array();
59
60 if ( 'forms' == $resource ) {
61 $response = isset( $api_response['forms']) ? $api_response['forms'] : array();
62 foreach( $response as $form ) {
63 $_resource[] = $form;
64 }
65 } elseif ( 'landing_pages' == $resource ) {
66
67 $response = isset( $api_response['forms']) ? $api_response['forms'] : array();
68 foreach( $response as $landing_page ){
69 if ( 'hosted' == $landing_page['type'] ){
70 $_resource[] = $landing_page;
71 }
72 }
73 } elseif ( 'subscription_forms' == $resource ) {
74 foreach( $api_response as $mapping ){
75 $_resource[ $mapping['id'] ] = $mapping['form_id'];
76 }
77 }
78
79 $this->resources[$resource] = $_resource;
80 }
81 }
82
83 return $this->resources[$resource];
84 }
85
86 /**
87 * Adds a subscriber to a form
88 *
89 * @param string $form_id Form ID
90 * @param array $options Array of user data
91 * @return object
92 */
93 public function form_subscribe($form_id, $options) {
94 $request = sprintf('forms/%s/subscribe', $form_id);
95
96 $args = array(
97 'email' => $options['email'],
98 'fname' => $options['fname']
99 );
100
101 return $this->make_request($request, 'POST', $args);
102 }
103
104 /**
105 * Remove subscription from a form
106 *
107 * @param array $options Array of user data
108 * @return object Response object
109 */
110 public function form_unsubscribe($options) {
111 $request = 'unsubscribe';
112
113 $args = array(
114 'api_secret' => $this->api_secret,
115 'email' => $options['email']
116 );
117
118 return $this->make_request($request, 'POST', $args);
119 }
120
121 /**
122 * Get markup from ConvertKit for the provided $url
123 *
124 * @param $url
125 * @return string
126 */
127 public function get_resource($url) {
128 $resource = '';
129
130 if(!empty($url) && isset($this->markup[$url])) {
131 $resource = $this->markup[$url];
132 } else if(!empty($url)) {
133 $response = wp_remote_get($url, array( 'timeout' => 10 ));
134
135 if(!is_wp_error($response)) {
136 if(!function_exists('str_get_html')) {
137 require_once(dirname(__FILE__).'/../vendor/simple-html-dom/simple-html-dom.php');
138 }
139
140 if(!function_exists('url_to_absolute')) {
141 require_once(dirname(__FILE__).'/../vendor/url-to-absolute/url-to-absolute.php');
142 }
143
144 $url_parts = parse_url($url);
145
146 $body = wp_remote_retrieve_body($response);
147 $html = str_get_html($body);
148 foreach($html->find('a, link') as $element) {
149 if(isset($element->href)) {
150 $element->href = url_to_absolute($url, $element->href);
151 }
152 }
153
154 foreach($html->find('img, script') as $element) {
155 if(isset($element->src)) {
156 $element->src = url_to_absolute($url, $element->src);
157 }
158 }
159
160 foreach($html->find('form') as $element) {
161 if(isset($element->action)) {
162 $element->action = url_to_absolute($url, $element->action);
163 } else {
164 $element->action = $url;
165 }
166 }
167
168 $this->markup[$url] = $resource = $html->save();
169 }
170 }
171
172 return $resource;
173 }
174
175 /**
176 * Do a remote request.
177 *
178 * @param string $path
179 * @return array
180 */
181 private function _get_api_response($path = '') {
182 $args = array('api_key' => $this->api_key);
183 $api_path = $this->api_url_base . $this->api_version;
184 $url = add_query_arg($args, path_join($api_path, $path));
185 $response = wp_remote_get($url, array( 'timeout' => 10, 'sslverify' => false));
186
187 if(is_wp_error($response)) {
188 return array();
189 } else {
190 $data = json_decode(wp_remote_retrieve_body($response), true);
191 }
192
193 return $data;
194 }
195
196 /**
197 * Make a request to the ConvertKit API
198 *
199 * @param string $request Request string
200 * @param string $method HTTP Method
201 * @param array $args Request arguments
202 * @return object Response object
203 */
204 public function make_request($request, $method = 'GET', $args = array()) {
205 $url = $this->build_request_url($request, $args);
206
207 $ch = curl_init();
208 curl_setopt($ch, CURLOPT_URL, $url);
209 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
210 curl_setopt($ch, CURLOPT_HEADER, false);
211 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
212
213 $results = curl_exec($ch);
214 curl_close($ch);
215
216 return json_decode($results);
217 }
218
219 /**
220 * Merge default request arguments with those of this request
221 *
222 * @param array $args Request arguments
223 * @return array Request arguments
224 */
225 public function filter_request_arguments($args = array()) {
226 return array_merge($args, array('k' => $this->api_key, 'v' => $this->api_version));
227 }
228
229 /**
230 * Build the full request URL
231 *
232 * @param string $request Request path
233 * @param array $args Request arguments
234 * @return string Request URL
235 */
236 public function build_request_url($request, array $args) {
237 return $this->api_url_base . $request . '?' . http_build_query($this->filter_request_arguments($args));
238 }
239
240 }
241