PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14.1
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14.1
7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / providers / convertkit / hustle-convertkit-api-v2.php
wordpress-popup / inc / providers / convertkit Last commit date
images 3 months ago convertkit.php 3 months ago hustle-convertkit-api-intefrace.php 3 months ago hustle-convertkit-api-v2.php 3 months ago hustle-convertkit-api.php 3 months ago hustle-convertkit-form-hooks.php 3 months ago hustle-convertkit-form-settings.php 5 months ago hustle-convertkit-v2.php 3 months ago hustle-convertkit.php 3 months ago
hustle-convertkit-api-v2.php
387 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Hustle_ConvertKit_API_V2 class
4 *
5 * @package Hustle
6 */
7
8 /**
9 * ConvertKit API V2 (implements Kit API V4)
10 *
11 * @class Hustle_ConvertKit_API_V2
12 **/
13 class Hustle_ConvertKit_API_V2 implements Hustle_ConvertKit_Api_Interface {
14
15 /**
16 * Api key
17 *
18 * @var string
19 */
20 private $api_key;
21
22 /**
23 * Endpoint
24 *
25 * @var string
26 */
27 private $endpoint = 'https://api.kit.com/v4/';
28
29 /**
30 * Constructs class with required data
31 *
32 * Hustle_ConvertKit_API_V2 constructor.
33 *
34 * @param string $api_key Api key.
35 * @param string $api_secret Api secret (not used in v4).
36 */
37 public function __construct( $api_key, $api_secret = '' ) {
38 $this->api_key = $api_key;
39 }
40
41 /**
42 * Sends request to the endpoint url with the provided $action
43 *
44 * @param string $action rest action.
45 * @param string $verb Verb.
46 * @param array $args Args.
47 * @return object|WP_Error
48 */
49 private function request( $action, $verb = 'GET', $args = array() ) {
50 $url = trailingslashit( $this->endpoint ) . $action;
51
52 $_args = array(
53 'method' => $verb,
54 'headers' => array(
55 'X-Kit-Api-Key' => $this->api_key,
56 'Content-Type' => 'application/json;charset=utf-8',
57 ),
58 );
59
60 if ( 'GET' === $verb ) {
61 if ( ! empty( $args ) ) {
62 $url .= ( '?' . http_build_query( $args ) );
63 }
64 } else {
65 $_args['body'] = wp_json_encode( $args );
66 }
67
68 $res = wp_remote_request( $url, $_args );
69
70 // logging data.
71 $utils = Hustle_Provider_Utils::get_instance();
72 $utils->last_url_request = $url;
73 $utils->last_data_sent = $_args;
74 $utils->last_data_received = $res;
75
76 if ( ! is_wp_error( $res ) && is_array( $res ) ) {
77 $code = $res['response']['code'];
78
79 if ( $code >= 200 && $code < 300 ) {
80 $body = wp_remote_retrieve_body( $res );
81 return json_decode( $body );
82 }
83
84 $err = new WP_Error();
85 $err->add( $code, $res['response']['message'] );
86 return $err;
87 }
88
89 return $res;
90 }
91
92 /**
93 * Sends rest GET request
94 *
95 * @param string $action Action.
96 * @param array $args Args.
97 * @return array|mixed|object|WP_Error
98 */
99 private function get( $action, $args = array() ) {
100 return $this->request( $action, 'GET', $args );
101 }
102
103 /**
104 * Sends rest POST request
105 *
106 * @param string $action Action.
107 * @param array $args Args.
108 * @return array|mixed|object|WP_Error
109 */
110 private function post( $action, $args = array() ) {
111 return $this->request( $action, 'POST', $args );
112 }
113
114 /**
115 * Sends rest PUT request
116 *
117 * @param string $action Action.
118 * @param array $args Args.
119 * @return array|mixed|object|WP_Error
120 */
121 private function put( $action, $args = array() ) {
122 return $this->request( $action, 'PUT', $args );
123 }
124
125 /**
126 * Retrieves ConvertKit forms as array of objects
127 *
128 * @return array|WP_Error
129 */
130 public function get_forms() {
131 $response = $this->get( 'forms' );
132
133 if ( is_wp_error( $response ) ) {
134 return $response;
135 }
136
137 if ( ! isset( $response->forms ) ) {
138 return new WP_Error( 'forms_not_found', __( 'Not found forms with this api key.', 'hustle' ) );
139 }
140
141 return $response->forms;
142 }
143
144 /**
145 * Retrieves ConvertKit subscribers as array of objects
146 *
147 * @return array|WP_Error
148 */
149 public function get_subscribers() {
150 $response = $this->get( 'subscribers' );
151
152 if ( is_wp_error( $response ) ) {
153 return $response;
154 }
155
156 if ( ! isset( $response->subscribers ) ) {
157 return new WP_Error( 'subscribers_not_found', __( 'Not found subscribers with this api key.', 'hustle' ) );
158 }
159
160 return $response->subscribers;
161 }
162
163 /**
164 * Retrieves ConvertKit form's custom fields as array of objects
165 *
166 * @return array|WP_Error
167 */
168 public function get_form_custom_fields() {
169 $response = $this->get( 'custom_fields' );
170
171 if ( is_wp_error( $response ) ) {
172 return $response;
173 }
174
175 if ( ! isset( $response->custom_fields ) ) {
176 return new WP_Error( 'custom_fields_not_found', __( 'Not found custom fields with this api key.', 'hustle' ) );
177 }
178
179 return $response->custom_fields;
180 }
181
182 /**
183 * Add new custom fields to subscription
184 *
185 * @param array $field_data Fields data.
186 * @return array|mixed|object|WP_Error
187 */
188 public function create_custom_fields( $field_data ) {
189 $args = array(
190 'label' => $field_data['label'],
191 );
192
193 $res = $this->post( 'custom_fields', $args );
194
195 return is_wp_error( $res ) ? $res : __( 'Successfully added custom field', 'hustle' );
196 }
197
198 /**
199 * Add new subscriber
200 *
201 * @param string $form_id Form ID.
202 * @param array $data Data.
203 * @return array|mixed|object|WP_Error
204 */
205 public function subscribe( $form_id, $data ) {
206 // First, create or update the subscriber.
207 $subscriber_data = array(
208 'email_address' => $data['email'],
209 );
210
211 if ( isset( $data['first_name'] ) ) {
212 $subscriber_data['first_name'] = $data['first_name'];
213 }
214
215 if ( isset( $data['fields'] ) ) {
216 $subscriber_data['fields'] = $data['fields'];
217 }
218
219 // Create subscriber using v4 API.
220 $subscriber_res = $this->post( 'subscribers', $subscriber_data );
221
222 if ( is_wp_error( $subscriber_res ) ) {
223 return $subscriber_res;
224 }
225
226 if ( ! isset( $subscriber_res->subscriber ) ) {
227 return new WP_Error( 'subscriber_creation_failed', __( 'Failed to create subscriber.', 'hustle' ) );
228 }
229
230 $subscriber_id = $subscriber_res->subscriber->id;
231
232 // Now add subscriber to the form.
233 $form_data = array(
234 'referrer' => isset( $data['referrer'] ) ? $data['referrer'] : '',
235 );
236
237 $url = 'forms/' . $form_id . '/subscribers/' . $subscriber_id;
238 $res = $this->post( $url, $form_data );
239
240 return is_wp_error( $res ) ? $res : __( 'Successful subscription', 'hustle' );
241 }
242
243 /**
244 * Update subscriber
245 *
246 * @since 4.0
247 *
248 * @param string $id ID.
249 * @param array $data Data.
250 * @return array|mixed|object|WP_Error
251 */
252 public function update_subscriber( $id, $data ) {
253 $url = 'subscribers/' . $id;
254 $res = $this->put( $url, $data );
255
256 return is_wp_error( $res ) ? $res : __( 'Successful subscription', 'hustle' );
257 }
258
259 /**
260 * Delete subscriber from the list
261 *
262 * @param string $list_id List ID.
263 * @param string $email Email.
264 *
265 * @return bool
266 */
267 public function delete_email( $list_id, $email ) {
268 // Get subscriber by email first.
269 $subscriber = $this->is_subscriber( $email );
270
271 if ( ! $subscriber ) {
272 return false;
273 }
274
275 $subscriber_id = is_object( $subscriber ) ? $subscriber->id : false;
276
277 if ( ! $subscriber_id ) {
278 return false;
279 }
280
281 // Unsubscribe the subscriber using v4 API.
282 $url = 'subscribers/' . $subscriber_id . '/unsubscribe';
283 $res = $this->post( $url, array() );
284
285 return ! is_wp_error( $res );
286 }
287
288 /**
289 * Verify if an email is already a subscriber.
290 *
291 * @param string $email Email.
292 *
293 * @return object|false Returns data of existing subscriber if exist otherwise false.
294 **/
295 public function is_subscriber( $email ) {
296 $args = array(
297 'email_address' => $email,
298 );
299
300 $res = $this->get( 'subscribers', $args );
301
302 if ( is_wp_error( $res ) ) {
303 return false;
304 }
305
306 if ( ! empty( $res->subscribers ) && is_array( $res->subscribers ) ) {
307 return array_shift( $res->subscribers );
308 }
309
310 return false;
311 }
312
313 /**
314 * Verify if an email is already a subscriber in a form.
315 *
316 * @param string $email Email.
317 * @param integer $form_id Form ID.
318 *
319 * @return boolean|integer Subscriber ID if the subscriber exists, otherwise false.
320 **/
321 public function is_form_subscriber( $email, $form_id ) {
322 $url = 'forms/' . $form_id . '/subscriptions';
323 $res = $this->get( $url );
324 $exist = false;
325
326 $utils = Hustle_Provider_Utils::get_instance();
327 $utils->last_data_received = $res;
328 $utils->last_url_request = trailingslashit( $this->endpoint ) . $url;
329
330 if ( is_wp_error( $res ) ) {
331 Hustle_Provider_Utils::maybe_log( 'There was an error retrieving the subscribers from Kit: ' . $res->get_error_message() );
332 return false;
333 }
334
335 if ( empty( $res->subscriptions ) ) {
336 return false;
337 }
338
339 // Check subscribers in the current page.
340 $subscribers = wp_list_pluck( $res->subscriptions, 'subscriber' );
341 $emails = wp_list_pluck( $subscribers, 'email_address' );
342 $subscribers_id = wp_list_pluck( $subscribers, 'id' );
343
344 $key = array_search( $email, $emails, true );
345 if ( false !== $key ) {
346 return $subscribers_id[ $key ];
347 }
348
349 // Handle pagination if there are more pages.
350 if ( isset( $res->pagination ) && $res->pagination->has_next_page && ! empty( $res->pagination->end_cursor ) ) {
351 $cursor = $res->pagination->end_cursor;
352
353 while ( $cursor ) {
354 $args = array( 'after' => $cursor );
355 $res = $this->get( $url, $args );
356
357 $utils = Hustle_Provider_Utils::get_instance();
358 $utils->last_data_received = $res;
359 $utils->last_url_request = trailingslashit( $this->endpoint ) . $url;
360 $utils->last_data_sent = $args;
361
362 if ( is_wp_error( $res ) || empty( $res->subscriptions ) ) {
363 break;
364 }
365
366 $subscribers = wp_list_pluck( $res->subscriptions, 'subscriber' );
367 $emails = wp_list_pluck( $subscribers, 'email_address' );
368 $subscribers_id = wp_list_pluck( $subscribers, 'id' );
369
370 $key = array_search( $email, $emails, true );
371 if ( false !== $key ) {
372 return $subscribers_id[ $key ];
373 }
374
375 // Move to next page if available.
376 if ( isset( $res->pagination ) && $res->pagination->has_next_page && ! empty( $res->pagination->end_cursor ) ) {
377 $cursor = $res->pagination->end_cursor;
378 } else {
379 break;
380 }
381 }
382 }
383
384 return false;
385 }
386 }
387