Core
3 weeks ago
Facebook
3 weeks ago
Config.php
3 weeks ago
EventRecorder.php
3 weeks ago
ReporterInterface.php
3 weeks ago
SmashUsageTracking.php
3 weeks ago
Config.php
118 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Smash Usage Tracking configuration. |
| 4 | * |
| 5 | * API URL and option names. Reusable across plugins. |
| 6 | * |
| 7 | * @package CustomFacebookFeed\UsageTracking |
| 8 | * @since 4.0 |
| 9 | */ |
| 10 | |
| 11 | namespace CustomFacebookFeed\UsageTracking; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | class Config { |
| 18 | |
| 19 | /** |
| 20 | * Option key: enabled flag and last_send timestamp. |
| 21 | * Reuses the legacy usage-tracking option so existing consent carries over. |
| 22 | */ |
| 23 | const OPTION_TRACKING = 'cff_usage_tracking'; |
| 24 | |
| 25 | /** |
| 26 | * Default enabled state when the option has never been saved. |
| 27 | * Matches the legacy default: opt-in required for the free plugin. |
| 28 | */ |
| 29 | const DEFAULT_ENABLED = false; |
| 30 | |
| 31 | /** |
| 32 | * Option key: site token returned by the API. |
| 33 | */ |
| 34 | const OPTION_SITE_TOKEN = 'cff_smash_usage_tracking_site_token'; |
| 35 | |
| 36 | /** |
| 37 | * Option key: schedule metadata (optional). |
| 38 | */ |
| 39 | const OPTION_SCHEDULE = 'cff_smash_usage_tracking_schedule'; |
| 40 | |
| 41 | /** |
| 42 | * Option key: dates when plugin was active (Y-m-d), for days_active metric. |
| 43 | */ |
| 44 | const OPTION_ACTIVE_DATES = 'cff_smash_usage_active_dates'; |
| 45 | |
| 46 | /** |
| 47 | * Option key: last N session durations in seconds, for session_duration metric. |
| 48 | */ |
| 49 | const OPTION_SESSION_DURATIONS = 'cff_smash_usage_session_durations'; |
| 50 | |
| 51 | /** |
| 52 | * Cron hook name. Different from the Pro plugin to avoid duplicate sends |
| 53 | * on sites where both free and pro are installed simultaneously. |
| 54 | */ |
| 55 | const CRON_HOOK = 'cff_free_smash_usage_tracking_cron'; |
| 56 | |
| 57 | /** |
| 58 | * Max request timeout in seconds for usage report (large payloads). |
| 59 | */ |
| 60 | const REQUEST_TIMEOUT = 30; |
| 61 | |
| 62 | /** |
| 63 | * Max payload size in bytes before send is skipped (default 2MB). |
| 64 | */ |
| 65 | const MAX_PAYLOAD_BYTES = 2097152; |
| 66 | |
| 67 | /** |
| 68 | * Register-site endpoint path (relative to API base). |
| 69 | */ |
| 70 | const REGISTER_SITE_PATH = '/v1/register-site'; |
| 71 | |
| 72 | /** |
| 73 | * Usage report endpoint path (relative to API base). |
| 74 | */ |
| 75 | const USAGE_REPORT_PATH = '/v1/usage-report'; |
| 76 | |
| 77 | /** |
| 78 | * Get the API base URL (filterable). |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public static function get_api_url() { |
| 83 | $url = defined( 'CFF_SMASH_USAGE_TRACKING_API_URL' ) ? CFF_SMASH_USAGE_TRACKING_API_URL : ''; |
| 84 | return (string) apply_filters( 'cff_smash_usage_tracking_api_url', $url ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get full URL for register-site endpoint. |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public static function get_register_site_url() { |
| 93 | return rtrim( self::get_api_url(), '/' ) . self::REGISTER_SITE_PATH; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get full URL for usage-report endpoint. |
| 98 | * |
| 99 | * @return string |
| 100 | */ |
| 101 | public static function get_usage_report_url() { |
| 102 | return rtrim( self::get_api_url(), '/' ) . self::USAGE_REPORT_PATH; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Check if tracking is enabled. |
| 107 | * |
| 108 | * @return bool |
| 109 | */ |
| 110 | public static function is_enabled() { |
| 111 | $opt = get_option( self::OPTION_TRACKING, false ); |
| 112 | if ( ! is_array( $opt ) || ! array_key_exists( 'enabled', $opt ) ) { |
| 113 | return self::DEFAULT_ENABLED; |
| 114 | } |
| 115 | return ! empty( $opt['enabled'] ); |
| 116 | } |
| 117 | } |
| 118 |