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

236 lines 5.8 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 = 'https://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 = $this->api_version . sprintf('/forms/%s/subscribe', $form_id);
95
96 $args = array(
97 'api_key' => $this->api_key,
98 'email' => $options['email'],
99 'name' => $options['name'],
100 );
101
102 return $this->make_request($request, 'POST', $args);
103 }
104
105 /**
106 * Remove subscription from a form
107 *
108 * @param array $options Array of user data
109 * @return object Response object
110 */
111 public function form_unsubscribe($options) {
112 $request = $this->api_version . '/unsubscribe';
113
114 $args = array(
115 'api_secret' => $this->api_secret,
116 'email' => $options['email']
117 );
118
119 return $this->make_request($request, 'PUT', $args);
120 }
121
122 /**
123 * Get markup from ConvertKit for the provided $url
124 *
125 * @param $url
126 * @return string
127 */
128 public function get_resource($url) {
129 $resource = '';
130
131 if(!empty($url) && isset($this->markup[$url])) {
132 $resource = $this->markup[$url];
133 } else if(!empty($url)) {
134 $response = wp_remote_get($url, array( 'timeout' => 10 ));
135
136 if(!is_wp_error($response)) {
137 if(!function_exists('str_get_html')) {
138 require_once(dirname(__FILE__).'/../vendor/simple-html-dom/simple-html-dom.php');
139 }
140
141 if(!function_exists('url_to_absolute')) {
142 require_once(dirname(__FILE__).'/../vendor/url-to-absolute/url-to-absolute.php');
143 }
144
145 $url_parts = parse_url($url);
146
147 $body = wp_remote_retrieve_body($response);
148 $html = str_get_html($body);
149 foreach($html->find('a, link') as $element) {
150 if(isset($element->href)) {
151 $element->href = url_to_absolute($url, $element->href);
152 }
153 }
154
155 foreach($html->find('img, script') as $element) {
156 if(isset($element->src)) {
157 $element->src = url_to_absolute($url, $element->src);
158 }
159 }
160
161 foreach($html->find('form') as $element) {
162 if(isset($element->action)) {
163 $element->action = url_to_absolute($url, $element->action);
164 } else {
165 $element->action = $url;
166 }
167 }
168
169 $this->markup[$url] = $resource = $html->save();
170 }
171 }
172
173 return $resource;
174 }
175
176 /**
177 * Do a remote request.
178 *
179 * @param string $path
180 * @return array
181 */
182 private function _get_api_response($path = '') {
183 $args = array('api_key' => $this->api_key);
184 $api_path = $this->api_url_base . $this->api_version;
185 $url = add_query_arg($args, path_join($api_path, $path));
186 $response = wp_remote_get($url, array( 'timeout' => 10, 'sslverify' => false));
187
188 if(is_wp_error($response)) {
189 return array();
190 } else {
191 $data = json_decode(wp_remote_retrieve_body($response), true);
192 }
193
194 return $data;
195 }
196
197 /**
198 * Make a request to the ConvertKit API
199 *
200 * @param string $request Request string
201 * @param string $method HTTP Method
202 * @param array $args Request arguments
203 * @return object Response object
204 */
205 public function make_request($request, $method = 'GET', $args = array()) {
206 $url = $this->build_request_url($request, $args);
207
208 $ch = curl_init();
209 curl_setopt($ch, CURLOPT_URL, $url);
210 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
211 curl_setopt($ch, CURLOPT_HEADER, false);
212 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
213 if ( 'PUT' == $method ){
214 curl_setopt($ch, CURLOPT_PUT, true);
215 //curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
216 }
217
218 $results = curl_exec($ch);
219 curl_close($ch);
220
221 return json_decode($results);
222 }
223
224 /**
225 * Build the full request URL
226 *
227 * @param string $request Request path
228 * @param array $args Request arguments
229 * @return string Request URL
230 */
231 public function build_request_url($request, array $args) {
232 return $this->api_url_base . $request . '?' . http_build_query( $args );
233 }
234
235 }
236