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
EventRecorder.php
89 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Records usage events for the current period (stored in cff_smash_usage_events). |
| 4 | * |
| 5 | * @package CustomFacebookFeed\UsageTracking |
| 6 | * @since 4.0 |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed\UsageTracking; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | class EventRecorder { |
| 16 | |
| 17 | const OPTION_NAME = 'cff_smash_usage_events'; |
| 18 | |
| 19 | /** |
| 20 | * Record one occurrence of an event (increments count and sets last_date to today). |
| 21 | * |
| 22 | * @param string $event_name Event name (e.g. plugin_installed, setting_changed). |
| 23 | */ |
| 24 | public static function record( $event_name ) { |
| 25 | if ( ! Config::is_enabled() ) { |
| 26 | return; |
| 27 | } |
| 28 | if ( '' === $event_name || ! is_string( $event_name ) ) { |
| 29 | return; |
| 30 | } |
| 31 | $event_name = sanitize_text_field( $event_name ); |
| 32 | if ( strlen( $event_name ) > 100 ) { |
| 33 | $event_name = substr( $event_name, 0, 100 ); |
| 34 | } |
| 35 | $events = get_option( self::OPTION_NAME, array() ); |
| 36 | if ( ! is_array( $events ) ) { |
| 37 | $events = array(); |
| 38 | } |
| 39 | $current = isset( $events[ $event_name ] ) ? $events[ $event_name ] : 0; |
| 40 | $count = is_array( $current ) && isset( $current['count'] ) ? (int) $current['count'] + 1 : (is_numeric( $current ) ? (int) $current + 1 : 1); |
| 41 | $events[ $event_name ] = array( |
| 42 | 'count' => $count, |
| 43 | 'last_date' => gmdate( 'Y-m-d' ), |
| 44 | ); |
| 45 | update_option( self::OPTION_NAME, $events, false ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Record today as an active day (for days_active metric). Keeps last 90 days. |
| 50 | */ |
| 51 | public static function record_active_day() { |
| 52 | $option_key = \CustomFacebookFeed\UsageTracking\Config::OPTION_ACTIVE_DATES; |
| 53 | $dates = get_option( $option_key, array() ); |
| 54 | if ( ! is_array( $dates ) ) { |
| 55 | $dates = array(); |
| 56 | } |
| 57 | $today = gmdate( 'Y-m-d' ); |
| 58 | if ( ! in_array( $today, $dates, true ) ) { |
| 59 | $dates[] = $today; |
| 60 | rsort( $dates ); |
| 61 | $dates = array_slice( $dates, 0, 90 ); |
| 62 | update_option( $option_key, $dates, false ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Record a session duration in seconds (for session_duration metric). Keeps last 10. |
| 68 | * |
| 69 | * @param int $seconds Duration in seconds. |
| 70 | */ |
| 71 | public static function record_session_duration( $seconds ) { |
| 72 | if ( ! Config::is_enabled() ) { |
| 73 | return; |
| 74 | } |
| 75 | $seconds = (int) $seconds; |
| 76 | if ( $seconds <= 0 || $seconds > 86400 ) { |
| 77 | return; |
| 78 | } |
| 79 | $option_key = \CustomFacebookFeed\UsageTracking\Config::OPTION_SESSION_DURATIONS; |
| 80 | $durations = get_option( $option_key, array() ); |
| 81 | if ( ! is_array( $durations ) ) { |
| 82 | $durations = array(); |
| 83 | } |
| 84 | $durations[] = $seconds; |
| 85 | $durations = array_slice( $durations, -10 ); |
| 86 | update_option( $option_key, $durations, false ); |
| 87 | } |
| 88 | } |
| 89 |