| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Freemius helper. |
| 5 |
* |
| 6 |
* @package CTC |
| 7 |
*/ |
| 8 |
use CTC\Telemetry; |
| 9 |
/** |
| 10 |
* Create a helper function for easy SDK access. |
| 11 |
* |
| 12 |
* @return object|null Freemius SDK instance, or null if not yet initialized. |
| 13 |
*/ |
| 14 |
function ctc_fs() { |
| 15 |
global $ctc_fs; |
| 16 |
// Return existing instance if already initialized. |
| 17 |
if ( isset( $ctc_fs ) ) { |
| 18 |
return $ctc_fs; |
| 19 |
} |
| 20 |
// Don't initialize before plugins_loaded to avoid early translation loading (WP 6.7+). |
| 21 |
if ( !did_action( 'plugins_loaded' ) ) { |
| 22 |
return null; |
| 23 |
} |
| 24 |
// Include Freemius SDK from Composer vendor directory. |
| 25 |
$freemius_sdk_path = dirname( __DIR__ ) . '/vendor/freemius/wordpress-sdk/start.php'; |
| 26 |
if ( !file_exists( $freemius_sdk_path ) ) { |
| 27 |
wp_die( 'Freemius SDK not found. Please run: composer install' ); |
| 28 |
} |
| 29 |
require_once $freemius_sdk_path; |
| 30 |
$ctc_fs = fs_dynamic_init( [ |
| 31 |
'id' => '2780', |
| 32 |
'slug' => 'ctc', |
| 33 |
'type' => 'plugin', |
| 34 |
'public_key' => 'pk_15a174f8c30f506a9a35ccbf0fa76', |
| 35 |
'is_premium' => false, |
| 36 |
'premium_suffix' => 'Premium', |
| 37 |
'has_addons' => false, |
| 38 |
'has_paid_plans' => true, |
| 39 |
'has_affiliation' => 'selected', |
| 40 |
'menu' => [ |
| 41 |
'slug' => 'ctc-global-injector', |
| 42 |
'override_exact' => true, |
| 43 |
'first-path' => 'options-general.php?page=ctc-global-injector', |
| 44 |
'contact' => true, |
| 45 |
'support' => true, |
| 46 |
'affiliation' => false, |
| 47 |
'parent' => [ |
| 48 |
'slug' => 'options-general.php', |
| 49 |
], |
| 50 |
], |
| 51 |
'is_live' => true, |
| 52 |
'is_org_compliant' => true, |
| 53 |
] ); |
| 54 |
// Signal that SDK was initiated. |
| 55 |
do_action( 'ctc/freemius/loaded' ); |
| 56 |
return $ctc_fs; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Initialize Freemius SDK and add filters. |
| 61 |
* |
| 62 |
* Called on plugins_loaded to avoid early translation loading issues. |
| 63 |
*/ |
| 64 |
function ctc_fs_init() { |
| 65 |
$fs = ctc_fs(); |
| 66 |
if ( !$fs ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
// @phpstan-ignore-next-line |
| 70 |
$fs->add_filter( 'connect_url', 'ctc_fs_settings_url' ); |
| 71 |
// @phpstan-ignore-next-line |
| 72 |
$fs->add_filter( 'after_skip_url', 'ctc_fs_settings_url' ); |
| 73 |
// @phpstan-ignore-next-line |
| 74 |
$fs->add_filter( 'after_connect_url', 'ctc_fs_settings_url' ); |
| 75 |
// @phpstan-ignore-next-line |
| 76 |
$fs->add_filter( 'after_pending_connect_url', 'ctc_fs_settings_url' ); |
| 77 |
// @phpstan-ignore-next-line |
| 78 |
$fs->add_filter( 'after_pending_connect_url', 'ctc_enable_telemetry' ); |
| 79 |
// Clean up telemetry options when user uninstalls (Freemius runs this via after_uninstall). |
| 80 |
// Do not add uninstall.php in plugin root — Freemius requires it absent to track uninstall feedback. |
| 81 |
$fs->add_action( 'after_uninstall', 'ctc_cleanup_on_uninstall' ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Enable telemetry. |
| 86 |
*/ |
| 87 |
function ctc_enable_telemetry() { |
| 88 |
Telemetry::set_opt_in( true ); |
| 89 |
Telemetry::maybe_send(); |
| 90 |
Telemetry::schedule_cron(); |
| 91 |
return ctc_fs_settings_url(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Clean up plugin options on uninstall (called by Freemius after_uninstall hook). |
| 96 |
* |
| 97 |
* Install identity is domain-based (no stored install id), so reinstall on same domain keeps the same id. |
| 98 |
*/ |
| 99 |
function ctc_cleanup_on_uninstall() { |
| 100 |
delete_option( 'ctc_telemetry_opt_in' ); |
| 101 |
delete_option( 'ctc_telemetry_first_seen' ); |
| 102 |
delete_option( 'ctc_telemetry_last_sent' ); |
| 103 |
delete_option( 'ctc_install_id' ); |
| 104 |
wp_clear_scheduled_hook( 'ctc_telemetry_weekly_send' ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get the settings URL. |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
function ctc_fs_settings_url() { |
| 113 |
return admin_url( 'options-general.php?page=ctc-global-injector' ); |
| 114 |
} |
| 115 |
|
| 116 |
// Initialize Freemius on plugins_loaded (after textdomain is loaded). |
| 117 |
add_action( 'plugins_loaded', 'ctc_fs_init', 10 ); |