PluginProbe ʕ •ᴥ•ʔ
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More / trunk
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More vtrunk
2.11.0 2.10.0 2.9.0 2.8.0 2.7.0 2.6.7 2.6.8 2.6.5 2.6.4 2.6.3 2.6.2 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1 1.1.1 1.1.2 1.2.0 2.0 2.1.0 2.1.1 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1
reviews-feed / class / Common / UsageTracking / EventRecorder.php
reviews-feed / class / Common / UsageTracking Last commit date
Core 6 days ago Reviews 6 days ago Config.php 6 days ago EventRecorder.php 6 days ago ReporterInterface.php 6 days ago SmashUsageTracking.php 6 days ago
EventRecorder.php
95 lines
1 <?php
2
3 /**
4 * Records usage events for the current period (stored in sbr_smash_usage_events).
5 *
6 * @package SmashBalloon\Reviews\Common\UsageTracking
7 * @since 1.0
8 */
9
10 namespace SmashBalloon\Reviews\Common\UsageTracking;
11
12 if (! defined('ABSPATH')) {
13 exit;
14 }
15
16 class EventRecorder {
17 const OPTION_NAME = 'sbr_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. feed_saved, source_connected).
23 */
24 public static function record($event_name): void
25 {
26 if (! Config::is_enabled()) {
27 return;
28 }
29 if ('' === $event_name || ! is_string($event_name)) {
30 return;
31 }
32 $event_name = sanitize_text_field($event_name);
33 if (strlen($event_name) > 100) {
34 $event_name = substr($event_name, 0, 100);
35 }
36 $events = get_option(self::OPTION_NAME, array());
37 if (! is_array($events)) {
38 $events = array();
39 }
40 $current = isset($events[ $event_name ]) ? $events[ $event_name ] : 0;
41 $count = is_array($current) && isset($current['count']) ? (int) $current['count'] + 1 : (is_numeric($current) ? (int) $current + 1 : 1);
42 $events[ $event_name ] = array(
43 'count' => $count,
44 'last_date' => gmdate('Y-m-d'),
45 );
46 update_option(self::OPTION_NAME, $events, false);
47 }
48
49 /**
50 * Record today as an active day (for days_active metric). Keeps last 90 days.
51 */
52 public static function record_active_day(): void
53 {
54 if (! Config::is_enabled()) {
55 return;
56 }
57 $option_key = Config::OPTION_ACTIVE_DATES;
58 $dates = get_option($option_key, array());
59 if (! is_array($dates)) {
60 $dates = array();
61 }
62 $today = gmdate('Y-m-d');
63 if (! in_array($today, $dates, true)) {
64 $dates[] = $today;
65 rsort($dates);
66 $dates = array_slice($dates, 0, 90);
67 update_option($option_key, $dates, false);
68 }
69 }
70
71 /**
72 * Record a session duration in seconds (for session_duration metric). Keeps last 10.
73 *
74 * @param int $seconds Duration in seconds.
75 */
76 public static function record_session_duration($seconds): void
77 {
78 if (! Config::is_enabled()) {
79 return;
80 }
81 $seconds = (int) $seconds;
82 if ($seconds <= 0 || $seconds > 86400) {
83 return;
84 }
85 $option_key = Config::OPTION_SESSION_DURATIONS;
86 $durations = get_option($option_key, array());
87 if (! is_array($durations)) {
88 $durations = array();
89 }
90 $durations[] = $seconds;
91 $durations = array_slice($durations, -10);
92 update_option($option_key, $durations, false);
93 }
94 }
95