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