| @@ -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 |
| @@ -129,27 +151,16 @@ | ||
| 129 | 151 | * Helper method to get supported Post Types for Restricted Content (Member's Content) |
| 130 | 152 | * |
| 131 | 153 | * @since 2.1.0 |
| 132 | 154 | * |
| 155 | + * @deprecated 2.4.3 No longer used by internal code and not recommended. Use `convertkit_get_supported_post_types` instead. | |
| 156 | + * | |
| 133 | 157 | * @return array Post Types |
| 134 | 158 | */ |
| 135 | 159 | function convertkit_get_supported_restrict_content_post_types() { |
| 136 | 160 | |
| 137 | - $post_types = array( | |
| 138 | - 'page', | |
| 139 | - ); | |
| 161 | + return convertkit_get_supported_post_types(); | |
| 140 | 162 | |
| 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 | 163 | } |
| 153 | 164 | |
| 154 | 165 | /** |
| 155 | 166 | * Helper method to get registered Shortcodes. |
| @@ -223,8 +234,81 @@ | ||
| 223 | 234 | |
| 224 | 235 | } |
| 225 | 236 | |
| 226 | 237 | /** |
| 238 | + * Helper method to get registered plugin sidebars. | |
| 239 | + * | |
| 240 | + * @since 3.3.0 | |
| 241 | + * | |
| 242 | + * @return array Plugin sidebars | |
| 243 | + */ | |
| 244 | +function convertkit_get_plugin_sidebars() { | |
| 245 | + | |
| 246 | + $plugin_sidebars = array(); | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Registers plugin sidebars for the WordPress block editor. | |
| 250 | + * | |
| 251 | + * @since 3.3.0 | |
| 252 | + * | |
| 253 | + * @param array $plugin_sidebars Plugin sidebars. | |
| 254 | + */ | |
| 255 | + $plugin_sidebars = apply_filters( 'convertkit_plugin_sidebars', $plugin_sidebars ); | |
| 256 | + | |
| 257 | + return $plugin_sidebars; | |
| 258 | + | |
| 259 | +} | |
| 260 | + | |
| 261 | +/** | |
| 262 | + * Helper method to get registered pre-publish actions. | |
| 263 | + * | |
| 264 | + * @since 2.4.0 | |
| 265 | + * | |
| 266 | + * @return array Pre-publish actions | |
| 267 | + */ | |
| 268 | +function convertkit_get_pre_publish_actions() { | |
| 269 | + | |
| 270 | + $pre_publish_actions = array(); | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * Registers pre-publish actions for the ConvertKit Plugin. | |
| 274 | + * | |
| 275 | + * @since 2.4.0 | |
| 276 | + * | |
| 277 | + * @param array $pre_publish_panels Pre-publish actions. | |
| 278 | + */ | |
| 279 | + $pre_publish_actions = apply_filters( 'convertkit_get_pre_publish_actions', $pre_publish_actions ); | |
| 280 | + | |
| 281 | + return $pre_publish_actions; | |
| 282 | + | |
| 283 | +} | |
| 284 | + | |
| 285 | +/** | |
| 286 | + * Helper method to get registered importers that can replace third party | |
| 287 | + * form shortcodes and blocks with Kit form shortcodes and blocks. | |
| 288 | + * | |
| 289 | + * @since 3.1.7 | |
| 290 | + * | |
| 291 | + * @return array Importers. | |
| 292 | + */ | |
| 293 | +function convertkit_get_form_importers() { | |
| 294 | + | |
| 295 | + $importers = array(); | |
| 296 | + | |
| 297 | + /** | |
| 298 | + * Registers form importers for the ConvertKit Plugin. | |
| 299 | + * | |
| 300 | + * @since 3.1.7 | |
| 301 | + * | |
| 302 | + * @param array $importers Importers. | |
| 303 | + */ | |
| 304 | + $importers = apply_filters( 'convertkit_get_form_importers', $importers ); | |
| 305 | + | |
| 306 | + return $importers; | |
| 307 | + | |
| 308 | +} | |
| 309 | + | |
| 310 | +/** | |
| 227 | 311 | * Helper method to return the Plugin Settings Link |
| 228 | 312 | * |
| 229 | 313 | * @since 1.9.6 |
| 230 | 314 | * |
| @@ -244,8 +328,29 @@ | ||
| 244 | 328 | |
| 245 | 329 | } |
| 246 | 330 | |
| 247 | 331 | /** |
| 332 | + * Helper method to return the Plugin Settings Link | |
| 333 | + * | |
| 334 | + * @since 2.2.4 | |
| 335 | + * | |
| 336 | + * @param array $query_args Optional Query Args. | |
| 337 | + * @return string Settings Link | |
| 338 | + */ | |
| 339 | +function convertkit_get_setup_wizard_plugin_link( $query_args = array() ) { | |
| 340 | + | |
| 341 | + $query_args = array_merge( | |
| 342 | + $query_args, | |
| 343 | + array( | |
| 344 | + 'page' => 'convertkit-setup', | |
| 345 | + ) | |
| 346 | + ); | |
| 347 | + | |
| 348 | + return add_query_arg( $query_args, admin_url( 'options.php' ) ); | |
| 349 | + | |
| 350 | +} | |
| 351 | + | |
| 352 | +/** | |
| 248 | 353 | * Helper method to return the URL the user needs to visit to register a ConvertKit account. |
| 249 | 354 | * |
| 250 | 355 | * @since 1.9.8.4 |
| 251 | 356 | * |
| @@ -258,9 +363,9 @@ | ||
| 258 | 363 | 'utm_source' => 'wordpress', |
| 259 | 364 | 'utm_term' => get_locale(), |
| 260 | 365 | 'utm_content' => 'convertkit', |
| 261 | 366 | ), |
| 262 | - 'https://app.convertkit.com/users/signup' | |
| 367 | + 'https://app.kit.com/users/signup' | |
| 263 | 368 | ); |
| 264 | 369 | |
| 265 | 370 | } |
| 266 | 371 | |
| @@ -278,21 +383,101 @@ | ||
| 278 | 383 | 'utm_source' => 'wordpress', |
| 279 | 384 | 'utm_term' => get_locale(), |
| 280 | 385 | 'utm_content' => 'convertkit', |
| 281 | 386 | ), |
| 282 | - 'https://app.convertkit.com/' | |
| 387 | + 'https://app.kit.com/' | |
| 283 | 388 | ); |
| 284 | 389 | |
| 285 | 390 | } |
| 286 | 391 | |
| 287 | 392 | /** |
| 288 | - * Helper method to return the URL the user needs to visit on the ConvertKit app to obtain their API Key and Secret. | |
| 393 | + * Helper method to return the URL the user needs to visit to manage thier billing. | |
| 289 | 394 | * |
| 290 | - * @since 1.9.6.1 | |
| 395 | + * @since 2.2.7 | |
| 291 | 396 | * |
| 397 | + * @return string ConvertKit Billing URL. | |
| 398 | + */ | |
| 399 | +function convertkit_get_billing_url() { | |
| 400 | + | |
| 401 | + return add_query_arg( | |
| 402 | + array( | |
| 403 | + 'utm_source' => 'wordpress', | |
| 404 | + 'utm_term' => get_locale(), | |
| 405 | + 'utm_content' => 'convertkit', | |
| 406 | + ), | |
| 407 | + 'https://app.kit.com/account_settings/billing/' | |
| 408 | + ); | |
| 409 | + | |
| 410 | +} | |
| 411 | + | |
| 412 | +/** | |
| 413 | + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Form or Landing Page. | |
| 414 | + * | |
| 415 | + * @since 2.2.3 | |
| 416 | + * | |
| 417 | + * @return string ConvertKit App URL | |
| 418 | + */ | |
| 419 | +function convertkit_get_new_form_url() { | |
| 420 | + | |
| 421 | + return add_query_arg( | |
| 422 | + array( | |
| 423 | + 'utm_source' => 'wordpress', | |
| 424 | + 'utm_term' => get_locale(), | |
| 425 | + 'utm_content' => 'convertkit', | |
| 426 | + ), | |
| 427 | + 'https://app.kit.com/forms/new/' | |
| 428 | + ); | |
| 429 | + | |
| 430 | +} | |
| 431 | + | |
| 432 | +/** | |
| 433 | + * Helper method to return the URL the user needs to visit to edit ConvertKit forms. | |
| 434 | + * | |
| 435 | + * @since 2.2.3 | |
| 436 | + * | |
| 437 | + * @return string ConvertKit Form Editor URL. | |
| 438 | + */ | |
| 439 | +function convertkit_get_form_editor_url() { | |
| 440 | + | |
| 441 | + return add_query_arg( | |
| 442 | + array( | |
| 443 | + 'utm_source' => 'wordpress', | |
| 444 | + 'utm_term' => get_locale(), | |
| 445 | + 'utm_content' => 'convertkit', | |
| 446 | + ), | |
| 447 | + 'https://app.kit.com/forms' | |
| 448 | + ); | |
| 449 | + | |
| 450 | +} | |
| 451 | + | |
| 452 | +/** | |
| 453 | + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Landing Page. | |
| 454 | + * | |
| 455 | + * @since 2.5.5 | |
| 456 | + * | |
| 457 | + * @return string ConvertKit App URL | |
| 458 | + */ | |
| 459 | +function convertkit_get_new_landing_page_url() { | |
| 460 | + | |
| 461 | + return add_query_arg( | |
| 462 | + array( | |
| 463 | + 'utm_source' => 'wordpress', | |
| 464 | + 'utm_term' => get_locale(), | |
| 465 | + 'utm_content' => 'convertkit', | |
| 466 | + ), | |
| 467 | + 'https://app.kit.com/pages/new/' | |
| 468 | + ); | |
| 469 | + | |
| 470 | +} | |
| 471 | + | |
| 472 | +/** | |
| 473 | + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Tag. | |
| 474 | + * | |
| 475 | + * @since 2.3.3 | |
| 476 | + * | |
| 292 | 477 | * @return string ConvertKit App URL. |
| 293 | 478 | */ |
| 294 | -function convertkit_get_api_key_url() { | |
| 479 | +function convertkit_get_new_tag_url() { | |
| 295 | 480 | |
| 296 | 481 | return add_query_arg( |
| 297 | 482 | array( |
| 298 | 483 | 'utm_source' => 'wordpress', |
| @@ -298,14 +483,106 @@ | ||
| 298 | 483 | 'utm_source' => 'wordpress', |
| 299 | 484 | 'utm_term' => get_locale(), |
| 300 | 485 | 'utm_content' => 'convertkit', |
| 301 | 486 | ), |
| 302 | - 'https://app.convertkit.com/account_settings/advanced_settings/' | |
| 487 | + 'https://app.kit.com/subscribers/' | |
| 303 | 488 | ); |
| 304 | 489 | |
| 305 | 490 | } |
| 306 | 491 | |
| 307 | 492 | /** |
| 493 | + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Broadcast. | |
| 494 | + * | |
| 495 | + * @since 2.2.6 | |
| 496 | + * | |
| 497 | + * @return string ConvertKit App URL. | |
| 498 | + */ | |
| 499 | +function convertkit_get_new_broadcast_url() { | |
| 500 | + | |
| 501 | + return add_query_arg( | |
| 502 | + array( | |
| 503 | + 'utm_source' => 'wordpress', | |
| 504 | + 'utm_term' => get_locale(), | |
| 505 | + 'utm_content' => 'convertkit', | |
| 506 | + ), | |
| 507 | + 'https://app.kit.com/campaigns/' | |
| 508 | + ); | |
| 509 | + | |
| 510 | +} | |
| 511 | + | |
| 512 | +/** | |
| 513 | + * Helper method to return the URL the user needs to visit on the ConvertKit app to edit a draft Broadcast. | |
| 514 | + * | |
| 515 | + * @since 2.4.0 | |
| 516 | + * | |
| 517 | + * @param int $broadcast_id ConvertKit Broadcast ID. | |
| 518 | + * @return string ConvertKit App URL. | |
| 519 | + */ | |
| 520 | +function convertkit_get_edit_broadcast_url( $broadcast_id ) { | |
| 521 | + | |
| 522 | + return add_query_arg( | |
| 523 | + array( | |
| 524 | + 'utm_source' => 'wordpress', | |
| 525 | + 'utm_term' => get_locale(), | |
| 526 | + 'utm_content' => 'convertkit', | |
| 527 | + ), | |
| 528 | + sprintf( | |
| 529 | + 'https://app.kit.com/campaigns/%s/draft', | |
| 530 | + $broadcast_id | |
| 531 | + ) | |
| 532 | + ); | |
| 533 | + | |
| 534 | +} | |
| 535 | + | |
| 536 | +/** | |
| 537 | + * Helper method to return the URL the user needs to visit on the ConvertKit app to create a new Product. | |
| 538 | + * | |
| 539 | + * @since 2.2.3 | |
| 540 | + * | |
| 541 | + * @return string ConvertKit App URL. | |
| 542 | + */ | |
| 543 | +function convertkit_get_new_product_url() { | |
| 544 | + | |
| 545 | + return add_query_arg( | |
| 546 | + array( | |
| 547 | + 'utm_source' => 'wordpress', | |
| 548 | + 'utm_term' => get_locale(), | |
| 549 | + 'utm_content' => 'convertkit', | |
| 550 | + ), | |
| 551 | + 'https://app.kit.com/products/new/' | |
| 552 | + ); | |
| 553 | + | |
| 554 | +} | |
| 555 | + | |
| 556 | +/** | |
| 557 | + * Helper method to enqueue the frontend CSS file. | |
| 558 | + * | |
| 559 | + * @since 3.2.0 | |
| 560 | + */ | |
| 561 | +function convertkit_enqueue_frontend_css() { | |
| 562 | + | |
| 563 | + wp_enqueue_style( 'convertkit-frontend', CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/frontend.css', array(), CONVERTKIT_PLUGIN_VERSION ); | |
| 564 | + | |
| 565 | +} | |
| 566 | + | |
| 567 | +/** | |
| 568 | + * Helper method to enqueue the frontend JS file. | |
| 569 | + * | |
| 570 | + * @since 3.2.0 | |
| 571 | + */ | |
| 572 | +function convertkit_enqueue_frontend_js() { | |
| 573 | + | |
| 574 | + wp_enqueue_script( | |
| 575 | + 'convertkit-js', | |
| 576 | + CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/dist/frontend.min.js', | |
| 577 | + array(), | |
| 578 | + CONVERTKIT_PLUGIN_VERSION, | |
| 579 | + true | |
| 580 | + ); | |
| 581 | + | |
| 582 | +} | |
| 583 | + | |
| 584 | +/** | |
| 308 | 585 | * Helper method to enqueue Select2 scripts for use within the ConvertKit Plugin. |
| 309 | 586 | * |
| 310 | 587 | * @since 1.9.6.4 |
| 311 | 588 | */ |
| @@ -337,22 +614,272 @@ | ||
| 337 | 614 | * @return string File contents. |
| 338 | 615 | */ |
| 339 | 616 | function convertkit_get_file_contents( $local_file ) { |
| 340 | 617 | |
| 341 | - // Call globals. | |
| 342 | - global $wp_filesystem; | |
| 618 | + // Bail if the file doesn't exist. | |
| 619 | + if ( ! file_exists( $local_file ) ) { | |
| 620 | + return ''; | |
| 621 | + } | |
| 343 | 622 | |
| 344 | - // Load filesystem class. | |
| 345 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 623 | + // Read file. | |
| 624 | + $contents = file_get_contents( $local_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 346 | 625 | |
| 347 | - // Initiate. | |
| 348 | - WP_Filesystem(); | |
| 349 | - | |
| 350 | - // Bail if the file doesn't exist. | |
| 351 | - if ( ! $wp_filesystem->exists( $local_file ) ) { | |
| 626 | + // Return an empty string if the contents of the file could not be read. | |
| 627 | + if ( ! $contents ) { | |
| 352 | 628 | return ''; |
| 353 | 629 | } |
| 354 | 630 | |
| 355 | 631 | // Return file's contents. |
| 356 | - return $wp_filesystem->get_contents( $local_file ); | |
| 632 | + return $contents; | |
| 357 | 633 | |
| 358 | 634 | } |
| 635 | + | |
| 636 | +/** | |
| 637 | + * Returns a dropdown field commonly used for settings, comprising of: | |
| 638 | + * - Do not subscribe | |
| 639 | + * - Subscribe | |
| 640 | + * - Subscribe to Form | |
| 641 | + * | |
| 642 | + * @since 2.5.2 | |
| 643 | + * | |
| 644 | + * @param string $name Field name. | |
| 645 | + * @param string $value Field value. | |
| 646 | + * @param string $id Field ID attribute. | |
| 647 | + * @param string $css_class Field CSS class(es). | |
| 648 | + * @param string $context Resource context. | |
| 649 | + * @param bool|array $additional_options Additional <option> key/value pairs. | |
| 650 | + */ | |
| 651 | +function convertkit_get_subscription_dropdown_field( $name, $value, $id, $css_class = '', $context = '', $additional_options = false ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | |
| 652 | + | |
| 653 | + // Load resource classes. | |
| 654 | + $forms = new ConvertKit_Resource_Forms( $context ); | |
| 655 | + $tags = new ConvertKit_Resource_Tags( $context ); | |
| 656 | + $sequences = new ConvertKit_Resource_Sequences( $context ); | |
| 657 | + | |
| 658 | + ob_start(); | |
| 659 | + include CONVERTKIT_PLUGIN_PATH . '/views/backend/subscription-dropdown-field.php'; | |
| 660 | + $output = trim( ob_get_clean() ); | |
| 661 | + | |
| 662 | + // Return output. | |
| 663 | + return $output; | |
| 664 | + | |
| 665 | +} | |
| 666 | + | |
| 667 | +/** | |
| 668 | + * Helper method to safely call get_current_screen(), returning false | |
| 669 | + * if the function is not available or returns null. | |
| 670 | + * | |
| 671 | + * Otherwise returns the given WP_Screen property. | |
| 672 | + * | |
| 673 | + * @since 2.5.9 | |
| 674 | + * | |
| 675 | + * @param string $property WP_Screen property to return. | |
| 676 | + * @return bool|string | |
| 677 | + */ | |
| 678 | +function convertkit_get_current_screen( $property ) { | |
| 679 | + | |
| 680 | + // Bail if we cannot determine the screen. | |
| 681 | + if ( ! function_exists( 'get_current_screen' ) ) { | |
| 682 | + return false; | |
| 683 | + } | |
| 684 | + | |
| 685 | + // Get screen. | |
| 686 | + $screen = get_current_screen(); | |
| 687 | + | |
| 688 | + // Bail if the screen couldn't be determined. | |
| 689 | + if ( is_null( $screen ) ) { | |
| 690 | + return false; | |
| 691 | + } | |
| 692 | + | |
| 693 | + // Return property. | |
| 694 | + return $screen->$property; | |
| 695 | + | |
| 696 | +} | |
| 697 | + | |
| 698 | +/** | |
| 699 | + * Outputs the Intercom help widget script. | |
| 700 | + * | |
| 701 | + * @since 2.7.2 | |
| 702 | + */ | |
| 703 | +function convertkit_output_intercom_messenger() { | |
| 704 | + | |
| 705 | + ?> | |
| 706 | + <script> | |
| 707 | + const KIT_INTERCOM_APP_ID = 'e4n3xtxz'; | |
| 708 | + window.intercomSettings = { | |
| 709 | + api_base: 'https://api-iam.intercom.io', | |
| 710 | + app_id: KIT_INTERCOM_APP_ID | |
| 711 | + }; | |
| 712 | + </script> | |
| 713 | + | |
| 714 | + <script> | |
| 715 | + (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);}}})(); | |
| 716 | + </script> | |
| 717 | + <?php | |
| 718 | + | |
| 719 | +} | |
| 720 | + | |
| 721 | +/** | |
| 722 | + * Checks if the given Theme is active. | |
| 723 | + * | |
| 724 | + * @since 3.1.4 | |
| 725 | + * | |
| 726 | + * @param string $theme_name Theme name. | |
| 727 | + * @return bool | |
| 728 | + */ | |
| 729 | +function convertkit_is_theme_active( $theme_name ) { | |
| 730 | + | |
| 731 | + // Assume Theme isn't active if we can't detect it. | |
| 732 | + if ( ! function_exists( 'wp_get_theme' ) ) { | |
| 733 | + return false; | |
| 734 | + } | |
| 735 | + | |
| 736 | + // Check the Parent Theme if we're on a Child Theme. | |
| 737 | + if ( wp_get_theme()->parent() ) { | |
| 738 | + $theme = wp_get_theme()->parent(); | |
| 739 | + } else { | |
| 740 | + $theme = wp_get_theme(); | |
| 741 | + } | |
| 742 | + | |
| 743 | + return strtolower( $theme->get( 'Name' ) ) === strtolower( $theme_name ); | |
| 744 | + | |
| 745 | +} | |
| 746 | + | |
| 747 | +/** | |
| 748 | + * Returns permitted HTML output when using wp_kses( ..., convertkit_kses_allowed_html()). | |
| 749 | + * | |
| 750 | + * @since 2.8.5 | |
| 751 | + */ | |
| 752 | +function convertkit_kses_allowed_html() { | |
| 753 | + | |
| 754 | + // Get WordPress' permitted HTML elements. | |
| 755 | + $elements = wp_kses_allowed_html( 'post' ); | |
| 756 | + | |
| 757 | + // Add form elements. | |
| 758 | + $form_elements = array( | |
| 759 | + 'input' => array( | |
| 760 | + 'type' => true, | |
| 761 | + 'id' => true, | |
| 762 | + 'name' => true, | |
| 763 | + 'class' => true, | |
| 764 | + 'value' => true, | |
| 765 | + 'checked' => true, | |
| 766 | + 'min' => true, | |
| 767 | + 'max' => true, | |
| 768 | + 'step' => true, | |
| 769 | + 'data-*' => true, | |
| 770 | + ), | |
| 771 | + 'select' => array( | |
| 772 | + 'id' => true, | |
| 773 | + 'name' => true, | |
| 774 | + 'class' => true, | |
| 775 | + 'size' => true, | |
| 776 | + 'multiple' => true, | |
| 777 | + 'data-*' => true, | |
| 778 | + ), | |
| 779 | + 'option' => array( | |
| 780 | + 'value' => true, | |
| 781 | + 'selected' => true, | |
| 782 | + 'data-*' => true, | |
| 783 | + ), | |
| 784 | + 'optgroup' => array( | |
| 785 | + 'label' => true, | |
| 786 | + 'data-*' => true, | |
| 787 | + ), | |
| 788 | + 'label' => array( | |
| 789 | + 'for' => true, | |
| 790 | + ), | |
| 791 | + ); | |
| 792 | + | |
| 793 | + return array_merge( $elements, $form_elements ); | |
| 794 | + | |
| 795 | +} | |
| 796 | + | |
| 797 | +/** | |
| 798 | + * Saves the new access token, refresh token and its expiry, and schedules | |
| 799 | + * a WordPress Cron event to refresh the token on expiry. | |
| 800 | + * | |
| 801 | + * @since 3.1.1 | |
| 802 | + * | |
| 803 | + * @param array $result New Access Token, Refresh Token and Expiry. | |
| 804 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 805 | + */ | |
| 806 | +function convertkit_maybe_update_credentials( $result, $client_id ) { | |
| 807 | + | |
| 808 | + // Don't save these credentials if they're not for this Client ID. | |
| 809 | + // They're for another Kit Plugin that uses OAuth. | |
| 810 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 811 | + return; | |
| 812 | + } | |
| 813 | + | |
| 814 | + $settings = new ConvertKit_Settings(); | |
| 815 | + $settings->update_credentials( $result ); | |
| 816 | + | |
| 817 | +} | |
| 818 | + | |
| 819 | +/** | |
| 820 | + * Deletes the stored access token, refresh token and its expiry from the Plugin settings, | |
| 821 | + * and clears any existing scheduled WordPress Cron event to refresh the token on expiry, | |
| 822 | + * when the user revokes the access token. | |
| 823 | + * | |
| 824 | + * @since 3.2.4 | |
| 825 | + * | |
| 826 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 827 | + */ | |
| 828 | +function convertkit_delete_credentials( $client_id ) { | |
| 829 | + | |
| 830 | + // Don't delete these credentials if they're not for this Client ID. | |
| 831 | + // They're for another Kit Plugin that uses OAuth. | |
| 832 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 833 | + return; | |
| 834 | + } | |
| 835 | + | |
| 836 | + // Delete Access and Refresh Tokens. | |
| 837 | + $settings = new ConvertKit_Settings(); | |
| 838 | + $settings->delete_credentials(); | |
| 839 | + | |
| 840 | +} | |
| 841 | + | |
| 842 | +/** | |
| 843 | + * Deletes the stored access token, refresh token and its expiry from the Plugin settings, | |
| 844 | + * and clears any existing scheduled WordPress Cron event to refresh the token on expiry, | |
| 845 | + * when either: | |
| 846 | + * - The access token is invalid | |
| 847 | + * - The access token expired, and refreshing failed | |
| 848 | + * | |
| 849 | + * @since 3.1.1 | |
| 850 | + * | |
| 851 | + * @param WP_Error $result Error result. | |
| 852 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 853 | + */ | |
| 854 | +function convertkit_maybe_delete_credentials( $result, $client_id ) { | |
| 855 | + | |
| 856 | + // Don't save these credentials if they're not for this Client ID. | |
| 857 | + // They're for another Kit Plugin that uses OAuth. | |
| 858 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 859 | + return; | |
| 860 | + } | |
| 861 | + | |
| 862 | + // If the error isn't a 401, don't delete credentials. | |
| 863 | + // This could be e.g. a temporary network error, rate limit or similar. | |
| 864 | + if ( $result->get_error_data( 'convertkit_api_error' ) !== 401 ) { | |
| 865 | + return; | |
| 866 | + } | |
| 867 | + | |
| 868 | + // Persist an error notice in the WordPress Administration until the user fixes the problem. | |
| 869 | + WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' ); | |
| 870 | + | |
| 871 | + $settings = new ConvertKit_Settings(); | |
| 872 | + $settings->delete_credentials(); | |
| 873 | + | |
| 874 | +} | |
| 875 | + | |
| 876 | +// Update Access Token when refreshed by the API class. | |
| 877 | +add_action( 'convertkit_api_get_access_token', 'convertkit_maybe_update_credentials', 10, 2 ); | |
| 878 | +add_action( 'convertkit_api_refresh_token', 'convertkit_maybe_update_credentials', 10, 2 ); | |
| 879 | + | |
| 880 | +// Delete credentials when the user revokes the access and refresh tokens. | |
| 881 | +add_action( 'convertkit_api_revoke_tokens', 'convertkit_delete_credentials', 10, 1 ); | |
| 882 | + | |
| 883 | +// Delete credentials if the API class uses a invalid access token. | |
| 884 | +// This prevents the Plugin making repetitive API requests that will 401. | |
| 885 | +add_action( 'convertkit_api_access_token_invalid', 'convertkit_maybe_delete_credentials', 10, 2 ); | |