PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
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
← All changes | includes/functions.php +689 -20 2.2.0trunk View file →
@@ -16,8 +16,9 @@
16 16 function convertkit_plugin_activate( $network_wide ) {
17 17
18 18 // Initialise Plugin.
19 19 $convertkit = WP_ConvertKit();
20 + $convertkit->initialize();
20 21
21 22 // Check if we are on a multisite install, activating network wide, or a single install.
22 23 if ( ! is_multisite() || ! $network_wide ) {
23 24 // Single Site activation.
@@ -57,8 +58,9 @@
57 58 }
58 59
59 60 // Initialise Plugin.
60 61 $convertkit = WP_ConvertKit();
62 + $convertkit->initialize();
61 63
62 64 // Run installation routine.
63 65 switch_to_blog( $site_or_blog_id );
64 66 $convertkit->get_class( 'setup' )->activate();
@@ -76,8 +78,9 @@
76 78 function convertkit_plugin_deactivate( $network_wide ) {
77 79
78 80 // Initialise Plugin.
79 81 $convertkit = WP_ConvertKit();
82 + $convertkit->initialize();
80 83
81 84 // Check if we are on a multisite install, activating network wide, or a single install.
82 85 if ( ! is_multisite() || ! $network_wide ) {
83 86 // Single Site activation.
@@ -106,13 +109,32 @@
106 109 * @return array Post Types
107 110 */
108 111 function convertkit_get_supported_post_types() {
109 112
113 + // Define supported Post Types.
110 114 $post_types = array(
111 115 'page',
112 116 'post',
113 117 );
114 118
119 + // If public Custom Post Types can be fetched, include them now.
120 + if ( function_exists( 'get_post_types' ) ) {
121 + // Get public Custom Post Types.
122 + $custom_post_types = (array) get_post_types(
123 + array(
124 + 'public' => true,
125 +
126 + // Don't include WordPress' built in Post Types, such as attachment, revisino and nav_menu_item.
127 + '_builtin' => false,
128 + )
129 + );
130 +
131 + $post_types = array_merge(
132 + $post_types,
133 + array_keys( $custom_post_types )
134 + );
135 + }
136 +
115 137 /**
116 138 * Defines the Post Types that support ConvertKit Forms.
117 139 *
118 140 * @since 1.9.6
@@ -125,31 +147,40 @@
125 147
126 148 }
127 149
128 150 /**
151 + * Determines whether the current user can create and publish the given Post Type.
152 + *
153 + * @since 3.3.9
154 + *
155 + * @param string $post_type Post Type.
156 + * @return bool User can create and publish Post Type.
157 + */
158 +function convertkit_user_can_create_published_post_type( $post_type ) {
159 +
160 + $post_type_object = get_post_type_object( $post_type );
161 +
162 + if ( ! $post_type_object ) {
163 + return false;
164 + }
165 +
166 + return current_user_can( $post_type_object->cap->create_posts ) && current_user_can( $post_type_object->cap->publish_posts );
167 +
168 +}
169 +
170 +/**
129 171 * Helper method to get supported Post Types for Restricted Content (Member's Content)
130 172 *
131 173 * @since 2.1.0
132 174 *
175 + * @deprecated 2.4.3 No longer used by internal code and not recommended. Use `convertkit_get_supported_post_types` instead.
176 + *
133 177 * @return array Post Types
134 178 */
135 179 function convertkit_get_supported_restrict_content_post_types() {
136 180
137 - $post_types = array(
138 - 'page',
139 - );
181 + return convertkit_get_supported_post_types();
140 182
141 - /**
142 - * Defines the Post Types that support Restricted Content / Members Content functionality.
143 - *
144 - * @since 2.0.0
145 - *
146 - * @param array $post_types Post Types
147 - */
148 - $post_types = apply_filters( 'convertkit_get_supported_restrict_content_post_types', $post_types );
149 -
150 - return $post_types;
151 -
152 183 }
153 184
154 185 /**
155 186 * Helper method to get registered Shortcodes.
@@ -223,8 +254,153 @@
223 254
224 255 }
225 256
226 257 /**
258 + * Helper method to get registered plugin sidebars.
259 + *
260 + * @since 3.3.0
261 + *
262 + * @return array Plugin sidebars
263 + */
264 +function convertkit_get_plugin_sidebars() {
265 +
266 + $plugin_sidebars = array();
267 +
268 + /**
269 + * Registers plugin sidebars for the WordPress block editor.
270 + *
271 + * @since 3.3.0
272 + *
273 + * @param array $plugin_sidebars Plugin sidebars.
274 + */
275 + $plugin_sidebars = apply_filters( 'convertkit_plugin_sidebars', $plugin_sidebars );
276 +
277 + return $plugin_sidebars;
278 +
279 +}
280 +
281 +/**
282 + * Helper method to get registered pre-publish actions.
283 + *
284 + * @since 2.4.0
285 + *
286 + * @return array Pre-publish actions
287 + */
288 +function convertkit_get_pre_publish_actions() {
289 +
290 + $pre_publish_actions = array();
291 +
292 + /**
293 + * Registers pre-publish actions for the ConvertKit Plugin.
294 + *
295 + * @since 2.4.0
296 + *
297 + * @param array $pre_publish_panels Pre-publish actions.
298 + */
299 + $pre_publish_actions = apply_filters( 'convertkit_get_pre_publish_actions', $pre_publish_actions );
300 +
301 + return $pre_publish_actions;
302 +
303 +}
304 +
305 +/**
306 + * Helper method to get registered importers that can replace third party
307 + * form shortcodes and blocks with Kit form shortcodes and blocks.
308 + *
309 + * @since 3.1.7
310 + *
311 + * @return array Importers.
312 + */
313 +function convertkit_get_form_importers() {
314 +
315 + $importers = array();
316 +
317 + /**
318 + * Registers form importers for the ConvertKit Plugin.
319 + *
320 + * @since 3.1.7
321 + *
322 + * @param array $importers Importers.
323 + */
324 + $importers = apply_filters( 'convertkit_get_form_importers', $importers );
325 +
326 + return $importers;
327 +
328 +}
329 +
330 +/**
331 + * Helper method to get registered abilities.
332 + *
333 + * @since 3.4.0
334 + *
335 + * @return array Abilities.
336 + */
337 +function convertkit_get_abilities() {
338 +
339 + $abilities = array();
340 +
341 + /**
342 + * Registers abilities for the Kit Plugin.
343 + *
344 + * @since 3.4.0
345 + *
346 + * @param array $abilities Abilities.
347 + */
348 + $abilities = apply_filters( 'convertkit_abilities', $abilities );
349 +
350 + return $abilities;
351 +
352 +}
353 +
354 +/**
355 + * Helper method to get registered MCP resources.
356 + *
357 + * @since 3.4.2
358 + *
359 + * @return array Resources.
360 + */
361 +function convertkit_get_resources() {
362 +
363 + $resources = array();
364 +
365 + /**
366 + * Registers MCP resources for the Kit Plugin.
367 + *
368 + * @since 3.4.2
369 + *
370 + * @param array $resources Resources.
371 + */
372 + $resources = apply_filters( 'convertkit_resources', $resources );
373 +
374 + return $resources;
375 +
376 +}
377 +
378 +/**
379 + * Helper method to get registered MCP prompts.
380 + *
381 + * @since 3.4.2
382 + *
383 + * @return array Prompts.
384 + */
385 +function convertkit_get_prompts() {
386 +
387 + $prompts = array();
388 +
389 + /**
390 + * Registers MCP prompts for the Kit Plugin.
391 + *
392 + * @since 3.4.2
393 + *
394 + * @param array $prompts Prompts.
395 + */
396 + $prompts = apply_filters( 'convertkit_prompts', $prompts );
397 +
398 + return $prompts;
399 +
400 +}
401 +
402 +/**
227 403 * Helper method to return the Plugin Settings Link
228 404 *
229 405 * @since 1.9.6
230 406 *
@@ -244,8 +420,29 @@
244 420
245 421 }
246 422
247 423 /**
424 + * Helper method to return the Plugin Settings Link
425 + *
426 + * @since 2.2.4
427 + *
428 + * @param array $query_args Optional Query Args.
429 + * @return string Settings Link
430 + */
431 +function convertkit_get_setup_wizard_plugin_link( $query_args = array() ) {
432 +
433 + $query_args = array_merge(
434 + $query_args,
435 + array(
436 + 'page' => 'convertkit-setup',
437 + )
438 + );
439 +
440 + return add_query_arg( $query_args, admin_url( 'options.php' ) );
441 +
442 +}
443 +
444 +/**
248 445 * Helper method to return the URL the user needs to visit to register a ConvertKit account.
249 446 *
250 447 * @since 1.9.8.4
251 448 *
@@ -252,9 +449,16 @@
252 449 * @return string ConvertKit Registration URL.
253 450 */
254 451 function convertkit_get_registration_url() {
255 452
256 - return 'https://app.convertkit.com/users/signup?utm_source=wordpress&utm_content=convertkit';
453 + return add_query_arg(
454 + array(
455 + 'utm_source' => 'wordpress',
456 + 'utm_term' => get_locale(),
457 + 'utm_content' => 'convertkit',
458 + ),
459 + 'https://app.kit.com/users/signup'
460 + );
257 461
258 462 }
259 463
260 464 /**
@@ -265,26 +469,212 @@
265 469 * @return string ConvertKit Login URL.
266 470 */
267 471 function convertkit_get_sign_in_url() {
268 472
269 - return 'https://app.convertkit.com/?utm_source=wordpress&utm_content=convertkit';
473 + return add_query_arg(
474 + array(
475 + 'utm_source' => 'wordpress',
476 + 'utm_term' => get_locale(),
477 + 'utm_content' => 'convertkit',
478 + ),
479 + 'https://app.kit.com/'
480 + );
270 481
271 482 }
272 483
273 484 /**
274 - * Helper method to return the URL the user needs to visit on the ConvertKit app to obtain their API Key and Secret.
485 + * Helper method to return the URL the user needs to visit to manage thier billing.
275 486 *
276 - * @since 1.9.6.1
487 + * @since 2.2.7
277 488 *
489 + * @return string ConvertKit Billing URL.
490 + */
491 +function convertkit_get_billing_url() {
492 +
493 + return add_query_arg(
494 + array(
495 + 'utm_source' => 'wordpress',
496 + 'utm_term' => get_locale(),
497 + 'utm_content' => 'convertkit',
498 + ),
499 + 'https://app.kit.com/account_settings/billing/'
500 + );
501 +
502 +}
503 +
504 +/**
505 + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Form or Landing Page.
506 + *
507 + * @since 2.2.3
508 + *
509 + * @return string ConvertKit App URL
510 + */
511 +function convertkit_get_new_form_url() {
512 +
513 + return add_query_arg(
514 + array(
515 + 'utm_source' => 'wordpress',
516 + 'utm_term' => get_locale(),
517 + 'utm_content' => 'convertkit',
518 + ),
519 + 'https://app.kit.com/forms/new/'
520 + );
521 +
522 +}
523 +
524 +/**
525 + * Helper method to return the URL the user needs to visit to edit ConvertKit forms.
526 + *
527 + * @since 2.2.3
528 + *
529 + * @return string ConvertKit Form Editor URL.
530 + */
531 +function convertkit_get_form_editor_url() {
532 +
533 + return add_query_arg(
534 + array(
535 + 'utm_source' => 'wordpress',
536 + 'utm_term' => get_locale(),
537 + 'utm_content' => 'convertkit',
538 + ),
539 + 'https://app.kit.com/forms'
540 + );
541 +
542 +}
543 +
544 +/**
545 + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Landing Page.
546 + *
547 + * @since 2.5.5
548 + *
549 + * @return string ConvertKit App URL
550 + */
551 +function convertkit_get_new_landing_page_url() {
552 +
553 + return add_query_arg(
554 + array(
555 + 'utm_source' => 'wordpress',
556 + 'utm_term' => get_locale(),
557 + 'utm_content' => 'convertkit',
558 + ),
559 + 'https://app.kit.com/pages/new/'
560 + );
561 +
562 +}
563 +
564 +/**
565 + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Tag.
566 + *
567 + * @since 2.3.3
568 + *
278 569 * @return string ConvertKit App URL.
279 570 */
280 -function convertkit_get_api_key_url() {
571 +function convertkit_get_new_tag_url() {
281 572
282 - return 'https://app.convertkit.com/account_settings/advanced_settings/?utm_source=wordpress&utm_content=convertkit';
573 + return add_query_arg(
574 + array(
575 + 'utm_source' => 'wordpress',
576 + 'utm_term' => get_locale(),
577 + 'utm_content' => 'convertkit',
578 + ),
579 + 'https://app.kit.com/subscribers/'
580 + );
283 581
284 582 }
285 583
286 584 /**
585 + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Broadcast.
586 + *
587 + * @since 2.2.6
588 + *
589 + * @return string ConvertKit App URL.
590 + */
591 +function convertkit_get_new_broadcast_url() {
592 +
593 + return add_query_arg(
594 + array(
595 + 'utm_source' => 'wordpress',
596 + 'utm_term' => get_locale(),
597 + 'utm_content' => 'convertkit',
598 + ),
599 + 'https://app.kit.com/campaigns/'
600 + );
601 +
602 +}
603 +
604 +/**
605 + * Helper method to return the URL the user needs to visit on the ConvertKit app to edit a draft Broadcast.
606 + *
607 + * @since 2.4.0
608 + *
609 + * @param int $broadcast_id ConvertKit Broadcast ID.
610 + * @return string ConvertKit App URL.
611 + */
612 +function convertkit_get_edit_broadcast_url( $broadcast_id ) {
613 +
614 + return add_query_arg(
615 + array(
616 + 'utm_source' => 'wordpress',
617 + 'utm_term' => get_locale(),
618 + 'utm_content' => 'convertkit',
619 + ),
620 + sprintf(
621 + 'https://app.kit.com/campaigns/%s/draft',
622 + $broadcast_id
623 + )
624 + );
625 +
626 +}
627 +
628 +/**
629 + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Product.
630 + *
631 + * @since 2.2.3
632 + *
633 + * @return string ConvertKit App URL.
634 + */
635 +function convertkit_get_new_product_url() {
636 +
637 + return add_query_arg(
638 + array(
639 + 'utm_source' => 'wordpress',
640 + 'utm_term' => get_locale(),
641 + 'utm_content' => 'convertkit',
642 + ),
643 + 'https://app.kit.com/products/new/'
644 + );
645 +
646 +}
647 +
648 +/**
649 + * Helper method to enqueue the frontend CSS file.
650 + *
651 + * @since 3.2.0
652 + */
653 +function convertkit_enqueue_frontend_css() {
654 +
655 + wp_enqueue_style( 'convertkit-frontend', CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/frontend.css', array(), CONVERTKIT_PLUGIN_VERSION );
656 +
657 +}
658 +
659 +/**
660 + * Helper method to enqueue the frontend JS file.
661 + *
662 + * @since 3.2.0
663 + */
664 +function convertkit_enqueue_frontend_js() {
665 +
666 + wp_enqueue_script(
667 + 'convertkit-js',
668 + CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/dist/frontend.min.js',
669 + array(),
670 + CONVERTKIT_PLUGIN_VERSION,
671 + true
672 + );
673 +
674 +}
675 +
676 +/**
287 677 * Helper method to enqueue Select2 scripts for use within the ConvertKit Plugin.
288 678 *
289 679 * @since 1.9.6.4
290 680 */
@@ -305,4 +695,283 @@
305 695 wp_enqueue_style( 'convertkit-select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css', array(), CONVERTKIT_PLUGIN_VERSION );
306 696 wp_enqueue_style( 'convertkit-admin-select2', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/select2.css', array(), CONVERTKIT_PLUGIN_VERSION );
307 697
308 698 }
699 +
700 +/**
701 + * Return the contents of the given local file.
702 + *
703 + * @since 2.2.2
704 + *
705 + * @param string $local_file Local file, including path.
706 + * @return string File contents.
707 + */
708 +function convertkit_get_file_contents( $local_file ) {
709 +
710 + // Bail if the file doesn't exist.
711 + if ( ! file_exists( $local_file ) ) {
712 + return '';
713 + }
714 +
715 + // Read file.
716 + $contents = file_get_contents( $local_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
717 +
718 + // Return an empty string if the contents of the file could not be read.
719 + if ( ! $contents ) {
720 + return '';
721 + }
722 +
723 + // Return file's contents.
724 + return $contents;
725 +
726 +}
727 +
728 +/**
729 + * Returns a dropdown field commonly used for settings, comprising of:
730 + * - Do not subscribe
731 + * - Subscribe
732 + * - Subscribe to Form
733 + *
734 + * @since 2.5.2
735 + *
736 + * @param string $name Field name.
737 + * @param string $value Field value.
738 + * @param string $id Field ID attribute.
739 + * @param string $css_class Field CSS class(es).
740 + * @param string $context Resource context.
741 + * @param bool|array $additional_options Additional <option> key/value pairs.
742 + */
743 +function convertkit_get_subscription_dropdown_field( $name, $value, $id, $css_class = '', $context = '', $additional_options = false ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
744 +
745 + // Load resource classes.
746 + $forms = new ConvertKit_Resource_Forms( $context );
747 + $tags = new ConvertKit_Resource_Tags( $context );
748 + $sequences = new ConvertKit_Resource_Sequences( $context );
749 +
750 + ob_start();
751 + include CONVERTKIT_PLUGIN_PATH . '/views/backend/subscription-dropdown-field.php';
752 + $output = trim( ob_get_clean() );
753 +
754 + // Return output.
755 + return $output;
756 +
757 +}
758 +
759 +/**
760 + * Helper method to safely call get_current_screen(), returning false
761 + * if the function is not available or returns null.
762 + *
763 + * Otherwise returns the given WP_Screen property.
764 + *
765 + * @since 2.5.9
766 + *
767 + * @param string $property WP_Screen property to return.
768 + * @return bool|string
769 + */
770 +function convertkit_get_current_screen( $property ) {
771 +
772 + // Bail if we cannot determine the screen.
773 + if ( ! function_exists( 'get_current_screen' ) ) {
774 + return false;
775 + }
776 +
777 + // Get screen.
778 + $screen = get_current_screen();
779 +
780 + // Bail if the screen couldn't be determined.
781 + if ( is_null( $screen ) ) {
782 + return false;
783 + }
784 +
785 + // Return property.
786 + return $screen->$property;
787 +
788 +}
789 +
790 +/**
791 + * Outputs the Intercom help widget script.
792 + *
793 + * @since 2.7.2
794 + */
795 +function convertkit_output_intercom_messenger() {
796 +
797 + ?>
798 + <script>
799 + const KIT_INTERCOM_APP_ID = 'e4n3xtxz';
800 + window.intercomSettings = {
801 + api_base: 'https://api-iam.intercom.io',
802 + app_id: KIT_INTERCOM_APP_ID
803 + };
804 + </script>
805 +
806 + <script>
807 + (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/' + KIT_INTERCOM_APP_ID;var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s, x);};if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
808 + </script>
809 + <?php
810 +
811 +}
812 +
813 +/**
814 + * Checks if the given Theme is active.
815 + *
816 + * @since 3.1.4
817 + *
818 + * @param string $theme_name Theme name.
819 + * @return bool
820 + */
821 +function convertkit_is_theme_active( $theme_name ) {
822 +
823 + // Assume Theme isn't active if we can't detect it.
824 + if ( ! function_exists( 'wp_get_theme' ) ) {
825 + return false;
826 + }
827 +
828 + // Check the Parent Theme if we're on a Child Theme.
829 + if ( wp_get_theme()->parent() ) {
830 + $theme = wp_get_theme()->parent();
831 + } else {
832 + $theme = wp_get_theme();
833 + }
834 +
835 + return strtolower( $theme->get( 'Name' ) ) === strtolower( $theme_name );
836 +
837 +}
838 +
839 +/**
840 + * Returns permitted HTML output when using wp_kses( ..., convertkit_kses_allowed_html()).
841 + *
842 + * @since 2.8.5
843 + */
844 +function convertkit_kses_allowed_html() {
845 +
846 + // Get WordPress' permitted HTML elements.
847 + $elements = wp_kses_allowed_html( 'post' );
848 +
849 + // Add form elements.
850 + $form_elements = array(
851 + 'input' => array(
852 + 'type' => true,
853 + 'id' => true,
854 + 'name' => true,
855 + 'class' => true,
856 + 'value' => true,
857 + 'checked' => true,
858 + 'min' => true,
859 + 'max' => true,
860 + 'step' => true,
861 + 'data-*' => true,
862 + ),
863 + 'select' => array(
864 + 'id' => true,
865 + 'name' => true,
866 + 'class' => true,
867 + 'size' => true,
868 + 'multiple' => true,
869 + 'data-*' => true,
870 + ),
871 + 'option' => array(
872 + 'value' => true,
873 + 'selected' => true,
874 + 'data-*' => true,
875 + ),
876 + 'optgroup' => array(
877 + 'label' => true,
878 + 'data-*' => true,
879 + ),
880 + 'label' => array(
881 + 'for' => true,
882 + ),
883 + );
884 +
885 + return array_merge( $elements, $form_elements );
886 +
887 +}
888 +
889 +/**
890 + * Saves the new access token, refresh token and its expiry, and schedules
891 + * a WordPress Cron event to refresh the token on expiry.
892 + *
893 + * @since 3.1.1
894 + *
895 + * @param array $result New Access Token, Refresh Token and Expiry.
896 + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
897 + */
898 +function convertkit_maybe_update_credentials( $result, $client_id ) {
899 +
900 + // Don't save these credentials if they're not for this Client ID.
901 + // They're for another Kit Plugin that uses OAuth.
902 + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
903 + return;
904 + }
905 +
906 + $settings = new ConvertKit_Settings();
907 + $settings->update_credentials( $result );
908 +
909 +}
910 +
911 +/**
912 + * Deletes the stored access token, refresh token and its expiry from the Plugin settings,
913 + * and clears any existing scheduled WordPress Cron event to refresh the token on expiry,
914 + * when the user revokes the access token.
915 + *
916 + * @since 3.2.4
917 + *
918 + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
919 + */
920 +function convertkit_delete_credentials( $client_id ) {
921 +
922 + // Don't delete these credentials if they're not for this Client ID.
923 + // They're for another Kit Plugin that uses OAuth.
924 + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
925 + return;
926 + }
927 +
928 + // Delete Access and Refresh Tokens.
929 + $settings = new ConvertKit_Settings();
930 + $settings->delete_credentials();
931 +
932 +}
933 +
934 +/**
935 + * Deletes the stored access token, refresh token and its expiry from the Plugin settings,
936 + * and clears any existing scheduled WordPress Cron event to refresh the token on expiry,
937 + * when either:
938 + * - The access token is invalid
939 + * - The access token expired, and refreshing failed
940 + *
941 + * @since 3.1.1
942 + *
943 + * @param WP_Error $result Error result.
944 + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
945 + */
946 +function convertkit_maybe_delete_credentials( $result, $client_id ) {
947 +
948 + // Don't save these credentials if they're not for this Client ID.
949 + // They're for another Kit Plugin that uses OAuth.
950 + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
951 + return;
952 + }
953 +
954 + // If the error isn't a 401, don't delete credentials.
955 + // This could be e.g. a temporary network error, rate limit or similar.
956 + if ( $result->get_error_data( 'convertkit_api_error' ) !== 401 ) {
957 + return;
958 + }
959 +
960 + // Persist an error notice in the WordPress Administration until the user fixes the problem.
961 + WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' );
962 +
963 + $settings = new ConvertKit_Settings();
964 + $settings->delete_credentials();
965 +
966 +}
967 +
968 +// Update Access Token when refreshed by the API class.
969 +add_action( 'convertkit_api_get_access_token', 'convertkit_maybe_update_credentials', 10, 2 );
970 +add_action( 'convertkit_api_refresh_token', 'convertkit_maybe_update_credentials', 10, 2 );
971 +
972 +// Delete credentials when the user revokes the access and refresh tokens.
973 +add_action( 'convertkit_api_revoke_tokens', 'convertkit_delete_credentials', 10, 1 );
974 +
975 +// Delete credentials if the API class uses a invalid access token.
976 +// This prevents the Plugin making repetitive API requests that will 401.
977 +add_action( 'convertkit_api_access_token_invalid', 'convertkit_maybe_delete_credentials', 10, 2 );