PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.1.9
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.1.9
3.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 All 195 releases
convertkit / includes / functions.php

functions.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.1.9, at includes/functions.php

808 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit general plugin functions.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Runs the activation and update routines when the plugin is activated.
11 *
12 * @since 1.9.7.4
13 *
14 * @param bool $network_wide Is network wide activation.
15 */
16 function convertkit_plugin_activate( $network_wide ) {
17
18 // Initialise Plugin.
19 $convertkit = WP_ConvertKit();
20 $convertkit->initialize();
21
22 // Check if we are on a multisite install, activating network wide, or a single install.
23 if ( ! is_multisite() || ! $network_wide ) {
24 // Single Site activation.
25 $convertkit->get_class( 'setup' )->activate();
26
27 // Set a transient for 30 seconds to redirect to the setup screen on activation.
28 set_transient( 'convertkit-setup', true, 30 );
29 } else {
30 // Multisite network wide activation.
31 $sites = get_sites(
32 array(
33 'number' => 0,
34 )
35 );
36 foreach ( $sites as $site ) {
37 switch_to_blog( (int) $site->blog_id );
38 $convertkit->get_class( 'setup' )->activate();
39 restore_current_blog();
40 }
41 }
42
43 }
44
45 /**
46 * Runs the activation and update routines when the plugin is activated
47 * on a WordPress multisite setup.
48 *
49 * @since 1.9.7.4
50 *
51 * @param WP_Site|int $site_or_blog_id WP_Site or Blog ID.
52 */
53 function convertkit_plugin_activate_new_site( $site_or_blog_id ) {
54
55 // Check if $site_or_blog_id is a WP_Site or a blog ID.
56 if ( is_a( $site_or_blog_id, 'WP_Site' ) ) {
57 $site_or_blog_id = $site_or_blog_id->blog_id;
58 }
59
60 // Initialise Plugin.
61 $convertkit = WP_ConvertKit();
62 $convertkit->initialize();
63
64 // Run installation routine.
65 switch_to_blog( $site_or_blog_id );
66 $convertkit->get_class( 'setup' )->activate();
67 restore_current_blog();
68
69 }
70
71 /**
72 * Runs the deactivation routine when the plugin is deactivated.
73 *
74 * @since 1.9.7.4
75 *
76 * @param bool $network_wide Is network wide deactivation.
77 */
78 function convertkit_plugin_deactivate( $network_wide ) {
79
80 // Initialise Plugin.
81 $convertkit = WP_ConvertKit();
82 $convertkit->initialize();
83
84 // Check if we are on a multisite install, activating network wide, or a single install.
85 if ( ! is_multisite() || ! $network_wide ) {
86 // Single Site activation.
87 $convertkit->get_class( 'setup' )->deactivate();
88 } else {
89 // Multisite network wide activation.
90 $sites = get_sites(
91 array(
92 'number' => 0,
93 )
94 );
95 foreach ( $sites as $site ) {
96 switch_to_blog( (int) $site->blog_id );
97 $convertkit->get_class( 'setup' )->deactivate();
98 restore_current_blog();
99 }
100 }
101
102 }
103
104 /**
105 * Helper method to get supported Post Types.
106 *
107 * @since 1.9.6
108 *
109 * @return array Post Types
110 */
111 function convertkit_get_supported_post_types() {
112
113 // Define supported Post Types.
114 $post_types = array(
115 'page',
116 'post',
117 );
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
137 /**
138 * Defines the Post Types that support ConvertKit Forms.
139 *
140 * @since 1.9.6
141 *
142 * @param array $post_types Post Types
143 */
144 $post_types = apply_filters( 'convertkit_get_supported_post_types', $post_types );
145
146 return $post_types;
147
148 }
149
150 /**
151 * Helper method to get supported Post Types for Restricted Content (Member's Content)
152 *
153 * @since 2.1.0
154 *
155 * @deprecated 2.4.3 No longer used by internal code and not recommended. Use `convertkit_get_supported_post_types` instead.
156 *
157 * @return array Post Types
158 */
159 function convertkit_get_supported_restrict_content_post_types() {
160
161 return convertkit_get_supported_post_types();
162
163 }
164
165 /**
166 * Helper method to get registered Shortcodes.
167 *
168 * @since 1.9.6.5
169 *
170 * @return array Shortcodes
171 */
172 function convertkit_get_shortcodes() {
173
174 $shortcodes = array();
175
176 /**
177 * Registers shortcodes for the ConvertKit Plugin.
178 *
179 * @since 1.9.6.5
180 *
181 * @param array $shortcodes Shortcodes
182 */
183 $shortcodes = apply_filters( 'convertkit_shortcodes', $shortcodes );
184
185 return $shortcodes;
186
187 }
188
189 /**
190 * Helper method to get registered Blocks.
191 *
192 * @since 1.9.6
193 *
194 * @return array Blocks
195 */
196 function convertkit_get_blocks() {
197
198 $blocks = array();
199
200 /**
201 * Registers blocks for the ConvertKit Plugin.
202 *
203 * @since 1.9.6
204 *
205 * @param array $blocks Blocks
206 */
207 $blocks = apply_filters( 'convertkit_blocks', $blocks );
208
209 return $blocks;
210
211 }
212
213 /**
214 * Helper method to get registered Block formatters for Gutenberg.
215 *
216 * @since 2.2.0
217 *
218 * @return array Block formatters
219 */
220 function convertkit_get_block_formatters() {
221
222 $block_formatters = array();
223
224 /**
225 * Registers block formatters in Gutenberg for the ConvertKit Plugin.
226 *
227 * @since 2.2.0
228 *
229 * @param array $block_formatters Block formatters.
230 */
231 $block_formatters = apply_filters( 'convertkit_get_block_formatters', $block_formatters );
232
233 return $block_formatters;
234
235 }
236
237 /**
238 * Helper method to get registered pre-publish actions.
239 *
240 * @since 2.4.0
241 *
242 * @return array Pre-publish actions
243 */
244 function convertkit_get_pre_publish_actions() {
245
246 $pre_publish_actions = array();
247
248 /**
249 * Registers pre-publish actions for the ConvertKit Plugin.
250 *
251 * @since 2.4.0
252 *
253 * @param array $pre_publish_panels Pre-publish actions.
254 */
255 $pre_publish_actions = apply_filters( 'convertkit_get_pre_publish_actions', $pre_publish_actions );
256
257 return $pre_publish_actions;
258
259 }
260
261 /**
262 * Helper method to get registered importers that can replace third party
263 * form shortcodes and blocks with Kit form shortcodes and blocks.
264 *
265 * @since 3.1.7
266 *
267 * @return array Importers.
268 */
269 function convertkit_get_form_importers() {
270
271 $importers = array();
272
273 /**
274 * Registers form importers for the ConvertKit Plugin.
275 *
276 * @since 3.1.7
277 *
278 * @param array $importers Importers.
279 */
280 $importers = apply_filters( 'convertkit_get_form_importers', $importers );
281
282 return $importers;
283
284 }
285
286 /**
287 * Helper method to return the Plugin Settings Link
288 *
289 * @since 1.9.6
290 *
291 * @param array $query_args Optional Query Args.
292 * @return string Settings Link
293 */
294 function convertkit_get_settings_link( $query_args = array() ) {
295
296 $query_args = array_merge(
297 $query_args,
298 array(
299 'page' => '_wp_convertkit_settings',
300 )
301 );
302
303 return add_query_arg( $query_args, admin_url( 'options-general.php' ) );
304
305 }
306
307 /**
308 * Helper method to return the Plugin Settings Link
309 *
310 * @since 2.2.4
311 *
312 * @param array $query_args Optional Query Args.
313 * @return string Settings Link
314 */
315 function convertkit_get_setup_wizard_plugin_link( $query_args = array() ) {
316
317 $query_args = array_merge(
318 $query_args,
319 array(
320 'page' => 'convertkit-setup',
321 )
322 );
323
324 return add_query_arg( $query_args, admin_url( 'options.php' ) );
325
326 }
327
328 /**
329 * Helper method to return the URL the user needs to visit to register a ConvertKit account.
330 *
331 * @since 1.9.8.4
332 *
333 * @return string ConvertKit Registration URL.
334 */
335 function convertkit_get_registration_url() {
336
337 return add_query_arg(
338 array(
339 'utm_source' => 'wordpress',
340 'utm_term' => get_locale(),
341 'utm_content' => 'convertkit',
342 ),
343 'https://app.kit.com/users/signup'
344 );
345
346 }
347
348 /**
349 * Helper method to return the URL the user needs to visit to sign in to their ConvertKit account.
350 *
351 * @since 1.9.6.1
352 *
353 * @return string ConvertKit Login URL.
354 */
355 function convertkit_get_sign_in_url() {
356
357 return add_query_arg(
358 array(
359 'utm_source' => 'wordpress',
360 'utm_term' => get_locale(),
361 'utm_content' => 'convertkit',
362 ),
363 'https://app.kit.com/'
364 );
365
366 }
367
368 /**
369 * Helper method to return the URL the user needs to visit to manage thier billing.
370 *
371 * @since 2.2.7
372 *
373 * @return string ConvertKit Billing URL.
374 */
375 function convertkit_get_billing_url() {
376
377 return add_query_arg(
378 array(
379 'utm_source' => 'wordpress',
380 'utm_term' => get_locale(),
381 'utm_content' => 'convertkit',
382 ),
383 'https://app.kit.com/account_settings/billing/'
384 );
385
386 }
387
388 /**
389 * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Form or Landing Page.
390 *
391 * @since 2.2.3
392 *
393 * @return string ConvertKit App URL
394 */
395 function convertkit_get_new_form_url() {
396
397 return add_query_arg(
398 array(
399 'utm_source' => 'wordpress',
400 'utm_term' => get_locale(),
401 'utm_content' => 'convertkit',
402 ),
403 'https://app.kit.com/forms/new/'
404 );
405
406 }
407
408 /**
409 * Helper method to return the URL the user needs to visit to edit ConvertKit forms.
410 *
411 * @since 2.2.3
412 *
413 * @return string ConvertKit Form Editor URL.
414 */
415 function convertkit_get_form_editor_url() {
416
417 return add_query_arg(
418 array(
419 'utm_source' => 'wordpress',
420 'utm_term' => get_locale(),
421 'utm_content' => 'convertkit',
422 ),
423 'https://app.kit.com/forms'
424 );
425
426 }
427
428 /**
429 * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Landing Page.
430 *
431 * @since 2.5.5
432 *
433 * @return string ConvertKit App URL
434 */
435 function convertkit_get_new_landing_page_url() {
436
437 return add_query_arg(
438 array(
439 'utm_source' => 'wordpress',
440 'utm_term' => get_locale(),
441 'utm_content' => 'convertkit',
442 ),
443 'https://app.kit.com/pages/new/'
444 );
445
446 }
447
448 /**
449 * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Tag.
450 *
451 * @since 2.3.3
452 *
453 * @return string ConvertKit App URL.
454 */
455 function convertkit_get_new_tag_url() {
456
457 return add_query_arg(
458 array(
459 'utm_source' => 'wordpress',
460 'utm_term' => get_locale(),
461 'utm_content' => 'convertkit',
462 ),
463 'https://app.kit.com/subscribers/'
464 );
465
466 }
467
468 /**
469 * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Broadcast.
470 *
471 * @since 2.2.6
472 *
473 * @return string ConvertKit App URL.
474 */
475 function convertkit_get_new_broadcast_url() {
476
477 return add_query_arg(
478 array(
479 'utm_source' => 'wordpress',
480 'utm_term' => get_locale(),
481 'utm_content' => 'convertkit',
482 ),
483 'https://app.kit.com/campaigns/'
484 );
485
486 }
487
488 /**
489 * Helper method to return the URL the user needs to visit on the ConvertKit app to edit a draft Broadcast.
490 *
491 * @since 2.4.0
492 *
493 * @param int $broadcast_id ConvertKit Broadcast ID.
494 * @return string ConvertKit App URL.
495 */
496 function convertkit_get_edit_broadcast_url( $broadcast_id ) {
497
498 return add_query_arg(
499 array(
500 'utm_source' => 'wordpress',
501 'utm_term' => get_locale(),
502 'utm_content' => 'convertkit',
503 ),
504 sprintf(
505 'https://app.kit.com/campaigns/%s/draft',
506 $broadcast_id
507 )
508 );
509
510 }
511
512 /**
513 * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Product.
514 *
515 * @since 2.2.3
516 *
517 * @return string ConvertKit App URL.
518 */
519 function convertkit_get_new_product_url() {
520
521 return add_query_arg(
522 array(
523 'utm_source' => 'wordpress',
524 'utm_term' => get_locale(),
525 'utm_content' => 'convertkit',
526 ),
527 'https://app.kit.com/products/new/'
528 );
529
530 }
531
532 /**
533 * Helper method to enqueue Select2 scripts for use within the ConvertKit Plugin.
534 *
535 * @since 1.9.6.4
536 */
537 function convertkit_select2_enqueue_scripts() {
538
539 wp_enqueue_script( 'convertkit-select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, false );
540 wp_enqueue_script( 'convertkit-admin-select2', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/select2.js', array( 'convertkit-select2' ), CONVERTKIT_PLUGIN_VERSION, false );
541
542 }
543
544 /**
545 * Helper method to enqueue Select2 stylesheets for use within the ConvertKit Plugin.
546 *
547 * @since 1.9.6.4
548 */
549 function convertkit_select2_enqueue_styles() {
550
551 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 );
552 wp_enqueue_style( 'convertkit-admin-select2', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/select2.css', array(), CONVERTKIT_PLUGIN_VERSION );
553
554 }
555
556 /**
557 * Return the contents of the given local file.
558 *
559 * @since 2.2.2
560 *
561 * @param string $local_file Local file, including path.
562 * @return string File contents.
563 */
564 function convertkit_get_file_contents( $local_file ) {
565
566 // Bail if the file doesn't exist.
567 if ( ! file_exists( $local_file ) ) {
568 return '';
569 }
570
571 // Read file.
572 $contents = file_get_contents( $local_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
573
574 // Return an empty string if the contents of the file could not be read.
575 if ( ! $contents ) {
576 return '';
577 }
578
579 // Return file's contents.
580 return $contents;
581
582 }
583
584 /**
585 * Returns a dropdown field commonly used for settings, comprising of:
586 * - Do not subscribe
587 * - Subscribe
588 * - Subscribe to Form
589 *
590 * @since 2.5.2
591 *
592 * @param string $name Field name.
593 * @param string $value Field value.
594 * @param string $id Field ID attribute.
595 * @param string $css_class Field CSS class(es).
596 * @param string $context Resource context.
597 * @param bool|array $additional_options Additional <option> key/value pairs.
598 */
599 function convertkit_get_subscription_dropdown_field( $name, $value, $id, $css_class = '', $context = '', $additional_options = false ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
600
601 // Load resource classes.
602 $forms = new ConvertKit_Resource_Forms( $context );
603 $tags = new ConvertKit_Resource_Tags( $context );
604 $sequences = new ConvertKit_Resource_Sequences( $context );
605
606 ob_start();
607 include CONVERTKIT_PLUGIN_PATH . '/views/backend/subscription-dropdown-field.php';
608 $output = trim( ob_get_clean() );
609
610 // Return output.
611 return $output;
612
613 }
614
615 /**
616 * Helper method to safely call get_current_screen(), returning false
617 * if the function is not available or returns null.
618 *
619 * Otherwise returns the given WP_Screen property.
620 *
621 * @since 2.5.9
622 *
623 * @param string $property WP_Screen property to return.
624 * @return bool|string
625 */
626 function convertkit_get_current_screen( $property ) {
627
628 // Bail if we cannot determine the screen.
629 if ( ! function_exists( 'get_current_screen' ) ) {
630 return false;
631 }
632
633 // Get screen.
634 $screen = get_current_screen();
635
636 // Bail if the screen couldn't be determined.
637 if ( is_null( $screen ) ) {
638 return false;
639 }
640
641 // Return property.
642 return $screen->$property;
643
644 }
645
646 /**
647 * Outputs the Intercom help widget script.
648 *
649 * @since 2.7.2
650 */
651 function convertkit_output_intercom_messenger() {
652
653 ?>
654 <script>
655 const KIT_INTERCOM_APP_ID = 'e4n3xtxz';
656 window.intercomSettings = {
657 api_base: 'https://api-iam.intercom.io',
658 app_id: KIT_INTERCOM_APP_ID
659 };
660 </script>
661
662 <script>
663 (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);}}})();
664 </script>
665 <?php
666
667 }
668
669 /**
670 * Checks if the given Theme is active.
671 *
672 * @since 3.1.4
673 *
674 * @param string $theme_name Theme name.
675 * @return bool
676 */
677 function convertkit_is_theme_active( $theme_name ) {
678
679 // Assume Theme isn't active if we can't detect it.
680 if ( ! function_exists( 'wp_get_theme' ) ) {
681 return false;
682 }
683
684 // Check the Parent Theme if we're on a Child Theme.
685 if ( wp_get_theme()->parent() ) {
686 $theme = wp_get_theme()->parent();
687 } else {
688 $theme = wp_get_theme();
689 }
690
691 return strtolower( $theme->get( 'Name' ) ) === strtolower( $theme_name );
692
693 }
694
695 /**
696 * Returns permitted HTML output when using wp_kses( ..., convertkit_kses_allowed_html()).
697 *
698 * @since 2.8.5
699 */
700 function convertkit_kses_allowed_html() {
701
702 // Get WordPress' permitted HTML elements.
703 $elements = wp_kses_allowed_html( 'post' );
704
705 // Add form elements.
706 $form_elements = array(
707 'input' => array(
708 'type' => true,
709 'id' => true,
710 'name' => true,
711 'class' => true,
712 'value' => true,
713 'checked' => true,
714 'min' => true,
715 'max' => true,
716 'step' => true,
717 'data-*' => true,
718 ),
719 'select' => array(
720 'id' => true,
721 'name' => true,
722 'class' => true,
723 'size' => true,
724 'multiple' => true,
725 'data-*' => true,
726 ),
727 'option' => array(
728 'value' => true,
729 'selected' => true,
730 'data-*' => true,
731 ),
732 'optgroup' => array(
733 'label' => true,
734 'data-*' => true,
735 ),
736 'label' => array(
737 'for' => true,
738 ),
739 );
740
741 return array_merge( $elements, $form_elements );
742
743 }
744
745 /**
746 * Saves the new access token, refresh token and its expiry, and schedules
747 * a WordPress Cron event to refresh the token on expiry.
748 *
749 * @since 3.1.1
750 *
751 * @param array $result New Access Token, Refresh Token and Expiry.
752 * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
753 */
754 function convertkit_maybe_update_credentials( $result, $client_id ) {
755
756 // Don't save these credentials if they're not for this Client ID.
757 // They're for another Kit Plugin that uses OAuth.
758 if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
759 return;
760 }
761
762 $settings = new ConvertKit_Settings();
763 $settings->update_credentials( $result );
764
765 }
766
767 /**
768 * Deletes the stored access token, refresh token and its expiry from the Plugin settings,
769 * and clears any existing scheduled WordPress Cron event to refresh the token on expiry,
770 * when either:
771 * - The access token is invalid
772 * - The access token expired, and refreshing failed
773 *
774 * @since 3.1.1
775 *
776 * @param WP_Error $result Error result.
777 * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
778 */
779 function convertkit_maybe_delete_credentials( $result, $client_id ) {
780
781 // Don't save these credentials if they're not for this Client ID.
782 // They're for another Kit Plugin that uses OAuth.
783 if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) {
784 return;
785 }
786
787 // If the error isn't a 401, don't delete credentials.
788 // This could be e.g. a temporary network error, rate limit or similar.
789 if ( $result->get_error_data( 'convertkit_api_error' ) !== 401 ) {
790 return;
791 }
792
793 // Persist an error notice in the WordPress Administration until the user fixes the problem.
794 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' );
795
796 $settings = new ConvertKit_Settings();
797 $settings->delete_credentials();
798
799 }
800
801 // Update Access Token when refreshed by the API class.
802 add_action( 'convertkit_api_get_access_token', 'convertkit_maybe_update_credentials', 10, 2 );
803 add_action( 'convertkit_api_refresh_token', 'convertkit_maybe_update_credentials', 10, 2 );
804
805 // Delete credentials if the API class uses a invalid access token.
806 // This prevents the Plugin making repetitive API requests that will 401.
807 add_action( 'convertkit_api_access_token_invalid', 'convertkit_maybe_delete_credentials', 10, 2 );
808