PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 5.6.2 All 33 releases
automatorwp / integrations / convertkit / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.2, at integrations/convertkit/includes/functions.php

558 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\ConvertKit\Functions
6 * @since 1.0.0
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Helper function to check API secret
13 *
14 * @since 1.0.0
15 *
16 * @param string $secret API secret
17 *
18 * @return array|false
19 */
20 function automatorwp_convertkit_check_api_secret( $secret ) {
21
22 $return = false;
23
24 $response = wp_remote_get( 'https://api.convertkit.com/v3/account?api_secret=' . $secret, array() );
25
26 $status_code = wp_remote_retrieve_response_code( $response );
27
28 if ( 200 !== $status_code ) {
29 wp_send_json_error (array( 'message' => __( 'Please, check your API secret', 'automatorwp-convertkit' ) ) );
30 return $return;
31 } else {
32 $return = true;
33 }
34
35 return $return;
36
37 }
38
39 /**
40 * Helper function to check API key
41 *
42 * @since 1.0.0
43 *
44 * @param string $key API key
45 *
46 * @return array|false
47 */
48 function automatorwp_convertkit_check_api_key( $key ) {
49
50 $return = false;
51
52 $response = wp_remote_get( 'https://api.convertkit.com/v3/forms?api_key=' . $key, array() );
53
54 $status_code = wp_remote_retrieve_response_code( $response );
55
56 if ( 200 !== $status_code ) {
57 wp_send_json_error (array( 'message' => __( 'Please, check your API key', 'automatorwp-convertkit' ) ) );
58 return $return;
59 } else {
60 $return = true;
61 }
62
63 return true;
64
65 }
66
67 /**
68 * Helper function to get the ConvertKit API parameters
69 *
70 * @since 1.0.0
71 *
72 * @return array|false
73 */
74 function automatorwp_convertkit_get_api() {
75
76 $key = automatorwp_convertkit_get_option( 'key', '' );
77 $secret = automatorwp_convertkit_get_option( 'secret', '' );
78 $url = 'https://api.convertkit.com/v3';
79
80 if( empty( $key ) || empty( $secret ) ) {
81 return false;
82 }
83
84 return array(
85 'key' => $key,
86 'secret' => $secret,
87 'url' => $url,
88 );
89
90 }
91
92 /**
93 * Get forms from ConvertKit
94 *
95 * @since 1.0.0
96 *
97 * @param stdClass $field
98 *
99 * @return array
100 */
101 function automatorwp_convertkit_options_cb_form( $field ) {
102
103 // Setup vars
104 $value = $field->escaped_value;
105 $none_value = 'any';
106 $none_label = __( 'any form', 'automatorwp-convertkit' );
107 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
108
109 if( ! empty( $value ) ) {
110 if( ! is_array( $value ) ) {
111 $value = array( $value );
112 }
113
114 foreach( $value as $form_id ) {
115
116 // Skip option none
117 if( $form_id === $none_value ) {
118 continue;
119 }
120
121 $options[$form_id] = automatorwp_convertkit_get_form_name( $form_id );
122 }
123 }
124
125 return $options;
126
127 }
128
129 /**
130 * Get forms from ConvertKit
131 *
132 * @since 1.0.0
133 *
134 * @return array
135 */
136 function automatorwp_convertkit_get_forms( ) {
137
138 $forms = array();
139
140 $api = automatorwp_convertkit_get_api();
141
142 if( ! $api ) {
143 return $forms;
144 }
145
146 $response = wp_remote_get( $api['url'] . '/forms', array(
147 'body' => array(
148 'api_key' => $api['key'],
149 )
150 ) );
151
152 $response = json_decode( wp_remote_retrieve_body( $response ), true );
153
154 foreach ( $response['forms'] as $form ){
155 $forms[] = array(
156 'id' => $form['id'],
157 'name' => $form['name'],
158 );
159
160 }
161
162 return $forms;
163
164 }
165
166 /**
167 * Get form name
168 *
169 * @since 1.0.0
170 *
171 * @param int $form_id ID Form
172 *
173 * @return string
174 */
175 function automatorwp_convertkit_get_form_name( $form_id ) {
176
177 // Empty title if no ID provided
178 if( absint( $form_id ) === 0 ) {
179 return '';
180 }
181
182 $forms = automatorwp_convertkit_get_forms();
183 $form_name = '';
184
185 foreach( $forms as $form ) {
186
187 if( absint( $form['id'] ) === absint( $form_id ) ) {
188 $form_name = $form['name'];
189 break;
190 }
191
192 }
193
194 return $form_name;
195
196 }
197
198 /**
199 * Get sequences from ConvertKit
200 *
201 * @since 1.0.0
202 *
203 * @param stdClass $field
204 *
205 * @return array
206 */
207 function automatorwp_convertkit_options_cb_sequence( $field ) {
208
209 // Setup vars
210 $value = $field->escaped_value;
211 $none_value = 'any';
212 $none_label = __( 'any form', 'automatorwp-convertkit' );
213 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
214
215 if( ! empty( $value ) ) {
216 if( ! is_array( $value ) ) {
217 $value = array( $value );
218 }
219
220 foreach( $value as $sequence_id ) {
221
222 // Skip option none
223 if( $sequence_id === $none_value ) {
224 continue;
225 }
226
227 $options[$sequence_id] = automatorwp_convertkit_get_sequence_name( $sequence_id );
228 }
229 }
230
231 return $options;
232
233 }
234
235 /**
236 * Get sequences from ConvertKit
237 *
238 * @since 1.0.0
239 *
240 * @return array
241 */
242 function automatorwp_convertkit_get_sequences( ) {
243
244 $sequences = array();
245
246 $api = automatorwp_convertkit_get_api();
247
248 if( ! $api ) {
249 return $sequences;
250 }
251
252 $response = wp_remote_get( $api['url'] . '/sequences', array(
253 'body' => array(
254 'api_key' => $api['key'],
255 )
256 ) );
257
258 $response = json_decode( wp_remote_retrieve_body( $response ), true );
259
260 foreach ( $response['courses'] as $sequence ){
261 $sequences[] = array(
262 'id' => $sequence['id'],
263 'name' => $sequence['name'],
264 );
265
266 }
267
268 return $sequences;
269
270 }
271
272 /**
273 * Get sequence name
274 *
275 * @since 1.0.0
276 *
277 * @param int $sequence_id ID Sequence
278 *
279 * @return string
280 */
281 function automatorwp_convertkit_get_sequence_name( $sequence_id ) {
282
283 // Empty title if no ID provided
284 if( absint( $sequence_id ) === 0 ) {
285 return '';
286 }
287
288 $sequences = automatorwp_convertkit_get_sequences();
289 $sequence_name = '';
290
291 foreach( $sequences as $sequence ) {
292
293 if( absint( $sequence['id'] ) === absint( $sequence_id ) ) {
294 $sequence_name = $sequence['name'];
295 break;
296 }
297
298 }
299
300 return $sequence_name;
301
302 }
303
304 /**
305 * Add subscriber to form
306 *
307 * @since 1.0.0
308 *
309 * @param int $form_id ID form
310 * @param string $email Subscriber email
311 * @param string $first_name Subscriber name
312 *
313 * @return int
314 */
315 function automatorwp_convertkit_add_subscriber_form( $form_id, $email, $first_name = '' ) {
316
317 $api = automatorwp_convertkit_get_api();
318
319 if( ! $api ) {
320 return false;
321 }
322
323 $response = wp_remote_post( $api['url'] . '/forms/' . $form_id . '/subscribe', array(
324 'headers' => array(
325 'Content-Type' => 'application/json',
326 'charset' => 'utf-8'
327 ),
328 'body' => json_encode( array(
329 'api_key' => $api['key'],
330 'email' => $email,
331 'first_name' => ( !empty( $first_name ) ? $first_name : '' ),
332 )
333 ) ) );
334
335 $status_code = wp_remote_retrieve_response_code( $response );
336
337 return $status_code;
338
339 }
340
341 /**
342 * Add subscriber to sequence
343 *
344 * @since 1.0.0
345 *
346 * @param int $sequence_id ID sequence
347 * @param string $email Subscriber email
348 * @param string $first_name Subscriber name
349 *
350 * @return int
351 */
352 function automatorwp_convertkit_add_subscriber_sequence( $sequence_id, $email, $first_name = '' ) {
353
354 $api = automatorwp_convertkit_get_api();
355
356 if( ! $api ) {
357 return false;
358 }
359
360 $response = wp_remote_post( $api['url'] . '/sequences/' . $sequence_id . '/subscribe', array(
361 'headers' => array(
362 'Content-Type' => 'application/json',
363 'charset' => 'utf-8'
364 ),
365 'body' => json_encode( array(
366 'api_key' => $api['key'],
367 'email' => $email,
368 'first_name' => ( !empty( $first_name ) ? $first_name : '' ),
369 )
370 ) ) );
371
372 $status_code = wp_remote_retrieve_response_code( $response );
373
374 return $status_code;
375
376 }
377
378 /**
379 * Get tags from ConvertKit
380 *
381 * @since 1.0.0
382 *
383 * @param stdClass $field
384 *
385 * @return array
386 */
387 function automatorwp_convertkit_options_cb_tag( $field ) {
388
389 // Setup vars
390 $value = $field->escaped_value;
391 $none_value = 'any';
392 $none_label = __( 'any tag', 'automatorwp-convertkit' );
393 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
394
395 if( ! empty( $value ) ) {
396 if( ! is_array( $value ) ) {
397 $value = array( $value );
398 }
399
400 foreach( $value as $tag_id ) {
401
402 // Skip option none
403 if( $tag_id === $none_value ) {
404 continue;
405 }
406
407 $options[$tag_id] = automatorwp_convertkit_get_tag_name( $tag_id );
408 }
409 }
410
411 return $options;
412
413 }
414
415 /**
416 * Get tags from ConvertKit
417 *
418 * @since 1.0.0
419 *
420 * @param string $search
421 * @param int $page
422 *
423 * @return array
424 */
425 function automatorwp_convertkit_get_tags() {
426
427 $tags = array();
428
429 $api = automatorwp_convertkit_get_api();
430
431 if( ! $api ) {
432 return $tags;
433 }
434
435 $response = wp_remote_get( $api['url'] . '/tags', array(
436 'body' => array(
437 'api_key' => $api['key'],
438 )
439 ) );
440
441 $response = json_decode( wp_remote_retrieve_body( $response ), true );
442
443 foreach ( $response['tags'] as $tag ){
444 $tags[] = array(
445 'id' => $tag['id'],
446 'name' => $tag['name'],
447 );
448
449 }
450
451 return $tags;
452
453 }
454
455 /**
456 * Get tag name
457 *
458 * @since 1.0.0
459 *
460 * @param int $tag_id ID tag
461 *
462 * @return string
463 */
464 function automatorwp_convertkit_get_tag_name( $tag_id ) {
465
466 // Empty title if no ID provided
467 if( absint( $tag_id ) === 0 ) {
468 return '';
469 }
470
471 $tags = automatorwp_convertkit_get_tags();
472 $tag_name = '';
473
474 foreach( $tags as $tag ) {
475
476 if( absint( $tag['id'] ) === absint( $tag_id ) ) {
477 $tag_name = $tag['name'];
478 break;
479 }
480
481 }
482
483 return $tag_name;
484
485 }
486
487 /**
488 * Add tag to subscriber
489 *
490 * @since 1.0.0
491 *
492 * @param string $email Subscriber email
493 * @param string $tag_id Tag ID
494 * @param string $first_name Subscriber name
495 *
496 * @return int
497 */
498 function automatorwp_convertkit_add_subscriber_tag( $email, $tag_id, $first_name = '' ){
499
500 $api = automatorwp_convertkit_get_api();
501
502 if( ! $api ) {
503 return false;
504 }
505
506 $response = wp_remote_post( $api['url'] . '/tags/' . $tag_id . '/subscribe', array(
507 'headers' => array(
508 'Content-Type' => 'application/json',
509 'charset' => 'utf-8'
510 ),
511 'body' => json_encode( array(
512 'api_secret' => $api['secret'],
513 'email' => $email,
514 'first_name' => ( !empty( $first_name ) ? $first_name : '' ),
515 )
516 ) ) );
517
518 $status_code = wp_remote_retrieve_response_code( $response );
519
520 return $status_code;
521
522 }
523
524 /**
525 * Remove tag from subscriber
526 *
527 * @since 1.0.0
528 *
529 * @param string $email Subscriber email
530 * @param string $tag_id Tag ID
531 *
532 * @return int
533 */
534 function automatorwp_convertkit_remove_subscriber_tag( $email, $tag_id ){
535
536 $api = automatorwp_convertkit_get_api();
537
538 if( ! $api ) {
539 return false;
540 }
541
542 $response = wp_remote_post( $api['url'] . '/tags/' . $tag_id . '/unsubscribe', array(
543 'headers' => array(
544 'Content-Type' => 'application/json',
545 'charset' => 'utf-8'
546 ),
547 'body' => json_encode( array(
548 'api_secret' => $api['secret'],
549 'email' => $email,
550 )
551 ) ) );
552
553 $status_code = wp_remote_retrieve_response_code( $response );
554
555 return $status_code;
556
557 }
558