PluginProbe ʕ •ᴥ•ʔ
Custom Twitter Feeds – A Tweets Widget or X Feed Widget / 2.7.0
Custom Twitter Feeds – A Tweets Widget or X Feed Widget v2.7.0
2.8.0 2.7.0 2.6.1 2.6.0 1.0 1.0.1 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.4 1.4.1 1.5 1.5.1 1.6 1.6.1 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.1.2 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.5.4 2.5.5 trunk
custom-twitter-feeds / inc / UsageTracking / EventRecorder.php
custom-twitter-feeds / inc / UsageTracking Last commit date
Core 1 month ago Twitter 1 month ago Config.php 1 month ago EventRecorder.php 1 month ago ReporterInterface.php 1 month ago SmashUsageTracking.php 1 month ago
EventRecorder.php
89 lines
1 <?php
2 /**
3 * Records usage events for the current period (stored in ctf_smash_usage_events).
4 *
5 * @package TwitterFeed\UsageTracking
6 * @since 2.6
7 */
8
9 namespace TwitterFeed\UsageTracking;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 class EventRecorder {
16
17 const OPTION_NAME = 'ctf_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 = \TwitterFeed\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 = \TwitterFeed\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